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