Skip to content

Commit

Permalink
Remove CKEditor paste event listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBitZesty committed Jul 10, 2024
1 parent 126e4a5 commit 84f5c91
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 185 deletions.
138 changes: 46 additions & 92 deletions app/assets/javascripts/ckeditor/plugins/wordcount/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,52 +293,6 @@ CKEDITOR.plugins.add("wordcount", {
return true;
}

//If the limit is already over, allow the deletion of characters/words. Otherwise,
//the user would have to delete at one go the number of offending characters
var deltaWord = wordCount - lastWordCount;
var deltaChar = charCount - lastCharCount;

lastWordCount = wordCount;
lastCharCount = charCount;

if (lastWordCount == -1) {
lastWordCount = wordCount;
}
if (lastCharCount == -1) {
lastCharCount = charCount;
}

// Check for word limit and/or char limit
if ((config.maxWordCount > -1 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && deltaWord > 0) ||
(config.maxCharCount > -1 && charCount > config.maxCharCount && deltaChar > 0)) {

limitReached(editorInstance, limitReachedNotified);
} else if ((config.maxWordCount == -1 || wordCount <= maxWordCountWithBuffer(config.maxWordCount)) &&
(config.maxCharCount == -1 || charCount <= config.maxCharCount)) {

limitRestored(editorInstance);
} else {
snapShot = editorInstance.getSnapshot();
}

// Fire Custom Events
if (config.charCountGreaterThanMaxLengthEvent && config.charCountLessThanMaxLengthEvent) {
if (charCount > config.maxCharCount && config.maxCharCount > -1) {
config.charCountGreaterThanMaxLengthEvent(charCount, config.maxCharCount);
} else {
config.charCountLessThanMaxLengthEvent(charCount, config.maxCharCount);
}
}

if (config.wordCountGreaterThanMaxLengthEvent && config.wordCountLessThanMaxLengthEvent) {
if (wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.maxWordCount > -1) {
config.wordCountGreaterThanMaxLengthEvent(wordCount, config.maxWordCount);

} else {
config.wordCountLessThanMaxLengthEvent(wordCount, config.maxWordCount);
}
}

return true;
}

Expand Down Expand Up @@ -381,52 +335,52 @@ CKEDITOR.plugins.add("wordcount", {
updateCounter(event.editor);
}, editor, null, 100);

editor.on("paste", function(event) {
if (config.maxWordCount > 0 || config.maxCharCount > 0) {

// Check if pasted content is above the limits
var wordCount = -1,
charCount = -1,
text = event.editor.getData() + event.data.dataValue;


if (config.showCharCount) {
charCount = countCharacters(text, event.editor);
}

if (config.showWordCount) {
wordCount = countWords(text);
}


// Instantiate the notification when needed and only have one instance
if(notification === null) {
notification = new CKEDITOR.plugins.notification(event.editor, {
message: event.editor.lang.wordcount.pasteWarning,
type: 'warning',
duration: config.pasteWarningDuration
});
}

if (config.maxCharCount > 0 && charCount > config.maxCharCount && config.hardLimit) {
if(!notification.isVisible()) {
notification.show();
}
event.cancel();
}

if (config.maxWordCount > 0 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.hardLimit) {
if(!notification.isVisible()) {
notification.show();
}
event.cancel();
}
}
}, editor, null, 100);

editor.on("afterPaste", function (event) {
updateCounter(event.editor);
}, editor, null, 100);
// editor.on("paste", function(event) {
// if (config.maxWordCount > 0 || config.maxCharCount > 0) {

// // Check if pasted content is above the limits
// var wordCount = -1,
// charCount = -1,
// text = event.editor.getData() + event.data.dataValue;


// if (config.showCharCount) {
// charCount = countCharacters(text, event.editor);
// }

// if (config.showWordCount) {
// wordCount = countWords(text);
// }


// // Instantiate the notification when needed and only have one instance
// if(notification === null) {
// notification = new CKEDITOR.plugins.notification(event.editor, {
// message: event.editor.lang.wordcount.pasteWarning,
// type: 'warning',
// duration: config.pasteWarningDuration
// });
// }

// if (config.maxCharCount > 0 && charCount > config.maxCharCount && config.hardLimit) {
// if(!notification.isVisible()) {
// notification.show();
// }
// event.cancel();
// }

// if (config.maxWordCount > 0 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.hardLimit) {
// if(!notification.isVisible()) {
// notification.show();
// }
// event.cancel();
// }
// }
// }, editor, null, 100);

// editor.on("afterPaste", function (event) {
// updateCounter(event.editor);
// }, editor, null, 100);

