2020-09-10 18:56:45 +08:00
|
|
|
|
/* eslint max-len: 0 */
|
2020-09-11 18:39:56 +08:00
|
|
|
|
|
2020-10-01 17:10:40 +08:00
|
|
|
|
(doc => {
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
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-10 18:56:45 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
i18n = (str, def) => rl.i18n(str) || def,
|
2020-09-11 00:47:28 +08:00
|
|
|
|
|
2020-10-01 17:10:40 +08:00
|
|
|
|
ctrlKey = shortcuts.getMetaKey().replace('meta','⌘') + ' + ',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
|
2020-09-22 21:03:14 +08:00
|
|
|
|
tpl = doc.createElement('template'),
|
2020-09-28 17:26:20 +08:00
|
|
|
|
clr = doc.createElement('input'),
|
2020-09-22 21:03:14 +08:00
|
|
|
|
|
2020-09-30 23:11:06 +08:00
|
|
|
|
trimLines = html => html.trim().replace(/^(<div>\s*<br\s*\/?>\s*<\/div>)+/, '').trim(),
|
|
|
|
|
clearHtmlLine = html => rl.Utils.htmlToPlain(html).trim(),
|
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
getFragmentOfChildren = parent => {
|
|
|
|
|
let frag = doc.createDocumentFragment();
|
|
|
|
|
frag.append(...parent.childNodes);
|
|
|
|
|
return frag;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
|
|
|
|
|
SquireDefaultConfig = {
|
|
|
|
|
/*
|
|
|
|
|
blockTag: 'P',
|
|
|
|
|
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
|
2020-09-10 18:56:45 +08:00
|
|
|
|
*/
|
2020-09-11 18:39:56 +08:00
|
|
|
|
sanitizeToDOMFragment: (html, isPaste/*, squire*/) => {
|
|
|
|
|
tpl.innerHTML = html
|
|
|
|
|
.replace(/<\/?(BODY|HTML)[^>]*>/gi,'')
|
|
|
|
|
.replace(/<!--[^>]+-->/g,'')
|
2020-09-22 21:03:14 +08:00
|
|
|
|
.replace(/<span[^>]*>\s*<\/span>/gi,'')
|
2020-09-11 18:39:56 +08:00
|
|
|
|
.trim();
|
2020-09-22 21:03:14 +08:00
|
|
|
|
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 => {
|
2020-09-23 17:48:08 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-22 21:03:14 +08:00
|
|
|
|
return tpl.content;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
2020-09-11 18:39:56 +08:00
|
|
|
|
},
|
2020-09-11 03:44:01 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
rl_signature_replacer = (editor, text, signature, isHtml, insertBefore) => {
|
|
|
|
|
let
|
|
|
|
|
prevSignature = editor.__previous_signature,
|
|
|
|
|
skipInsert = false,
|
2020-09-30 23:11:06 +08:00
|
|
|
|
isEmptyText = false;
|
2020-09-11 18:39:56 +08:00
|
|
|
|
|
|
|
|
|
isEmptyText = !text.trim();
|
|
|
|
|
if (!isEmptyText && isHtml) {
|
|
|
|
|
isEmptyText = !clearHtmlLine(text);
|
2020-09-11 03:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
if (prevSignature && !isEmptyText) {
|
|
|
|
|
if (isHtml && !prevSignature.isHtml) {
|
|
|
|
|
prevSignature = {
|
|
|
|
|
body: rl.Utils.plainToHtml(prevSignature.body),
|
|
|
|
|
isHtml: true
|
|
|
|
|
};
|
|
|
|
|
} else if (!isHtml && prevSignature.isHtml) {
|
|
|
|
|
prevSignature = {
|
|
|
|
|
body: rl.Utils.htmlToPlain(prevSignature.body),
|
|
|
|
|
isHtml: true
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isHtml) {
|
|
|
|
|
var clearSig = clearHtmlLine(prevSignature.body);
|
|
|
|
|
text = text.replace(/<signature>([\s\S]*)<\/signature>/igm, all => {
|
|
|
|
|
var c = clearSig === clearHtmlLine(all);
|
|
|
|
|
if (!c) {
|
|
|
|
|
skipInsert = true;
|
|
|
|
|
}
|
|
|
|
|
return c ? '' : all;
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
var textLen = text.length;
|
|
|
|
|
text = text
|
2020-09-30 23:11:06 +08:00
|
|
|
|
.replace(prevSignature.body, '')
|
|
|
|
|
.replace(prevSignature.body, '');
|
2020-09-11 18:39:56 +08:00
|
|
|
|
skipInsert = textLen === text.length;
|
|
|
|
|
}
|
2020-09-11 03:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
if (!skipInsert) {
|
2020-09-30 23:11:06 +08:00
|
|
|
|
signature = (isHtml ? '<br/><br/><signature>' : "\n\n") + signature + (isHtml ? '</signature>' : '');
|
2020-09-11 03:44:01 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
text = insertBefore ? signature + text : text + signature;
|
2020-09-11 03:44:01 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
if (10 < signature.length) {
|
|
|
|
|
prevSignature = {
|
|
|
|
|
body: signature,
|
|
|
|
|
isHtml: isHtml
|
|
|
|
|
};
|
|
|
|
|
}
|
2020-09-11 03:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
editor.__previous_signature = prevSignature;
|
2020-09-11 03:44:01 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
return text;
|
|
|
|
|
};
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
2020-09-28 17:26:20 +08:00
|
|
|
|
clr.type = "color";
|
|
|
|
|
clr.style.display = 'none';
|
|
|
|
|
doc.body.append(clr);
|
|
|
|
|
|
2020-09-10 18:56:45 +08:00
|
|
|
|
class SquireUI
|
|
|
|
|
{
|
|
|
|
|
constructor(container) {
|
|
|
|
|
const
|
2020-09-30 18:31:34 +08:00
|
|
|
|
doClr = fn => () => {
|
|
|
|
|
clr.value = '';
|
|
|
|
|
clr.onchange = () => squire[fn](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"
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
cmd: s => squire.setFontFace(s.value)
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
fontSize: {
|
|
|
|
|
select: ['11px','13px','16px','20px','24px','30px'],
|
|
|
|
|
cmd: s => squire.setFontSize(s.value)
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
colors: {
|
|
|
|
|
textColor: {
|
2020-09-28 17:26:20 +08:00
|
|
|
|
html: 'A<sub>▾</sub>',
|
2020-09-30 18:31:34 +08:00
|
|
|
|
cmd: doClr('setTextColour'),
|
2020-09-11 18:39:56 +08:00
|
|
|
|
hint: 'Text color'
|
|
|
|
|
},
|
|
|
|
|
backgroundColor: {
|
2020-09-28 17:26:20 +08:00
|
|
|
|
html: '🎨', /* ▧ */
|
2020-09-30 18:31:34 +08:00
|
|
|
|
cmd: doClr('setHighlightColour'),
|
2020-09-11 18:39:56 +08:00
|
|
|
|
hint: 'Background color'
|
|
|
|
|
},
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
|
|
|
|
/*
|
2020-09-11 18:39:56 +08:00
|
|
|
|
bidi: {
|
2020-09-11 20:40:13 +08:00
|
|
|
|
bdoLtr: {
|
|
|
|
|
html: '‎𝐁',
|
|
|
|
|
cmd: () => this.doAction('bold','B'),
|
|
|
|
|
hint: 'Bold'
|
|
|
|
|
},
|
|
|
|
|
bdoRtl: {
|
|
|
|
|
html: '‏𝐁',
|
|
|
|
|
cmd: () => this.doAction('bold','B'),
|
|
|
|
|
hint: 'Bold'
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
*/
|
|
|
|
|
inline: {
|
|
|
|
|
bold: {
|
2020-09-13 20:13:16 +08:00
|
|
|
|
html: 'B',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
cmd: () => this.doAction('bold','B'),
|
|
|
|
|
key: 'B',
|
|
|
|
|
hint: 'Bold'
|
2020-09-11 03:44:01 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
italic: {
|
2020-09-13 20:13:16 +08:00
|
|
|
|
html: 'I',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
cmd: () => this.doAction('italic','I'),
|
|
|
|
|
key: 'I',
|
|
|
|
|
hint: 'Italic'
|
|
|
|
|
},
|
|
|
|
|
underline: {
|
|
|
|
|
html: '<u>U</u>',
|
|
|
|
|
cmd: () => this.doAction('underline','U'),
|
|
|
|
|
key: 'U',
|
|
|
|
|
hint: 'Underline'
|
2020-09-11 03:44:01 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
strike: {
|
|
|
|
|
html: '<s>S</s>',
|
|
|
|
|
cmd: () => this.doAction('strikethrough','S'),
|
|
|
|
|
key: 'Shift + 7',
|
|
|
|
|
hint: 'Strikethrough'
|
|
|
|
|
},
|
|
|
|
|
sub: {
|
2020-09-13 20:13:16 +08:00
|
|
|
|
html: 'Xₙ',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
cmd: () => this.doAction('subscript','SUB'),
|
|
|
|
|
key: 'Shift + 5',
|
|
|
|
|
hint: 'Subscript'
|
|
|
|
|
},
|
|
|
|
|
sup: {
|
2020-09-13 20:13:16 +08:00
|
|
|
|
html: 'Xⁿ',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
cmd: () => this.doAction('superscript','SUP'),
|
|
|
|
|
key: 'Shift + 6',
|
|
|
|
|
hint: 'Superscript'
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
2020-09-11 18:39:56 +08:00
|
|
|
|
block: {
|
|
|
|
|
ol: {
|
|
|
|
|
html: '#',
|
|
|
|
|
cmd: () => this.doList('OL'),
|
|
|
|
|
key: 'Shift + 8',
|
|
|
|
|
hint: 'Ordered list'
|
2020-09-11 03:44:01 +08:00
|
|
|
|
},
|
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-10 18:56:45 +08:00
|
|
|
|
},
|
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'
|
|
|
|
|
},
|
2020-09-11 20:40:13 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
},
|
2020-09-11 20:40:13 +08:00
|
|
|
|
hint: 'Image URL'
|
2020-09-11 18:39:56 +08:00
|
|
|
|
},
|
|
|
|
|
imageUpload: {
|
2020-09-11 20:40:13 +08:00
|
|
|
|
html: '📂️',
|
|
|
|
|
cmd: () => browseImage.click(),
|
|
|
|
|
hint: 'Image select',
|
2020-09-11 18:39:56 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2020-09-10 18:56:45 +08:00
|
|
|
|
/*
|
2020-09-11 18:39:56 +08:00
|
|
|
|
table: {
|
|
|
|
|
// TODO
|
2020-09-10 18:56:45 +08:00
|
|
|
|
},
|
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-10 18:56:45 +08:00
|
|
|
|
}
|
2020-09-11 18:39:56 +08:00
|
|
|
|
},
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
plain = doc.createElement('textarea'),
|
|
|
|
|
wysiwyg = doc.createElement('div'),
|
|
|
|
|
toolbar = doc.createElement('div'),
|
2020-09-11 20:40:13 +08:00
|
|
|
|
browseImage = doc.createElement('input'),
|
2020-09-11 18:39:56 +08:00
|
|
|
|
squire = new Squire(wysiwyg, SquireDefaultConfig);
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
2020-09-11 20:40:13 +08:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 00:47:28 +08:00
|
|
|
|
plain.className = 'squire-plain cke_plain cke_editable';
|
|
|
|
|
wysiwyg.className = 'squire-wysiwyg cke_wysiwyg_div cke_editable';
|
2020-09-11 03:44:01 +08:00
|
|
|
|
this.mode = ''; // 'plain' | 'wysiwyg'
|
2020-09-11 00:47:28 +08:00
|
|
|
|
this.__plain = {
|
|
|
|
|
getRawData: () => this.plain.value,
|
|
|
|
|
setRawData: plain => this.plain.value = plain
|
|
|
|
|
};
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
2020-09-11 00:47:28 +08:00
|
|
|
|
this.container = container;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
this.squire = squire;
|
2020-09-11 00:47:28 +08:00
|
|
|
|
this.plain = plain;
|
|
|
|
|
this.wysiwyg = wysiwyg;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
|
|
|
|
toolbar.className = 'squire-toolbar cke_top';
|
2020-09-22 17:19:52 +08:00
|
|
|
|
let touchTap;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
for (let group in actions) {
|
2020-09-11 20:40:13 +08:00
|
|
|
|
if ('bidi' == group && !rl.settings.app('allowHtmlEditorBitiButtons')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
let toolgroup = doc.createElement('div');
|
|
|
|
|
toolgroup.className = 'squire-toolgroup cke_toolgroup';
|
2020-09-11 00:47:28 +08:00
|
|
|
|
toolgroup.id = 'squire-toolgroup-'+group;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
for (let action in actions[group]) {
|
|
|
|
|
if ('source' == action && !rl.settings.app('allowHtmlEditorSourceButton')) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-09-22 17:19:52 +08:00
|
|
|
|
let cfg = actions[group][action], input, ev = 'click';
|
2020-09-10 18:56:45 +08:00
|
|
|
|
if (cfg.input) {
|
|
|
|
|
input = doc.createElement('input');
|
|
|
|
|
input.type = cfg.input;
|
2020-09-22 17:19:52 +08:00
|
|
|
|
ev = 'change';
|
2020-09-10 18:56:45 +08:00
|
|
|
|
} else if (cfg.select) {
|
|
|
|
|
input = doc.createElement('select');
|
|
|
|
|
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]) => {
|
|
|
|
|
let group = doc.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';
|
2020-09-10 18:56:45 +08:00
|
|
|
|
} else {
|
|
|
|
|
input = doc.createElement('button');
|
|
|
|
|
input.type = 'button';
|
|
|
|
|
input.innerHTML = cfg.html;
|
|
|
|
|
input.action_cmd = cfg.cmd;
|
2020-10-13 21:16:57 +08:00
|
|
|
|
input.addEventListener('touchstart', () => touchTap = input, {passive:true});
|
|
|
|
|
input.addEventListener('touchmove', () => touchTap = null, {passive:true});
|
2020-09-28 23:23:22 +08:00
|
|
|
|
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-10 18:56:45 +08:00
|
|
|
|
}
|
2020-09-22 17:19:52 +08:00
|
|
|
|
input.addEventListener(ev, () => cfg.cmd(input));
|
2020-09-10 18:56:45 +08:00
|
|
|
|
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;
|
|
|
|
|
cfg.input = input;
|
|
|
|
|
toolgroup.append(input);
|
|
|
|
|
}
|
|
|
|
|
toolgroup.children.length && toolbar.append(toolgroup);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 18:39:56 +08:00
|
|
|
|
let changes = actions.changes;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
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());
|
|
|
|
|
|
2020-09-11 00:47:28 +08:00
|
|
|
|
container.append(toolbar, wysiwyg, plain);
|
2020-09-10 18:56:45 +08:00
|
|
|
|
|
|
|
|
|
/*
|
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 )
|
2020-09-10 18:56:45 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// CKEditor gimmicks used by HtmlEditor
|
|
|
|
|
this.plugins = {
|
2020-09-11 00:47:28 +08:00
|
|
|
|
plain: true
|
2020-09-10 18:56:45 +08:00
|
|
|
|
};
|
|
|
|
|
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) {
|
2020-10-17 20:08:27 +08:00
|
|
|
|
return validation.test(this.squire.getPath()) || this.squire.hasFormat(format);
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setMode(mode) {
|
2021-02-02 19:23:32 +08:00
|
|
|
|
if (this.mode != mode) {
|
|
|
|
|
let cl = this.container.classList;
|
|
|
|
|
cl.remove('squire-mode-'+this.mode);
|
|
|
|
|
if ('plain' == mode) {
|
|
|
|
|
this.plain.value = rl.Utils.htmlToPlain(this.squire.getHTML(), true).trim();
|
|
|
|
|
} else {
|
|
|
|
|
this.setData(rl.Utils.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);
|
2020-09-11 00:47:28 +08:00
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-11 00:47:28 +08:00
|
|
|
|
// CKeditor gimmicks used by HtmlEditor
|
2020-09-10 18:56:45 +08:00
|
|
|
|
on(type, fn) {
|
2020-09-11 00:47:28 +08:00
|
|
|
|
if ('mode' == type) {
|
|
|
|
|
this.onModeChange = fn;
|
|
|
|
|
} else {
|
|
|
|
|
this.squire.addEventListener(type, fn);
|
|
|
|
|
this.plain.addEventListener(type, fn);
|
|
|
|
|
}
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
execCommand(cmd, cfg) {
|
|
|
|
|
if ('insertSignature' == cmd) {
|
2020-09-11 03:44:01 +08:00
|
|
|
|
cfg = Object.assign({
|
|
|
|
|
clearCache: false,
|
|
|
|
|
isHtml: false,
|
|
|
|
|
insertBefore: false,
|
|
|
|
|
signature: ''
|
|
|
|
|
}, cfg);
|
|
|
|
|
|
2020-09-10 18:56:45 +08:00
|
|
|
|
if (cfg.clearCache) {
|
2020-09-11 03:44:01 +08:00
|
|
|
|
this.__previous_signature = null;
|
|
|
|
|
} else try {
|
|
|
|
|
if ('plain' === this.mode) {
|
|
|
|
|
if (cfg.isHtml) {
|
|
|
|
|
cfg.signature = rl.Utils.htmlToPlain(cfg.signature);
|
|
|
|
|
}
|
|
|
|
|
this.plain.value = rl_signature_replacer(this, this.plain.value, cfg.signature, false, cfg.insertBefore);
|
|
|
|
|
} else {
|
|
|
|
|
if (!cfg.isHtml) {
|
|
|
|
|
cfg.signature = rl.Utils.plainToHtml(cfg.signature);
|
|
|
|
|
}
|
2020-09-30 23:11:06 +08:00
|
|
|
|
this.setData(rl_signature_replacer(this, this.getData(), cfg.signature, true, cfg.insertBefore));
|
2020-09-11 03:44:01 +08:00
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getData() {
|
2020-09-30 23:11:06 +08:00
|
|
|
|
return trimLines(this.squire.getHTML());
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setData(html) {
|
2021-02-02 19:23:32 +08:00
|
|
|
|
// this.plain.value = html;
|
2020-09-30 23:11:06 +08:00
|
|
|
|
this.squire.setHTML(trimLines(html));
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focus() {
|
2020-09-11 18:39:56 +08:00
|
|
|
|
('plain' == this.mode ? this.plain : this.squire).focus();
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resize(width, height) {
|
2020-09-11 00:47:28 +08:00
|
|
|
|
height = Math.max(200, (height - this.wysiwyg.offsetTop)) + 'px';
|
|
|
|
|
this.wysiwyg.style.height = height;
|
|
|
|
|
this.plain.style.height = height;
|
2020-09-10 18:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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);
|