forked from laysense/laysite
Address LaySite 1.4.5 editor and release review suggestions
This commit is contained in:
@@ -4,6 +4,15 @@ jQuery(function ($) {
|
||||
$('[data-head-settings]').each(function () {
|
||||
const $details = $(this);
|
||||
const source = $details.find('[data-head-source]').get(0);
|
||||
const $limit = $details.find('[data-head-limit]');
|
||||
const encoder = new TextEncoder();
|
||||
const size = (value) => encoder.encode(value).length;
|
||||
function fits(current, removed, added) {
|
||||
const original = size(current);
|
||||
const next = original - size(removed) + size(added);
|
||||
$limit.prop('hidden', next <= source.maxLength);
|
||||
return next <= source.maxLength || next < original;
|
||||
}
|
||||
let editor;
|
||||
$details.on('toggle', () => {
|
||||
if (!this.open || !window.CodeMirror) return;
|
||||
@@ -12,6 +21,9 @@ jQuery(function ($) {
|
||||
mode: 'htmlmixed', lineNumbers: true, lineWrapping: true, indentUnit: 2, tabSize: 2,
|
||||
});
|
||||
editor.getInputField().setAttribute('aria-labelledby', 'head-source-label');
|
||||
editor.on('beforeChange', (cm, change) => {
|
||||
if (!fits(cm.getValue(), cm.getRange(change.from, change.to), change.text.join('\n'))) change.cancel();
|
||||
});
|
||||
editor.on('change', () => editor.save());
|
||||
}
|
||||
editor.refresh();
|
||||
@@ -26,7 +38,7 @@ jQuery(function ($) {
|
||||
if (editor) {
|
||||
editor.replaceSelection(value);
|
||||
editor.focus();
|
||||
} else {
|
||||
} else if (fits(source.value, source.value.slice(source.selectionStart, source.selectionEnd), value)) {
|
||||
source.setRangeText(value, source.selectionStart, source.selectionEnd, 'end');
|
||||
source.focus();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user