Move status icon out of core grammar checking service

Status icon is user-implemented now.
This commit is contained in:
2024-06-27 11:35:40 +02:00
parent 042a6b288c
commit 4e3559408e
14 changed files with 264 additions and 142 deletions

View File

@@ -9,6 +9,7 @@
<script src="https://cdn.ckeditor.com/ckeditor5/41.1.0/classic/ckeditor.js"></script>
<script>const besUrl = 'http://localhost:225/api/v2';</script>
<script src="../service.js"></script>
<script src="common.js"></script>
</head>
<body>
<p class="my-block">This is an example of a CKEditor edit control. Edit the text, resize the control or browser window, scroll around, click...</p>
@@ -22,7 +23,7 @@
<bes-popup-el/>
<script>
ClassicEditor.create(document.querySelector('#editor'))
.then(newEditor => BesCKService.register(newEditor.ui.view.editable.element, newEditor))
.then(newEditor => BesCKService.register(newEditor.ui.view.editable.element, newEditor, new BesCKStatusIconEventSink()))
.catch(error => console.error(error))
</script>
</body>

140
samples/common.js Normal file
View File

@@ -0,0 +1,140 @@
/**
* Class to receive grammar checking service notification
*
* This implementation handles the status icon in the lower-right corner of the host element.
*/
class BesStatusIconEventSink {
/**
* Called when grammar checking service is registered
*
* @param {BesService} service Grammar checking service
*/
register(service) {
this.statusDiv = document.createElement('div')
this.statusDiv.classList.add('bes-status-div')
this.statusIcon = document.createElement('div')
this.statusIcon.classList.add('bes-status-icon')
this.statusDiv.appendChild(this.statusIcon)
this.setStatusDivPosition(service)
service.textElement.parentNode.insertBefore(
this.statusDiv,
service.textElement.nextSibling
)
}
/**
* Called when grammar checking service is unregistered
*
* @param {BesService} service Grammar checking service
*/
unregister(service) {
this.statusDiv.remove()
this.statusIcon.remove()
}
/**
* Called to report repositioning
*
* @param {BesService} service Grammar checking service
*/
reposition(service) {
this.setStatusDivPosition(service)
}
/**
* Called to report resizing
*
* @param {BesService} service Grammar checking service
*/
resize(service) {
this.setStatusDivPosition(service)
}
/**
* Repositions status DIV element.
*
* @param {BesService} service Grammar checking service
*/
setStatusDivPosition(service) {
const rect = service.textElement.getBoundingClientRect()
const scrollbarWidth =
service.textElement.offsetWidth - service.textElement.clientWidth
this.statusDiv.style.left = `${rect.right - 40 - scrollbarWidth}px`
const scrollbarHeight =
service.textElement.offsetHeight - service.textElement.clientHeight
this.statusDiv.style.top = `${rect.bottom - 30 - scrollbarHeight}px`
}
/**
* Called to report grammar proofing started
*
* @param {BesService} service Grammar checking service
*/
startProofing(service) {
this.updateStatusIcon('bes-status-loading', 'Besana preverja pravopis.')
}
/**
* Called to report grammar-checking failed (as 500 Internal server error, timeout, etc.)
*
* @param {BesService} service Grammar checking service
* @param {Response} response HTTP response
*/
failedProofing(service, response) {
this.updateStatusIcon(
'bes-status-error',
`Pri preverjanju pravopisa je prišlo do napake ${response.status} ${response.statusText}.`
)
}
/**
* Called to report grammar-checking run is ended
*
* @param {BesService} service Grammar checking service
*/
endProofing(service) {
if (service.proofingError) {
this.updateStatusIcon(
'bes-status-error',
`Pri obdelavi odgovora pravopisnega strežnika je prišlo do napake: ${service.proofingError}`
)
} else if (service.proofingMatches > 0)
this.updateStatusIcon(
'bes-status-mistakes',
`Število napak: ${service.proofingMatches}`
)
else this.updateStatusIcon('bes-status-success', 'V besedilu ni napak.')
}
/**
* Sets status icon style and title.
*
* @param {String} status CSS class name to set status icon to
* @param {String} title Title of the status icon
*/
updateStatusIcon(status, title) {
const statuses = [
'bes-status-loading',
'bes-status-success',
'bes-status-mistakes',
'bes-status-error'
]
statuses.forEach(statusClass => {
this.statusIcon.classList.remove(statusClass)
})
this.statusIcon.classList.add(status)
this.statusDiv.title = title
}
}
class BesCKStatusIconEventSink extends BesStatusIconEventSink {
/**
* Repositions status DIV element.
*
* @param {BesService} service Grammar checking service
*/
setStatusDivPosition(service) {
this.statusDiv.style.right = `10px`
this.statusDiv.style.bottom = `10px`
}
}