editor.on("blur", function () {
if (intervalId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def trade_step1

textarea :major_issues_overcome, "Please explain any major issues that you have overcome in recent years and the remedial steps you have taken." do
ref "A 2.2"
classes "sub-question"
classes "sub-question text-words-max"
required
context %(
<p class="govuk-body">For example, what steps did you take following a major Health and Safety incident.</p>
Expand Down
138 changes: 46 additions & 92 deletions public/ckeditor/plugins/wordcount/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,52 +293,6 @@ CKEDITOR.plugins.add("wordcount", {
return true;
}

//If the limit is already over, allow the deletion of characters/words. Otherwise,
//the user would have to delete at one go the number of offending characters
var deltaWord = wordCount - lastWordCount;
var deltaChar = charCount - lastCharCount;

lastWordCount = wordCount;
lastCharCount = charCount;

if (lastWordCount == -1) {
lastWordCount = wordCount;
}
if (lastCharCount == -1) {
lastCharCount = charCount;
}

// Check for word limit and/or char limit
if ((config.maxWordCount > -1 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && deltaWord > 0) ||
(config.maxCharCount > -1 && charCount > config.maxCharCount && deltaChar > 0)) {

limitReached(editorInstance, limitReachedNotified);
} else if ((config.maxWordCount == -1 || wordCount <= maxWordCountWithBuffer(config.maxWordCount)) &&
(config.maxCharCount == -1 || charCount <= config.maxCharCount)) {

limitRestored(editorInstance);
} else {
snapShot = editorInstance.getSnapshot();
}

// Fire Custom Events
if (config.charCountGreaterThanMaxLengthEvent && config.charCountLessThanMaxLengthEvent) {
if (charCount > config.maxCharCount && config.maxCharCount > -1) {
config.charCountGreaterThanMaxLengthEvent(charCount, config.maxCharCount);
} else {
config.charCountLessThanMaxLengthEvent(charCount, config.maxCharCount);
}
}

if (config.wordCountGreaterThanMaxLengthEvent && config.wordCountLessThanMaxLengthEvent) {
if (wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.maxWordCount > -1) {
config.wordCountGreaterThanMaxLengthEvent(wordCount, config.maxWordCount);

} else {
config.wordCountLessThanMaxLengthEvent(wordCount, config.maxWordCount);
}
}

return true;
}

Expand Down Expand Up @@ -381,52 +335,52 @@ CKEDITOR.plugins.add("wordcount", {
updateCounter(event.editor);
}, editor, null, 100);

editor.on("paste", function(event) {
if (config.maxWordCount > 0 || config.maxCharCount > 0) {

// Check if pasted content is above the limits
var wordCount = -1,
charCount = -1,
text = event.editor.getData() + event.data.dataValue;


if (config.showCharCount) {
charCount = countCharacters(text, event.editor);
}

if (config.showWordCount) {
wordCount = countWords(text);
}


// Instantiate the notification when needed and only have one instance
if(notification === null) {
notification = new CKEDITOR.plugins.notification(event.editor, {
message: event.editor.lang.wordcount.pasteWarning,
type: 'warning',
duration: config.pasteWarningDuration
});
}

if (config.maxCharCount > 0 && charCount > config.maxCharCount && config.hardLimit) {
if(!notification.isVisible()) {
notification.show();
}
event.cancel();
}

if (config.maxWordCount > 0 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.hardLimit) {
if(!notification.isVisible()) {
notification.show();
}
event.cancel();
}
}
}, editor, null, 100);

editor.on("afterPaste", function (event) {
updateCounter(event.editor);
}, editor, null, 100);
// editor.on("paste", function(event) {
// if (config.maxWordCount > 0 || config.maxCharCount > 0) {

// // Check if pasted content is above the limits
// var wordCount = -1,
// charCount = -1,
// text = event.editor.getData() + event.data.dataValue;


// if (config.showCharCount) {
// charCount = countCharacters(text, event.editor);
// }

// if (config.showWordCount) {
// wordCount = countWords(text);
// }


// // Instantiate the notification when needed and only have one instance
// if(notification === null) {
// notification = new CKEDITOR.plugins.notification(event.editor, {
// message: event.editor.lang.wordcount.pasteWarning,
// type: 'warning',
// duration: config.pasteWarningDuration
// });
// }

// if (config.maxCharCount > 0 && charCount > config.maxCharCount && config.hardLimit) {
// if(!notification.isVisible()) {
// notification.show();
// }
// event.cancel();
// }

// if (config.maxWordCount > 0 && wordCount > maxWordCountWithBuffer(config.maxWordCount) && config.hardLimit) {
// if(!notification.isVisible()) {
// notification.show();
// }
// event.cancel();
// }
// }
// }, editor, null, 100);

// editor.on("afterPaste", function (event) {
// updateCounter(event.editor);
// }, editor, null, 100);

editor.on("blur", function () {
if (intervalId) {
Expand Down

0 comments on commit 84f5c91

Please sign in to comment.