plugin.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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.2.0 (2020-02-13)
  8. */
  9. (function (domGlobals) {
  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. var clone = function () {
  20. return Cell(get());
  21. };
  22. return {
  23. get: get,
  24. set: set,
  25. clone: clone
  26. };
  27. };
  28. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  29. var hasProPlugin = function (editor) {
  30. if (/(^|[ ,])tinymcespellchecker([, ]|$)/.test(editor.settings.plugins) && global.get('tinymcespellchecker')) {
  31. if (typeof domGlobals.window.console !== 'undefined' && domGlobals.window.console.log) {
  32. domGlobals.window.console.log('Spell Checker Pro is incompatible with Spell Checker plugin! ' + 'Remove \'spellchecker\' from the \'plugins\' option.');
  33. }
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. };
  39. var DetectProPlugin = { hasProPlugin: hasProPlugin };
  40. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  41. var global$2 = tinymce.util.Tools.resolve('tinymce.util.URI');
  42. var global$3 = tinymce.util.Tools.resolve('tinymce.util.XHR');
  43. var fireSpellcheckStart = function (editor) {
  44. return editor.fire('SpellcheckStart');
  45. };
  46. var fireSpellcheckEnd = function (editor) {
  47. return editor.fire('SpellcheckEnd');
  48. };
  49. var Events = {
  50. fireSpellcheckStart: fireSpellcheckStart,
  51. fireSpellcheckEnd: fireSpellcheckEnd
  52. };
  53. var getLanguages = function (editor) {
  54. var defaultLanguages = 'English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr_FR,German=de,Italian=it,Polish=pl,Portuguese=pt_BR,Spanish=es,Swedish=sv';
  55. return editor.getParam('spellchecker_languages', defaultLanguages);
  56. };
  57. var getLanguage = function (editor) {
  58. var defaultLanguage = editor.getParam('language', 'en');
  59. return editor.getParam('spellchecker_language', defaultLanguage);
  60. };
  61. var getRpcUrl = function (editor) {
  62. return editor.getParam('spellchecker_rpc_url');
  63. };
  64. var getSpellcheckerCallback = function (editor) {
  65. return editor.getParam('spellchecker_callback');
  66. };
  67. var getSpellcheckerWordcharPattern = function (editor) {
  68. var defaultPattern = new RegExp('[^' + '\\s!"#$%&()*+,-./:;<=>?@[\\]^_{|}`' + '\xA7\xA9\xAB\xAE\xB1\xB6\xB7\xB8\xBB' + '\xBC\xBD\xBE\xBF\xD7\xF7\xA4\u201D\u201C\u201E\xA0\u2002\u2003\u2009' + ']+', 'g');
  69. return editor.getParam('spellchecker_wordchar_pattern', defaultPattern);
  70. };
  71. var Settings = {
  72. getLanguages: getLanguages,
  73. getLanguage: getLanguage,
  74. getRpcUrl: getRpcUrl,
  75. getSpellcheckerCallback: getSpellcheckerCallback,
  76. getSpellcheckerWordcharPattern: getSpellcheckerWordcharPattern
  77. };
  78. function isContentEditableFalse(node) {
  79. return node && node.nodeType === 1 && node.contentEditable === 'false';
  80. }
  81. var DomTextMatcher = function (node, editor) {
  82. var m, matches = [], text;
  83. var dom = editor.dom;
  84. var blockElementsMap, hiddenTextElementsMap, shortEndedElementsMap;
  85. blockElementsMap = editor.schema.getBlockElements();
  86. hiddenTextElementsMap = editor.schema.getWhiteSpaceElements();
  87. shortEndedElementsMap = editor.schema.getShortEndedElements();
  88. function createMatch(m, data) {
  89. if (!m[0]) {
  90. throw new Error('findAndReplaceDOMText cannot handle zero-length matches');
  91. }
  92. return {
  93. start: m.index,
  94. end: m.index + m[0].length,
  95. text: m[0],
  96. data: data
  97. };
  98. }
  99. function getText(node) {
  100. var txt;
  101. if (node.nodeType === 3) {
  102. return node.data;
  103. }
  104. if (hiddenTextElementsMap[node.nodeName] && !blockElementsMap[node.nodeName]) {
  105. return '';
  106. }
  107. if (isContentEditableFalse(node)) {
  108. return '\n';
  109. }
  110. txt = '';
  111. if (blockElementsMap[node.nodeName] || shortEndedElementsMap[node.nodeName]) {
  112. txt += '\n';
  113. }
  114. if (node = node.firstChild) {
  115. do {
  116. txt += getText(node);
  117. } while (node = node.nextSibling);
  118. }
  119. return txt;
  120. }
  121. function stepThroughMatches(node, matches, replaceFn) {
  122. var startNode, endNode, startNodeIndex, endNodeIndex, innerNodes = [], atIndex = 0, curNode = node, matchLocation, matchIndex = 0;
  123. matches = matches.slice(0);
  124. matches.sort(function (a, b) {
  125. return a.start - b.start;
  126. });
  127. matchLocation = matches.shift();
  128. out:
  129. while (true) {
  130. if (blockElementsMap[curNode.nodeName] || shortEndedElementsMap[curNode.nodeName] || isContentEditableFalse(curNode)) {
  131. atIndex++;
  132. }
  133. if (curNode.nodeType === 3) {
  134. if (!endNode && curNode.length + atIndex >= matchLocation.end) {
  135. endNode = curNode;
  136. endNodeIndex = matchLocation.end - atIndex;
  137. } else if (startNode) {
  138. innerNodes.push(curNode);
  139. }
  140. if (!startNode && curNode.length + atIndex > matchLocation.start) {
  141. startNode = curNode;
  142. startNodeIndex = matchLocation.start - atIndex;
  143. }
  144. atIndex += curNode.length;
  145. }
  146. if (startNode && endNode) {
  147. curNode = replaceFn({
  148. startNode: startNode,
  149. startNodeIndex: startNodeIndex,
  150. endNode: endNode,
  151. endNodeIndex: endNodeIndex,
  152. innerNodes: innerNodes,
  153. match: matchLocation.text,
  154. matchIndex: matchIndex
  155. });
  156. atIndex -= endNode.length - endNodeIndex;
  157. startNode = null;
  158. endNode = null;
  159. innerNodes = [];
  160. matchLocation = matches.shift();
  161. matchIndex++;
  162. if (!matchLocation) {
  163. break;
  164. }
  165. } else if ((!hiddenTextElementsMap[curNode.nodeName] || blockElementsMap[curNode.nodeName]) && curNode.firstChild) {
  166. if (!isContentEditableFalse(curNode)) {
  167. curNode = curNode.firstChild;
  168. continue;
  169. }
  170. } else if (curNode.nextSibling) {
  171. curNode = curNode.nextSibling;
  172. continue;
  173. }
  174. while (true) {
  175. if (curNode.nextSibling) {
  176. curNode = curNode.nextSibling;
  177. break;
  178. } else if (curNode.parentNode !== node) {
  179. curNode = curNode.parentNode;
  180. } else {
  181. break out;
  182. }
  183. }
  184. }
  185. }
  186. function genReplacer(callback) {
  187. function makeReplacementNode(fill, matchIndex) {
  188. var match = matches[matchIndex];
  189. if (!match.stencil) {
  190. match.stencil = callback(match);
  191. }
  192. var clone = match.stencil.cloneNode(false);
  193. clone.setAttribute('data-mce-index', matchIndex);
  194. if (fill) {
  195. clone.appendChild(dom.doc.createTextNode(fill));
  196. }
  197. return clone;
  198. }
  199. return function (range) {
  200. var before;
  201. var after;
  202. var parentNode;
  203. var startNode = range.startNode;
  204. var endNode = range.endNode;
  205. var matchIndex = range.matchIndex;
  206. var doc = dom.doc;
  207. if (startNode === endNode) {
  208. var node_1 = startNode;
  209. parentNode = node_1.parentNode;
  210. if (range.startNodeIndex > 0) {
  211. before = doc.createTextNode(node_1.data.substring(0, range.startNodeIndex));
  212. parentNode.insertBefore(before, node_1);
  213. }
  214. var el = makeReplacementNode(range.match, matchIndex);
  215. parentNode.insertBefore(el, node_1);
  216. if (range.endNodeIndex < node_1.length) {
  217. after = doc.createTextNode(node_1.data.substring(range.endNodeIndex));
  218. parentNode.insertBefore(after, node_1);
  219. }
  220. node_1.parentNode.removeChild(node_1);
  221. return el;
  222. }
  223. before = doc.createTextNode(startNode.data.substring(0, range.startNodeIndex));
  224. after = doc.createTextNode(endNode.data.substring(range.endNodeIndex));
  225. var elA = makeReplacementNode(startNode.data.substring(range.startNodeIndex), matchIndex);
  226. for (var i = 0, l = range.innerNodes.length; i < l; ++i) {
  227. var innerNode = range.innerNodes[i];
  228. var innerEl = makeReplacementNode(innerNode.data, matchIndex);
  229. innerNode.parentNode.replaceChild(innerEl, innerNode);
  230. }
  231. var elB = makeReplacementNode(endNode.data.substring(0, range.endNodeIndex), matchIndex);
  232. parentNode = startNode.parentNode;
  233. parentNode.insertBefore(before, startNode);
  234. parentNode.insertBefore(elA, startNode);
  235. parentNode.removeChild(startNode);
  236. parentNode = endNode.parentNode;
  237. parentNode.insertBefore(elB, endNode);
  238. parentNode.insertBefore(after, endNode);
  239. parentNode.removeChild(endNode);
  240. return elB;
  241. };
  242. }
  243. function unwrapElement(element) {
  244. var parentNode = element.parentNode;
  245. while (element.childNodes.length > 0) {
  246. parentNode.insertBefore(element.childNodes[0], element);
  247. }
  248. parentNode.removeChild(element);
  249. }
  250. function hasClass(elm) {
  251. return elm.className.indexOf('mce-spellchecker-word') !== -1;
  252. }
  253. function getWrappersByIndex(index) {
  254. var elements = node.getElementsByTagName('*'), wrappers = [];
  255. index = typeof index === 'number' ? '' + index : null;
  256. for (var i = 0; i < elements.length; i++) {
  257. var element = elements[i], dataIndex = element.getAttribute('data-mce-index');
  258. if (dataIndex !== null && dataIndex.length && hasClass(element)) {
  259. if (dataIndex === index || index === null) {
  260. wrappers.push(element);
  261. }
  262. }
  263. }
  264. return wrappers;
  265. }
  266. function indexOf(match) {
  267. var i = matches.length;
  268. while (i--) {
  269. if (matches[i] === match) {
  270. return i;
  271. }
  272. }
  273. return -1;
  274. }
  275. function filter(callback) {
  276. var filteredMatches = [];
  277. each(function (match, i) {
  278. if (callback(match, i)) {
  279. filteredMatches.push(match);
  280. }
  281. });
  282. matches = filteredMatches;
  283. return this;
  284. }
  285. function each(callback) {
  286. for (var i = 0, l = matches.length; i < l; i++) {
  287. if (callback(matches[i], i) === false) {
  288. break;
  289. }
  290. }
  291. return this;
  292. }
  293. function wrap(callback) {
  294. if (matches.length) {
  295. stepThroughMatches(node, matches, genReplacer(callback));
  296. }
  297. return this;
  298. }
  299. function find(regex, data) {
  300. if (text && regex.global) {
  301. while (m = regex.exec(text)) {
  302. matches.push(createMatch(m, data));
  303. }
  304. }
  305. return this;
  306. }
  307. function unwrap(match) {
  308. var i;
  309. var elements = getWrappersByIndex(match ? indexOf(match) : null);
  310. i = elements.length;
  311. while (i--) {
  312. unwrapElement(elements[i]);
  313. }
  314. return this;
  315. }
  316. function matchFromElement(element) {
  317. return matches[element.getAttribute('data-mce-index')];
  318. }
  319. function elementFromMatch(match) {
  320. return getWrappersByIndex(indexOf(match))[0];
  321. }
  322. function add(start, length, data) {
  323. matches.push({
  324. start: start,
  325. end: start + length,
  326. text: text.substr(start, length),
  327. data: data
  328. });
  329. return this;
  330. }
  331. function rangeFromMatch(match) {
  332. var wrappers = getWrappersByIndex(indexOf(match));
  333. var rng = editor.dom.createRng();
  334. rng.setStartBefore(wrappers[0]);
  335. rng.setEndAfter(wrappers[wrappers.length - 1]);
  336. return rng;
  337. }
  338. function replace(match, text) {
  339. var rng = rangeFromMatch(match);
  340. rng.deleteContents();
  341. if (text.length > 0) {
  342. rng.insertNode(editor.dom.doc.createTextNode(text));
  343. }
  344. return rng;
  345. }
  346. function reset() {
  347. matches.splice(0, matches.length);
  348. unwrap();
  349. return this;
  350. }
  351. text = getText(node);
  352. return {
  353. text: text,
  354. matches: matches,
  355. each: each,
  356. filter: filter,
  357. reset: reset,
  358. matchFromElement: matchFromElement,
  359. elementFromMatch: elementFromMatch,
  360. find: find,
  361. add: add,
  362. wrap: wrap,
  363. unwrap: unwrap,
  364. replace: replace,
  365. rangeFromMatch: rangeFromMatch,
  366. indexOf: indexOf
  367. };
  368. };
  369. var noop = function () {
  370. };
  371. var constant = function (value) {
  372. return function () {
  373. return value;
  374. };
  375. };
  376. var never = constant(false);
  377. var always = constant(true);
  378. var none = function () {
  379. return NONE;
  380. };
  381. var NONE = function () {
  382. var eq = function (o) {
  383. return o.isNone();
  384. };
  385. var call = function (thunk) {
  386. return thunk();
  387. };
  388. var id = function (n) {
  389. return n;
  390. };
  391. var me = {
  392. fold: function (n, s) {
  393. return n();
  394. },
  395. is: never,
  396. isSome: never,
  397. isNone: always,
  398. getOr: id,
  399. getOrThunk: call,
  400. getOrDie: function (msg) {
  401. throw new Error(msg || 'error: getOrDie called on none.');
  402. },
  403. getOrNull: constant(null),
  404. getOrUndefined: constant(undefined),
  405. or: id,
  406. orThunk: call,
  407. map: none,
  408. each: noop,
  409. bind: none,
  410. exists: never,
  411. forall: always,
  412. filter: none,
  413. equals: eq,
  414. equals_: eq,
  415. toArray: function () {
  416. return [];
  417. },
  418. toString: constant('none()')
  419. };
  420. if (Object.freeze) {
  421. Object.freeze(me);
  422. }
  423. return me;
  424. }();
  425. var hasOwnProperty = Object.hasOwnProperty;
  426. var isEmpty = function (r) {
  427. for (var x in r) {
  428. if (hasOwnProperty.call(r, x)) {
  429. return false;
  430. }
  431. }
  432. return true;
  433. };
  434. var getTextMatcher = function (editor, textMatcherState) {
  435. if (!textMatcherState.get()) {
  436. var textMatcher = DomTextMatcher(editor.getBody(), editor);
  437. textMatcherState.set(textMatcher);
  438. }
  439. return textMatcherState.get();
  440. };
  441. var defaultSpellcheckCallback = function (editor, pluginUrl, currentLanguageState) {
  442. return function (method, text, doneCallback, errorCallback) {
  443. var data = {
  444. method: method,
  445. lang: currentLanguageState.get()
  446. };
  447. var postData = '';
  448. data[method === 'addToDictionary' ? 'word' : 'text'] = text;
  449. global$1.each(data, function (value, key) {
  450. if (postData) {
  451. postData += '&';
  452. }
  453. postData += key + '=' + encodeURIComponent(value);
  454. });
  455. global$3.send({
  456. url: new global$2(pluginUrl).toAbsolute(Settings.getRpcUrl(editor)),
  457. type: 'post',
  458. content_type: 'application/x-www-form-urlencoded',
  459. data: postData,
  460. success: function (result) {
  461. var parseResult = JSON.parse(result);
  462. if (!parseResult) {
  463. var message = editor.translate('Server response wasn\'t proper JSON.');
  464. errorCallback(message);
  465. } else if (parseResult.error) {
  466. errorCallback(parseResult.error);
  467. } else {
  468. doneCallback(parseResult);
  469. }
  470. },
  471. error: function () {
  472. var message = editor.translate('The spelling service was not found: (') + Settings.getRpcUrl(editor) + editor.translate(')');
  473. errorCallback(message);
  474. }
  475. });
  476. };
  477. };
  478. var sendRpcCall = function (editor, pluginUrl, currentLanguageState, name, data, successCallback, errorCallback) {
  479. var userSpellcheckCallback = Settings.getSpellcheckerCallback(editor);
  480. var spellCheckCallback = userSpellcheckCallback ? userSpellcheckCallback : defaultSpellcheckCallback(editor, pluginUrl, currentLanguageState);
  481. spellCheckCallback.call(editor.plugins.spellchecker, name, data, successCallback, errorCallback);
  482. };
  483. var spellcheck = function (editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState) {
  484. if (finish(editor, startedState, textMatcherState)) {
  485. return;
  486. }
  487. var errorCallback = function (message) {
  488. editor.notificationManager.open({
  489. text: message,
  490. type: 'error'
  491. });
  492. editor.setProgressState(false);
  493. finish(editor, startedState, textMatcherState);
  494. };
  495. var successCallback = function (data) {
  496. markErrors(editor, startedState, textMatcherState, lastSuggestionsState, data);
  497. };
  498. editor.setProgressState(true);
  499. sendRpcCall(editor, pluginUrl, currentLanguageState, 'spellcheck', getTextMatcher(editor, textMatcherState).text, successCallback, errorCallback);
  500. editor.focus();
  501. };
  502. var checkIfFinished = function (editor, startedState, textMatcherState) {
  503. if (!editor.dom.select('span.mce-spellchecker-word').length) {
  504. finish(editor, startedState, textMatcherState);
  505. }
  506. };
  507. var addToDictionary = function (editor, pluginUrl, startedState, textMatcherState, currentLanguageState, word, spans) {
  508. editor.setProgressState(true);
  509. sendRpcCall(editor, pluginUrl, currentLanguageState, 'addToDictionary', word, function () {
  510. editor.setProgressState(false);
  511. editor.dom.remove(spans, true);
  512. checkIfFinished(editor, startedState, textMatcherState);
  513. }, function (message) {
  514. editor.notificationManager.open({
  515. text: message,
  516. type: 'error'
  517. });
  518. editor.setProgressState(false);
  519. });
  520. };
  521. var ignoreWord = function (editor, startedState, textMatcherState, word, spans, all) {
  522. editor.selection.collapse();
  523. if (all) {
  524. global$1.each(editor.dom.select('span.mce-spellchecker-word'), function (span) {
  525. if (span.getAttribute('data-mce-word') === word) {
  526. editor.dom.remove(span, true);
  527. }
  528. });
  529. } else {
  530. editor.dom.remove(spans, true);
  531. }
  532. checkIfFinished(editor, startedState, textMatcherState);
  533. };
  534. var finish = function (editor, startedState, textMatcherState) {
  535. var bookmark = editor.selection.getBookmark();
  536. getTextMatcher(editor, textMatcherState).reset();
  537. editor.selection.moveToBookmark(bookmark);
  538. textMatcherState.set(null);
  539. if (startedState.get()) {
  540. startedState.set(false);
  541. Events.fireSpellcheckEnd(editor);
  542. return true;
  543. }
  544. };
  545. var getElmIndex = function (elm) {
  546. var value = elm.getAttribute('data-mce-index');
  547. if (typeof value === 'number') {
  548. return '' + value;
  549. }
  550. return value;
  551. };
  552. var findSpansByIndex = function (editor, index) {
  553. var nodes;
  554. var spans = [];
  555. nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
  556. if (nodes.length) {
  557. for (var i = 0; i < nodes.length; i++) {
  558. var nodeIndex = getElmIndex(nodes[i]);
  559. if (nodeIndex === null || !nodeIndex.length) {
  560. continue;
  561. }
  562. if (nodeIndex === index.toString()) {
  563. spans.push(nodes[i]);
  564. }
  565. }
  566. }
  567. return spans;
  568. };
  569. var markErrors = function (editor, startedState, textMatcherState, lastSuggestionsState, data) {
  570. var hasDictionarySupport = !!data.dictionary;
  571. var suggestions = data.words;
  572. editor.setProgressState(false);
  573. if (isEmpty(suggestions)) {
  574. var message = editor.translate('No misspellings found.');
  575. editor.notificationManager.open({
  576. text: message,
  577. type: 'info'
  578. });
  579. startedState.set(false);
  580. return;
  581. }
  582. lastSuggestionsState.set({
  583. suggestions: suggestions,
  584. hasDictionarySupport: hasDictionarySupport
  585. });
  586. var bookmark = editor.selection.getBookmark();
  587. getTextMatcher(editor, textMatcherState).find(Settings.getSpellcheckerWordcharPattern(editor)).filter(function (match) {
  588. return !!suggestions[match.text];
  589. }).wrap(function (match) {
  590. return editor.dom.create('span', {
  591. 'class': 'mce-spellchecker-word',
  592. 'aria-invalid': 'spelling',
  593. 'data-mce-bogus': 1,
  594. 'data-mce-word': match.text
  595. });
  596. });
  597. editor.selection.moveToBookmark(bookmark);
  598. startedState.set(true);
  599. Events.fireSpellcheckStart(editor);
  600. };
  601. var Actions = {
  602. spellcheck: spellcheck,
  603. checkIfFinished: checkIfFinished,
  604. addToDictionary: addToDictionary,
  605. ignoreWord: ignoreWord,
  606. findSpansByIndex: findSpansByIndex,
  607. getElmIndex: getElmIndex,
  608. markErrors: markErrors
  609. };
  610. var get = function (editor, startedState, lastSuggestionsState, textMatcherState, currentLanguageState, url) {
  611. var getLanguage = function () {
  612. return currentLanguageState.get();
  613. };
  614. var getWordCharPattern = function () {
  615. return Settings.getSpellcheckerWordcharPattern(editor);
  616. };
  617. var markErrors = function (data) {
  618. Actions.markErrors(editor, startedState, textMatcherState, lastSuggestionsState, data);
  619. };
  620. var getTextMatcher = function () {
  621. return textMatcherState.get();
  622. };
  623. return {
  624. getTextMatcher: getTextMatcher,
  625. getWordCharPattern: getWordCharPattern,
  626. markErrors: markErrors,
  627. getLanguage: getLanguage
  628. };
  629. };
  630. var Api = { get: get };
  631. var register = function (editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState) {
  632. editor.addCommand('mceSpellCheck', function () {
  633. Actions.spellcheck(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  634. });
  635. };
  636. var Commands = { register: register };
  637. var __assign = function () {
  638. __assign = Object.assign || function __assign(t) {
  639. for (var s, i = 1, n = arguments.length; i < n; i++) {
  640. s = arguments[i];
  641. for (var p in s)
  642. if (Object.prototype.hasOwnProperty.call(s, p))
  643. t[p] = s[p];
  644. }
  645. return t;
  646. };
  647. return __assign.apply(this, arguments);
  648. };
  649. var spellcheckerEvents = 'SpellcheckStart SpellcheckEnd';
  650. var buildMenuItems = function (listName, languageValues) {
  651. var items = [];
  652. global$1.each(languageValues, function (languageValue) {
  653. items.push({
  654. selectable: true,
  655. text: languageValue.name,
  656. data: languageValue.value
  657. });
  658. });
  659. return items;
  660. };
  661. var getItems = function (editor) {
  662. return global$1.map(Settings.getLanguages(editor).split(','), function (langPair) {
  663. langPair = langPair.split('=');
  664. return {
  665. name: langPair[0],
  666. value: langPair[1]
  667. };
  668. });
  669. };
  670. var register$1 = function (editor, pluginUrl, startedState, textMatcherState, currentLanguageState, lastSuggestionsState) {
  671. var languageMenuItems = buildMenuItems('Language', getItems(editor));
  672. var startSpellchecking = function () {
  673. Actions.spellcheck(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  674. };
  675. var buttonArgs = {
  676. tooltip: 'Spellcheck',
  677. onAction: startSpellchecking,
  678. icon: 'spell-check',
  679. onSetup: function (buttonApi) {
  680. var setButtonState = function () {
  681. buttonApi.setActive(startedState.get());
  682. };
  683. editor.on(spellcheckerEvents, setButtonState);
  684. return function () {
  685. editor.off(spellcheckerEvents, setButtonState);
  686. };
  687. }
  688. };
  689. var splitButtonArgs = __assign(__assign({}, buttonArgs), {
  690. type: 'splitbutton',
  691. select: function (value) {
  692. return value === currentLanguageState.get();
  693. },
  694. fetch: function (callback) {
  695. var items = global$1.map(languageMenuItems, function (languageItem) {
  696. return {
  697. type: 'choiceitem',
  698. value: languageItem.data,
  699. text: languageItem.text
  700. };
  701. });
  702. callback(items);
  703. },
  704. onItemAction: function (splitButtonApi, value) {
  705. currentLanguageState.set(value);
  706. }
  707. });
  708. if (languageMenuItems.length > 1) {
  709. editor.ui.registry.addSplitButton('spellchecker', splitButtonArgs);
  710. } else {
  711. editor.ui.registry.addToggleButton('spellchecker', buttonArgs);
  712. }
  713. editor.ui.registry.addToggleMenuItem('spellchecker', {
  714. text: 'Spellcheck',
  715. onSetup: function (menuApi) {
  716. menuApi.setActive(startedState.get());
  717. var setMenuItemCheck = function () {
  718. menuApi.setActive(startedState.get());
  719. };
  720. editor.on(spellcheckerEvents, setMenuItemCheck);
  721. return function () {
  722. editor.off(spellcheckerEvents, setMenuItemCheck);
  723. };
  724. },
  725. onAction: startSpellchecking
  726. });
  727. };
  728. var Buttons = { register: register$1 };
  729. var ignoreAll = true;
  730. var getSuggestions = function (editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState, word, spans) {
  731. var items = [];
  732. var suggestions = lastSuggestionsState.get().suggestions[word];
  733. global$1.each(suggestions, function (suggestion) {
  734. items.push({
  735. text: suggestion,
  736. onAction: function () {
  737. editor.insertContent(editor.dom.encode(suggestion));
  738. editor.dom.remove(spans);
  739. Actions.checkIfFinished(editor, startedState, textMatcherState);
  740. }
  741. });
  742. });
  743. var hasDictionarySupport = lastSuggestionsState.get().hasDictionarySupport;
  744. if (hasDictionarySupport) {
  745. items.push({ type: 'separator' });
  746. items.push({
  747. text: 'Add to dictionary',
  748. onAction: function () {
  749. Actions.addToDictionary(editor, pluginUrl, startedState, textMatcherState, currentLanguageState, word, spans);
  750. }
  751. });
  752. }
  753. items.push.apply(items, [
  754. { type: 'separator' },
  755. {
  756. text: 'Ignore',
  757. onAction: function () {
  758. Actions.ignoreWord(editor, startedState, textMatcherState, word, spans);
  759. }
  760. },
  761. {
  762. text: 'Ignore all',
  763. onAction: function () {
  764. Actions.ignoreWord(editor, startedState, textMatcherState, word, spans, ignoreAll);
  765. }
  766. }
  767. ]);
  768. return items;
  769. };
  770. var setup = function (editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState) {
  771. var update = function (element) {
  772. var target = element;
  773. if (target.className === 'mce-spellchecker-word') {
  774. var spans = Actions.findSpansByIndex(editor, Actions.getElmIndex(target));
  775. if (spans.length > 0) {
  776. var rng = editor.dom.createRng();
  777. rng.setStartBefore(spans[0]);
  778. rng.setEndAfter(spans[spans.length - 1]);
  779. editor.selection.setRng(rng);
  780. return getSuggestions(editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState, target.getAttribute('data-mce-word'), spans);
  781. }
  782. } else {
  783. return [];
  784. }
  785. };
  786. editor.ui.registry.addContextMenu('spellchecker', { update: update });
  787. };
  788. var SuggestionsMenu = { setup: setup };
  789. function Plugin () {
  790. global.add('spellchecker', function (editor, pluginUrl) {
  791. if (DetectProPlugin.hasProPlugin(editor) === false) {
  792. var startedState = Cell(false);
  793. var currentLanguageState = Cell(Settings.getLanguage(editor));
  794. var textMatcherState = Cell(null);
  795. var lastSuggestionsState = Cell(null);
  796. Buttons.register(editor, pluginUrl, startedState, textMatcherState, currentLanguageState, lastSuggestionsState);
  797. SuggestionsMenu.setup(editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState);
  798. Commands.register(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  799. return Api.get(editor, startedState, lastSuggestionsState, textMatcherState, currentLanguageState, pluginUrl);
  800. }
  801. });
  802. }
  803. Plugin();
  804. }(window));