View File

@@ -8,10 +8,11 @@
<link rel="stylesheet" href="styles.css" />
<script>const besUrl = 'http://localhost:225/api/v2';</script>
<script src="../service.js"></script>
<script src="common.js"></script>
</head>
<body>
<p class="my-block">This is an example of a simple <code>&lt;div contenteditable="plaintext-only"&gt;</code> edit control. Edit the text, resize the control or browser window, scroll around, click...</p>
<div class="my-block my-control bes-service" contenteditable="plaintext-only">Tukaj vpišite besedilo ki ga želite popraviti.
<div id="my-editor" class="my-block my-control" contenteditable="plaintext-only">Tukaj vpišite besedilo ki ga želite popraviti.
Prišla je njena lepa hčera. Smatram da tega nebi bilo potrebno storiti. Predavanje je trajalo dve ure. S njim grem v Kamnik. Janez jutri nebo prišel. Prišel je z 100 idejami.
@@ -21,5 +22,8 @@ Na mizo nisem položil knjigo.
Kvazimodo ji je ponavadi prinesel hrano in pijačo, medtem ko je spala, da ne bi videla njegov iznakažen in grd obraz. Poleg tega ji je pustil tudi piščalko, da bi ga lahko priklicala, če bi bilo to potrebno. Kvazimodo se je odločil, da razveseli Esmeraldo in ji obljubi, da ji bo pripeljal Febusa. Toda Febus ni želel priti. Kvazimodo ji je raje lagal, da ni mogel najti Febusa, kot da Esmeraldi pove resnico, ker bi ona trpela.</div>
<bes-popup-el/>
<script>
BesService.registerByElement(document.getElementById('my-editor'), new BesStatusIconEventSink())
</script>
</body>
</html>

View File

