Resolve issue #143

This commit is contained in:
djmaze 2021-09-13 13:32:06 +02:00
parent 96e2e6576c
commit 47b75755c4
2 changed files with 22 additions and 28 deletions

View file

@ -27,7 +27,6 @@ export class HtmlEditor {
this.blurTimer = 0; this.blurTimer = 0;
this.onBlur = onBlur; this.onBlur = onBlur;
this.onReady = onReady ? [onReady] : [];
this.onModeChange = onModeChange; this.onModeChange = onModeChange;
this.resize = (() => { this.resize = (() => {
@ -39,18 +38,21 @@ export class HtmlEditor {
if (element) { if (element) {
let editor; let editor;
const onReady = () => { onReady = onReady ? [onReady] : [];
this.onReady = fn => onReady.push(fn);
const readyCallback = () => {
this.editor = editor; this.editor = editor;
this.resize(); this.resize();
this.onReady.forEach(fn => fn()); this.onReady = fn => fn();
onReady.forEach(fn => fn());
}; };
if (rl.createWYSIWYG) { if (rl.createWYSIWYG) {
editor = rl.createWYSIWYG(element, onReady); editor = rl.createWYSIWYG(element, readyCallback);
} }
if (!editor) { if (!editor) {
editor = new SquireUI(element); editor = new SquireUI(element);
setTimeout(onReady,1); setTimeout(readyCallback, 1);
} }
editor.on('blur', () => this.blurTrigger()); editor.on('blur', () => this.blurTrigger());
@ -87,10 +89,9 @@ export class HtmlEditor {
* @returns {void} * @returns {void}
*/ */
clearCachedSignature() { clearCachedSignature() {
const fn = () => this.editor.execCommand('insertSignature', { this.onReady(() => this.editor.execCommand('insertSignature', {
clearCache: true clearCache: true
}); }));
this.editor ? fn() : this.onReady.push(fn);
} }
/** /**
@ -100,12 +101,11 @@ export class HtmlEditor {
* @returns {void} * @returns {void}
*/ */
setSignature(signature, html, insertBefore = false) { setSignature(signature, html, insertBefore = false) {
const fn = () => this.editor.execCommand('insertSignature', { this.onReady(() => this.editor.execCommand('insertSignature', {
isHtml: html, isHtml: html,
insertBefore: insertBefore, insertBefore: insertBefore,
signature: signature signature: signature
}); }));
this.editor ? fn() : this.onReady.push(fn);
} }
/** /**
@ -140,14 +140,10 @@ export class HtmlEditor {
} }
modeWysiwyg() { modeWysiwyg() {
try { this.onReady(() => this.editor.setMode('wysiwyg'));
this.editor && this.editor.setMode('wysiwyg');
} catch (e) { console.error(e); }
} }
modePlain() { modePlain() {
try { this.onReady(() => this.editor.setMode('plain'));
this.editor && this.editor.setMode('plain');
} catch (e) { console.error(e); }
} }
setHtmlOrPlain(text) { setHtmlOrPlain(text) {
@ -159,7 +155,7 @@ export class HtmlEditor {
} }
setData(mode, data) { setData(mode, data) {
const fn = () => { this.onReady(() => {
const editor = this.editor; const editor = this.editor;
this.clearCachedSignature(); this.clearCachedSignature();
try { try {
@ -170,8 +166,7 @@ export class HtmlEditor {
editor.setData(data); editor.setData(data);
} }
} catch (e) { console.error(e); } } catch (e) { console.error(e); }
}; });
this.editor ? fn() : this.onReady.push(fn);
} }
setHtml(html) { setHtml(html) {
@ -183,9 +178,7 @@ export class HtmlEditor {
} }
focus() { focus() {
try { this.onReady(() => this.editor.focus());
this.editor && this.editor.focus();
} catch (e) {} // eslint-disable-line no-empty
} }
hasFocus() { hasFocus() {
@ -197,12 +190,10 @@ export class HtmlEditor {
} }
blur() { blur() {
try { this.onReady(() => this.editor.focusManager.blur(true));
this.editor && this.editor.focusManager.blur(true);
} catch (e) {} // eslint-disable-line no-empty
} }
clear() { clear() {
this.setHtml(''); this.onReady(() => this.isPlain() ? this.setPlain('') : this.setHtml(''));
} }
} }

View file

@ -81,7 +81,9 @@ export function htmlToPlain(html) {
.replace(/"/gi, '"') .replace(/"/gi, '"')
.replace(/<[^>]*>/gm, ''); .replace(/<[^>]*>/gm, '');
text = tpl.content.textContent text = tpl.content.textContent;
if (text) {
text = text
.replace(/\n[ \t]+/gm, '\n') .replace(/\n[ \t]+/gm, '\n')
.replace(/[\n]{3,}/gm, '\n\n') .replace(/[\n]{3,}/gm, '\n\n')
.replace(/&gt;/gi, '>') .replace(/&gt;/gi, '>')
@ -89,6 +91,7 @@ export function htmlToPlain(html) {
.replace(/&amp;/gi, '&') .replace(/&amp;/gi, '&')
// wordwrap max line length 100 // wordwrap max line length 100
.match(/.{1,100}(\s|$)|\S+?(\s|$)/g).join('\n'); .match(/.{1,100}(\s|$)|\S+?(\s|$)/g).join('\n');
}
while (0 < --limit) { while (0 < --limit) {
iP1 = text.indexOf('__bq__start__', pos); iP1 = text.indexOf('__bq__start__', pos);