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.
This commit is contained in:
Simon Rozman 2024-01-31 08:51:25 +01:00
parent 3e4a4e6e91
commit 4470c4fccf

View File

@ -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 = /<div\b[^>]*>/i
const firstDiv = text.match(divRegex)
if (!firstDiv) return `<div>${text}</div>`
const slicedText = text?.slice(0, firstDiv.index)
const firstParagraph = `<div>${slicedText}</div>`
const newText = firstParagraph + text.slice(firstDiv.index)
return newText
}
function separateParagraphs(text) {
let paragraphs = text.match(/<div>.*?<\/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 `<span class="typo-mistake" data-info="Dodaten text za posamezno napako">${word}</span>`
// }
// 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('')
// }