2020-09-23 17:20:00 +08:00
|
|
|
import { createCKEditor } from 'External/CKEditor.js';
|
2020-08-25 15:34: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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-09-11 20:40:13 +08:00
|
|
|
this.resize = (() => {
|
|
|
|
try {
|
|
|
|
this.editor && this.__resizable && this.editor.resize(element.clientWidth, element.clientHeight);
|
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
|
|
|
}).throttle(100);
|
2015-11-19 01:32:29 +08:00
|
|
|
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
blurTrigger() {
|
2019-07-05 03:19:24 +08:00
|
|
|
if (this.onBlur) {
|
2020-08-12 06:25:36 +08:00
|
|
|
clearTimeout(this.blurTimer);
|
2020-09-11 20:40:13 +08:00
|
|
|
this.blurTimer = setTimeout(() => this.onBlur && this.onBlur(), 200);
|
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() {
|
2020-09-17 22:10:27 +08:00
|
|
|
return this.editor ? 'plain' !== this.editor.mode : false;
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2016-08-31 05:31:51 +08:00
|
|
|
/**
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
clearCachedSignature() {
|
2020-09-11 20:40:13 +08:00
|
|
|
this.editor && this.editor.execCommand('insertSignature', {
|
|
|
|
clearCache: true
|
|
|
|
});
|
2016-08-31 05:31:51 +08:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-09-11 20:40:13 +08:00
|
|
|
this.editor && this.editor.execCommand('insertSignature', {
|
|
|
|
isHtml: html,
|
|
|
|
insertBefore: insertBefore,
|
|
|
|
signature: signature
|
|
|
|
});
|
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
|
|
|
*/
|
2016-08-17 06:01:20 +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
|
|
|
*/
|
2016-08-17 06:01:20 +08:00
|
|
|
getDataWithHtmlMark(wrapIsHtml = false) {
|
|
|
|
return (this.isHtml() ? ':HTML:' : '') + this.getData(wrapIsHtml);
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
2020-09-02 23:06:35 +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) {
|
2016-08-17 06:01:20 +08:00
|
|
|
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
|
|
|
|
2020-09-03 18:51:15 +08:00
|
|
|
focus && this.focus();
|
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
|
|
|
}
|
|
|
|
|
2020-09-03 18:51:15 +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) {
|
2020-09-11 00:47:28 +08:00
|
|
|
const onReady = () => {
|
|
|
|
if (this.editor.removeMenuItem) {
|
|
|
|
this.editor.removeMenuItem('cut');
|
|
|
|
this.editor.removeMenuItem('copy');
|
|
|
|
this.editor.removeMenuItem('paste');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.__resizable = true;
|
|
|
|
this.__inited = true;
|
|
|
|
|
|
|
|
this.resize();
|
|
|
|
|
|
|
|
this.onReady && this.onReady();
|
2019-07-05 03:19:24 +08:00
|
|
|
};
|
2015-11-19 01:32:29 +08:00
|
|
|
|
2020-09-23 17:20:00 +08:00
|
|
|
if (window.CKEDITOR) {
|
|
|
|
this.editor = createCKEditor(this.element);
|
|
|
|
this.editor.on('instanceReady', onReady);
|
2019-07-05 03:19:24 +08:00
|
|
|
} else {
|
2020-09-23 17:20:00 +08:00
|
|
|
this.editor = new SquireUI(this.element, this.editor);
|
|
|
|
setTimeout(onReady,1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.editor) {
|
|
|
|
this.editor.on('blur', () => this.blurTrigger());
|
|
|
|
this.editor.on('focus', () => this.blurTimer && clearTimeout(this.blurTimer));
|
|
|
|
this.editor.on('mode', () => {
|
|
|
|
this.blurTrigger();
|
|
|
|
this.onModeChange && this.onModeChange('plain' !== this.editor.mode);
|
|
|
|
});
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
2020-09-03 18:51:15 +08:00
|
|
|
try {
|
|
|
|
this.editor && this.editor.focus();
|
|
|
|
} catch (e) {} // eslint-disable-line no-empty
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hasFocus() {
|
2020-09-03 18:51:15 +08:00
|
|
|
try {
|
|
|
|
return this.editor && !!this.editor.focusManager.hasFocus;
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
2015-11-19 01:32:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blur() {
|
2020-09-03 18:51:15 +08:00
|
|
|
try {
|
|
|
|
this.editor && this.editor.focusManager.blur(true);
|
|
|
|
} 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 };
|