Split SquireUI from HtmlEditor.js and improved SquireUI with styling

This commit is contained in:
djmaze 2020-09-10 12:56:45 +02:00
parent acda0e8883
commit 72cbdfa7f2
4 changed files with 433 additions and 258 deletions

View file

@ -1,11 +1,10 @@
import { EventKeyCode } from 'Common/Enums';
import { SquireUI } from 'External/SquireUI';
/**
* @type {Object}
*/
const doc = document,
CKEditorDefaultConfig = {
const CKEditorDefaultConfig = {
'title': false,
'stylesSet': false,
'customConfig': '',
@ -43,35 +42,6 @@ CKEditorDefaultConfig = {
'fontSize_sizes': '10/10px;12/12px;13/13px;14/14px;16/16px;18/18px;20/20px;24/24px;28/28px;36/36px;48/48px'
},
SquireDefaultConfig = {
/*
blockTag: 'DIV',
blockAttributes: null,
tagAttributes: {
blockquote: null,
ul: null,
ol: null,
li: null,
a: null
},
classNames: {
colour: 'colour',
fontFamily: 'font',
fontSize: 'size',
highlight: 'highlight'
},
leafNodeNames: leafNodeNames,
undo: {
documentSizeThreshold: -1, // -1 means no threshold
undoLimit: -1 // -1 means no limit
},
isInsertedHTMLSanitized: true,
isSetHTMLSanitized: true,
willCutCopy: null,
addLinks: true // allow_smart_html_links
*/
},
/**
* @type {Object}
*/
@ -108,232 +78,6 @@ htmlEditorLangsMap = {
'zh_tw': 'zh'
};
class SquireUI
{
constructor(container) {
const actions = {
source: {
html: '〈〉',
cmd: () => this.doAction('bold','B')
},
/*
bidi: {
allowHtmlEditorBitiButtons
},
*/
bold: {
html: '𝐁',
cmd: () => this.doAction('bold','B')
},
italic: {
html: '𝐼',
cmd: () => this.doAction('italic','I')
},
underline: {
html: 'U',
cmd: () => this.doAction('underline','U'),
style: 'text-decoration:underline;'
},
strike: {
html: 'S',
cmd: () => this.doAction('strikethrough','S'),
style: 'text-decoration:line-through;'
},
sub: {
html: 'S<sub>x</sub>',
cmd: () => this.doAction('subscript','SUB')
},
sup: {
html: 'S<sup>x</sup>',
cmd: () => this.doAction('superscript','SUP')
},
ol: {
html: '#',
cmd: () => this.doList('OL')
},
ul: {
html: '⋮',
cmd: () => this.doList('UL')
},
quote: {
html: '"',
cmd: () => {
let parent = this.getParentNodeName('UL,OL');
(parent && 'BLOCKQUOTE' == parent) ? this.squire.decreaseQuoteLevel() : this.squire.increaseQuoteLevel();
}
},
indentDecrease: {
html: '⇤',
cmd: () => this.squire.changeIndentationLevel('decrease')
},
indentIncrease: {
html: '⇥',
cmd: () => this.squire.changeIndentationLevel('increase')
},
link: {
html: '🔗',
cmd: () => {
if ('A' === this.getParentNodeName()) {
this.squire.removeLink();
} else {
let url = prompt("Link","https://");
url != null && url.length && this.squire.makeLink(url);
}
}
},
image: {
html: '📷🖼️',
cmd: () => {
if ('IMG' === this.getParentNodeName()) {
// wysiwyg.removeLink();
} else {
let src = prompt("Image","https://");
src != null && src.length && this.squire.insertImage(src);
}
}
},
undo: {
html: '↶',
cmd: () => this.squire.undo()
},
redo: {
html: '↷',
cmd: () => this.squire.redo()
},
},
content = doc.createElement('div'),
toolbar = doc.createElement('div'),
squire = new Squire(content, SquireDefaultConfig);
content.id = 'squire-content';
content.style.minHeight = '200px';
this.squire = squire;
this.content = content;
toolbar.id = 'squire-toolbar';
for (let action in actions) {
if ('source' == action && !rl.settings.app('allowHtmlEditorSourceButton')) {
continue;
}
let cfg = actions[action],
btn = cfg.btn = doc.createElement('button');
btn.type = 'button';
btn.dataset.action = action;
btn.action_cmd = cfg.cmd;
btn.innerHTML = cfg.html;
btn.style.padding = 0;
cfg.style && btn.setAttribute('style', cfg.style);
toolbar.append(btn);
}
toolbar.addEventListener('click', e => {
e.target.action_cmd && e.target.action_cmd();
});
actions.undo.btn.disabled = actions.redo.btn.disabled = true;
squire.addEventListener('undoStateChange', state => {
actions.undo.btn.disabled = !state.canUndo;
actions.redo.btn.disabled = !state.canRedo;
});
container.append(toolbar, content);
/*
squire-raw.js:2161: this.fireEvent( 'dragover', {
squire-raw.js:2168: this.fireEvent( 'drop', {
squire-raw.js:2583: this.fireEvent( event.type, event );
squire-raw.js:2864: this.fireEvent( 'pathChange', { path: newPath } );
squire-raw.js:2867: this.fireEvent( range.collapsed ? 'cursor' : 'select', {
squire-raw.js:3004: this.fireEvent( 'input' );
squire-raw.js:3080: this.fireEvent( 'input' );
squire-raw.js:3101: this.fireEvent( 'input' );
squire-raw.js:4036: this.fireEvent( 'willPaste', event );
squire-raw.js:4089: this.fireEvent( 'willPaste', event );
*/
// CKEditor gimmicks
this.mode = 'wysiwyg'; // 'plain'
this.plugins = {
plain: false
};
// .plugins.plain && this.editor.__plain
this.focusManager = {
hasFocus: () => squire._isFocused,
blur: () => squire.blur()
};
}
doAction(name, tag) {
if (this.testPresenceinSelection(tag, new RegExp('>'+tag+'\\b'))) {
name = 'remove' + (name.toUpperCase()[0]) + name.substr(1);
}
this.squire[name]();
}
getParentNodeName(selector) {
let parent = this.squire.getSelectionClosest(selector);
return parent ? parent.nodeName : null;
}
doList(type) {
let parent = this.getParentNodeName('UL,OL'),
fn = {UL:'makeUnorderedList',OL:'makeOrderedList'};
(parent && parent == type) ? this.squire.removeList() : this.squire[fn[type]]();
}
testPresenceinSelection(format, validation) {
return validation.test(this.squire.getPath()) | this.squire.hasFormat(format);
}
// CKeditor gimmicks
setMode(mode) {
this.mode = mode; // 'wysiwyg' or 'plain'
}
on(type, fn) {
this.squire.addEventListener(type, fn);
}
execCommand(cmd, cfg) {
if ('insertSignature' == cmd) {
if (cfg.clearCache) {
// remove it;
} else {
cfg.isHtml; // bool
cfg.insertBefore; // bool
cfg.signature; // string
}
}
}
checkDirty() {
return false;
}
resetDirty() {}
getData() {
return this.squire.getHTML();
}
setData(html) {
this.squire.setHTML(html);
}
focus() {
this.squire.focus();
}
resize(width, height) {
this.content.style.height = Math.max(200, (height - this.content.offsetTop)) + 'px';
}
setReadOnly(bool) {
this.content.contentEditable = !!bool;
}
}
class HtmlEditor {
editor;
blurTimer = 0;