From 4470c4fccf5c22b2add2a099368870caca6595c5 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Wed, 31 Jan 2024 08:51:25 +0100 Subject: [PATCH] Unique function names and cleanup Since this component is about to be included in other solutions, chance of name collisions with others should be minimized. --- online-editor.js | 63 +++++++++--------------------------------------- 1 file changed, 11 insertions(+), 52 deletions(-) diff --git a/online-editor.js b/online-editor.js index 6e356ea..e3cdd31 100644 --- a/online-editor.js +++ b/online-editor.js @@ -1,4 +1,4 @@ -let editors = {} // Collection of all editors on page +let besEditors = {} // Collection of all editors on page window.onload = () => { // Search and prepare all our editors found in the document. @@ -7,18 +7,18 @@ window.onload = () => { ignoreInput: false, timer: null } - editors[ed.id] = editor - checkText(ed.id) + besEditors[ed.id] = editor + besCheckText(ed.id) - ed.addEventListener('beforeinput', e => beforeTextChange(ed.id, e), false) + ed.addEventListener('beforeinput', e => besBeforeInput(ed.id, e), false) ed.addEventListener('click', e => { - handleClick(e) + besHandleClick(e) }) }) } -function handleClick(e) { +function besHandleClick(e) { switch (e.target) { case e.target.closest('span'): const clicked = e.target.closest('span') @@ -30,9 +30,9 @@ function handleClick(e) { } } -async function checkText(editorId) { +async function besCheckText(editorId) { let text = '' - let editor = editors[editorId] + let editor = besEditors[editorId] let ed = document.getElementById(editorId) let paragraphs = [] let divElements = ed.getElementsByTagName('div') @@ -92,13 +92,13 @@ async function checkText(editorId) { } } -function beforeTextChange(editorId, event) +function besBeforeInput(editorId, event) { - let editor = editors[editorId] + let editor = besEditors[editorId] if (editor.ignoreInput) return if (editor.timer) clearTimeout(editor.timer) - editor.timer = setTimeout(function(){ checkText(editorId) }, 1000) + editor.timer = setTimeout(function(){ besCheckText(editorId) }, 1000) let ed = document.getElementById(editorId) event.getTargetRanges().forEach(range => { @@ -114,21 +114,6 @@ function beforeTextChange(editorId, event) }) } -function createFirstParagraph(text) { - const divRegex = /]*>/i - const firstDiv = text.match(divRegex) - if (!firstDiv) return `
${text}
` - const slicedText = text?.slice(0, firstDiv.index) - const firstParagraph = `
${slicedText}
` - const newText = firstParagraph + text.slice(firstDiv.index) - return newText -} - -function separateParagraphs(text) { - let paragraphs = text.match(/
.*?<\/div>/g) - return paragraphs -} - async function ajaxCheck(paragraph) { const url = 'http://localhost:225/api/v2/check' const data = { @@ -158,29 +143,3 @@ async function ajaxCheck(paragraph) { throw new Error('Request to backend server failed: ' + error) }) } - -function renderMistakes(matches, paragraph) {} - -// //TODO: Popravi dodajanje presledkov -// function mockSpellChecker(text) { -// const specificWords = ['Tole', 'nov', 'test'] -// const words = text.split(/(?=<)|(?<=\>)|\s/g).filter(word => word !== '') -// const modifiedWords = words.map(word => { -// const wordWithoutTags = word.replace(/<[^>]*>/g, '') -// if (specificWords.includes(wordWithoutTags)) { -// return `${word}` -// } -// return word -// }) - -// // This is necessary to remove spaces between opening and closing tags -// // TODO: improve this or find a better way to do it -// return modifiedWords -// .map((word, index) => { -// if (index === 0 || index === 1 || index === modifiedWords.length - 1) { -// return word -// } -// return ' ' + word -// }) -// .join('') -// }