snappymail/dev/Common/Html.js

200 lines
4.1 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]);
}
2021-08-20 21:40:07 +08:00
export 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.blurTimer = 0;
2015-11-19 01:32:29 +08:00
this.onBlur = onBlur;
this.onModeChange = onModeChange;
this.resize = (() => {
try {
2021-08-17 20:43:48 +08:00
this.editor && this.editor.resize(element.clientWidth, element.clientHeight);
} catch (e) {} // eslint-disable-line no-empty
}).throttle(100);
2015-11-19 01:32:29 +08:00
2021-08-17 20:43:48 +08:00
if (element) {
let editor;
2021-09-13 19:32:06 +08:00
onReady = onReady ? [onReady] : [];
this.onReady = fn => onReady.push(fn);
const readyCallback = () => {
2021-08-17 20:43:48 +08:00
this.editor = editor;
this.resize();
2021-09-13 19:32:06 +08:00
this.onReady = fn => fn();
onReady.forEach(fn => fn());
2021-08-17 20:43:48 +08:00
};
if (rl.createWYSIWYG) {
2021-09-13 19:32:06 +08:00
editor = rl.createWYSIWYG(element, readyCallback);
2021-08-17 20:43:48 +08:00
}
if (!editor) {
editor = new SquireUI(element);
2021-09-13 19:32:06 +08:00
setTimeout(readyCallback, 1);
2021-08-17 20:43:48 +08:00
}
editor.on('blur', () => this.blurTrigger());
editor.on('focus', () => this.blurTimer && clearTimeout(this.blurTimer));
editor.on('mode', () => {
this.blurTrigger();
this.onModeChange && this.onModeChange(!this.isPlain());
});
}
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.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() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.execCommand('insertSignature', {
clearCache: true
2021-09-13 19:32:06 +08:00
}));
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) {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.execCommand('insertSignature', {
isHtml: html,
insertBefore: insertBefore,
signature: signature
2021-09-13 19:32:06 +08:00
}));
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() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.setMode('wysiwyg'));
}
modePlain() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.setMode('plain'));
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) {
2021-09-13 19:32:06 +08:00
this.onReady(() => {
2021-08-17 20:43:48 +08:00
const editor = this.editor;
2016-08-31 05:31:51 +08:00
this.clearCachedSignature();
2015-11-19 01:32:29 +08:00
try {
2021-08-17 20:43:48 +08:00
editor.setMode(mode);
if (this.isPlain() && editor.plugins.plain && editor.__plain) {
editor.__plain.setRawData(data);
} else {
2021-08-17 20:43:48 +08:00
editor.setData(data);
}
} catch (e) { console.error(e); }
2021-09-13 19:32:06 +08:00
});
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
}
focus() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.focus());
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() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.editor.focusManager.blur(true));
2015-11-19 01:32:29 +08:00
}
clear() {
2021-09-13 19:32:06 +08:00
this.onReady(() => this.isPlain() ? this.setPlain('') : this.setHtml(''));
2015-11-19 01:32:29 +08:00
}
}