UNPKG

on-codemerge

Version:

A WYSIWYG editor for on-codemerge is a user-friendly interface that allows users to edit and view their code in real time, exactly as it will appear in the final product

2 lines (1 loc) 7.97 kB
"use strict";var m=Object.defineProperty;var g=(i,e,t)=>e in i?m(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var r=(i,e,t)=>g(i,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const k=require("../ToolbarPlugin/utils.cjs"),C=require("../../icons/spell-check.svg.cjs"),x=require("../../../node_modules/.pnpm/spellchecker-wasm@0.3.3/node_modules/spellchecker-wasm/lib/spellchecker-wasm.wasm.cjs"),v=Object.assign({"./dictionaries/ar.txt":()=>Promise.resolve().then(()=>require("./dictionaries/ar.txt.cjs")).then(i=>i.default),"./dictionaries/cs.txt":()=>Promise.resolve().then(()=>require("./dictionaries/cs.txt.cjs")).then(i=>i.default),"./dictionaries/de.txt":()=>Promise.resolve().then(()=>require("./dictionaries/de.txt.cjs")).then(i=>i.default),"./dictionaries/en.txt":()=>Promise.resolve().then(()=>require("./dictionaries/en.txt.cjs")).then(i=>i.default),"./dictionaries/es.txt":()=>Promise.resolve().then(()=>require("./dictionaries/es.txt.cjs")).then(i=>i.default),"./dictionaries/fr.txt":()=>Promise.resolve().then(()=>require("./dictionaries/fr.txt.cjs")).then(i=>i.default),"./dictionaries/hi.txt":()=>Promise.resolve().then(()=>require("./dictionaries/hi.txt.cjs")).then(i=>i.default),"./dictionaries/id.txt":()=>Promise.resolve().then(()=>require("./dictionaries/id.txt.cjs")).then(i=>i.default),"./dictionaries/it.txt":()=>Promise.resolve().then(()=>require("./dictionaries/it.txt.cjs")).then(i=>i.default),"./dictionaries/ja.txt":()=>Promise.resolve().then(()=>require("./dictionaries/ja.txt.cjs")).then(i=>i.default),"./dictionaries/ko.txt":()=>Promise.resolve().then(()=>require("./dictionaries/ko.txt.cjs")).then(i=>i.default),"./dictionaries/nl.txt":()=>Promise.resolve().then(()=>require("./dictionaries/nl.txt.cjs")).then(i=>i.default),"./dictionaries/pl.txt":()=>Promise.resolve().then(()=>require("./dictionaries/pl.txt.cjs")).then(i=>i.default),"./dictionaries/pt.txt":()=>Promise.resolve().then(()=>require("./dictionaries/pt.txt.cjs")).then(i=>i.default),"./dictionaries/ru.txt":()=>Promise.resolve().then(()=>require("./dictionaries/ru.txt.cjs")).then(i=>i.default),"./dictionaries/th.txt":()=>Promise.resolve().then(()=>require("./dictionaries/th.txt.cjs")).then(i=>i.default),"./dictionaries/tr.txt":()=>Promise.resolve().then(()=>require("./dictionaries/tr.txt.cjs")).then(i=>i.default),"./dictionaries/vi.txt":()=>Promise.resolve().then(()=>require("./dictionaries/vi.txt.cjs")).then(i=>i.default),"./dictionaries/zh.txt":()=>Promise.resolve().then(()=>require("./dictionaries/zh.txt.cjs")).then(i=>i.default)});class S{constructor(){r(this,"name","spellchecker");r(this,"hotkeys",[{keys:"Ctrl+Shift+L",description:"Run spell checker",command:"spellchecker",icon:"🔍✅"}]);r(this,"editor",null);r(this,"toolbarButton",null);r(this,"isSpellCheckEnabled",!1);r(this,"spellChecker",null);r(this,"misspelledWords",new Map);r(this,"lastResults",[])}async initialize(e){this.editor=e,this.addToolbarButton(),this.enableSpellCheck(this.isSpellCheckEnabled),this.editor.on("spellchecker",async()=>{var t,s;this.isSpellCheckEnabled=!this.isSpellCheckEnabled,this.isSpellCheckEnabled&&await this.loadDictionary(((t=this.editor)==null?void 0:t.getLocale())??"en"),this.enableSpellCheck(this.isSpellCheckEnabled),(s=this.toolbarButton)==null||s.classList.toggle("active",this.isSpellCheckEnabled)})}async loadDictionary(e){try{const t=await fetch(x.default),s=v[`./dictionaries/${e}.txt`];if(!s)throw new Error(`Dictionary files for locale ${e} not found`);const l=await s(),n=await fetch(l);if(!t.ok||!n.ok)throw new Error("Failed to load dictionary files");const{SpellcheckerWasm:h}=await Promise.resolve().then(()=>require("../../../_virtual/index.cjs")).then(o=>o.index);this.spellChecker=new h(o=>{this.lastResults=o}),await this.spellChecker.prepareSpellchecker(t,n)}catch(t){console.error("Failed to load dictionary:",t),this.spellChecker=null}}addToolbarButton(){var t;const e=document.querySelector(".editor-toolbar");e&&(this.toolbarButton=k.createToolbarButton({icon:C.default,title:(t=this.editor)==null?void 0:t.t("Spell Check"),onClick:async()=>{var s,l;this.isSpellCheckEnabled=!this.isSpellCheckEnabled,this.isSpellCheckEnabled&&await this.loadDictionary(((s=this.editor)==null?void 0:s.getLocale())??"en"),this.enableSpellCheck(this.isSpellCheckEnabled),(l=this.toolbarButton)==null||l.classList.toggle("active",this.isSpellCheckEnabled)}}),e.appendChild(this.toolbarButton))}enableSpellCheck(e){var s;((s=this.editor)==null?void 0:s.getContainer())&&(e?(this.addSpellCheckListeners(),this.checkAllText()):(this.removeSpellCheckListeners(),this.clearHighlights()))}addSpellCheckListeners(){var t;const e=(t=this.editor)==null?void 0:t.getContainer();e&&(e.addEventListener("input",this.handleInput.bind(this)),e.addEventListener("mouseup",this.handleMouseUp.bind(this)),e.addEventListener("contextmenu",this.handleContextMenu.bind(this)))}removeSpellCheckListeners(){var t;const e=(t=this.editor)==null?void 0:t.getContainer();e&&(e.removeEventListener("input",this.handleInput.bind(this)),e.removeEventListener("mouseup",this.handleMouseUp.bind(this)),e.removeEventListener("contextmenu",this.handleContextMenu.bind(this)))}handleInput(e){this.checkAllText()}handleMouseUp(e){const t=window.getSelection();if(t&&t.rangeCount>0){const s=t.getRangeAt(0);s.toString()&&this.checkSpelling(s)}}handleContextMenu(e){const t=window.getSelection();if(t&&t.rangeCount>0){const s=t.getRangeAt(0),l=s.toString();if(l&&this.isMisspelled(l)){e.preventDefault();const n=this.getSpellingSuggestions(l);n.length>0&&this.showContextMenu(e,n,s)}}}checkAllText(){var l;const e=(l=this.editor)==null?void 0:l.getContainer();if(!e)return;this.clearHighlights();const t=document.createTreeWalker(e,NodeFilter.SHOW_TEXT);let s;for(;s=t.nextNode();){const n=s,h=n.textContent;if(h){const o=h.split(/\s+/);let d=0;o.forEach(a=>{if(this.isMisspelled(a)){const c=document.createRange();c.setStart(n,d),c.setEnd(n,d+a.length),this.highlightMisspelledWord(n,c)}d+=a.length+1})}}}checkSpelling(e){const t=e.toString();if(this.isMisspelled(t)){const s=e.startContainer;this.highlightMisspelledWord(s,e)}}isMisspelled(e){return this.spellChecker?(this.lastResults=[],this.spellChecker.checkSpelling(e,{includeUnknown:!0,maxEditDistance:2,verbosity:1,includeSelf:!1}),this.lastResults.length>0):!1}getSpellingSuggestions(e){return this.spellChecker?(this.lastResults=[],this.spellChecker.checkSpelling(e,{includeUnknown:!0,maxEditDistance:2,verbosity:2,includeSelf:!1}),this.lastResults.map(t=>t.term)):[]}highlightMisspelledWord(e,t){var s;(s=e.parentElement)==null||s.classList.add("misspelled-word"),this.misspelledWords.set(e,t)}clearHighlights(){this.misspelledWords.forEach((e,t)=>{var s;(s=t.parentElement)==null||s.classList.remove("misspelled-word")}),this.misspelledWords.clear()}showContextMenu(e,t,s){var p;const l=document.createElement("div");l.style.position="fixed",l.style.backgroundColor="#fff",l.style.border="1px solid #ccc",l.style.boxShadow="0 2px 5px rgba(0, 0, 0, 0.2)",l.style.zIndex="1000",l.style.maxHeight="200px",l.style.overflowY="auto",l.style.padding="4px 0";const n=200,h=Math.min(t.length*32,200),o=window.innerWidth,d=window.innerHeight;let a=e.clientY,c=e.clientX;a+h>d&&(a=d-h),c+n>o&&(c=o-n),l.style.top=`${a}px`,l.style.left=`${c}px`,t.forEach(f=>{const u=document.createElement("div");u.style.padding="8px 16px",u.style.cursor="pointer",u.textContent=f,u.addEventListener("click",()=>{this.replaceMisspelledWord(s,f),l.remove()}),l.appendChild(u)}),(p=this.editor)==null||p.getInnerContainer().appendChild(l),document.addEventListener("click",()=>l.remove(),{once:!0})}replaceMisspelledWord(e,t){e.deleteContents(),e.insertNode(document.createTextNode(t)),this.checkAllText()}destroy(){var e;this.enableSpellCheck(!1),(e=this.toolbarButton)==null||e.remove(),this.toolbarButton=null,this.editor=null,this.spellChecker=null}}exports.SpellCheckerPlugin=S;