UNPKG

webspellcheck

Version:

Simple WebSpellChecker initializer for CKEditor or Monaco Editor

116 lines (115 loc) 5.15 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.load = load; exports.init = init; exports.config = config; function load(cdnUrl) { if (cdnUrl === void 0) { cdnUrl = 'https://svc.webspellchecker.net/spellcheck31/wscbundle/wscbundle.js'; } return new Promise(function (resolve, reject) { if (typeof WEBSPELLCHECKER !== 'undefined') { console.log('[dsc] WSC already loaded'); resolve(); return; } var script = document.createElement('script'); script.src = cdnUrl; script.async = true; script.onload = function () { console.log('[dsc] WebSpellChecker loaded'); resolve(); }; script.onerror = function () { return reject(new Error('Failed to load WebSpellChecker script')); }; document.head.appendChild(script); }); } function init(editorInstance, config) { var _a, _b, _c; var editableElement = (_c = (_b = (_a = editorInstance === null || editorInstance === void 0 ? void 0 : editorInstance.ui) === null || _a === void 0 ? void 0 : _a.view) === null || _b === void 0 ? void 0 : _b.editable) === null || _c === void 0 ? void 0 : _c.element; if (!editableElement) { console.warn('No editable element found in the editor instance'); return; } setTimeout(function () { var _a; if (typeof WEBSPELLCHECKER !== 'undefined') { try { WEBSPELLCHECKER.init({ container: editableElement, autoStartup: (_a = config.autoStartup) !== null && _a !== void 0 ? _a : true, lang: config.lang || 'auto', serviceId: config.serviceId }); } catch (e) { console.error('WSC init error:', e); } } else { console.warn('WebSpellChecker not available yet. Did you forget to call loadWSC()?'); } }, 200); } function config(editorInstance, serviceId, userData) { if (!editorInstance || !serviceId || !userData) { console.warn('[wsc-handler] Missing required parameters.'); return; } document.addEventListener('click', function (event) { var _a, _b; var button = (_a = event.target) === null || _a === void 0 ? void 0 : _a.closest('.wsc-button'); if (!(button instanceof HTMLElement)) return; var ariaLabel = button.getAttribute('aria-label') || ''; if (!ariaLabel.startsWith('Replace ') || !ariaLabel.includes(' with ') || ariaLabel === 'No suggestions') { return; } var textToReplace = ariaLabel.split('Replace ')[1].split(' with ')[0].trim(); var replacementText = ariaLabel.split(' with ')[1].trim(); var model = editorInstance.model; var doc = model.document; if ((_b = window.WEBSPELLCHECKER) === null || _b === void 0 ? void 0 : _b.destroyAll) { window.WEBSPELLCHECKER.destroyAll(); } setTimeout(function () { model.change(function (writer) { var selection = doc.selection; var range = selection.getFirstRange(); if (!range) return; writer.remove(range); var insertPosition = range.start; var deletionSpan = writer.createElement('span', __assign(__assign({}, userData), { class: 'ck-suggestion-marker-deletion' })); writer.insertText(textToReplace, deletionSpan); writer.insert(deletionSpan, insertPosition); var insertionSpan = writer.createElement('span', __assign(__assign({}, userData), { class: 'ck-suggestion-marker-insertion' })); writer.insertText(replacementText, insertionSpan); writer.insert(insertionSpan, writer.createPositionAfter(deletionSpan)); }); setTimeout(function () { var _a, _b, _c; if (typeof window.WEBSPELLCHECKER !== 'undefined') { window.WEBSPELLCHECKER.init({ container: (_c = (_b = (_a = editorInstance === null || editorInstance === void 0 ? void 0 : editorInstance.ui) === null || _a === void 0 ? void 0 : _a.view) === null || _b === void 0 ? void 0 : _b.editable) === null || _c === void 0 ? void 0 : _c.element, autoStartup: true, lang: 'auto', serviceId: serviceId, }); console.log('[wsc-handler] WSC reinitialized after replacement'); } }, 50); }, 0); }); }