snappymail/dev/Common/HtmlEditor.js

371 lines
7.6 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import window from 'window';
import $ from '$';
2019-07-05 03:19:24 +08:00
import { htmlEditorDefaultConfig, htmlEditorLangsMap } from 'Common/Globals';
import { EventKeyCode, Magics } from 'Common/Enums';
2016-06-16 07:36:44 +08:00
import * as Settings from 'Storage/Settings';
2015-11-19 01:32:29 +08:00
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;
$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.$element = $(element);
// throttle
var t, o = this;
this.resize = ()=>{
if (!t) {
t = setTimeout(()=>{
o.resizeEditor();
t = 0;
}, 100);
}
};
2015-11-19 01:32:29 +08:00
this.init();
}
2016-09-10 06:38:16 +08:00
runOnBlur() {
2019-07-05 03:19:24 +08:00
if (this.onBlur) {
2016-09-10 06:38:16 +08:00
this.onBlur();
}
}
2015-11-19 01:32:29 +08:00
blurTrigger() {
2019-07-05 03:19:24 +08:00
if (this.onBlur) {
2015-11-19 01:32:29 +08:00
window.clearTimeout(this.blurTimer);
this.blurTimer = window.setTimeout(() => {
2016-09-10 06:38:16 +08:00
this.runOnBlur();
}, Magics.Time200ms);
2015-11-19 01:32:29 +08:00
}
}
focusTrigger() {
2019-07-05 03:19:24 +08:00
if (this.onBlur) {
2015-11-19 01:32:29 +08:00
window.clearTimeout(this.blurTimer);
}
}
/**
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() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
this.editor.resetDirty();
}
}
/**
* @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, resize) {
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
2019-07-05 03:19:24 +08:00
if (resize) {
2015-11-19 01:32:29 +08:00
this.resize();
}
}
}
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
2019-07-05 03:19:24 +08:00
if (focus) {
2015-11-19 01:32:29 +08:00
this.focus();
}
}
}
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
}
2019-07-05 03:19:24 +08:00
if (focus) {
2015-11-19 01:32:29 +08:00
this.focus();
}
}
}
init() {
2019-07-05 03:19:24 +08:00
if (this.element && !this.editor) {
const initFunc = () => {
const config = htmlEditorDefaultConfig,
language = Settings.settingsGet('Language'),
allowSource = !!Settings.appSettingsGet('allowHtmlEditorSourceButton'),
biti = !!Settings.appSettingsGet('allowHtmlEditorBitiButtons');
if ((allowSource || !biti) && !config.toolbarGroups.__cfgInited) {
config.toolbarGroups.__cfgInited = true;
if (allowSource) {
config.removeButtons = config.removeButtons.replace(',Source', '');
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
if (!biti) {
config.removePlugins += (config.removePlugins ? ',' : '') + 'bidi';
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
}
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
config.enterMode = window.CKEDITOR.ENTER_BR;
config.shiftEnterMode = window.CKEDITOR.ENTER_P;
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
config.language = htmlEditorLangsMap[(language || 'en').toLowerCase()] || 'en';
if (window.CKEDITOR.env) {
window.CKEDITOR.env.isCompatible = true;
}
2016-07-01 06:50:11 +08:00
2019-07-05 03:19:24 +08:00
this.editor = window.CKEDITOR.appendTo(this.element, config);
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
this.editor.on('key', (event) => {
if (event && event.data && EventKeyCode.Tab === event.data.keyCode) {
return false;
}
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
return true;
});
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
this.editor.on('blur', () => {
this.blurTrigger();
});
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
this.editor.on('mode', () => {
this.blurTrigger();
if (this.onModeChange) {
this.onModeChange('plain' !== this.editor.mode);
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
});
this.editor.on('focus', () => {
this.focusTrigger();
});
if (window.FileReader) {
this.editor.on('drop', (event) => {
if (0 < event.data.dataTransfer.getFilesCount()) {
const file = event.data.dataTransfer.getFile(0);
if (file && window.FileReader && event.data.dataTransfer.id && file.type && file.type.match(/^image/i)) {
const id = event.data.dataTransfer.id,
imageId = `[img=${id}]`,
reader = new window.FileReader();
reader.onloadend = () => {
if (reader.result) {
this.replaceHtml(imageId, `<img src="${reader.result}" />`);
}
};
reader.readAsDataURL(file);
event.data.dataTransfer.setData('text/html', imageId);
}
2015-11-19 01:32:29 +08:00
}
2019-07-05 03:19:24 +08:00
});
}
2015-11-19 01:32:29 +08:00
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
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
2019-07-05 03:19:24 +08:00
if (this.onReady) {
this.onReady();
}
});
};
2015-11-19 01:32:29 +08:00
2019-07-05 03:19:24 +08:00
if (window.CKEDITOR) {
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() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
try {
this.editor.focus();
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
hasFocus() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
try {
return !!this.editor.focusManager.hasFocus;
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
return false;
}
blur() {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
try {
this.editor.focusManager.blur(true);
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
2016-09-10 06:38:16 +08:00
resizeEditor() {
2019-07-05 03:19:24 +08:00
if (this.editor && this.__resizable) {
2015-11-19 01:32:29 +08:00
try {
this.editor.resize(this.$element.width(), this.$element.innerHeight());
2019-07-05 03:19:24 +08:00
} catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
setReadOnly(value) {
2019-07-05 03:19:24 +08:00
if (this.editor) {
2015-11-19 01:32:29 +08:00
try {
this.editor.setReadOnly(!!value);
2019-07-05 03:19:24 +08:00
} 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 };