plugin.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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.1.0 (2019-10-17)
  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. parentNode.insertBefore(element.firstChild, element);
  246. element.parentNode.removeChild(element);
  247. }
  248. function hasClass(elm) {
  249. return elm.className.indexOf('mce-spellchecker-word') !== -1;
  250. }
  251. function getWrappersByIndex(index) {
  252. var elements = node.getElementsByTagName('*'), wrappers = [];
  253. index = typeof index === 'number' ? '' + index : null;
  254. for (var i = 0; i < elements.length; i++) {
  255. var element = elements[i], dataIndex = element.getAttribute('data-mce-index');
  256. if (dataIndex !== null && dataIndex.length && hasClass(element)) {
  257. if (dataIndex === index || index === null) {
  258. wrappers.push(element);
  259. }
  260. }
  261. }
  262. return wrappers;
  263. }
  264. function indexOf(match) {
  265. var i = matches.length;
  266. while (i--) {
  267. if (matches[i] === match) {
  268. return i;
  269. }
  270. }
  271. return -1;
  272. }
  273. function filter(callback) {
  274. var filteredMatches = [];
  275. each(function (match, i) {
  276. if (callback(match, i)) {
  277. filteredMatches.push(match);
  278. }
  279. });
  280. matches = filteredMatches;
  281. return this;
  282. }
  283. function each(callback) {
  284. for (var i = 0, l = matches.length; i < l; i++) {
  285. if (callback(matches[i], i) === false) {
  286. break;
  287. }
  288. }
  289. return this;
  290. }
  291. function wrap(callback) {
  292. if (matches.length) {
  293. stepThroughMatches(node, matches, genReplacer(callback));
  294. }
  295. return this;
  296. }
  297. function find(regex, data) {
  298. if (text && regex.global) {
  299. while (m = regex.exec(text)) {
  300. matches.push(createMatch(m, data));
  301. }
  302. }
  303. return this;
  304. }
  305. function unwrap(match) {
  306. var i;
  307. var elements = getWrappersByIndex(match ? indexOf(match) : null);
  308. i = elements.length;
  309. while (i--) {
  310. unwrapElement(elements[i]);
  311. }
  312. return this;
  313. }
  314. function matchFromElement(element) {
  315. return matches[element.getAttribute('data-mce-index')];
  316. }
  317. function elementFromMatch(match) {
  318. return getWrappersByIndex(indexOf(match))[0];
  319. }
  320. function add(start, length, data) {
  321. matches.push({
  322. start: start,
  323. end: start + length,
  324. text: text.substr(start, length),
  325. data: data
  326. });
  327. return this;
  328. }
  329. function rangeFromMatch(match) {
  330. var wrappers = getWrappersByIndex(indexOf(match));
  331. var rng = editor.dom.createRng();
  332. rng.setStartBefore(wrappers[0]);
  333. rng.setEndAfter(wrappers[wrappers.length - 1]);
  334. return rng;
  335. }
  336. function replace(match, text) {
  337. var rng = rangeFromMatch(match);
  338. rng.deleteContents();
  339. if (text.length > 0) {
  340. rng.insertNode(editor.dom.doc.createTextNode(text));
  341. }
  342. return rng;
  343. }
  344. function reset() {
  345. matches.splice(0, matches.length);
  346. unwrap();
  347. return this;
  348. }
  349. text = getText(node);
  350. return {
  351. text: text,
  352. matches: matches,
  353. each: each,
  354. filter: filter,
  355. reset: reset,
  356. matchFromElement: matchFromElement,
  357. elementFromMatch: elementFromMatch,
  358. find: find,
  359. add: add,
  360. wrap: wrap,
  361. unwrap: unwrap,
  362. replace: replace,
  363. rangeFromMatch: rangeFromMatch,
  364. indexOf: indexOf
  365. };
  366. };
  367. var getTextMatcher = function (editor, textMatcherState) {
  368. if (!textMatcherState.get()) {
  369. var textMatcher = DomTextMatcher(editor.getBody(), editor);
  370. textMatcherState.set(textMatcher);
  371. }
  372. return textMatcherState.get();
  373. };
  374. var isEmpty = function (obj) {
  375. for (var _ in obj) {
  376. return false;
  377. }
  378. return true;
  379. };
  380. var defaultSpellcheckCallback = function (editor, pluginUrl, currentLanguageState) {
  381. return function (method, text, doneCallback, errorCallback) {
  382. var data = {
  383. method: method,
  384. lang: currentLanguageState.get()
  385. };
  386. var postData = '';
  387. data[method === 'addToDictionary' ? 'word' : 'text'] = text;
  388. global$1.each(data, function (value, key) {
  389. if (postData) {
  390. postData += '&';
  391. }
  392. postData += key + '=' + encodeURIComponent(value);
  393. });
  394. global$3.send({
  395. url: new global$2(pluginUrl).toAbsolute(Settings.getRpcUrl(editor)),
  396. type: 'post',
  397. content_type: 'application/x-www-form-urlencoded',
  398. data: postData,
  399. success: function (result) {
  400. var parseResult = JSON.parse(result);
  401. if (!parseResult) {
  402. var message = editor.translate('Server response wasn\'t proper JSON.');
  403. errorCallback(message);
  404. } else if (parseResult.error) {
  405. errorCallback(parseResult.error);
  406. } else {
  407. doneCallback(parseResult);
  408. }
  409. },
  410. error: function () {
  411. var message = editor.translate('The spelling service was not found: (') + Settings.getRpcUrl(editor) + editor.translate(')');
  412. errorCallback(message);
  413. }
  414. });
  415. };
  416. };
  417. var sendRpcCall = function (editor, pluginUrl, currentLanguageState, name, data, successCallback, errorCallback) {
  418. var userSpellcheckCallback = Settings.getSpellcheckerCallback(editor);
  419. var spellCheckCallback = userSpellcheckCallback ? userSpellcheckCallback : defaultSpellcheckCallback(editor, pluginUrl, currentLanguageState);
  420. spellCheckCallback.call(editor.plugins.spellchecker, name, data, successCallback, errorCallback);
  421. };
  422. var spellcheck = function (editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState) {
  423. if (finish(editor, startedState, textMatcherState)) {
  424. return;
  425. }
  426. var errorCallback = function (message) {
  427. editor.notificationManager.open({
  428. text: message,
  429. type: 'error'
  430. });
  431. editor.setProgressState(false);
  432. finish(editor, startedState, textMatcherState);
  433. };
  434. var successCallback = function (data) {
  435. markErrors(editor, startedState, textMatcherState, lastSuggestionsState, data);
  436. };
  437. editor.setProgressState(true);
  438. sendRpcCall(editor, pluginUrl, currentLanguageState, 'spellcheck', getTextMatcher(editor, textMatcherState).text, successCallback, errorCallback);
  439. editor.focus();
  440. };
  441. var checkIfFinished = function (editor, startedState, textMatcherState) {
  442. if (!editor.dom.select('span.mce-spellchecker-word').length) {
  443. finish(editor, startedState, textMatcherState);
  444. }
  445. };
  446. var addToDictionary = function (editor, pluginUrl, startedState, textMatcherState, currentLanguageState, word, spans) {
  447. editor.setProgressState(true);
  448. sendRpcCall(editor, pluginUrl, currentLanguageState, 'addToDictionary', word, function () {
  449. editor.setProgressState(false);
  450. editor.dom.remove(spans, true);
  451. checkIfFinished(editor, startedState, textMatcherState);
  452. }, function (message) {
  453. editor.notificationManager.open({
  454. text: message,
  455. type: 'error'
  456. });
  457. editor.setProgressState(false);
  458. });
  459. };
  460. var ignoreWord = function (editor, startedState, textMatcherState, word, spans, all) {
  461. editor.selection.collapse();
  462. if (all) {
  463. global$1.each(editor.dom.select('span.mce-spellchecker-word'), function (span) {
  464. if (span.getAttribute('data-mce-word') === word) {
  465. editor.dom.remove(span, true);
  466. }
  467. });
  468. } else {
  469. editor.dom.remove(spans, true);
  470. }
  471. checkIfFinished(editor, startedState, textMatcherState);
  472. };
  473. var finish = function (editor, startedState, textMatcherState) {
  474. var bookmark = editor.selection.getBookmark();
  475. getTextMatcher(editor, textMatcherState).reset();
  476. editor.selection.moveToBookmark(bookmark);
  477. textMatcherState.set(null);
  478. if (startedState.get()) {
  479. startedState.set(false);
  480. Events.fireSpellcheckEnd(editor);
  481. return true;
  482. }
  483. };
  484. var getElmIndex = function (elm) {
  485. var value = elm.getAttribute('data-mce-index');
  486. if (typeof value === 'number') {
  487. return '' + value;
  488. }
  489. return value;
  490. };
  491. var findSpansByIndex = function (editor, index) {
  492. var nodes;
  493. var spans = [];
  494. nodes = global$1.toArray(editor.getBody().getElementsByTagName('span'));
  495. if (nodes.length) {
  496. for (var i = 0; i < nodes.length; i++) {
  497. var nodeIndex = getElmIndex(nodes[i]);
  498. if (nodeIndex === null || !nodeIndex.length) {
  499. continue;
  500. }
  501. if (nodeIndex === index.toString()) {
  502. spans.push(nodes[i]);
  503. }
  504. }
  505. }
  506. return spans;
  507. };
  508. var markErrors = function (editor, startedState, textMatcherState, lastSuggestionsState, data) {
  509. var hasDictionarySupport = !!data.dictionary;
  510. var suggestions = data.words;
  511. editor.setProgressState(false);
  512. if (isEmpty(suggestions)) {
  513. var message = editor.translate('No misspellings found.');
  514. editor.notificationManager.open({
  515. text: message,
  516. type: 'info'
  517. });
  518. startedState.set(false);
  519. return;
  520. }
  521. lastSuggestionsState.set({
  522. suggestions: suggestions,
  523. hasDictionarySupport: hasDictionarySupport
  524. });
  525. var bookmark = editor.selection.getBookmark();
  526. getTextMatcher(editor, textMatcherState).find(Settings.getSpellcheckerWordcharPattern(editor)).filter(function (match) {
  527. return !!suggestions[match.text];
  528. }).wrap(function (match) {
  529. return editor.dom.create('span', {
  530. 'class': 'mce-spellchecker-word',
  531. 'aria-invalid': 'spelling',
  532. 'data-mce-bogus': 1,
  533. 'data-mce-word': match.text
  534. });
  535. });
  536. editor.selection.moveToBookmark(bookmark);
  537. startedState.set(true);
  538. Events.fireSpellcheckStart(editor);
  539. };
  540. var Actions = {
  541. spellcheck: spellcheck,
  542. checkIfFinished: checkIfFinished,
  543. addToDictionary: addToDictionary,
  544. ignoreWord: ignoreWord,
  545. findSpansByIndex: findSpansByIndex,
  546. getElmIndex: getElmIndex,
  547. markErrors: markErrors
  548. };
  549. var get = function (editor, startedState, lastSuggestionsState, textMatcherState, currentLanguageState, url) {
  550. var getLanguage = function () {
  551. return currentLanguageState.get();
  552. };
  553. var getWordCharPattern = function () {
  554. return Settings.getSpellcheckerWordcharPattern(editor);
  555. };
  556. var markErrors = function (data) {
  557. Actions.markErrors(editor, startedState, textMatcherState, lastSuggestionsState, data);
  558. };
  559. var getTextMatcher = function () {
  560. return textMatcherState.get();
  561. };
  562. return {
  563. getTextMatcher: getTextMatcher,
  564. getWordCharPattern: getWordCharPattern,
  565. markErrors: markErrors,
  566. getLanguage: getLanguage
  567. };
  568. };
  569. var Api = { get: get };
  570. var register = function (editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState) {
  571. editor.addCommand('mceSpellCheck', function () {
  572. Actions.spellcheck(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  573. });
  574. };
  575. var Commands = { register: register };
  576. var __assign = function () {
  577. __assign = Object.assign || function __assign(t) {
  578. for (var s, i = 1, n = arguments.length; i < n; i++) {
  579. s = arguments[i];
  580. for (var p in s)
  581. if (Object.prototype.hasOwnProperty.call(s, p))
  582. t[p] = s[p];
  583. }
  584. return t;
  585. };
  586. return __assign.apply(this, arguments);
  587. };
  588. var spellcheckerEvents = 'SpellcheckStart SpellcheckEnd';
  589. var buildMenuItems = function (listName, languageValues) {
  590. var items = [];
  591. global$1.each(languageValues, function (languageValue) {
  592. items.push({
  593. selectable: true,
  594. text: languageValue.name,
  595. data: languageValue.value
  596. });
  597. });
  598. return items;
  599. };
  600. var getItems = function (editor) {
  601. return global$1.map(Settings.getLanguages(editor).split(','), function (langPair) {
  602. langPair = langPair.split('=');
  603. return {
  604. name: langPair[0],
  605. value: langPair[1]
  606. };
  607. });
  608. };
  609. var register$1 = function (editor, pluginUrl, startedState, textMatcherState, currentLanguageState, lastSuggestionsState) {
  610. var languageMenuItems = buildMenuItems('Language', getItems(editor));
  611. var startSpellchecking = function () {
  612. Actions.spellcheck(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  613. };
  614. var buttonArgs = {
  615. tooltip: 'Spellcheck',
  616. onAction: startSpellchecking,
  617. icon: 'spell-check',
  618. onSetup: function (buttonApi) {
  619. var setButtonState = function () {
  620. buttonApi.setActive(startedState.get());
  621. };
  622. editor.on(spellcheckerEvents, setButtonState);
  623. return function () {
  624. editor.off(spellcheckerEvents, setButtonState);
  625. };
  626. }
  627. };
  628. var splitButtonArgs = __assign(__assign({}, buttonArgs), {
  629. type: 'splitbutton',
  630. select: function (value) {
  631. return value === currentLanguageState.get();
  632. },
  633. fetch: function (callback) {
  634. var items = global$1.map(languageMenuItems, function (languageItem) {
  635. return {
  636. type: 'choiceitem',
  637. value: languageItem.data,
  638. text: languageItem.text
  639. };
  640. });
  641. callback(items);
  642. },
  643. onItemAction: function (splitButtonApi, value) {
  644. currentLanguageState.set(value);
  645. }
  646. });
  647. if (languageMenuItems.length > 1) {
  648. editor.ui.registry.addSplitButton('spellchecker', splitButtonArgs);
  649. } else {
  650. editor.ui.registry.addToggleButton('spellchecker', buttonArgs);
  651. }
  652. editor.ui.registry.addToggleMenuItem('spellchecker', {
  653. text: 'Spellcheck',
  654. onSetup: function (menuApi) {
  655. menuApi.setActive(startedState.get());
  656. var setMenuItemCheck = function () {
  657. menuApi.setActive(startedState.get());
  658. };
  659. editor.on(spellcheckerEvents, setMenuItemCheck);
  660. return function () {
  661. editor.off(spellcheckerEvents, setMenuItemCheck);
  662. };
  663. },
  664. onAction: startSpellchecking
  665. });
  666. };
  667. var Buttons = { register: register$1 };
  668. var ignoreAll = true;
  669. var getSuggestions = function (editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState, word, spans) {
  670. var items = [];
  671. var suggestions = lastSuggestionsState.get().suggestions[word];
  672. global$1.each(suggestions, function (suggestion) {
  673. items.push({
  674. text: suggestion,
  675. onAction: function () {
  676. editor.insertContent(editor.dom.encode(suggestion));
  677. editor.dom.remove(spans);
  678. Actions.checkIfFinished(editor, startedState, textMatcherState);
  679. }
  680. });
  681. });
  682. var hasDictionarySupport = lastSuggestionsState.get().hasDictionarySupport;
  683. if (hasDictionarySupport) {
  684. items.push({ type: 'separator' });
  685. items.push({
  686. text: 'Add to dictionary',
  687. onAction: function () {
  688. Actions.addToDictionary(editor, pluginUrl, startedState, textMatcherState, currentLanguageState, word, spans);
  689. }
  690. });
  691. }
  692. items.push.apply(items, [
  693. { type: 'separator' },
  694. {
  695. text: 'Ignore',
  696. onAction: function () {
  697. Actions.ignoreWord(editor, startedState, textMatcherState, word, spans);
  698. }
  699. },
  700. {
  701. text: 'Ignore all',
  702. onAction: function () {
  703. Actions.ignoreWord(editor, startedState, textMatcherState, word, spans, ignoreAll);
  704. }
  705. }
  706. ]);
  707. return items;
  708. };
  709. var setup = function (editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState) {
  710. var update = function (element) {
  711. var target = element;
  712. if (target.className === 'mce-spellchecker-word') {
  713. var spans = Actions.findSpansByIndex(editor, Actions.getElmIndex(target));
  714. if (spans.length > 0) {
  715. var rng = editor.dom.createRng();
  716. rng.setStartBefore(spans[0]);
  717. rng.setEndAfter(spans[spans.length - 1]);
  718. editor.selection.setRng(rng);
  719. return getSuggestions(editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState, target.getAttribute('data-mce-word'), spans);
  720. }
  721. } else {
  722. return [];
  723. }
  724. };
  725. editor.ui.registry.addContextMenu('spellchecker', { update: update });
  726. };
  727. var SuggestionsMenu = { setup: setup };
  728. function Plugin () {
  729. global.add('spellchecker', function (editor, pluginUrl) {
  730. if (DetectProPlugin.hasProPlugin(editor) === false) {
  731. var startedState = Cell(false);
  732. var currentLanguageState = Cell(Settings.getLanguage(editor));
  733. var textMatcherState = Cell(null);
  734. var lastSuggestionsState = Cell(null);
  735. Buttons.register(editor, pluginUrl, startedState, textMatcherState, currentLanguageState, lastSuggestionsState);
  736. SuggestionsMenu.setup(editor, pluginUrl, lastSuggestionsState, startedState, textMatcherState, currentLanguageState);
  737. Commands.register(editor, pluginUrl, startedState, textMatcherState, lastSuggestionsState, currentLanguageState);
  738. return Api.get(editor, startedState, lastSuggestionsState, textMatcherState, currentLanguageState, pluginUrl);
  739. }
  740. });
  741. }
  742. Plugin();
  743. }(window));