plugin.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.10.2 (2021-11-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var Cell = function (initial) {
  12. var value = initial;
  13. var get = function () {
  14. return value;
  15. };
  16. var set = function (v) {
  17. value = v;
  18. };
  19. return {
  20. get: get,
  21. set: set
  22. };
  23. };
  24. var global$3 = tinymce.util.Tools.resolve('tinymce.PluginManager');
  25. var __assign = function () {
  26. __assign = Object.assign || function __assign(t) {
  27. for (var s, i = 1, n = arguments.length; i < n; i++) {
  28. s = arguments[i];
  29. for (var p in s)
  30. if (Object.prototype.hasOwnProperty.call(s, p))
  31. t[p] = s[p];
  32. }
  33. return t;
  34. };
  35. return __assign.apply(this, arguments);
  36. };
  37. var typeOf = function (x) {
  38. var t = typeof x;
  39. if (x === null) {
  40. return 'null';
  41. } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
  42. return 'array';
  43. } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
  44. return 'string';
  45. } else {
  46. return t;
  47. }
  48. };
  49. var isType$1 = function (type) {
  50. return function (value) {
  51. return typeOf(value) === type;
  52. };
  53. };
  54. var isSimpleType = function (type) {
  55. return function (value) {
  56. return typeof value === type;
  57. };
  58. };
  59. var isString = isType$1('string');
  60. var isArray = isType$1('array');
  61. var isBoolean = isSimpleType('boolean');
  62. var isNumber = isSimpleType('number');
  63. var noop = function () {
  64. };
  65. var constant = function (value) {
  66. return function () {
  67. return value;
  68. };
  69. };
  70. var identity = function (x) {
  71. return x;
  72. };
  73. var never = constant(false);
  74. var always = constant(true);
  75. var punctuationStr = '[!-#%-*,-\\/:;?@\\[-\\]_{}\xA1\xAB\xB7\xBB\xBF;\xB7\u055A-\u055F\u0589\u058A\u05BE\u05C0\u05C3\u05C6\u05F3\u05F4\u0609\u060A\u060C\u060D\u061B\u061E\u061F\u066A-\u066D\u06D4\u0700-\u070D\u07F7-\u07F9\u0830-\u083E\u085E\u0964\u0965\u0970\u0DF4\u0E4F\u0E5A\u0E5B\u0F04-\u0F12\u0F3A-\u0F3D\u0F85\u0FD0-\u0FD4\u0FD9\u0FDA\u104A-\u104F\u10FB\u1361-\u1368\u1400\u166D\u166E\u169B\u169C\u16EB-\u16ED\u1735\u1736\u17D4-\u17D6\u17D8-\u17DA\u1800-\u180A\u1944\u1945\u1A1E\u1A1F\u1AA0-\u1AA6\u1AA8-\u1AAD\u1B5A-\u1B60\u1BFC-\u1BFF\u1C3B-\u1C3F\u1C7E\u1C7F\u1CD3\u2010-\u2027\u2030-\u2043\u2045-\u2051\u2053-\u205E\u207D\u207E\u208D\u208E\u3008\u3009\u2768-\u2775\u27C5\u27C6\u27E6-\u27EF\u2983-\u2998\u29D8-\u29DB\u29FC\u29FD\u2CF9-\u2CFC\u2CFE\u2CFF\u2D70\u2E00-\u2E2E\u2E30\u2E31\u3001-\u3003\u3008-\u3011\u3014-\u301F\u3030\u303D\u30A0\u30FB\uA4FE\uA4FF\uA60D-\uA60F\uA673\uA67E\uA6F2-\uA6F7\uA874-\uA877\uA8CE\uA8CF\uA8F8-\uA8FA\uA92E\uA92F\uA95F\uA9C1-\uA9CD\uA9DE\uA9DF\uAA5C-\uAA5F\uAADE\uAADF\uABEB\uFD3E\uFD3F\uFE10-\uFE19\uFE30-\uFE52\uFE54-\uFE61\uFE63\uFE68\uFE6A\uFE6B\uFF01-\uFF03\uFF05-\uFF0A\uFF0C-\uFF0F\uFF1A\uFF1B\uFF1F\uFF20\uFF3B-\uFF3D\uff3f\uFF5B\uFF5D\uFF5F-\uFF65]';
  76. var punctuation$1 = constant(punctuationStr);
  77. var none = function () {
  78. return NONE;
  79. };
  80. var NONE = function () {
  81. var call = function (thunk) {
  82. return thunk();
  83. };
  84. var id = identity;
  85. var me = {
  86. fold: function (n, _s) {
  87. return n();
  88. },
  89. isSome: never,
  90. isNone: always,
  91. getOr: id,
  92. getOrThunk: call,
  93. getOrDie: function (msg) {
  94. throw new Error(msg || 'error: getOrDie called on none.');
  95. },
  96. getOrNull: constant(null),
  97. getOrUndefined: constant(undefined),
  98. or: id,
  99. orThunk: call,
  100. map: none,
  101. each: noop,
  102. bind: none,
  103. exists: never,
  104. forall: always,
  105. filter: function () {
  106. return none();
  107. },
  108. toArray: function () {
  109. return [];
  110. },
  111. toString: constant('none()')
  112. };
  113. return me;
  114. }();
  115. var some = function (a) {
  116. var constant_a = constant(a);
  117. var self = function () {
  118. return me;
  119. };
  120. var bind = function (f) {
  121. return f(a);
  122. };
  123. var me = {
  124. fold: function (n, s) {
  125. return s(a);
  126. },
  127. isSome: always,
  128. isNone: never,
  129. getOr: constant_a,
  130. getOrThunk: constant_a,
  131. getOrDie: constant_a,
  132. getOrNull: constant_a,
  133. getOrUndefined: constant_a,
  134. or: self,
  135. orThunk: self,
  136. map: function (f) {
  137. return some(f(a));
  138. },
  139. each: function (f) {
  140. f(a);
  141. },
  142. bind: bind,
  143. exists: bind,
  144. forall: bind,
  145. filter: function (f) {
  146. return f(a) ? me : NONE;
  147. },
  148. toArray: function () {
  149. return [a];
  150. },
  151. toString: function () {
  152. return 'some(' + a + ')';
  153. }
  154. };
  155. return me;
  156. };
  157. var from = function (value) {
  158. return value === null || value === undefined ? NONE : some(value);
  159. };
  160. var Optional = {
  161. some: some,
  162. none: none,
  163. from: from
  164. };
  165. var punctuation = punctuation$1;
  166. var global$2 = tinymce.util.Tools.resolve('tinymce.Env');
  167. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  168. var nativeSlice = Array.prototype.slice;
  169. var nativePush = Array.prototype.push;
  170. var map = function (xs, f) {
  171. var len = xs.length;
  172. var r = new Array(len);
  173. for (var i = 0; i < len; i++) {
  174. var x = xs[i];
  175. r[i] = f(x, i);
  176. }
  177. return r;
  178. };
  179. var each = function (xs, f) {
  180. for (var i = 0, len = xs.length; i < len; i++) {
  181. var x = xs[i];
  182. f(x, i);
  183. }
  184. };
  185. var eachr = function (xs, f) {
  186. for (var i = xs.length - 1; i >= 0; i--) {
  187. var x = xs[i];
  188. f(x, i);
  189. }
  190. };
  191. var groupBy = function (xs, f) {
  192. if (xs.length === 0) {
  193. return [];
  194. } else {
  195. var wasType = f(xs[0]);
  196. var r = [];
  197. var group = [];
  198. for (var i = 0, len = xs.length; i < len; i++) {
  199. var x = xs[i];
  200. var type = f(x);
  201. if (type !== wasType) {
  202. r.push(group);
  203. group = [];
  204. }
  205. wasType = type;
  206. group.push(x);
  207. }
  208. if (group.length !== 0) {
  209. r.push(group);
  210. }
  211. return r;
  212. }
  213. };
  214. var foldl = function (xs, f, acc) {
  215. each(xs, function (x, i) {
  216. acc = f(acc, x, i);
  217. });
  218. return acc;
  219. };
  220. var flatten = function (xs) {
  221. var r = [];
  222. for (var i = 0, len = xs.length; i < len; ++i) {
  223. if (!isArray(xs[i])) {
  224. throw new Error('Arr.flatten item ' + i + ' was not an array, input: ' + xs);
  225. }
  226. nativePush.apply(r, xs[i]);
  227. }
  228. return r;
  229. };
  230. var bind = function (xs, f) {
  231. return flatten(map(xs, f));
  232. };
  233. var sort = function (xs, comparator) {
  234. var copy = nativeSlice.call(xs, 0);
  235. copy.sort(comparator);
  236. return copy;
  237. };
  238. var hasOwnProperty = Object.hasOwnProperty;
  239. var has = function (obj, key) {
  240. return hasOwnProperty.call(obj, key);
  241. };
  242. typeof window !== 'undefined' ? window : Function('return this;')();
  243. var DOCUMENT = 9;
  244. var DOCUMENT_FRAGMENT = 11;
  245. var ELEMENT = 1;
  246. var TEXT = 3;
  247. var type = function (element) {
  248. return element.dom.nodeType;
  249. };
  250. var isType = function (t) {
  251. return function (element) {
  252. return type(element) === t;
  253. };
  254. };
  255. var isText$1 = isType(TEXT);
  256. var rawSet = function (dom, key, value) {
  257. if (isString(value) || isBoolean(value) || isNumber(value)) {
  258. dom.setAttribute(key, value + '');
  259. } else {
  260. console.error('Invalid call to Attribute.set. Key ', key, ':: Value ', value, ':: Element ', dom);
  261. throw new Error('Attribute value was not simple');
  262. }
  263. };
  264. var set = function (element, key, value) {
  265. rawSet(element.dom, key, value);
  266. };
  267. var compareDocumentPosition = function (a, b, match) {
  268. return (a.compareDocumentPosition(b) & match) !== 0;
  269. };
  270. var documentPositionPreceding = function (a, b) {
  271. return compareDocumentPosition(a, b, Node.DOCUMENT_POSITION_PRECEDING);
  272. };
  273. var fromHtml = function (html, scope) {
  274. var doc = scope || document;
  275. var div = doc.createElement('div');
  276. div.innerHTML = html;
  277. if (!div.hasChildNodes() || div.childNodes.length > 1) {
  278. console.error('HTML does not have a single root node', html);
  279. throw new Error('HTML must have a single root node');
  280. }
  281. return fromDom(div.childNodes[0]);
  282. };
  283. var fromTag = function (tag, scope) {
  284. var doc = scope || document;
  285. var node = doc.createElement(tag);
  286. return fromDom(node);
  287. };
  288. var fromText = function (text, scope) {
  289. var doc = scope || document;
  290. var node = doc.createTextNode(text);
  291. return fromDom(node);
  292. };
  293. var fromDom = function (node) {
  294. if (node === null || node === undefined) {
  295. throw new Error('Node cannot be null or undefined');
  296. }
  297. return { dom: node };
  298. };
  299. var fromPoint = function (docElm, x, y) {
  300. return Optional.from(docElm.dom.elementFromPoint(x, y)).map(fromDom);
  301. };
  302. var SugarElement = {
  303. fromHtml: fromHtml,
  304. fromTag: fromTag,
  305. fromText: fromText,
  306. fromDom: fromDom,
  307. fromPoint: fromPoint
  308. };
  309. var bypassSelector = function (dom) {
  310. return dom.nodeType !== ELEMENT && dom.nodeType !== DOCUMENT && dom.nodeType !== DOCUMENT_FRAGMENT || dom.childElementCount === 0;
  311. };
  312. var all = function (selector, scope) {
  313. var base = scope === undefined ? document : scope.dom;
  314. return bypassSelector(base) ? [] : map(base.querySelectorAll(selector), SugarElement.fromDom);
  315. };
  316. var parent = function (element) {
  317. return Optional.from(element.dom.parentNode).map(SugarElement.fromDom);
  318. };
  319. var children = function (element) {
  320. return map(element.dom.childNodes, SugarElement.fromDom);
  321. };
  322. var spot = function (element, offset) {
  323. return {
  324. element: element,
  325. offset: offset
  326. };
  327. };
  328. var leaf = function (element, offset) {
  329. var cs = children(element);
  330. return cs.length > 0 && offset < cs.length ? spot(cs[offset], 0) : spot(element, offset);
  331. };
  332. var before = function (marker, element) {
  333. var parent$1 = parent(marker);
  334. parent$1.each(function (v) {
  335. v.dom.insertBefore(element.dom, marker.dom);
  336. });
  337. };
  338. var append = function (parent, element) {
  339. parent.dom.appendChild(element.dom);
  340. };
  341. var wrap = function (element, wrapper) {
  342. before(element, wrapper);
  343. append(wrapper, element);
  344. };
  345. var NodeValue = function (is, name) {
  346. var get = function (element) {
  347. if (!is(element)) {
  348. throw new Error('Can only get ' + name + ' value of a ' + name + ' node');
  349. }
  350. return getOption(element).getOr('');
  351. };
  352. var getOption = function (element) {
  353. return is(element) ? Optional.from(element.dom.nodeValue) : Optional.none();
  354. };
  355. var set = function (element, value) {
  356. if (!is(element)) {
  357. throw new Error('Can only set raw ' + name + ' value of a ' + name + ' node');
  358. }
  359. element.dom.nodeValue = value;
  360. };
  361. return {
  362. get: get,
  363. getOption: getOption,
  364. set: set
  365. };
  366. };
  367. var api = NodeValue(isText$1, 'text');
  368. var get$1 = function (element) {
  369. return api.get(element);
  370. };
  371. var descendants = function (scope, selector) {
  372. return all(selector, scope);
  373. };
  374. var global = tinymce.util.Tools.resolve('tinymce.dom.TreeWalker');
  375. var isSimpleBoundary = function (dom, node) {
  376. return dom.isBlock(node) || has(dom.schema.getShortEndedElements(), node.nodeName);
  377. };
  378. var isContentEditableFalse = function (dom, node) {
  379. return dom.getContentEditable(node) === 'false';
  380. };
  381. var isContentEditableTrueInCef = function (dom, node) {
  382. return dom.getContentEditable(node) === 'true' && dom.getContentEditableParent(node.parentNode) === 'false';
  383. };
  384. var isHidden = function (dom, node) {
  385. return !dom.isBlock(node) && has(dom.schema.getWhiteSpaceElements(), node.nodeName);
  386. };
  387. var isBoundary = function (dom, node) {
  388. return isSimpleBoundary(dom, node) || isContentEditableFalse(dom, node) || isHidden(dom, node) || isContentEditableTrueInCef(dom, node);
  389. };
  390. var isText = function (node) {
  391. return node.nodeType === 3;
  392. };
  393. var nuSection = function () {
  394. return {
  395. sOffset: 0,
  396. fOffset: 0,
  397. elements: []
  398. };
  399. };
  400. var toLeaf = function (node, offset) {
  401. return leaf(SugarElement.fromDom(node), offset);
  402. };
  403. var walk = function (dom, walkerFn, startNode, callbacks, endNode, skipStart) {
  404. if (skipStart === void 0) {
  405. skipStart = true;
  406. }
  407. var next = skipStart ? walkerFn(false) : startNode;
  408. while (next) {
  409. var isCefNode = isContentEditableFalse(dom, next);
  410. if (isCefNode || isHidden(dom, next)) {
  411. var stopWalking = isCefNode ? callbacks.cef(next) : callbacks.boundary(next);
  412. if (stopWalking) {
  413. break;
  414. } else {
  415. next = walkerFn(true);
  416. continue;
  417. }
  418. } else if (isSimpleBoundary(dom, next)) {
  419. if (callbacks.boundary(next)) {
  420. break;
  421. }
  422. } else if (isText(next)) {
  423. callbacks.text(next);
  424. }
  425. if (next === endNode) {
  426. break;
  427. } else {
  428. next = walkerFn(false);
  429. }
  430. }
  431. };
  432. var collectTextToBoundary = function (dom, section, node, rootNode, forwards) {
  433. if (isBoundary(dom, node)) {
  434. return;
  435. }
  436. var rootBlock = dom.getParent(rootNode, dom.isBlock);
  437. var walker = new global(node, rootBlock);
  438. var walkerFn = forwards ? walker.next.bind(walker) : walker.prev.bind(walker);
  439. walk(dom, walkerFn, node, {
  440. boundary: always,
  441. cef: always,
  442. text: function (next) {
  443. if (forwards) {
  444. section.fOffset += next.length;
  445. } else {
  446. section.sOffset += next.length;
  447. }
  448. section.elements.push(SugarElement.fromDom(next));
  449. }
  450. });
  451. };
  452. var collect = function (dom, rootNode, startNode, endNode, callbacks, skipStart) {
  453. if (skipStart === void 0) {
  454. skipStart = true;
  455. }
  456. var walker = new global(startNode, rootNode);
  457. var sections = [];
  458. var current = nuSection();
  459. collectTextToBoundary(dom, current, startNode, rootNode, false);
  460. var finishSection = function () {
  461. if (current.elements.length > 0) {
  462. sections.push(current);
  463. current = nuSection();
  464. }
  465. return false;
  466. };
  467. walk(dom, walker.next.bind(walker), startNode, {
  468. boundary: finishSection,
  469. cef: function (node) {
  470. finishSection();
  471. if (callbacks) {
  472. sections.push.apply(sections, callbacks.cef(node));
  473. }
  474. return false;
  475. },
  476. text: function (next) {
  477. current.elements.push(SugarElement.fromDom(next));
  478. if (callbacks) {
  479. callbacks.text(next, current);
  480. }
  481. }
  482. }, endNode, skipStart);
  483. if (endNode) {
  484. collectTextToBoundary(dom, current, endNode, rootNode, true);
  485. }
  486. finishSection();
  487. return sections;
  488. };
  489. var collectRangeSections = function (dom, rng) {
  490. var start = toLeaf(rng.startContainer, rng.startOffset);
  491. var startNode = start.element.dom;
  492. var end = toLeaf(rng.endContainer, rng.endOffset);
  493. var endNode = end.element.dom;
  494. return collect(dom, rng.commonAncestorContainer, startNode, endNode, {
  495. text: function (node, section) {
  496. if (node === endNode) {
  497. section.fOffset += node.length - end.offset;
  498. } else if (node === startNode) {
  499. section.sOffset += start.offset;
  500. }
  501. },
  502. cef: function (node) {
  503. var sections = bind(descendants(SugarElement.fromDom(node), '*[contenteditable=true]'), function (e) {
  504. var ceTrueNode = e.dom;
  505. return collect(dom, ceTrueNode, ceTrueNode);
  506. });
  507. return sort(sections, function (a, b) {
  508. return documentPositionPreceding(a.elements[0].dom, b.elements[0].dom) ? 1 : -1;
  509. });
  510. }
  511. }, false);
  512. };
  513. var fromRng = function (dom, rng) {
  514. return rng.collapsed ? [] : collectRangeSections(dom, rng);
  515. };
  516. var fromNode = function (dom, node) {
  517. var rng = dom.createRng();
  518. rng.selectNode(node);
  519. return fromRng(dom, rng);
  520. };
  521. var fromNodes = function (dom, nodes) {
  522. return bind(nodes, function (node) {
  523. return fromNode(dom, node);
  524. });
  525. };
  526. var find$2 = function (text, pattern, start, finish) {
  527. if (start === void 0) {
  528. start = 0;
  529. }
  530. if (finish === void 0) {
  531. finish = text.length;
  532. }
  533. var regex = pattern.regex;
  534. regex.lastIndex = start;
  535. var results = [];
  536. var match;
  537. while (match = regex.exec(text)) {
  538. var matchedText = match[pattern.matchIndex];
  539. var matchStart = match.index + match[0].indexOf(matchedText);
  540. var matchFinish = matchStart + matchedText.length;
  541. if (matchFinish > finish) {
  542. break;
  543. }
  544. results.push({
  545. start: matchStart,
  546. finish: matchFinish
  547. });
  548. regex.lastIndex = matchFinish;
  549. }
  550. return results;
  551. };
  552. var extract = function (elements, matches) {
  553. var nodePositions = foldl(elements, function (acc, element) {
  554. var content = get$1(element);
  555. var start = acc.last;
  556. var finish = start + content.length;
  557. var positions = bind(matches, function (match, matchIdx) {
  558. if (match.start < finish && match.finish > start) {
  559. return [{
  560. element: element,
  561. start: Math.max(start, match.start) - start,
  562. finish: Math.min(finish, match.finish) - start,
  563. matchId: matchIdx
  564. }];
  565. } else {
  566. return [];
  567. }
  568. });
  569. return {
  570. results: acc.results.concat(positions),
  571. last: finish
  572. };
  573. }, {
  574. results: [],
  575. last: 0
  576. }).results;
  577. return groupBy(nodePositions, function (position) {
  578. return position.matchId;
  579. });
  580. };
  581. var find$1 = function (pattern, sections) {
  582. return bind(sections, function (section) {
  583. var elements = section.elements;
  584. var content = map(elements, get$1).join('');
  585. var positions = find$2(content, pattern, section.sOffset, content.length - section.fOffset);
  586. return extract(elements, positions);
  587. });
  588. };
  589. var mark = function (matches, replacementNode) {
  590. eachr(matches, function (match, idx) {
  591. eachr(match, function (pos) {
  592. var wrapper = SugarElement.fromDom(replacementNode.cloneNode(false));
  593. set(wrapper, 'data-mce-index', idx);
  594. var textNode = pos.element.dom;
  595. if (textNode.length === pos.finish && pos.start === 0) {
  596. wrap(pos.element, wrapper);
  597. } else {
  598. if (textNode.length !== pos.finish) {
  599. textNode.splitText(pos.finish);
  600. }
  601. var matchNode = textNode.splitText(pos.start);
  602. wrap(SugarElement.fromDom(matchNode), wrapper);
  603. }
  604. });
  605. });
  606. };
  607. var findAndMark = function (dom, pattern, node, replacementNode) {
  608. var textSections = fromNode(dom, node);
  609. var matches = find$1(pattern, textSections);
  610. mark(matches, replacementNode);
  611. return matches.length;
  612. };
  613. var findAndMarkInSelection = function (dom, pattern, selection, replacementNode) {
  614. var bookmark = selection.getBookmark();
  615. var nodes = dom.select('td[data-mce-selected],th[data-mce-selected]');
  616. var textSections = nodes.length > 0 ? fromNodes(dom, nodes) : fromRng(dom, selection.getRng());
  617. var matches = find$1(pattern, textSections);
  618. mark(matches, replacementNode);
  619. selection.moveToBookmark(bookmark);
  620. return matches.length;
  621. };
  622. var getElmIndex = function (elm) {
  623. var value = elm.getAttribute('data-mce-index');
  624. if (typeof value === 'number') {
  625. return '' + value;
  626. }
  627. return value;
  628. };
  629. var markAllMatches = function (editor, currentSearchState, pattern, inSelection) {
  630. var marker = editor.dom.create('span', { 'data-mce-bogus': 1 });
  631. marker.className = 'mce-match-marker';
  632. var node = editor.getBody();
  633. done(editor, currentSearchState, false);
  634. if (inSelection) {
  635. return findAndMarkInSelection(editor.dom, pattern, editor.selection, marker);
  636. } else {
  637. return findAndMark(editor.dom, pattern, node, marker);
  638. }
  639. };
  640. var unwrap = function (node) {
  641. var parentNode = node.parentNode;
  642. if (node.firstChild) {
  643. parentNode.insertBefore(node.firstChild, node);
  644. }
  645. node.parentNode.removeChild(node);
  646. };
  647. var findSpansByIndex = function (editor, index) {
  648. var spans = [];
  649. var nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
  650. if (nodes.length) {
  651. for (var i = 0; i < nodes.length; i++) {
  652. var nodeIndex = getElmIndex(nodes[i]);
  653. if (nodeIndex === null || !nodeIndex.length) {
  654. continue;
  655. }
  656. if (nodeIndex === index.toString()) {
  657. spans.push(nodes[i]);
  658. }
  659. }
  660. }
  661. return spans;
  662. };
  663. var moveSelection = function (editor, currentSearchState, forward) {
  664. var searchState = currentSearchState.get();
  665. var testIndex = searchState.index;
  666. var dom = editor.dom;
  667. forward = forward !== false;
  668. if (forward) {
  669. if (testIndex + 1 === searchState.count) {
  670. testIndex = 0;
  671. } else {
  672. testIndex++;
  673. }
  674. } else {
  675. if (testIndex - 1 === -1) {
  676. testIndex = searchState.count - 1;
  677. } else {
  678. testIndex--;
  679. }
  680. }
  681. dom.removeClass(findSpansByIndex(editor, searchState.index), 'mce-match-marker-selected');
  682. var spans = findSpansByIndex(editor, testIndex);
  683. if (spans.length) {
  684. dom.addClass(findSpansByIndex(editor, testIndex), 'mce-match-marker-selected');
  685. editor.selection.scrollIntoView(spans[0]);
  686. return testIndex;
  687. }
  688. return -1;
  689. };
  690. var removeNode = function (dom, node) {
  691. var parent = node.parentNode;
  692. dom.remove(node);
  693. if (dom.isEmpty(parent)) {
  694. dom.remove(parent);
  695. }
  696. };
  697. var escapeSearchText = function (text, wholeWord) {
  698. var escapedText = text.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&').replace(/\s/g, '[^\\S\\r\\n\\uFEFF]');
  699. var wordRegex = '(' + escapedText + ')';
  700. return wholeWord ? '(?:^|\\s|' + punctuation() + ')' + wordRegex + ('(?=$|\\s|' + punctuation() + ')') : wordRegex;
  701. };
  702. var find = function (editor, currentSearchState, text, matchCase, wholeWord, inSelection) {
  703. var selection = editor.selection;
  704. var escapedText = escapeSearchText(text, wholeWord);
  705. var isForwardSelection = selection.isForward();
  706. var pattern = {
  707. regex: new RegExp(escapedText, matchCase ? 'g' : 'gi'),
  708. matchIndex: 1
  709. };
  710. var count = markAllMatches(editor, currentSearchState, pattern, inSelection);
  711. if (global$2.browser.isSafari()) {
  712. selection.setRng(selection.getRng(), isForwardSelection);
  713. }
  714. if (count) {
  715. var newIndex = moveSelection(editor, currentSearchState, true);
  716. currentSearchState.set({
  717. index: newIndex,
  718. count: count,
  719. text: text,
  720. matchCase: matchCase,
  721. wholeWord: wholeWord,
  722. inSelection: inSelection
  723. });
  724. }
  725. return count;
  726. };
  727. var next = function (editor, currentSearchState) {
  728. var index = moveSelection(editor, currentSearchState, true);
  729. currentSearchState.set(__assign(__assign({}, currentSearchState.get()), { index: index }));
  730. };
  731. var prev = function (editor, currentSearchState) {
  732. var index = moveSelection(editor, currentSearchState, false);
  733. currentSearchState.set(__assign(__assign({}, currentSearchState.get()), { index: index }));
  734. };
  735. var isMatchSpan = function (node) {
  736. var matchIndex = getElmIndex(node);
  737. return matchIndex !== null && matchIndex.length > 0;
  738. };
  739. var replace = function (editor, currentSearchState, text, forward, all) {
  740. var searchState = currentSearchState.get();
  741. var currentIndex = searchState.index;
  742. var currentMatchIndex, nextIndex = currentIndex;
  743. forward = forward !== false;
  744. var node = editor.getBody();
  745. var nodes = global$1.grep(global$1.toArray(node.getElementsByTagName('span')), isMatchSpan);
  746. for (var i = 0; i < nodes.length; i++) {
  747. var nodeIndex = getElmIndex(nodes[i]);
  748. var matchIndex = currentMatchIndex = parseInt(nodeIndex, 10);
  749. if (all || matchIndex === searchState.index) {
  750. if (text.length) {
  751. nodes[i].firstChild.nodeValue = text;
  752. unwrap(nodes[i]);
  753. } else {
  754. removeNode(editor.dom, nodes[i]);
  755. }
  756. while (nodes[++i]) {
  757. matchIndex = parseInt(getElmIndex(nodes[i]), 10);
  758. if (matchIndex === currentMatchIndex) {
  759. removeNode(editor.dom, nodes[i]);
  760. } else {
  761. i--;
  762. break;
  763. }
  764. }
  765. if (forward) {
  766. nextIndex--;
  767. }
  768. } else if (currentMatchIndex > currentIndex) {
  769. nodes[i].setAttribute('data-mce-index', String(currentMatchIndex - 1));
  770. }
  771. }
  772. currentSearchState.set(__assign(__assign({}, searchState), {
  773. count: all ? 0 : searchState.count - 1,
  774. index: nextIndex
  775. }));
  776. if (forward) {
  777. next(editor, currentSearchState);
  778. } else {
  779. prev(editor, currentSearchState);
  780. }
  781. return !all && currentSearchState.get().count > 0;
  782. };
  783. var done = function (editor, currentSearchState, keepEditorSelection) {
  784. var startContainer, endContainer;
  785. var searchState = currentSearchState.get();
  786. var nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
  787. for (var i = 0; i < nodes.length; i++) {
  788. var nodeIndex = getElmIndex(nodes[i]);
  789. if (nodeIndex !== null && nodeIndex.length) {
  790. if (nodeIndex === searchState.index.toString()) {
  791. if (!startContainer) {
  792. startContainer = nodes[i].firstChild;
  793. }
  794. endContainer = nodes[i].firstChild;
  795. }
  796. unwrap(nodes[i]);
  797. }
  798. }
  799. currentSearchState.set(__assign(__assign({}, searchState), {
  800. index: -1,
  801. count: 0,
  802. text: ''
  803. }));
  804. if (startContainer && endContainer) {
  805. var rng = editor.dom.createRng();
  806. rng.setStart(startContainer, 0);
  807. rng.setEnd(endContainer, endContainer.data.length);
  808. if (keepEditorSelection !== false) {
  809. editor.selection.setRng(rng);
  810. }
  811. return rng;
  812. }
  813. };
  814. var hasNext = function (editor, currentSearchState) {
  815. return currentSearchState.get().count > 1;
  816. };
  817. var hasPrev = function (editor, currentSearchState) {
  818. return currentSearchState.get().count > 1;
  819. };
  820. var get = function (editor, currentState) {
  821. var done$1 = function (keepEditorSelection) {
  822. return done(editor, currentState, keepEditorSelection);
  823. };
  824. var find$1 = function (text, matchCase, wholeWord, inSelection) {
  825. if (inSelection === void 0) {
  826. inSelection = false;
  827. }
  828. return find(editor, currentState, text, matchCase, wholeWord, inSelection);
  829. };
  830. var next$1 = function () {
  831. return next(editor, currentState);
  832. };
  833. var prev$1 = function () {
  834. return prev(editor, currentState);
  835. };
  836. var replace$1 = function (text, forward, all) {
  837. return replace(editor, currentState, text, forward, all);
  838. };
  839. return {
  840. done: done$1,
  841. find: find$1,
  842. next: next$1,
  843. prev: prev$1,
  844. replace: replace$1
  845. };
  846. };
  847. var singleton = function (doRevoke) {
  848. var subject = Cell(Optional.none());
  849. var revoke = function () {
  850. return subject.get().each(doRevoke);
  851. };
  852. var clear = function () {
  853. revoke();
  854. subject.set(Optional.none());
  855. };
  856. var isSet = function () {
  857. return subject.get().isSome();
  858. };
  859. var get = function () {
  860. return subject.get();
  861. };
  862. var set = function (s) {
  863. revoke();
  864. subject.set(Optional.some(s));
  865. };
  866. return {
  867. clear: clear,
  868. isSet: isSet,
  869. get: get,
  870. set: set
  871. };
  872. };
  873. var value = function () {
  874. var subject = singleton(noop);
  875. var on = function (f) {
  876. return subject.get().each(f);
  877. };
  878. return __assign(__assign({}, subject), { on: on });
  879. };
  880. var open = function (editor, currentSearchState) {
  881. var dialogApi = value();
  882. editor.undoManager.add();
  883. var selectedText = global$1.trim(editor.selection.getContent({ format: 'text' }));
  884. var updateButtonStates = function (api) {
  885. var updateNext = hasNext(editor, currentSearchState) ? api.enable : api.disable;
  886. updateNext('next');
  887. var updatePrev = hasPrev(editor, currentSearchState) ? api.enable : api.disable;
  888. updatePrev('prev');
  889. };
  890. var updateSearchState = function (api) {
  891. var data = api.getData();
  892. var current = currentSearchState.get();
  893. currentSearchState.set(__assign(__assign({}, current), {
  894. matchCase: data.matchcase,
  895. wholeWord: data.wholewords,
  896. inSelection: data.inselection
  897. }));
  898. };
  899. var disableAll = function (api, disable) {
  900. var buttons = [
  901. 'replace',
  902. 'replaceall',
  903. 'prev',
  904. 'next'
  905. ];
  906. var toggle = disable ? api.disable : api.enable;
  907. each(buttons, toggle);
  908. };
  909. var notFoundAlert = function (api) {
  910. editor.windowManager.alert('Could not find the specified string.', function () {
  911. api.focus('findtext');
  912. });
  913. };
  914. var focusButtonIfRequired = function (api, name) {
  915. if (global$2.browser.isSafari() && global$2.deviceType.isTouch() && (name === 'find' || name === 'replace' || name === 'replaceall')) {
  916. api.focus(name);
  917. }
  918. };
  919. var reset = function (api) {
  920. done(editor, currentSearchState, false);
  921. disableAll(api, true);
  922. updateButtonStates(api);
  923. };
  924. var doFind = function (api) {
  925. var data = api.getData();
  926. var last = currentSearchState.get();
  927. if (!data.findtext.length) {
  928. reset(api);
  929. return;
  930. }
  931. if (last.text === data.findtext && last.matchCase === data.matchcase && last.wholeWord === data.wholewords) {
  932. next(editor, currentSearchState);
  933. } else {
  934. var count = find(editor, currentSearchState, data.findtext, data.matchcase, data.wholewords, data.inselection);
  935. if (count <= 0) {
  936. notFoundAlert(api);
  937. }
  938. disableAll(api, count === 0);
  939. }
  940. updateButtonStates(api);
  941. };
  942. var initialState = currentSearchState.get();
  943. var initialData = {
  944. findtext: selectedText,
  945. replacetext: '',
  946. wholewords: initialState.wholeWord,
  947. matchcase: initialState.matchCase,
  948. inselection: initialState.inSelection
  949. };
  950. var spec = {
  951. title: 'Find and Replace',
  952. size: 'normal',
  953. body: {
  954. type: 'panel',
  955. items: [
  956. {
  957. type: 'bar',
  958. items: [
  959. {
  960. type: 'input',
  961. name: 'findtext',
  962. placeholder: 'Find',
  963. maximized: true,
  964. inputMode: 'search'
  965. },
  966. {
  967. type: 'button',
  968. name: 'prev',
  969. text: 'Previous',
  970. icon: 'action-prev',
  971. disabled: true,
  972. borderless: true
  973. },
  974. {
  975. type: 'button',
  976. name: 'next',
  977. text: 'Next',
  978. icon: 'action-next',
  979. disabled: true,
  980. borderless: true
  981. }
  982. ]
  983. },
  984. {
  985. type: 'input',
  986. name: 'replacetext',
  987. placeholder: 'Replace with',
  988. inputMode: 'search'
  989. }
  990. ]
  991. },
  992. buttons: [
  993. {
  994. type: 'menu',
  995. name: 'options',
  996. icon: 'preferences',
  997. tooltip: 'Preferences',
  998. align: 'start',
  999. items: [
  1000. {
  1001. type: 'togglemenuitem',
  1002. name: 'matchcase',
  1003. text: 'Match case'
  1004. },
  1005. {
  1006. type: 'togglemenuitem',
  1007. name: 'wholewords',
  1008. text: 'Find whole words only'
  1009. },
  1010. {
  1011. type: 'togglemenuitem',
  1012. name: 'inselection',
  1013. text: 'Find in selection'
  1014. }
  1015. ]
  1016. },
  1017. {
  1018. type: 'custom',
  1019. name: 'find',
  1020. text: 'Find',
  1021. primary: true
  1022. },
  1023. {
  1024. type: 'custom',
  1025. name: 'replace',
  1026. text: 'Replace',
  1027. disabled: true
  1028. },
  1029. {
  1030. type: 'custom',
  1031. name: 'replaceall',
  1032. text: 'Replace all',
  1033. disabled: true
  1034. }
  1035. ],
  1036. initialData: initialData,
  1037. onChange: function (api, details) {
  1038. if (details.name === 'findtext' && currentSearchState.get().count > 0) {
  1039. reset(api);
  1040. }
  1041. },
  1042. onAction: function (api, details) {
  1043. var data = api.getData();
  1044. switch (details.name) {
  1045. case 'find':
  1046. doFind(api);
  1047. break;
  1048. case 'replace':
  1049. if (!replace(editor, currentSearchState, data.replacetext)) {
  1050. reset(api);
  1051. } else {
  1052. updateButtonStates(api);
  1053. }
  1054. break;
  1055. case 'replaceall':
  1056. replace(editor, currentSearchState, data.replacetext, true, true);
  1057. reset(api);
  1058. break;
  1059. case 'prev':
  1060. prev(editor, currentSearchState);
  1061. updateButtonStates(api);
  1062. break;
  1063. case 'next':
  1064. next(editor, currentSearchState);
  1065. updateButtonStates(api);
  1066. break;
  1067. case 'matchcase':
  1068. case 'wholewords':
  1069. case 'inselection':
  1070. updateSearchState(api);
  1071. reset(api);
  1072. break;
  1073. }
  1074. focusButtonIfRequired(api, details.name);
  1075. },
  1076. onSubmit: function (api) {
  1077. doFind(api);
  1078. focusButtonIfRequired(api, 'find');
  1079. },
  1080. onClose: function () {
  1081. editor.focus();
  1082. done(editor, currentSearchState);
  1083. editor.undoManager.add();
  1084. }
  1085. };
  1086. dialogApi.set(editor.windowManager.open(spec, { inline: 'toolbar' }));
  1087. };
  1088. var register$1 = function (editor, currentSearchState) {
  1089. editor.addCommand('SearchReplace', function () {
  1090. open(editor, currentSearchState);
  1091. });
  1092. };
  1093. var showDialog = function (editor, currentSearchState) {
  1094. return function () {
  1095. open(editor, currentSearchState);
  1096. };
  1097. };
  1098. var register = function (editor, currentSearchState) {
  1099. editor.ui.registry.addMenuItem('searchreplace', {
  1100. text: 'Find and replace...',
  1101. shortcut: 'Meta+F',
  1102. onAction: showDialog(editor, currentSearchState),
  1103. icon: 'search'
  1104. });
  1105. editor.ui.registry.addButton('searchreplace', {
  1106. tooltip: 'Find and replace',
  1107. onAction: showDialog(editor, currentSearchState),
  1108. icon: 'search'
  1109. });
  1110. editor.shortcuts.add('Meta+F', '', showDialog(editor, currentSearchState));
  1111. };
  1112. function Plugin () {
  1113. global$3.add('searchreplace', function (editor) {
  1114. var currentSearchState = Cell({
  1115. index: -1,
  1116. count: 0,
  1117. text: '',
  1118. matchCase: false,
  1119. wholeWord: false,
  1120. inSelection: false
  1121. });
  1122. register$1(editor, currentSearchState);
  1123. register(editor, currentSearchState);
  1124. return get(editor, currentSearchState);
  1125. });
  1126. }
  1127. Plugin();
  1128. }());