snappymail/dev/External/SquireUI.js

490 lines
13 KiB
JavaScript
Raw Normal View History

/* eslint max-len: 0 */
2020-09-11 18:39:56 +08:00
2020-10-01 17:10:40 +08:00
(doc => {
2020-10-01 17:10:40 +08:00
const
2020-09-11 18:39:56 +08:00
removeElements = 'HEAD,LINK,META,NOSCRIPT,SCRIPT,TEMPLATE,TITLE',
allowedElements = 'A,B,BLOCKQUOTE,BR,DIV,FONT,H1,H2,H3,H4,H5,H6,HR,IMG,LI,OL,P,SPAN,STRONG,TABLE,TD,TH,TR,U,UL',
allowedAttributes = 'abbr,align,background,bgcolor,border,cellpadding,cellspacing,class,color,colspan,dir,face,frame,height,href,hspace,id,lang,rowspan,rules,scope,size,src,style,target,type,usemap,valign,vspace,width'.split(','),
2020-09-11 18:39:56 +08:00
i18n = (str, def) => rl.i18n(str) || def,
2021-08-13 16:03:13 +08:00
ctrlKey = shortcuts.getMetaKey() + ' + ',
2020-09-11 18:39:56 +08:00
2021-12-28 21:49:40 +08:00
createElement = name => doc.createElement(name),
tpl = createElement('template'),
clr = createElement('input'),
trimLines = html => html.trim().replace(/^(<div>\s*<br\s*\/?>\s*<\/div>)+/, '').trim(),
2022-02-08 20:48:39 +08:00
htmlToPlain = html => rl.Utils.htmlToPlain(html).trim(),
plainToHtml = text => rl.Utils.plainToHtml(text),
2020-09-11 18:39:56 +08:00
getFragmentOfChildren = parent => {
let frag = doc.createDocumentFragment();
frag.append(...parent.childNodes);
return frag;
},
2020-09-11 18:39:56 +08:00
SquireDefaultConfig = {
/*
blockTag: 'P',
undo: {
documentSizeThreshold: -1, // -1 means no threshold
undoLimit: -1 // -1 means no limit
},
addLinks: true // allow_smart_html_links
*/
2020-09-11 18:39:56 +08:00
sanitizeToDOMFragment: (html, isPaste/*, squire*/) => {
tpl.innerHTML = (html||'')
2020-09-11 18:39:56 +08:00
.replace(/<\/?(BODY|HTML)[^>]*>/gi,'')
.replace(/<!--[^>]+-->/g,'')
.replace(/<span[^>]*>\s*<\/span>/gi,'')
2020-09-11 18:39:56 +08:00
.trim();
tpl.querySelectorAll('a:empty,span:empty').forEach(el => el.remove());
tpl.querySelectorAll('[data-x-div-type]').forEach(el => el.replaceWith(getFragmentOfChildren(el)));
2020-09-11 18:39:56 +08:00
if (isPaste) {
tpl.querySelectorAll(removeElements).forEach(el => el.remove());
tpl.querySelectorAll('*').forEach(el => {
if (!el.matches(allowedElements)) {
el.replaceWith(getFragmentOfChildren(el));
} else if (el.hasAttributes()) {
2020-09-22 16:13:32 +08:00
[...el.attributes].forEach(attr => {
2020-09-11 18:39:56 +08:00
let name = attr.name.toLowerCase();
if (!allowedAttributes.includes(name)) {
el.removeAttribute(name);
}
});
}
});
}
return tpl.content;
}
2020-09-11 18:39:56 +08:00
};
clr.type = "color";
clr.style.display = 'none';
doc.body.append(clr);
class SquireUI
{
constructor(container) {
const
2022-02-26 17:22:14 +08:00
doClr = name => () => {
clr.value = '';
2022-02-26 17:22:14 +08:00
clr.onchange = () => squire.setStyle({[name]:clr.value});
clr.click();
},
2020-09-11 18:39:56 +08:00
actions = {
mode: {
plain: {
html: '〈〉',
cmd: () => this.setMode('plain' == this.mode ? 'wysiwyg' : 'plain'),
hint: i18n('EDITOR/TEXT_SWITCHER_PLAINT_TEXT', 'Plain')
}
},
font: {
fontFamily: {
select: {
'sans-serif': {
Arial: "'Nimbus Sans L', 'Liberation sans', 'Arial Unicode MS', Arial, Helvetica, Garuda, Utkal, FreeSans, sans-serif",
Tahoma: "'Luxi Sans', Tahoma, Loma, Geneva, Meera, sans-serif",
Trebuchet: "'DejaVu Sans Condensed', Trebuchet, 'Trebuchet MS', sans-serif",
Lucida: "'Lucida Sans Unicode', 'Lucida Sans', 'DejaVu Sans', 'Bitstream Vera Sans', 'DejaVu LGC Sans', sans-serif",
Verdana: "'DejaVu Sans', Verdana, Geneva, 'Bitstream Vera Sans', 'DejaVu LGC Sans', sans-serif"
},
monospace: {
Courier: "'Liberation Mono', 'Courier New', FreeMono, Courier, monospace",
Lucida: "'DejaVu Sans Mono', 'DejaVu LGC Sans Mono', 'Bitstream Vera Sans Mono', 'Lucida Console', Monaco, monospace"
},
sans: {
Times: "'Nimbus Roman No9 L', 'Times New Roman', Times, FreeSerif, serif",
Palatino: "'Bitstream Charter', 'Palatino Linotype', Palatino, Palladio, 'URW Palladio L', 'Book Antiqua', Times, serif",
Georgia: "'URW Palladio L', Georgia, Times, serif"
}
},
cmd: s => squire.setStyle({ fontFamily: s.value })
},
2020-09-11 18:39:56 +08:00
fontSize: {
select: ['11px','13px','16px','20px','24px','30px'],
cmd: s => squire.setStyle({ fontSize: s.value })
2020-09-11 18:39:56 +08:00
}
},
2020-09-11 18:39:56 +08:00
colors: {
textColor: {
html: 'A<sub>▾</sub>',
2022-02-26 17:22:14 +08:00
cmd: doClr('color'),
2020-09-11 18:39:56 +08:00
hint: 'Text color'
},
backgroundColor: {
html: '🎨', /* ▧ */
2022-02-26 17:22:14 +08:00
cmd: doClr('backgroundColor'),
2020-09-11 18:39:56 +08:00
hint: 'Background color'
},
},
2020-09-11 18:39:56 +08:00
inline: {
bold: {
2020-09-13 20:13:16 +08:00
html: 'B',
cmd: () => this.doAction('bold'),
2020-09-11 18:39:56 +08:00
key: 'B',
hint: 'Bold'
},
2020-09-11 18:39:56 +08:00
italic: {
2020-09-13 20:13:16 +08:00
html: 'I',
cmd: () => this.doAction('italic'),
2020-09-11 18:39:56 +08:00
key: 'I',
hint: 'Italic'
},
underline: {
html: '<u>U</u>',
cmd: () => this.doAction('underline'),
2020-09-11 18:39:56 +08:00
key: 'U',
hint: 'Underline'
},
2020-09-11 18:39:56 +08:00
strike: {
html: '<s>S</s>',
cmd: () => this.doAction('strikethrough'),
2020-09-11 18:39:56 +08:00
key: 'Shift + 7',
hint: 'Strikethrough'
},
sub: {
2020-09-13 20:13:16 +08:00
html: 'Xₙ',
cmd: () => this.doAction('subscript'),
2020-09-11 18:39:56 +08:00
key: 'Shift + 5',
hint: 'Subscript'
},
sup: {
2020-09-13 20:13:16 +08:00
html: 'Xⁿ',
cmd: () => this.doAction('superscript'),
2020-09-11 18:39:56 +08:00
key: 'Shift + 6',
hint: 'Superscript'
}
},
2020-09-11 18:39:56 +08:00
block: {
ol: {
html: '#',
cmd: () => this.doList('OL'),
key: 'Shift + 8',
hint: 'Ordered list'
},
2020-09-11 18:39:56 +08:00
ul: {
html: '⋮',
cmd: () => this.doList('UL'),
key: 'Shift + 9',
hint: 'Unordered list'
},
quote: {
html: '"',
cmd: () => {
let parent = this.getParentNodeName('UL,OL');
(parent && 'BLOCKQUOTE' == parent) ? squire.decreaseQuoteLevel() : squire.increaseQuoteLevel();
},
hint: 'Blockquote'
},
indentDecrease: {
html: '⇤',
cmd: () => squire.changeIndentationLevel('decrease'),
key: ']',
hint: 'Decrease indent'
},
indentIncrease: {
html: '⇥',
cmd: () => squire.changeIndentationLevel('increase'),
key: '[',
hint: 'Increase indent'
}
},
2020-09-11 18:39:56 +08:00
targets: {
link: {
html: '🔗',
cmd: () => {
if ('A' === this.getParentNodeName()) {
squire.removeLink();
} else {
let url = prompt("Link","https://");
url != null && url.length && squire.makeLink(url);
}
},
hint: 'Link'
},
imageUrl: {
2020-09-11 18:39:56 +08:00
html: '🖼️',
cmd: () => {
if ('IMG' === this.getParentNodeName()) {
// squire.removeLink();
} else {
let src = prompt("Image","https://");
src != null && src.length && squire.insertImage(src);
}
},
hint: 'Image URL'
2020-09-11 18:39:56 +08:00
},
imageUpload: {
html: '📂️',
cmd: () => browseImage.click(),
hint: 'Image select',
2020-09-11 18:39:56 +08:00
}
},
/*
2020-09-11 18:39:56 +08:00
table: {
// TODO
},
2020-09-11 18:39:56 +08:00
*/
changes: {
undo: {
html: '↶',
cmd: () => squire.undo(),
key: 'Z',
hint: 'Undo'
},
redo: {
html: '↷',
cmd: () => squire.redo(),
key: 'Y',
hint: 'Redo'
}
}
2020-09-11 18:39:56 +08:00
},
2021-12-28 21:49:40 +08:00
plain = createElement('textarea'),
wysiwyg = createElement('div'),
toolbar = createElement('div'),
browseImage = createElement('input'),
2020-09-11 18:39:56 +08:00
squire = new Squire(wysiwyg, SquireDefaultConfig);
browseImage.type = 'file';
browseImage.accept = 'image/*';
browseImage.style.display = 'none';
browseImage.onchange = () => {
if (browseImage.files.length) {
let reader = new FileReader();
reader.readAsDataURL(browseImage.files[0]);
reader.onloadend = () => reader.result && squire.insertImage(reader.result);
}
}
2021-02-19 19:11:20 +08:00
plain.className = 'squire-plain';
wysiwyg.className = 'squire-wysiwyg';
this.mode = ''; // 'plain' | 'wysiwyg'
this.__plain = {
getRawData: () => this.plain.value,
setRawData: plain => this.plain.value = plain
};
this.container = container;
this.squire = squire;
this.plain = plain;
this.wysiwyg = wysiwyg;
2021-02-19 21:40:13 +08:00
toolbar.className = 'squire-toolbar btn-toolbar';
let group, action, touchTap;
for (group in actions) {
/*
if ('bidi' == group && !rl.settings.app('allowHtmlEditorBitiButtons')) {
continue;
}
*/
2021-12-28 21:49:40 +08:00
let toolgroup = createElement('div');
2021-02-19 19:11:20 +08:00
toolgroup.className = 'btn-group';
toolgroup.id = 'squire-toolgroup-'+group;
for (action in actions[group]) {
2020-09-22 17:19:52 +08:00
let cfg = actions[group][action], input, ev = 'click';
if (cfg.input) {
2021-12-28 21:49:40 +08:00
input = createElement('input');
input.type = cfg.input;
2020-09-22 17:19:52 +08:00
ev = 'change';
} else if (cfg.select) {
2021-12-28 21:49:40 +08:00
input = createElement('select');
2021-02-19 21:40:13 +08:00
input.className = 'btn';
if (Array.isArray(cfg.select)) {
cfg.select.forEach(value => {
var option = new Option(value, value);
option.style[action] = value;
input.append(option);
});
} else {
Object.entries(cfg.select).forEach(([label, options]) => {
2021-12-28 21:49:40 +08:00
let group = createElement('optgroup');
group.label = label;
Object.entries(options).forEach(([text, value]) => {
var option = new Option(text, value);
option.style[action] = value;
group.append(option);
});
input.append(group);
});
}
2020-09-22 17:19:52 +08:00
ev = 'input';
} else {
2021-12-28 21:49:40 +08:00
input = createElement('button');
input.type = 'button';
2021-02-19 19:11:20 +08:00
input.className = 'btn';
input.innerHTML = cfg.html;
input.action_cmd = cfg.cmd;
input.addEventListener('touchstart', () => touchTap = input, {passive:true});
input.addEventListener('touchmove', () => touchTap = null, {passive:true});
input.addEventListener('touchcancel', () => touchTap = null);
2020-09-22 17:19:52 +08:00
input.addEventListener('touchend', e => {
if (touchTap === input) {
e.preventDefault();
cfg.cmd(input);
}
touchTap = null;
});
}
2020-09-22 17:19:52 +08:00
input.addEventListener(ev, () => cfg.cmd(input));
if (cfg.hint) {
input.title = cfg.key ? cfg.hint + ' (' + ctrlKey + cfg.key + ')' : cfg.hint;
} else if (cfg.key) {
input.title = ctrlKey + cfg.key;
}
input.dataset.action = action;
2021-07-14 18:03:09 +08:00
input.tabIndex = -1;
cfg.input = input;
toolgroup.append(input);
}
toolgroup.children.length && toolbar.append(toolgroup);
}
2020-09-11 18:39:56 +08:00
let changes = actions.changes;
changes.undo.input.disabled = changes.redo.input.disabled = true;
squire.addEventListener('undoStateChange', state => {
changes.undo.input.disabled = !state.canUndo;
changes.redo.input.disabled = !state.canRedo;
});
2020-09-26 18:25:57 +08:00
squire.addEventListener('focus', () => shortcuts.off());
squire.addEventListener('blur', () => shortcuts.on());
container.append(toolbar, wysiwyg, plain);
/*
2020-09-26 18:25:57 +08:00
squire.addEventListener('dragover', );
squire.addEventListener('drop', );
squire.addEventListener('pathChange', );
squire.addEventListener('cursor', );
squire.addEventListener('select', );
squire.addEventListener('input', );
squire.addEventListener('willPaste', );
squire.addEventListener( 'keydown keyup', monitorShiftKey )
squire.addEventListener( 'keydown', onKey )
*/
// CKEditor gimmicks used by HtmlEditor
this.plugins = {
plain: true
};
this.focusManager = {
hasFocus: () => squire._isFocused,
blur: () => squire.blur()
};
}
doAction(name) {
this.squire[name]();
this.squire.focus();
}
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);
}
setMode(mode) {
if (this.mode != mode) {
let cl = this.container.classList;
cl.remove('squire-mode-'+this.mode);
if ('plain' == mode) {
2022-02-08 20:48:39 +08:00
this.plain.value = htmlToPlain(this.squire.getHTML(), true);
} else {
2022-02-08 20:48:39 +08:00
this.setData(plainToHtml(this.plain.value, true));
mode = 'wysiwyg';
}
this.mode = mode; // 'wysiwyg' or 'plain'
cl.add('squire-mode-'+mode);
this.onModeChange && this.onModeChange();
setTimeout(()=>this.focus(),1);
}
}
// CKeditor gimmicks used by HtmlEditor
on(type, fn) {
if ('mode' == type) {
this.onModeChange = fn;
} else {
this.squire.addEventListener(type, fn);
this.plain.addEventListener(type, fn);
}
}
execCommand(cmd, cfg) {
if ('insertSignature' == cmd) {
cfg = Object.assign({
clearCache: false,
isHtml: false,
insertBefore: false,
signature: ''
}, cfg);
if (cfg.clearCache) {
2022-02-08 20:48:39 +08:00
this._prev_txt_sig = null;
} else try {
2022-02-08 20:48:39 +08:00
const signature = cfg.isHtml ? htmlToPlain(cfg.signature) : cfg.signature;
if ('plain' === this.mode) {
2022-02-08 20:48:39 +08:00
let
text = this.plain.value,
prevSignature = this._prev_txt_sig;
if (prevSignature) {
text = text.replace(prevSignature, '').trim();
}
2022-02-08 20:48:39 +08:00
this.plain.value = cfg.insertBefore ? '\n\n' + signature + '\n\n' + text : text + '\n\n' + signature;
} else {
2022-02-26 16:39:22 +08:00
const squire = this.squire,
root = squire.getRoot(),
range = squire.getSelection(),
2022-02-08 20:48:39 +08:00
div = createElement('div');
div.className = 'rl-signature';
div.innerHTML = cfg.isHtml ? cfg.signature : plainToHtml(cfg.signature);
root.querySelectorAll('div.rl-signature').forEach(node => node.remove());
cfg.insertBefore ? root.prepend(div) : root.append(div);
2022-02-26 16:39:22 +08:00
// Move cursor above signature
range.setStart(div, 0);
range.setEnd(div, 0);
squire.setSelection( range );
}
2022-02-08 20:48:39 +08:00
this._prev_txt_sig = signature;
} catch (e) {
console.error(e);
}
}
}
getData() {
return trimLines(this.squire.getHTML());
}
setData(html) {
// this.plain.value = html;
this.squire.setHTML(trimLines(html));
}
focus() {
2020-09-11 18:39:56 +08:00
('plain' == this.mode ? this.plain : this.squire).focus();
}
}
2020-10-01 17:10:40 +08:00
this.SquireUI = SquireUI;
2020-09-11 18:39:56 +08:00
2020-10-01 17:10:40 +08:00
})(document);