@@ -8,10 +8,11 @@
<link rel="stylesheet" href="styles.css" />
<script>const besUrl = 'http://localhost:225/api/v2';</script>
<script src="../service.js"></script>
<script src="common.js"></script>
</head>
<body>
<p class="my-block">This is an example of a simple <code>&lt;div contenteditable="true"&gt;</code> edit control. Edit the text, resize the control or browser window, scroll around, click...</p>
<div class="my-block my-control bes-service" contenteditable="true">
<div id="my-editor" class="my-block my-control" contenteditable="true">
<p>Tukaj vpišite besedilo ki ga želite popraviti.</p>
<p>Prišla je njena lepa hčera. Smatram da tega nebi bilo potrebno storiti. Predavanje je trajalo dve ure. S njim grem v Kamnik. Janez jutri nebo prišel. Prišel je z 100 idejami.</p>
<p>To velja tudi v Bledu. To se je zgodilo na velikemu vrtu. Prišel je na Kamnik. On je včeraj prišel z svojo torbo. Dve žemlje prosim. Pogosto brskam po temu forumu. Prišel je včeraj in sicer s otroci. To ne vem. Pogleda vse kar daš v odložišče. Nisem jo videl. Ona izgleda dobro. Pri zanikanju ne smete uporabljati tožilnik. Vlak gre v Ljubljano čez Zidani Most. Skočil je čez okno. Slovenija meji na avstrijo. Jaz pišem v Slovenščini vsak Torek. Novica, da je skupina 25 planincev hodila pod vodstvom gorskega vodnika je napačna in zavajujoča. Želim da poješ kosmizailo. Jaz pogosto brskam po temu forumu. Med tem ko je iskal ključe, so se odprla vrata. V takoimenovanem skladišču je bilo veliko ljudi. V sobi sta dve mize. Stekel je h mami. Videl sem Jurčič Micko. To je bil njegov življenski cilj. Po vrsti popravite vse kar želite. Preden zaspiva mi prebere pravljico. Prišel je s stricom. Oni zadanejo tarčo. Mi gremo teči po polju. Mi gremo peči kruh. Usedel se je k miza. Postreži kosilo! Skul je veslanje z dvemi vesli.</p>
@@ -19,5 +20,8 @@
<p>Kvazimodo ji je ponavadi prinesel hrano in pijačo, medtem ko je spala, da ne bi videla njegov iznakažen in grd obraz. Poleg tega ji je pustil tudi piščalko, da bi ga lahko priklicala, če bi bilo to potrebno. Kvazimodo se je odločil, da razveseli Esmeraldo in ji obljubi, da ji bo pripeljal Febusa. Toda Febus ni želel priti. Kvazimodo ji je raje lagal, da ni mogel najti Febusa, kot da Esmeraldi pove resnico, ker bi ona trpela.</p>
</div>
<bes-popup-el/>
<script>
BesService.registerByElement(document.getElementById('my-editor'), new BesStatusIconEventSink())
</script>
</body>
</html>

View File

