Files
huntingclubroster/js/rich-text.js
T

48 lines
2.0 KiB
JavaScript

/* Shared adapter for the locally hosted SunEditor build. */
window.HcrRichText = (function () {
'use strict';
const editors = new WeakMap();
function create(element) {
const buttons = element.closest('.card-body').querySelectorAll('.btnSave');
buttons.forEach(button => button.disabled = true);
const failure = function () {
const message = document.createElement('p');
message.setAttribute('role', 'alert');
message.textContent = 'The text editor could not load. Reload this page before saving.';
element.parentNode.appendChild(message);
};
let timer;
try {
timer = setTimeout(failure, 10000);
const editor = SUNEDITOR.create(element, {
height: '240px',
plugins: ['blockStyle', 'link', 'list_bulleted', 'list_numbered', 'font', 'fontSize', 'fontColor', 'align', 'hr', 'table'].map(name => SUNEDITOR.plugins[name]),
buttonList: [
['undo', 'redo'], ['blockStyle', 'bold', 'italic', 'underline'],
['link', 'list_bulleted', 'list_numbered'],
['font', 'fontSize', 'fontColor'], ['indent', 'outdent', 'align'],
['hr', 'table']
]
});
editor.$.eventManager.events.onload = function () {
clearTimeout(timer);
editors.set(element, editor);
element.hcrEditor = editor;
buttons.forEach(button => button.disabled = false);
};
} catch (error) {
console.error('Text editor initialization failed.', error);
clearTimeout(timer);
failure();
}
}
function sync(element) {
const editor = editors.get(element);
if (!editor)
return false;
element.value = editor.isEmpty() ? '' : editor.$.html.get();
return true;
}
return {create: create, sync: sync};
}());