123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735 |
- /**
- * Copyright (c) Tiny Technologies, Inc. All rights reserved.
- * Licensed under the LGPL or a commercial license.
- * For LGPL see License.txt in the project root for license information.
- * For commercial licenses see https://www.tiny.cloud/
- *
- * Version: 5.0.1 (2019-02-21)
- */
- (function () {
- var searchreplace = (function () {
- 'use strict';
- var Cell = function (initial) {
- var value = initial;
- var get = function () {
- return value;
- };
- var set = function (v) {
- value = v;
- };
- var clone = function () {
- return Cell(get());
- };
- return {
- get: get,
- set: set,
- clone: clone
- };
- };
- var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
- var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
- function isContentEditableFalse(node) {
- return node && node.nodeType === 1 && node.contentEditable === 'false';
- }
- function findAndReplaceDOMText(regex, node, replacementNode, captureGroup, schema) {
- var m;
- var matches = [];
- var text, count = 0, doc;
- var blockElementsMap, hiddenTextElementsMap, shortEndedElementsMap;
- doc = node.ownerDocument;
- blockElementsMap = schema.getBlockElements();
- hiddenTextElementsMap = schema.getWhiteSpaceElements();
- shortEndedElementsMap = schema.getShortEndedElements();
- function getMatchIndexes(m, captureGroup) {
- captureGroup = captureGroup || 0;
- if (!m[0]) {
- throw new Error('findAndReplaceDOMText cannot handle zero-length matches');
- }
- var index = m.index;
- if (captureGroup > 0) {
- var cg = m[captureGroup];
- if (!cg) {
- throw new Error('Invalid capture group');
- }
- index += m[0].indexOf(cg);
- m[0] = cg;
- }
- return [
- index,
- index + m[0].length,
- [m[0]]
- ];
- }
- function getText(node) {
- var txt;
- if (node.nodeType === 3) {
- return node.data;
- }
- if (hiddenTextElementsMap[node.nodeName] && !blockElementsMap[node.nodeName]) {
- return '';
- }
- txt = '';
- if (isContentEditableFalse(node)) {
- return '\n';
- }
- if (blockElementsMap[node.nodeName] || shortEndedElementsMap[node.nodeName]) {
- txt += '\n';
- }
- if (node = node.firstChild) {
- do {
- txt += getText(node);
- } while (node = node.nextSibling);
- }
- return txt;
- }
- function stepThroughMatches(node, matches, replaceFn) {
- var startNode, endNode, startNodeIndex, endNodeIndex, innerNodes = [], atIndex = 0, curNode = node, matchLocation = matches.shift(), matchIndex = 0;
- out:
- while (true) {
- if (blockElementsMap[curNode.nodeName] || shortEndedElementsMap[curNode.nodeName] || isContentEditableFalse(curNode)) {
- atIndex++;
- }
- if (curNode.nodeType === 3) {
- if (!endNode && curNode.length + atIndex >= matchLocation[1]) {
- endNode = curNode;
- endNodeIndex = matchLocation[1] - atIndex;
- } else if (startNode) {
- innerNodes.push(curNode);
- }
- if (!startNode && curNode.length + atIndex > matchLocation[0]) {
- startNode = curNode;
- startNodeIndex = matchLocation[0] - atIndex;
- }
- atIndex += curNode.length;
- }
- if (startNode && endNode) {
- curNode = replaceFn({
- startNode: startNode,
- startNodeIndex: startNodeIndex,
- endNode: endNode,
- endNodeIndex: endNodeIndex,
- innerNodes: innerNodes,
- match: matchLocation[2],
- matchIndex: matchIndex
- });
- atIndex -= endNode.length - endNodeIndex;
- startNode = null;
- endNode = null;
- innerNodes = [];
- matchLocation = matches.shift();
- matchIndex++;
- if (!matchLocation) {
- break;
- }
- } else if ((!hiddenTextElementsMap[curNode.nodeName] || blockElementsMap[curNode.nodeName]) && curNode.firstChild) {
- if (!isContentEditableFalse(curNode)) {
- curNode = curNode.firstChild;
- continue;
- }
- } else if (curNode.nextSibling) {
- curNode = curNode.nextSibling;
- continue;
- }
- while (true) {
- if (curNode.nextSibling) {
- curNode = curNode.nextSibling;
- break;
- } else if (curNode.parentNode !== node) {
- curNode = curNode.parentNode;
- } else {
- break out;
- }
- }
- }
- }
- function genReplacer(nodeName) {
- var makeReplacementNode;
- if (typeof nodeName !== 'function') {
- var stencilNode_1 = nodeName.nodeType ? nodeName : doc.createElement(nodeName);
- makeReplacementNode = function (fill, matchIndex) {
- var clone = stencilNode_1.cloneNode(false);
- clone.setAttribute('data-mce-index', matchIndex);
- if (fill) {
- clone.appendChild(doc.createTextNode(fill));
- }
- return clone;
- };
- } else {
- makeReplacementNode = nodeName;
- }
- return function (range) {
- var before;
- var after;
- var parentNode;
- var startNode = range.startNode;
- var endNode = range.endNode;
- var matchIndex = range.matchIndex;
- if (startNode === endNode) {
- var node_1 = startNode;
- parentNode = node_1.parentNode;
- if (range.startNodeIndex > 0) {
- before = doc.createTextNode(node_1.data.substring(0, range.startNodeIndex));
- parentNode.insertBefore(before, node_1);
- }
- var el = makeReplacementNode(range.match[0], matchIndex);
- parentNode.insertBefore(el, node_1);
- if (range.endNodeIndex < node_1.length) {
- after = doc.createTextNode(node_1.data.substring(range.endNodeIndex));
- parentNode.insertBefore(after, node_1);
- }
- node_1.parentNode.removeChild(node_1);
- return el;
- }
- before = doc.createTextNode(startNode.data.substring(0, range.startNodeIndex));
- after = doc.createTextNode(endNode.data.substring(range.endNodeIndex));
- var elA = makeReplacementNode(startNode.data.substring(range.startNodeIndex), matchIndex);
- for (var i = 0, l = range.innerNodes.length; i < l; ++i) {
- var innerNode = range.innerNodes[i];
- var innerEl = makeReplacementNode(innerNode.data, matchIndex);
- innerNode.parentNode.replaceChild(innerEl, innerNode);
- }
- var elB = makeReplacementNode(endNode.data.substring(0, range.endNodeIndex), matchIndex);
- parentNode = startNode.parentNode;
- parentNode.insertBefore(before, startNode);
- parentNode.insertBefore(elA, startNode);
- parentNode.removeChild(startNode);
- parentNode = endNode.parentNode;
- parentNode.insertBefore(elB, endNode);
- parentNode.insertBefore(after, endNode);
- parentNode.removeChild(endNode);
- return elB;
- };
- }
- text = getText(node);
- if (!text) {
- return;
- }
- if (regex.global) {
- while (m = regex.exec(text)) {
- matches.push(getMatchIndexes(m, captureGroup));
- }
- } else {
- m = text.match(regex);
- matches.push(getMatchIndexes(m, captureGroup));
- }
- if (matches.length) {
- count = matches.length;
- stepThroughMatches(node, matches, genReplacer(replacementNode));
- }
- return count;
- }
- var FindReplaceText = { findAndReplaceDOMText: findAndReplaceDOMText };
- var getElmIndex = function (elm) {
- var value = elm.getAttribute('data-mce-index');
- if (typeof value === 'number') {
- return '' + value;
- }
- return value;
- };
- var markAllMatches = function (editor, currentIndexState, regex) {
- var node, marker;
- marker = editor.dom.create('span', { 'data-mce-bogus': 1 });
- marker.className = 'mce-match-marker';
- node = editor.getBody();
- done(editor, currentIndexState, false);
- return FindReplaceText.findAndReplaceDOMText(regex, node, marker, false, editor.schema);
- };
- var unwrap = function (node) {
- var parentNode = node.parentNode;
- if (node.firstChild) {
- parentNode.insertBefore(node.firstChild, node);
- }
- node.parentNode.removeChild(node);
- };
- var findSpansByIndex = function (editor, index) {
- var nodes;
- var spans = [];
- nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
- if (nodes.length) {
- for (var i = 0; i < nodes.length; i++) {
- var nodeIndex = getElmIndex(nodes[i]);
- if (nodeIndex === null || !nodeIndex.length) {
- continue;
- }
- if (nodeIndex === index.toString()) {
- spans.push(nodes[i]);
- }
- }
- }
- return spans;
- };
- var moveSelection = function (editor, currentIndexState, forward) {
- var testIndex = currentIndexState.get();
- var dom = editor.dom;
- forward = forward !== false;
- if (forward) {
- testIndex++;
- } else {
- testIndex--;
- }
- dom.removeClass(findSpansByIndex(editor, currentIndexState.get()), 'mce-match-marker-selected');
- var 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;
- };
- var removeNode = function (dom, node) {
- var parent = node.parentNode;
- dom.remove(node);
- if (dom.isEmpty(parent)) {
- dom.remove(parent);
- }
- };
- var find = function (editor, currentIndexState, text, matchCase, wholeWord) {
- text = text.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
- text = text.replace(/\s/g, '[^\\S\\r\\n]');
- text = wholeWord ? '\\b' + text + '\\b' : text;
- var count = markAllMatches(editor, currentIndexState, new RegExp(text, matchCase ? 'g' : 'gi'));
- if (count) {
- currentIndexState.set(-1);
- currentIndexState.set(moveSelection(editor, currentIndexState, true));
- }
- return count;
- };
- var next = function (editor, currentIndexState) {
- var index = moveSelection(editor, currentIndexState, true);
- if (index !== -1) {
- currentIndexState.set(index);
- }
- };
- var prev = function (editor, currentIndexState) {
- var index = moveSelection(editor, currentIndexState, false);
- if (index !== -1) {
- currentIndexState.set(index);
- }
- };
- var isMatchSpan = function (node) {
- var matchIndex = getElmIndex(node);
- return matchIndex !== null && matchIndex.length > 0;
- };
- var replace = function (editor, currentIndexState, text, forward, all) {
- var i, nodes, node, matchIndex, currentMatchIndex, nextIndex = currentIndexState.get(), hasMore;
- forward = forward !== false;
- node = editor.getBody();
- nodes = global$1.grep(global$1.toArray(node.getElementsByTagName('span')), isMatchSpan);
- for (i = 0; i < nodes.length; i++) {
- var nodeIndex = getElmIndex(nodes[i]);
- matchIndex = currentMatchIndex = parseInt(nodeIndex, 10);
- if (all || matchIndex === currentIndexState.get()) {
- if (text.length) {
- nodes[i].firstChild.nodeValue = 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 > currentIndexState.get()) {
- nodes[i].setAttribute('data-mce-index', currentMatchIndex - 1);
- }
- }
- currentIndexState.set(nextIndex);
- if (forward) {
- hasMore = hasNext(editor, currentIndexState);
- next(editor, currentIndexState);
- } else {
- hasMore = hasPrev(editor, currentIndexState);
- prev(editor, currentIndexState);
- }
- return !all && hasMore;
- };
- var done = function (editor, currentIndexState, keepEditorSelection) {
- var i, nodes, startContainer, endContainer;
- nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
- for (i = 0; i < nodes.length; i++) {
- var nodeIndex = getElmIndex(nodes[i]);
- if (nodeIndex !== null && nodeIndex.length) {
- if (nodeIndex === currentIndexState.get().toString()) {
- if (!startContainer) {
- startContainer = nodes[i].firstChild;
- }
- endContainer = nodes[i].firstChild;
- }
- unwrap(nodes[i]);
- }
- }
- if (startContainer && endContainer) {
- var rng = editor.dom.createRng();
- rng.setStart(startContainer, 0);
- rng.setEnd(endContainer, endContainer.data.length);
- if (keepEditorSelection !== false) {
- editor.selection.setRng(rng);
- }
- return rng;
- }
- };
- var hasNext = function (editor, currentIndexState) {
- return findSpansByIndex(editor, currentIndexState.get() + 1).length > 0;
- };
- var hasPrev = function (editor, currentIndexState) {
- return findSpansByIndex(editor, currentIndexState.get() - 1).length > 0;
- };
- var Actions = {
- done: done,
- find: find,
- next: next,
- prev: prev,
- replace: replace,
- hasNext: hasNext,
- hasPrev: hasPrev
- };
- var get = function (editor, currentIndexState) {
- var done = function (keepEditorSelection) {
- return Actions.done(editor, currentIndexState, keepEditorSelection);
- };
- var find = function (text, matchCase, wholeWord) {
- return Actions.find(editor, currentIndexState, text, matchCase, wholeWord);
- };
- var next = function () {
- return Actions.next(editor, currentIndexState);
- };
- var prev = function () {
- return Actions.prev(editor, currentIndexState);
- };
- var replace = function (text, forward, all) {
- return Actions.replace(editor, currentIndexState, text, forward, all);
- };
- return {
- done: done,
- find: find,
- next: next,
- prev: prev,
- replace: replace
- };
- };
- var Api = { get: get };
- var constant = function (value) {
- return function () {
- return value;
- };
- };
- var never = constant(false);
- var always = constant(true);
- var never$1 = never;
- var always$1 = always;
- var none = function () {
- return NONE;
- };
- var NONE = function () {
- var eq = function (o) {
- return o.isNone();
- };
- var call = function (thunk) {
- return thunk();
- };
- var id = function (n) {
- return n;
- };
- var noop = function () {
- };
- var nul = function () {
- return null;
- };
- var undef = function () {
- return undefined;
- };
- var me = {
- fold: function (n, s) {
- return n();
- },
- is: never$1,
- isSome: never$1,
- isNone: always$1,
- getOr: id,
- getOrThunk: call,
- getOrDie: function (msg) {
- throw new Error(msg || 'error: getOrDie called on none.');
- },
- getOrNull: nul,
- getOrUndefined: undef,
- or: id,
- orThunk: call,
- map: none,
- ap: none,
- each: noop,
- bind: none,
- flatten: none,
- exists: never$1,
- forall: always$1,
- filter: none,
- equals: eq,
- equals_: eq,
- toArray: function () {
- return [];
- },
- toString: constant('none()')
- };
- if (Object.freeze)
- Object.freeze(me);
- return me;
- }();
- var typeOf = function (x) {
- if (x === null)
- return 'null';
- var t = typeof x;
- if (t === 'object' && Array.prototype.isPrototypeOf(x))
- return 'array';
- if (t === 'object' && String.prototype.isPrototypeOf(x))
- return 'string';
- return t;
- };
- var isType = function (type) {
- return function (value) {
- return typeOf(value) === type;
- };
- };
- var isFunction = isType('function');
- var each = function (xs, f) {
- for (var i = 0, len = xs.length; i < len; i++) {
- var x = xs[i];
- f(x, i, xs);
- }
- };
- var slice = Array.prototype.slice;
- var from = isFunction(Array.from) ? Array.from : function (x) {
- return slice.call(x);
- };
- var open = function (editor, currentIndexState) {
- var last = {}, selectedText;
- editor.undoManager.add();
- selectedText = global$1.trim(editor.selection.getContent({ format: 'text' }));
- function updateButtonStates(api) {
- var updateNext = Actions.hasNext(editor, currentIndexState) ? api.enable : api.disable;
- updateNext('next');
- var updatePrev = Actions.hasPrev(editor, currentIndexState) ? api.enable : api.disable;
- updatePrev('prev');
- }
- var disableAll = function (api, disable) {
- var buttons = [
- 'replace',
- 'replaceall',
- 'prev',
- 'next'
- ];
- var toggle = disable ? api.disable : api.enable;
- each(buttons, toggle);
- };
- function notFoundAlert(api) {
- editor.windowManager.alert('Could not find the specified string.', function () {
- api.focus('findtext');
- });
- }
- var doSubmit = function (api) {
- var data = api.getData();
- if (!data.findtext.length) {
- Actions.done(editor, currentIndexState, false);
- disableAll(api, true);
- updateButtonStates(api);
- return;
- }
- if (last.text === data.findtext && last.caseState === data.matchcase && last.wholeWord === data.wholewords) {
- if (!Actions.hasNext(editor, currentIndexState)) {
- notFoundAlert(api);
- return;
- }
- Actions.next(editor, currentIndexState);
- updateButtonStates(api);
- return;
- }
- var count = Actions.find(editor, currentIndexState, data.findtext, data.matchcase, data.wholewords);
- if (!count) {
- notFoundAlert(api);
- }
- disableAll(api, count === 0);
- updateButtonStates(api);
- last = {
- text: data.findtext,
- caseState: data.matchcase,
- wholeWord: data.wholewords
- };
- };
- var initialData = {
- findtext: selectedText,
- replacetext: '',
- matchcase: false,
- wholewords: false
- };
- editor.windowManager.open({
- title: 'Find and Replace',
- size: 'normal',
- body: {
- type: 'panel',
- items: [
- {
- type: 'input',
- name: 'findtext',
- label: 'Find'
- },
- {
- type: 'input',
- name: 'replacetext',
- label: 'Replace with'
- },
- {
- type: 'grid',
- columns: 2,
- items: [
- {
- type: 'checkbox',
- name: 'matchcase',
- label: 'Match case'
- },
- {
- type: 'checkbox',
- name: 'wholewords',
- label: 'Find whole words only'
- }
- ]
- }
- ]
- },
- buttons: [
- {
- type: 'custom',
- name: 'find',
- text: 'Find',
- align: 'start',
- primary: true
- },
- {
- type: 'custom',
- name: 'replace',
- text: 'Replace',
- align: 'start',
- disabled: true
- },
- {
- type: 'custom',
- name: 'replaceall',
- text: 'Replace All',
- align: 'start',
- disabled: true
- },
- {
- type: 'custom',
- name: 'prev',
- text: 'Previous',
- align: 'end',
- icon: 'arrow-left',
- disabled: true
- },
- {
- type: 'custom',
- name: 'next',
- text: 'Next',
- align: 'end',
- icon: 'arrow-right',
- disabled: true
- }
- ],
- initialData: initialData,
- onAction: function (api, details) {
- var data = api.getData();
- switch (details.name) {
- case 'find':
- doSubmit(api);
- break;
- case 'replace':
- if (!Actions.replace(editor, currentIndexState, data.replacetext)) {
- disableAll(api, true);
- currentIndexState.set(-1);
- last = {};
- }
- break;
- case 'replaceall':
- Actions.replace(editor, currentIndexState, data.replacetext, true, true);
- disableAll(api, true);
- last = {};
- break;
- case 'prev':
- Actions.prev(editor, currentIndexState);
- updateButtonStates(api);
- break;
- case 'next':
- Actions.next(editor, currentIndexState);
- updateButtonStates(api);
- break;
- default:
- break;
- }
- },
- onSubmit: doSubmit,
- onClose: function () {
- editor.focus();
- Actions.done(editor, currentIndexState);
- editor.undoManager.add();
- }
- });
- };
- var Dialog = { open: open };
- var register = function (editor, currentIndexState) {
- editor.addCommand('SearchReplace', function () {
- Dialog.open(editor, currentIndexState);
- });
- };
- var Commands = { register: register };
- var showDialog = function (editor, currentIndexState) {
- return function () {
- Dialog.open(editor, currentIndexState);
- };
- };
- var register$1 = function (editor, currentIndexState) {
- editor.ui.registry.addMenuItem('searchreplace', {
- text: 'Find and replace...',
- shortcut: 'Meta+F',
- onAction: showDialog(editor, currentIndexState),
- icon: 'search'
- });
- editor.ui.registry.addButton('searchreplace', {
- tooltip: 'Find and replace',
- onAction: showDialog(editor, currentIndexState),
- icon: 'search'
- });
- editor.shortcuts.add('Meta+F', '', showDialog(editor, currentIndexState));
- };
- var Buttons = { register: register$1 };
- global.add('searchreplace', function (editor) {
- var currentIndexState = Cell(-1);
- Commands.register(editor, currentIndexState);
- Buttons.register(editor, currentIndexState);
- return Api.get(editor, currentIndexState);
- });
- function Plugin () {
- }
- return Plugin;
- }());
- })();
|