snappymail/dev/Common/Html.js

226 lines
4.6 KiB
JavaScript
Raw Normal View History

const
htmlre = /[&<>"']/g,
htmlmap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
"'": '&#x27;'
};
/**
* @param {string} text
* @returns {string}
*/
export function encodeHtml(text) {
return (text && text.toString ? text.toString() : ''+text).replace(htmlre, m => htmlmap[m]);
}
2019-07-05 03:19:24 +08:00
class HtmlEditor {
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) {
this.editor;
this.blurTimer = 0;
this.__resizable = false;
this.__inited = false;
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 = (() => {
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) {
clearTimeout(this.blurTimer);
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() {
return this.editor ? !this.isPlain() : false;
}
/**
* @returns {boolean}
*/
isPlain() {
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() {
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) {
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
*/
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 (this.isPlain() && 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
}
modeWysiwyg() {
try {
this.editor && this.editor.setMode('wysiwyg');
} catch (e) { console.error(e); }
}
modePlain() {
try {
this.editor && this.editor.setMode('plain');
} catch (e) { console.error(e); }
2015-11-19 01:32:29 +08:00
}
setHtmlOrPlain(text) {
2019-07-05 03:19:24 +08:00
if (':HTML:' === text.substr(0, 6)) {
this.setHtml(text.substr(6));
2019-07-05 03:19:24 +08:00
} else {
this.setPlain(text);
2015-11-19 01:32:29 +08:00
}
}
setData(mode, data) {
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
try {
this.editor.setMode(mode);
if (this.isPlain() && this.editor.plugins.plain && this.editor.__plain) {
this.editor.__plain.setRawData(data);
} else {
this.editor.setData(data);
}
} catch (e) { console.error(e); }
2015-11-19 01:32:29 +08:00
}
}
setHtml(html) {
this.setData('wysiwyg', html/*.replace(/<p[^>]*><\/p>/gi, '')*/);
}
2015-11-19 01:32:29 +08:00
setPlain(txt) {
this.setData('plain', txt);
2015-11-19 01:32:29 +08:00
}
init() {
2019-07-05 03:19:24 +08:00
if (this.element && !this.editor) {
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
2021-03-25 20:35:15 +08:00
if (rl.createWYSIWYG) {
this.editor = rl.createWYSIWYG(this.element, onReady);
}
if (!this.editor) {
this.editor = new SquireUI(this.element);
2020-09-23 17:20:00 +08:00
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(!this.isPlain());
2020-09-23 17:20:00 +08:00
});
2015-11-19 01:32:29 +08:00
}
}
}
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
}
clear() {
this.setHtml('');
2015-11-19 01:32:29 +08:00
}
}
2019-07-05 03:19:24 +08:00
export { HtmlEditor, HtmlEditor as default };