/
Editor.vue
461 lines (396 loc) · 13.7 KB
/
Editor.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<template>
<!-- Two-way Data-Binding -->
<section class="editor">
<div class="columns">
<div class="column is-6">
<b-field label="Format">
<div>
<b-radio v-model="form.radioFormat"
@input="onFormatChange" :disabled="disabled" name="format"
native-value="richtext"
data-cy="check-richtext">{{ $t('campaigns.richText') }}</b-radio>
<b-radio v-model="form.radioFormat"
@input="onFormatChange" :disabled="disabled" name="format"
native-value="html"
data-cy="check-html">{{ $t('campaigns.rawHTML') }}</b-radio>
<b-radio v-model="form.radioFormat"
@input="onFormatChange" :disabled="disabled" name="format"
native-value="markdown"
data-cy="check-markdown">{{ $t('campaigns.markdown') }}</b-radio>
<b-radio v-model="form.radioFormat"
@input="onFormatChange" :disabled="disabled" name="format"
native-value="plain"
data-cy="check-plain">{{ $t('campaigns.plainText') }}</b-radio>
</div>
</b-field>
</div>
<div class="column is-6 has-text-right">
<b-button @click="onTogglePreview" type="is-primary"
icon-left="file-find-outline" data-cy="btn-preview">
{{ $t('campaigns.preview') }}
</b-button>
</div>
</div>
<!-- wsywig //-->
<template v-if="isRichtextReady && form.format === 'richtext'">
<tiny-mce
v-model="form.body"
:disabled="disabled"
:init="richtextConf"
/>
<b-modal scroll="keep" :width="1200"
:aria-modal="true" :active.sync="isRichtextSourceVisible">
<div>
<section expanded class="modal-card-body preview">
<html-editor v-model="richTextSourceBody" />
</section>
<footer class="modal-card-foot has-text-right">
<b-button @click="onFormatRichtextHTML">{{ $t('campaigns.formatHTML') }}</b-button>
<b-button @click="() => { this.isRichtextSourceVisible = false; }">
{{ $t('globals.buttons.close') }}
</b-button>
<b-button @click="onSaveRichTextSource" class="is-primary">
{{ $t('globals.buttons.save') }}
</b-button>
</footer>
</div>
</b-modal>
</template>
<!-- raw html editor //-->
<html-editor v-if="form.format === 'html'" v-model="form.body" />
<!-- plain text / markdown editor //-->
<b-input v-if="form.format === 'plain' || form.format === 'markdown'"
v-model="form.body" @input="onEditorChange"
type="textarea" name="content" ref="plainEditor" class="plain-editor" />
<!-- campaign preview //-->
<campaign-preview v-if="isPreviewing"
@close="onTogglePreview"
type="campaign"
:id="id"
:title="title"
:contentType="form.format"
:body="form.body"></campaign-preview>
<!-- image picker -->
<b-modal scroll="keep" :aria-modal="true" :active.sync="isMediaVisible" :width="900">
<div class="modal-card content" style="width: auto">
<section expanded class="modal-card-body">
<media isModal @selected="onMediaSelect" />
</section>
</div>
</b-modal>
</section>
</template>
<script>
import { mapState } from 'vuex';
import TurndownService from 'turndown';
import { indent } from 'indent.js';
import 'tinymce';
import 'tinymce/icons/default';
import 'tinymce/themes/silver';
import 'tinymce/skins/ui/oxide/skin.css';
import 'tinymce/plugins/autoresize';
import 'tinymce/plugins/autolink';
import 'tinymce/plugins/charmap';
import 'tinymce/plugins/colorpicker';
import 'tinymce/plugins/contextmenu';
import 'tinymce/plugins/emoticons';
import 'tinymce/plugins/emoticons/js/emojis';
import 'tinymce/plugins/fullscreen';
import 'tinymce/plugins/help';
import 'tinymce/plugins/hr';
import 'tinymce/plugins/image';
import 'tinymce/plugins/imagetools';
import 'tinymce/plugins/link';
import 'tinymce/plugins/lists';
import 'tinymce/plugins/paste';
import 'tinymce/plugins/searchreplace';
import 'tinymce/plugins/table';
import 'tinymce/plugins/textcolor';
import 'tinymce/plugins/visualblocks';
import 'tinymce/plugins/visualchars';
import 'tinymce/plugins/wordcount';
import TinyMce from '@tinymce/tinymce-vue';
import CampaignPreview from './CampaignPreview.vue';
import HTMLEditor from './HTMLEditor.vue';
import Media from '../views/Media.vue';
import { colors, uris } from '../constants';
const turndown = new TurndownService();
// Map of listmonk language codes to corresponding TinyMCE language files.
const LANGS = {
'cs-cz': 'cs',
de: 'de',
es: 'es_419',
fr: 'fr_FR',
it: 'it_IT',
pl: 'pl',
pt: 'pt_PT',
'pt-BR': 'pt_BR',
ro: 'ro',
tr: 'tr',
};
export default {
components: {
Media,
CampaignPreview,
'html-editor': HTMLEditor,
TinyMce,
},
props: {
id: Number,
title: String,
body: String,
contentType: String,
disabled: Boolean,
},
data() {
return {
isPreviewing: false,
isMediaVisible: false,
isEditorFullscreen: false,
isReady: false,
isRichtextReady: false,
isRichtextSourceVisible: false,
richtextConf: {},
isTrackLink: false,
richTextSourceBody: '',
form: {
body: '',
format: this.contentType,
// Model bound to the checkboxes. This changes on click of the radio,
// but is reverted by the change handler if the user cancels the
// conversion warning. This is used to set the value of form.format
// that the editor uses to render content.
radioFormat: this.contentType,
},
// Last position of the cursor in the editor before the media popup
// was opened. This is used to insert media on selection from the poup
// where the caret may be lost.
lastSel: null,
};
},
methods: {
initRichtextEditor() {
const { lang } = this.serverConfig;
this.richtextConf = {
init_instance_callback: () => { this.isReady = true; },
urlconverter_callback: this.onEditorURLConvert,
setup: (editor) => {
editor.on('init', () => {
this.onEditorDialogOpen(editor);
});
// Custom HTML editor.
editor.ui.registry.addButton('html', {
icon: 'sourcecode',
tooltip: 'Source code',
onAction: this.onRichtextViewSource,
});
},
min_height: 500,
toolbar_sticky: true,
entity_encoding: 'raw',
convert_urls: true,
plugins: [
'autoresize', 'autolink', 'charmap', 'emoticons', 'fullscreen', 'help',
'hr', 'image', 'imagetools', 'link', 'lists', 'paste', 'searchreplace',
'table', 'visualblocks', 'visualchars', 'wordcount',
],
toolbar: `undo redo | formatselect styleselect fontsizeselect |
bold italic underline strikethrough forecolor backcolor subscript superscript |
alignleft aligncenter alignright alignjustify |
bullist numlist table image | outdent indent | link hr removeformat |
html fullscreen help`,
fontsize_formats: '10px 11px 12px 14px 15px 16px 18px 24px 36px',
skin: false,
content_css: false,
content_style: `
body { font-family: 'Inter', sans-serif; font-size: 15px; }
img { max-width: 100%; }
a { color: ${colors.primary}; }
table, td { border-color: #ccc;}
`,
language: LANGS[lang] || null,
language_url: LANGS[lang] ? `${uris.static}/tinymce/lang/${LANGS[lang]}.js` : null,
file_picker_types: 'image',
file_picker_callback: (callback) => {
this.isMediaVisible = true;
this.runTinyMceImageCallback = callback;
},
};
this.isRichtextReady = true;
},
onFormatChange(format) {
this.$utils.confirm(
this.$t('campaigns.confirmSwitchFormat'),
() => {
this.form.format = format;
this.onEditorChange();
},
() => {
// On cancel, undo the radio selection.
this.form.radioFormat = this.form.format;
},
);
},
onEditorURLConvert(url) {
let u = url;
if (this.isTrackLink) {
u = `${u}@TrackLink`;
}
this.isTrackLink = false;
return u;
},
onRichtextViewSource() {
this.richTextSourceBody = this.form.body;
this.isRichtextSourceVisible = true;
},
onFormatRichtextHTML() {
this.richTextSourceBody = this.beautifyHTML(this.richTextSourceBody);
},
onSaveRichTextSource() {
this.form.body = this.richTextSourceBody;
window.tinymce.editors[0].setContent(this.form.body);
this.richTextSourceBody = '';
this.isRichtextSourceVisible = false;
},
onEditorDialogOpen(editor) {
const ed = editor;
const oldEd = ed.windowManager.open;
const self = this;
ed.windowManager.open = (t, r) => {
const isOK = t.initialData && 'url' in t.initialData && 'anchor' in t.initialData;
// Not the link modal.
if (!isOK) {
return oldEd.apply(this, [t, r]);
}
// If an existing link is being edited, check for the tracking flag `@TrackLink` at the end
// of the url. Remove that from the URL and instead check the checkbox.
let checked = false;
if (!t.initialData.link !== '') {
const t2 = t;
const url = t2.initialData.url.value.replace(/@TrackLink$/, '');
if (t2.initialData.url.value !== url) {
t2.initialData.url.value = url;
checked = true;
}
}
// Execute the modal.
const modal = oldEd.apply(this, [t, r]);
// Is it the link dialog?
if (isOK) {
// Insert tracking checkbox.
const c = document.createElement('input');
c.setAttribute('type', 'checkbox');
if (checked) {
c.setAttribute('checked', checked);
}
// Store the checkbox's state in the Vue instance to pick up from
// the TinyMCE link conversion callback.
c.onchange = (e) => {
self.isTrackLink = e.target.checked;
};
const l = document.createElement('label');
l.appendChild(c);
l.appendChild(document.createTextNode('Track link?'));
l.classList.add('tox-label', 'tox-track-link');
document.querySelector('.tox-form__controls-h-stack .tox-control-wrap').appendChild(l);
}
return modal;
};
},
onEditorChange() {
if (!this.isReady) {
return;
}
// The parent's v-model gets { contentType, body }.
this.$emit('input', { contentType: this.form.format, body: this.form.body });
},
onTogglePreview() {
this.isPreviewing = !this.isPreviewing;
},
onMediaSelect(media) {
this.runTinyMceImageCallback(media.url);
},
beautifyHTML(str) {
// Pad all tags with linebreaks.
let s = this.trimLines(str.replace(/(<([^>]+)>)/ig, '\n$1\n'), true);
// Remove extra linebreaks.
s = s.replace(/\n+/g, '\n');
return indent.html(s, { tabString: ' ' }).trim();
},
trimLines(str, removeEmptyLines) {
const out = str.split('\n');
for (let i = 0; i < out.length; i += 1) {
const line = out[i].trim();
if (removeEmptyLines) {
out[i] = line;
} else if (line === '') {
out[i] = '';
}
}
return out.join('\n').replace(/\n\s*\n\s*\n/g, '\n\n');
},
},
mounted() {
this.initRichtextEditor();
},
computed: {
...mapState(['serverConfig']),
htmlFormat() {
return this.form.format;
},
},
watch: {
// Capture contentType and body passed from the parent as props.
contentType(f) {
this.form.format = f;
this.form.radioFormat = f;
if (f !== 'richtext') {
this.isReady = true;
}
// Trigger the change event so that the body and content type
// are propagated to the parent on first load.
this.onEditorChange();
},
body(b) {
this.form.body = b;
this.onEditorChange();
},
// eslint-disable-next-line func-names
'form.body': function () {
this.onEditorChange();
},
htmlFormat(to, from) {
if ((from === 'richtext' || from === 'html') && to === 'plain') {
// richtext, html => plain
// Preserve line breaks when converting HTML to plaintext.
const d = document.createElement('div');
d.innerHTML = this.beautifyHTML(this.form.body);
this.$nextTick(() => {
this.form.body = this.trimLines(d.innerText.trim(), true);
});
} else if ((from === 'richtext' || from === 'html') && to === 'markdown') {
// richtext, html => markdown
this.form.body = turndown.turndown(this.form.body).replace(/\n\n+/ig, '\n\n');
} else if (from === 'plain' && (to === 'richtext' || to === 'html')) {
// plain => richtext, html
this.form.body = this.form.body.replace(/\n/ig, '<br>\n');
} else if (from === 'richtext' && to === 'html') {
// richtext => html
this.form.body = this.beautifyHTML(this.form.body);
} else if (from === 'markdown' && (to === 'richtext' || to === 'html')) {
// markdown => richtext, html.
this.$api.convertCampaignContent({
id: 1, body: this.form.body, from, to,
}).then((data) => {
this.form.body = this.beautifyHTML(data.trim());
// Update the HTML editor.
if (to === 'html') {
this.updateHTMLEditor();
}
});
}
this.onEditorChange();
},
},
};
</script>