plugin.js 26 KB

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