snappymail/dev/Common/HtmlEditor.js

432 lines
7.9 KiB
JavaScript
Raw Normal View History

2015-11-19 01:32:29 +08:00
2016-07-02 06:49:59 +08:00
import window from 'window';
import _ from '_';
import $ from '$';
import {htmlEditorDefaultConfig, htmlEditorLangsMap} from 'Common/Globals';
2016-09-10 06:38:16 +08:00
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
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
*/
constructor(element, onBlur = null, onReady = null, onModeChange = null)
{
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);
2016-09-10 06:38:16 +08:00
this.resize = _.throttle(_.bind(this.resizeEditor, this), 100);
2015-11-19 01:32:29 +08:00
this.init();
}
2016-09-10 06:38:16 +08:00
runOnBlur() {
if (this.onBlur)
{
this.onBlur();
}
}
2015-11-19 01:32:29 +08:00
blurTrigger() {
if (this.onBlur)
{
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() {
if (this.onBlur)
{
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() {
if (this.editor)
{
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
*/
setSignature(signature, html, insertBefore = false, first = false) {
2015-11-19 01:32:29 +08:00
if (this.editor)
{
this.editor.execCommand('insertSignature', {
2016-04-21 01:12:51 +08:00
isHtml: html,
isFirst: first,
2016-04-21 01:12:51 +08:00
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() {
if (this.editor)
{
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 = '';
if (this.editor)
{
try
{
if ('plain' === this.editor.mode && this.editor.plugins.plain && this.editor.__plain)
{
result = this.editor.__plain.getRawData();
}
else
{
2016-04-21 01:12:51 +08:00
result = wrapIsHtml ?
2015-11-19 01:32:29 +08:00
'<div data-html-editor-font-wrapper="true" style="font-family: arial, sans-serif; font-size: 13px;">' +
this.editor.getData() + '</div>' : this.editor.getData();
}
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +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) {
if (this.editor)
{
try {
if (plain)
{
if ('plain' === this.editor.mode)
{
this.editor.setMode('wysiwyg');
}
}
else if ('wysiwyg' === this.editor.mode)
2015-11-19 01:32:29 +08:00
{
this.editor.setMode('plain');
2015-11-19 01:32:29 +08:00
}
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
if (resize)
{
this.resize();
}
}
}
setHtmlOrPlain(text, focus) {
if (':HTML:' === text.substr(0, 6))
{
this.setHtml(text.substr(6), focus);
}
else
{
this.setPlain(text, focus);
}
}
setHtml(html, focus) {
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);
html = html.replace(/<p[^>]*><\/p>/ig, '');
try {
this.editor.setData(html);
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
if (focus)
{
this.focus();
}
}
}
replaceHtml(find, replaceHtml) {
if (this.editor && this.__inited && 'wysiwyg' === this.editor.mode)
{
try {
2016-09-10 06:38:16 +08:00
this.editor.setData(this.editor.getData().replace(find, replaceHtml));
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
setPlain(plain, focus) {
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);
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);
2015-11-19 01:32:29 +08:00
}
else
{
try {
this.editor.setData(plain);
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
if (focus)
{
this.focus();
}
}
}
init() {
2016-08-30 06:10:24 +08:00
if (this.element && !this.editor)
2015-11-19 01:32:29 +08:00
{
const
initFunc = () => {
const
config = htmlEditorDefaultConfig,
2015-11-19 01:32:29 +08:00
language = Settings.settingsGet('Language'),
2016-04-30 07:42:18 +08:00
allowSource = !!Settings.appSettingsGet('allowHtmlEditorSourceButton'),
2016-06-30 08:02:45 +08:00
biti = !!Settings.appSettingsGet('allowHtmlEditorBitiButtons');
2015-11-19 01:32:29 +08:00
if ((allowSource || !biti) && !config.toolbarGroups.__cfgInited)
{
config.toolbarGroups.__cfgInited = true;
if (allowSource)
{
config.removeButtons = config.removeButtons.replace(',Source', '');
}
if (!biti)
{
2016-04-21 01:12:51 +08:00
config.removePlugins += (config.removePlugins ? ',' : '') + 'bidi';
2015-11-19 01:32:29 +08:00
}
}
config.enterMode = window.CKEDITOR.ENTER_BR;
config.shiftEnterMode = window.CKEDITOR.ENTER_P;
config.language = htmlEditorLangsMap[(language || 'en').toLowerCase()] || 'en';
2015-11-19 01:32:29 +08:00
if (window.CKEDITOR.env)
{
window.CKEDITOR.env.isCompatible = true;
}
2016-08-30 06:10:24 +08:00
this.editor = window.CKEDITOR.appendTo(this.element, config);
2015-11-19 01:32:29 +08:00
this.editor.on('key', (event) => {
2016-08-09 06:54:25 +08:00
if (event && event.data && EventKeyCode.Tab === event.data.keyCode)
2015-11-19 01:32:29 +08:00
{
return false;
}
2016-07-01 06:50:11 +08:00
return true;
2015-11-19 01:32:29 +08:00
});
this.editor.on('blur', () => {
this.blurTrigger();
});
this.editor.on('mode', () => {
this.blurTrigger();
if (this.onModeChange)
{
this.onModeChange('plain' !== this.editor.mode);
}
});
this.editor.on('focus', () => {
this.focusTrigger();
});
if (window.FileReader)
{
2016-04-21 01:12:51 +08:00
this.editor.on('drop', (event) => {
2015-11-19 01:32:29 +08:00
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
2015-11-19 01:32:29 +08:00
id = event.data.dataTransfer.id,
imageId = `[img=${id}]`,
2016-06-30 08:02:45 +08:00
reader = new window.FileReader();
2015-11-19 01:32:29 +08:00
reader.onloadend = () => {
if (reader.result)
{
this.replaceHtml(imageId, `<img src="${reader.result}" />`);
}
};
reader.readAsDataURL(file);
event.data.dataTransfer.setData('text/html', imageId);
}
}
});
}
this.editor.on('instanceReady', () => {
if (this.editor.removeMenuItem)
{
this.editor.removeMenuItem('cut');
this.editor.removeMenuItem('copy');
this.editor.removeMenuItem('paste');
}
this.__resizable = true;
this.__inited = true;
this.resize();
if (this.onReady)
{
this.onReady();
}
});
2016-06-30 08:02:45 +08:00
};
2015-11-19 01:32:29 +08:00
if (window.CKEDITOR)
{
initFunc();
}
else
{
window.__initEditor = initFunc;
}
}
}
focus() {
if (this.editor)
{
try {
this.editor.focus();
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
hasFocus() {
if (this.editor)
{
try {
return !!this.editor.focusManager.hasFocus;
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
return false;
}
blur() {
if (this.editor)
{
try {
this.editor.focusManager.blur(true);
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +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() {
2015-11-19 01:32:29 +08:00
if (this.editor && this.__resizable)
{
try {
this.editor.resize(this.$element.width(), this.$element.innerHeight());
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
setReadOnly(value) {
if (this.editor)
{
try {
this.editor.setReadOnly(!!value);
2016-06-30 08:02:45 +08:00
}
2016-07-02 06:49:59 +08:00
catch (e) {} // eslint-disable-line no-empty
2015-11-19 01:32:29 +08:00
}
}
clear(focus) {
this.setHtml('', focus);
}
}
export {HtmlEditor, HtmlEditor as default};