@@ -9,6 +9,7 @@
<script src="https://cdn.ckeditor.com/ckeditor5/41.1.0/classic/ckeditor.js"></script>
<script>const besUrl = 'http://localhost:225/api/v2';</script>
<script src="../service.js"></script>
<script src="common.js"></script>
</head>
<body>
<p class="my-block">This is an example how to implement dynamic registration and deregistration of a grammar checking service on supported HTML controls.</p>
@@ -16,11 +17,11 @@
<script>
function toggle_grammar_service(el) {
if (el.checked) {
BesService.registerByElement(document.getElementById('textarea-control'))
BesService.registerByElement(document.getElementById('contenteditable-control'))
BesService.registerByElement(document.getElementById('readonly-control'))
BesService.registerByElement(document.getElementById('textarea-control'), new BesStatusIconEventSink())
BesService.registerByElement(document.getElementById('contenteditable-control'), new BesStatusIconEventSink())
BesService.registerByElement(document.getElementById('readonly-control'), new BesStatusIconEventSink())
if (my_ckeditor)
BesCKService.register(my_ckeditor.ui.view.editable.element, my_ckeditor)
BesCKService.register(my_ckeditor.ui.view.editable.element, my_ckeditor, new BesCKStatusIconEventSink())
} else {
BesService.unregisterByElement(document.getElementById('textarea-control'))
BesService.unregisterByElement(document.getElementById('contenteditable-control'))

View File

@@ -0,0 +1,11 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg fill="#000000" width="800px" height="800px" viewBox="0 0 24 24" id="check-mark-square" data-name="Flat Line" xmlns="http://www.w3.org/2000/svg" class="icon flat-line">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>
<g id="SVGRepo_iconCarrier">

After

Width:  |  Height:  |  Size: 979 B

View File

@@ -0,0 +1,16 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 0 24.00 24.00" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>
<g id="SVGRepo_iconCarrier">
<path fill-rule="evenodd" clip-rule="evenodd" d="M12 22c5.523 0 10-4.477 10-10S17.523 2 12 2 2 6.477 2 12s4.477 10 10 10zm-1.5-5.009c0-.867.659-1.491 1.491-1.491.85 0 1.509.624 1.509 1.491 0 .867-.659 1.509-1.509 1.509-.832 0-1.491-.642-1.491-1.509zM11.172 6a.5.5 0 0 0-.499.522l.306 7a.5.5 0 0 0 .5.478h1.043a.5.5 0 0 0 .5-.478l.305-7a.5.5 0 0 0-.5-.522h-1.655z" fill="#f03838"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 845 B

View File

@@ -0,0 +1,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>

After

Width:  |  Height:  |  Size: 776 B

View File

@@ -0,0 +1,12 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg fill="#000000" width="800px" height="800px" viewBox="0 0 24 24" id="file-corrupt" data-name="Line Color" xmlns="http://www.w3.org/2000/svg" class="icon line-color">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"/>
<g id="SVGRepo_iconCarrier">
<line id="secondary-upstroke" x1="18.95" y1="20.5" x2="19.05" y2="20.5" style="fill: none; stroke-linecap: round; stroke-linejoin: round; stroke-width: 2; stroke: #ec4141;"/>

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

@@ -0,0 +1,7 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Uploaded to: SVG Repo, www.svgrepo.com, Transformed by: SVG Repo Mixer Tools -->
<svg width="800px" height="800px" viewBox="0 -0.5 17 17" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="si-glyph si-glyph-turn-off" fill="#000000">
<g id="SVGRepo_bgCarrier" stroke-width="0"/>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -20,3 +20,35 @@
min-height: 100px;
max-height: 500px;
}
.bes-status-div {
position: absolute;
z-index: 3;
}
.bes-status-icon {
position: relative;
right: 5px;
bottom: 5px;
width: 30px;
height: 30px;
background-size: contain;
background-repeat: no-repeat;
cursor: help;
}
.bes-status-icon.bes-status-success {
background-image: url('images/checkmark-svgrepo-com.svg');
}
.bes-status-icon.bes-status-loading {
background-image: url('images/loading-svgrepo-com.svg');
}
.bes-status-icon.bes-status-error {
background-image: url('images/error-svgrepo-com.svg');
}
.bes-status-icon.bes-status-mistakes {
background-image: url('images/mistake-svgrepo-com.svg');
}

View File

@@ -8,11 +8,12 @@
<link rel="stylesheet" href="styles.css" />
<script>const besUrl = 'http://localhost:225/api/v2';</script>
<script src="../service.js"></script>
<script src="common.js"></script>
</head>
<body>
<p class="my-block">This is an example of a <code>&lt;textarea&gt;</code> edit control. Edit the text, resize the control or browser window, scroll around, click...</p>
<div class="my-block">
<textarea class="my-control bes-service">Tukaj vpišite besedilo ki ga želite popraviti.
<textarea id="my-editor" class="my-control">Tukaj vpišite besedilo ki ga želite popraviti.
Prišla je njena lepa hčera. Smatram da tega nebi bilo potrebno storiti. Predavanje je trajalo dve ure. S njim grem v Kamnik. Janez jutri nebo prišel. Prišel je z 100 idejami.
@@ -23,5 +24,8 @@ Na mizo nisem položil knjigo.
Kvazimodo ji je ponavadi prinesel hrano in pijačo, medtem ko je spala, da ne bi videla njegov iznakažen in grd obraz. Poleg tega ji je pustil tudi piščalko, da bi ga lahko priklicala, če bi bilo to potrebno. Kvazimodo se je odločil, da razveseli Esmeraldo in ji obljubi, da ji bo pripeljal Febusa. Toda Febus ni želel priti. Kvazimodo ji je raje lagal, da ni mogel najti Febusa, kot da Esmeraldi pove resnico, ker bi ona trpela.</textarea>
</div>
<bes-popup-el/>
<script>
BesService.registerByElement(document.getElementById('my-editor'), new BesStatusIconEventSink())
</script>
</body>
</html>