snappymail/dev/Common/HtmlEditor.js

441 lines
10 KiB
JavaScript
Raw Normal View History

2020-08-14 04:58:41 +08:00
import { EventKeyCode } from 'Common/Enums';
import { SquireUI } from 'External/SquireUI';
2015-11-19 01:32:29 +08:00
/**
* @type {Object}
*/
const CKEditorDefaultConfig = {
'title': false,
'stylesSet': false,
'customConfig': '',
'contentsCss': '',
'toolbarGroups': [
{ name: 'spec' },
{ name: 'styles' },
{ name: 'basicstyles', groups: ['basicstyles', 'cleanup', 'bidi'] },
{ name: 'colors' },
{ name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align'] },
{ name: 'links' },
{ name: 'insert' },
{ name: 'document', groups: ['mode', 'document', 'doctools'] },
{ name: 'others' }
],
'removePlugins': 'liststyle',
'removeButtons': 'Format,Undo,Redo,Cut,Copy,Paste,Anchor,Strike,Subscript,Superscript,Image,SelectAll,Source',
'removeDialogTabs': 'link:advanced;link:target;image:advanced;images:advanced',
'extraPlugins': 'plain,signature',
'allowedContent': true,
'extraAllowedContent': true,
'fillEmptyBlocks': false,
'ignoreEmptyParagraph': true,
'disableNativeSpellChecker': false,
'colorButton_enableAutomatic': false,
'colorButton_enableMore': true,
'font_defaultLabel': 'Arial',
'fontSize_defaultLabel': '13',
'fontSize_sizes': '10/10px;12/12px;13/13px;14/14px;16/16px;18/18px;20/20px;24/24px;28/28px;36/36px;48/48px'
},
/**
* @type {Object}
*/
htmlEditorLangsMap = {
'ar_sa': 'ar-sa',
'bg_bg': 'bg',
'cs_CZ': 'cs',
'de_de': 'de',
'el_gr': 'el',
'es_es': 'es',
'et_ee': 'et',
'fr_fr': 'fr',
'hu_hu': 'hu',
'is_is': 'is',
'it_it': 'it',
'ja_jp': 'ja',
'ko_kr': 'ko',
'lt_lt': 'lt',
'lv_lv': 'lv',
'fa_ir': 'fa',
'nb_no': 'nb',
'nl_nl': 'nl',
'pl_pl': 'pl',
'pt_br': 'pt-br',
'pt_pt': 'pt',
'ro_ro': 'ro',
'ru_ru': 'ru',
'sk_sk': 'sk',
'sl_si': 'sl',
'sv_se': 'sv',
'tr_tr': 'tr',
'uk_ua': 'uk',
'zh_cn': 'zh-cn',
'zh_tw': 'zh'
};
2019-07-05 03:19:24 +08:00
class HtmlEditor {
2016-09-10 06:38:16 +08:00
editor;
blurTimer = 0;
__resizable = false;
__inited = false;
onBlur = null;
onReady = null;
onModeChange = null;
element;
resize;
2015-11-19 01:32:29 +08:00
/**
* @param {Object} element
* @param {Function=} onBlur
* @param {Function=} onReady
* @param {Function=} onModeChange
*/
2019-07-05 03:19:24 +08:00
constructor(element, onBlur = null, onReady = null, onModeChange = null) {
2015-11-19 01:32:29 +08:00
this.onBlur = onBlur;
this.onReady = onReady;
this.onModeChange = onModeChange;
2016-08-30 06:10:24 +08:00
this.element = element;
2015-11-19 01:32:29 +08:00
this.resize = this.resizeEditor.throttle(100);
2015-11-19 01:32:29 +08:00
this.init();
}
2016-09-10 06:38:16 +08:00
runOnBlur() {
this.onBlur && this.onBlur();
2016-09-10 06:38:16 +08:00
}
2015-11-19 01:32:29 +08:00
blurTrigger() {
2019-07-05 03:19:24 +08:00
if (this.onBlur) {
clearTimeout(this.blurTimer);
this.blurTimer = setTimeout(() => this.runOnBlur(), 200);
2015-11-19 01:32:29 +08:00
}
}
focusTrigger() {
this.onBlur && clearTimeout(this.blurTimer);
2015-11-19 01:32:29 +08:00
}
/**
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2015-11-19 01:32:29 +08:00
*/
isHtml() {
return this.editor ? 'wysiwyg' === this.editor.mode : false;
}
2016-08-31 05:31:51 +08:00
/**
* @returns {void}
*/
clearCachedSignature() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2016-08-31 05:31:51 +08:00
this.editor.execCommand('insertSignature', {
clearCache: true
});
}
}
2015-11-19 01:32:29 +08:00
/**
* @param {string} signature
* @param {bool} html
* @param {bool} insertBefore
2016-08-31 05:31:51 +08:00
* @returns {void}
2015-11-19 01:32:29 +08:00
*/
2017-07-06 06:31:41 +08:00
setSignature(signature, html, insertBefore = false) {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
this.editor.execCommand('insertSignature', {
2016-04-21 01:12:51 +08:00
isHtml: html,
insertBefore: insertBefore,
signature: signature
2015-11-19 01:32:29 +08:00
});
}
}
/**
2016-06-30 08:02:45 +08:00
* @returns {boolean}
2015-11-19 01:32:29 +08:00
*/
checkDirty() {
return this.editor ? this.editor.checkDirty() : false;
}
resetDirty() {
this.editor && this.editor.resetDirty();
2015-11-19 01:32:29 +08:00
}
/**
* @param {boolean=} wrapIsHtml = false
2016-06-30 08:02:45 +08:00
* @returns {string}
2015-11-19 01:32:29 +08:00
*/
getData(wrapIsHtml = false) {
2015-11-19 01:32:29 +08:00
let result = '';
2019-07-05 03:19:24 +08:00
if (this.editor) {
try {
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain) {
2015-11-19 01:32:29 +08:00
result = this.editor.__plain.getRawData();
2019-07-05 03:19:24 +08:00
} else {
result = wrapIsHtml
? '<div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">' +
this.editor.getData() +
'</div>'
: this.editor.getData();
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
return result;
}
/**
* @param {boolean=} wrapIsHtml = false
2016-06-30 08:02:45 +08:00
* @returns {string}
2015-11-19 01:32:29 +08:00
*/
getDataWithHtmlMark(wrapIsHtml = false) {
return (this.isHtml() ? ':HTML:' : '') + this.getData(wrapIsHtml);
2015-11-19 01:32:29 +08:00
}
modeToggle(plain) {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
try {
2019-07-05 03:19:24 +08:00
if (plain) {
if ('plain' === this.editor.mode) {
2015-11-19 01:32:29 +08:00
this.editor.setMode('wysiwyg');
}
2019-07-05 03:19:24 +08:00
} else if ('wysiwyg' === this.editor.mode) {
this.editor.setMode('plain');
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
setHtmlOrPlain(text, focus) {
2019-07-05 03:19:24 +08:00
if (':HTML:' === text.substr(0, 6)) {
2015-11-19 01:32:29 +08:00
this.setHtml(text.substr(6), focus);
2019-07-05 03:19:24 +08:00
} else {
2015-11-19 01:32:29 +08:00
this.setPlain(text, focus);
}
}
setHtml(html, focus) {
2019-07-05 03:19:24 +08:00
if (this.editor && this.__inited) {
2016-08-31 05:31:51 +08:00
this.clearCachedSignature();
2015-11-19 01:32:29 +08:00
this.modeToggle(true);
2019-07-05 03:19:24 +08:00
html = html.replace(/<p[^>]*><\/p>/gi, '');
2015-11-19 01:32:29 +08:00
try {
this.editor.setData(html);
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
focus && this.focus();
2015-11-19 01:32:29 +08:00
}
}
replaceHtml(find, replaceHtml) {
2019-07-05 03:19:24 +08:00
if (this.editor && this.__inited && 'wysiwyg' === this.editor.mode) {
2015-11-19 01:32:29 +08:00
try {
2016-09-10 06:38:16 +08:00
this.editor.setData(this.editor.getData().replace(find, replaceHtml));
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
setPlain(plain, focus) {
2019-07-05 03:19:24 +08:00
if (this.editor && this.__inited) {
2016-08-31 05:31:51 +08:00
this.clearCachedSignature();
2015-11-19 01:32:29 +08:00
this.modeToggle(false);
2019-07-05 03:19:24 +08:00
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain) {
2016-04-21 01:12:51 +08:00
this.editor.__plain.setRawData(plain);
2019-07-05 03:19:24 +08:00
} else {
2015-11-19 01:32:29 +08:00
try {
this.editor.setData(plain);
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
focus && this.focus();
2015-11-19 01:32:29 +08:00
}
}
init() {
2019-07-05 03:19:24 +08:00
if (this.element && !this.editor) {
const initFunc = () => {
if (window.CKEDITOR) {
const config = CKEditorDefaultConfig,
language = rl.settings.get('Language'),
allowSource = !!rl.settings.app('allowHtmlEditorSourceButton'),
biti = !!rl.settings.app('allowHtmlEditorBitiButtons');
2019-07-05 03:19:24 +08:00
if ((allowSource || !biti) && !config.toolbarGroups.__cfgInited) {
config.toolbarGroups.__cfgInited = true;
2019-07-05 03:19:24 +08:00
if (allowSource) {
config.removeButtons = config.removeButtons.replace(',Source', '');
}
2015-11-19 01:32:29 +08:00
if (!biti) {
config.removePlugins += (config.removePlugins ? ',' : '') + 'bidi';
}
2015-11-19 01:32:29 +08:00
}
config.enterMode = CKEDITOR.ENTER_BR;
config.shiftEnterMode = CKEDITOR.ENTER_P;
2015-11-19 01:32:29 +08:00
config.language = htmlEditorLangsMap[(language || 'en').toLowerCase()] || 'en';
if (CKEDITOR.env) {
CKEDITOR.env.isCompatible = true;
}
2016-07-01 06:50:11 +08:00
this.editor = CKEDITOR.appendTo(this.element, config);
2015-11-19 01:32:29 +08:00
this.editor.on('key', event => !(event && event.data && EventKeyCode.Tab === event.data.keyCode));
2015-11-19 01:32:29 +08:00
this.editor.on('blur', () => this.blurTrigger());
2015-11-19 01:32:29 +08:00
this.editor.on('mode', () => {
this.blurTrigger();
this.onModeChange && this.onModeChange('plain' !== this.editor.mode);
});
2019-07-05 03:19:24 +08:00
this.editor.on('focus', () => this.focusTrigger());
2019-07-05 03:19:24 +08:00
if (window.FileReader) {
this.editor.on('drop', (event) => {
if (0 < event.data.dataTransfer.getFilesCount()) {
const file = event.data.dataTransfer.getFile(0);
if (file && event.data.dataTransfer.id && file.type && file.type.match(/^image/i)) {
const id = event.data.dataTransfer.id,
imageId = `[img=${id}]`,
reader = new FileReader();
2019-07-05 03:19:24 +08:00
reader.onloadend = () => {
if (reader.result) {
this.replaceHtml(imageId, `<img src="${reader.result}" />`);
}
};
2019-07-05 03:19:24 +08:00
reader.readAsDataURL(file);
2019-07-05 03:19:24 +08:00
event.data.dataTransfer.setData('text/html', imageId);
}
2019-07-05 03:19:24 +08:00
}
});
}
this.editor.on('instanceReady', () => {
if (this.editor.removeMenuItem) {
this.editor.removeMenuItem('cut');
this.editor.removeMenuItem('copy');
this.editor.removeMenuItem('paste');
2015-11-19 01:32:29 +08:00
}
this.__resizable = true;
this.__inited = true;
this.resize();
this.onReady && this.onReady();
2019-07-05 03:19:24 +08:00
});
}
else if (window.Squire) {
this.editor = new SquireUI(this.element, this.editor);
this.editor.on('blur', () => this.blurTrigger());
this.editor.on('focus', () => this.focusTrigger());
/*
// TODO
this.editor.on('key', event => !(event && event.data && EventKeyCode.Tab === event.data.keyCode));
this.editor.on('mode', () => {
this.blurTrigger();
this.onModeChange && this.onModeChange('plain' !== this.editor.mode);
});
if (window.FileReader) {
this.editor.on('dragover', (event) => {
event.dataTransfer = clipboardData
});
this.editor.on('drop', (event) => {
event.dataTransfer = clipboardData
if (0 < event.data.dataTransfer.getFilesCount()) {
const file = event.data.dataTransfer.getFile(0);
if (file && event.data.dataTransfer.id && file.type && file.type.match(/^image/i)) {
const id = event.data.dataTransfer.id,
imageId = `[img=${id}]`,
reader = new FileReader();
reader.onloadend = () => {
if (reader.result) {
this.replaceHtml(imageId, `<img src="${reader.result}" />`);
}
};
reader.readAsDataURL(file);
event.data.dataTransfer.setData('text/html', imageId);
}
}
});
2019-07-05 03:19:24 +08:00
}
*/
2019-07-05 03:19:24 +08:00
this.__resizable = true;
this.__inited = true;
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
this.resize();
2015-11-19 01:32:29 +08:00
this.onReady && setTimeout(() => this.onReady(), 1);
}
2019-07-05 03:19:24 +08:00
};
2015-11-19 01:32:29 +08:00
if (window.CKEDITOR || window.Squire) {
2015-11-19 01:32:29 +08:00
initFunc();
2019-07-05 03:19:24 +08:00
} else {
2015-11-19 01:32:29 +08:00
window.__initEditor = initFunc;
}
}
}
focus() {
try {
this.editor && this.editor.focus();
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
hasFocus() {
try {
return this.editor && !!this.editor.focusManager.hasFocus;
} catch (e) {
return false;
2015-11-19 01:32:29 +08:00
}
}
blur() {
try {
this.editor && this.editor.focusManager.blur(true);
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
2016-09-10 06:38:16 +08:00
resizeEditor() {
try {
this.editor && this.__resizable && this.editor.resize(this.element.clientWidth, this.element.clientHeight);
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
setReadOnly(value) {
try {
this.editor && this.editor.setReadOnly(!!value);
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
clear(focus) {
this.setHtml('', focus);
}
}
2019-07-05 03:19:24 +08:00
export { HtmlEditor, HtmlEditor as default };