Skip to content

Commit

Permalink
fix(core): Add timer to avoid spamming onEditorChanged for CKEditor 5…
Browse files Browse the repository at this point in the history
…. Fixes #5944.
  • Loading branch information
WoodySlum committed Apr 2, 2024
1 parent 127df39 commit bc804ae
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions UI/WebServerResources/js/Common/sgCkeditor.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@
var editor;
var editorChanged = false;
var modelChanged = false;
var editorChangedTimerValue = 2000;
var editorChangedTimer = null;


this.$onInit = function () {
vm.ngModelCtrl.$render = function () {
Expand Down Expand Up @@ -337,30 +340,36 @@
vm.editor.destroy(noUpdate);
}

function onEditorChange () {
var html = vm.editor.getData();
function onEditorChange() {
if (editorChangedTimer)
clearTimeout(editorChangedTimer);

var dom = document.createElement("DIV");
dom.innerHTML = html;
var text = (dom.textContent || dom.innerText);
editorChangedTimer = setTimeout(function () {
var html = vm.editor.getData();

if (text === '\n') {
text = '';
}
var dom = document.createElement("DIV");
dom.innerHTML = html;
var text = (dom.textContent || dom.innerText);

if (!modelChanged && html !== vm.ngModelCtrl.$viewValue) {
editorChanged = true;
vm.ngModelCtrl.$setViewValue(html);
validate(vm.checkTextLength ? text : html);
if (vm.onContentChanged) {
vm.onContentChanged({
'editor': vm.editor,
'html': html,
'text': text
});
if (text === '\n') {
text = '';
}
}
modelChanged = false;

if (!modelChanged && html !== vm.ngModelCtrl.$viewValue) {
editorChanged = true;
vm.ngModelCtrl.$setViewValue(html);
validate(vm.checkTextLength ? text : html);
if (vm.onContentChanged) {
vm.onContentChanged({
'editor': vm.editor,
'html': html,
'text': text
});
}
}
modelChanged = false;
editorChangedTimer = null;
}, editorChangedTimerValue);
}

function onEditorPaste (event) {
Expand Down

0 comments on commit bc804ae

Please sign in to comment.