-
Notifications
You must be signed in to change notification settings - Fork 584
/
AposRichTextWidgetEditor.vue
442 lines (414 loc) · 12.3 KB
/
AposRichTextWidgetEditor.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
<template>
<div>
<bubble-menu
class="bubble-menu"
:tippy-options="{ duration: 100 }"
:editor="editor"
v-if="editor"
>
<AposContextMenuDialog
menu-placement="top"
class-list="apos-rich-text-toolbar"
:has-tip="false"
:modifiers="['unpadded']"
>
<div class="apos-rich-text-toolbar__inner">
<component
v-for="(item, index) in toolbar"
:key="item + '-' + index"
:is="(tools[item] && tools[item].component) || 'AposTiptapUndefined'"
:name="item"
:tool="tools[item]"
:options="editorOptions"
:editor="editor"
/>
</div>
</AposContextMenuDialog>
</bubble-menu>
<div class="apos-rich-text-editor__editor" :class="editorModifiers">
<editor-content :editor="editor" :class="editorOptions.className" />
</div>
<div
v-if="showPlaceholder !== null && (!placeholderText || !isFocused)"
class="apos-rich-text-editor__editor_after" :class="editorModifiers"
>
{{ $t('apostrophe:emptyRichTextWidget') }}
</div>
</div>
</template>
<script>
import {
Editor,
EditorContent,
BubbleMenu
} from '@tiptap/vue-2';
import StarterKit from '@tiptap/starter-kit';
import TextAlign from '@tiptap/extension-text-align';
import Highlight from '@tiptap/extension-highlight';
import TextStyle from '@tiptap/extension-text-style';
import Underline from '@tiptap/extension-underline';
import Placeholder from '@tiptap/extension-placeholder';
export default {
name: 'AposRichTextWidgetEditor',
components: {
EditorContent,
BubbleMenu
},
props: {
type: {
type: String,
required: true
},
options: {
type: Object,
required: true
},
value: {
type: Object,
default() {
return {};
}
},
docId: {
type: String,
required: false,
default() {
return null;
}
},
focused: {
type: Boolean,
default: false
}
},
emits: [ 'update' ],
data() {
return {
editor: null,
docFields: {
data: {
...this.value
},
hasErrors: false
},
pending: null,
isFocused: null,
showPlaceholder: null
};
},
computed: {
moduleOptions() {
return apos.modules[apos.area.widgetManagers[this.type]];
},
defaultOptions() {
return this.moduleOptions.defaultOptions;
},
editorOptions() {
const activeOptions = Object.assign({}, this.options);
// Allow toolbar option to pass through if `false`
activeOptions.toolbar = (activeOptions.toolbar !== undefined)
? activeOptions.toolbar : this.defaultOptions.toolbar;
activeOptions.styles = this.enhanceStyles(
activeOptions.styles?.length
? activeOptions.styles
: this.defaultOptions.styles
);
activeOptions.className = (activeOptions.className !== undefined)
? activeOptions.className : this.moduleOptions.className;
return activeOptions;
},
autofocus() {
// Only true for a new rich text widget
return !this.stripPlaceholderBrs(this.value.content).length;
},
initialContent() {
const content = this.stripPlaceholderBrs(this.value.content);
if (!content.length) {
// If we don't supply a valid instance of the first style, then
// the text align control will not work until the user manually
// applies a style or refreshes the page
const defaultStyle = this.editorOptions.styles.find(style => style.def);
const _class = defaultStyle.class ? ` class="${defaultStyle.class}"` : '';
return `<${defaultStyle.tag}${_class}></${defaultStyle.tag}>`;
} else {
return content;
}
},
toolbar() {
return this.editorOptions.toolbar;
},
tools() {
return this.moduleOptions.tools;
},
isVisuallyEmpty () {
const div = document.createElement('div');
div.innerHTML = this.value.content;
return !div.textContent;
},
editorModifiers () {
const classes = [];
if (this.isVisuallyEmpty) {
classes.push('apos-is-visually-empty');
}
return classes;
},
tiptapTextCommands() {
return this.moduleOptions.tiptapTextCommands;
},
tiptapTypes() {
return this.moduleOptions.tiptapTypes;
},
placeholderText() {
return this.moduleOptions.placeholderText;
},
aposTiptapExtensions() {
return (apos.tiptapExtensions || [])
.map(extension => extension({
styles: this.editorOptions.styles.map(this.localizeStyle),
types: this.tiptapTypes
}));
}
},
watch: {
focused(newVal) {
if (!newVal) {
if (this.pending) {
this.emitWidgetUpdate();
}
}
}
},
mounted() {
const extensions = [
StarterKit,
TextAlign.configure({
types: [ 'heading', 'paragraph' ]
}),
Highlight,
TextStyle,
Underline,
// For this contextual widget, no need to check `widget.aposPlaceholder` value
// since `placeholderText` option is enough to decide whether to display it or not.
this.placeholderText && Placeholder.configure({
placeholder: () => {
// Avoid brief display of the placeholder when loading the page.
if (this.isFocused === null) {
return '';
}
// Display placeholder after loading the page.
if (this.showPlaceholder === null) {
return this.$t(this.placeholderText);
}
return this.showPlaceholder ? this.$t(this.placeholderText) : '';
}
})
]
.filter(Boolean)
.concat(this.aposTiptapExtensions);
this.editor = new Editor({
content: this.initialContent,
autofocus: this.autofocus,
onUpdate: this.editorUpdate,
extensions,
// The following events are triggered:
// - before the placeholder configuration function, when loading the page
// - after it, once the page is loaded and we interact with the editors
// To solve this issue, use another `this.showPlaceholder` variable
// and toggle it after the placeholder configuration function is called,
// thanks to nextTick.
// The proper thing would be to call nextTick inside the placeholder
// function so that it can rely on the focus state set by these event
// listeners, but the placeholder function is called synchronously...
onFocus: () => {
this.isFocused = true;
this.$nextTick(() => {
this.showPlaceholder = false;
});
},
onBlur: () => {
this.isFocused = false;
this.$nextTick(() => {
this.showPlaceholder = true;
});
}
});
},
beforeDestroy() {
this.editor.destroy();
},
methods: {
async editorUpdate() {
// Hint that we are typing, even though we're going to
// debounce the actual updates for performance
if (this.docId === window.apos.adminBar.contextId) {
apos.bus.$emit('context-editing');
}
// Debounce updates. We have our own plumbing for
// this so that we can change our minds to update
// right away if we lose focus.
if (this.pending) {
// Don't reset the timeout; we still want to save at
// least once per second if the user is actively typing
return;
}
this.pending = setTimeout(() => {
this.emitWidgetUpdate();
}, 1000);
},
emitWidgetUpdate() {
if (this.pending) {
clearTimeout(this.pending);
this.pending = null;
}
let content = this.editor.getHTML();
content = this.restorePlaceholderBrs(content);
const widget = this.docFields.data;
widget.content = content;
// ... removes need for deep watching in parent
this.$emit('update', { ...widget });
},
// Restore placeholder BRs for empty paragraphs. ProseMirror adds these
// temporarily so the editing experience doesn't break due to contenteditable
// issues with empty paragraphs, but strips them on save; however
// seeing them while editing creates a WYSIWYG expectation
// on the user's part, so we must maintain them
restorePlaceholderBrs(html) {
return html.replace(/<(p[^>]*)>(\s*)<\/p>/gi, '<$1><br /></p>');
},
// Strip the placeholder BRs again when populating the editor.
// Otherwise they get doubled by ProseMirror
stripPlaceholderBrs(html) {
return html.replace(/<(p[^>]*)>\s*<br \/>\s*<\/p>/gi, '<$1></p>');
},
// Enhances the dev-defined styles list with tiptap
// commands and parameters used internally.
enhanceStyles(styles) {
const self = this;
(styles || []).forEach(style => {
style.options = {};
for (const key in self.tiptapTextCommands) {
if (self.tiptapTextCommands[key].includes(style.tag)) {
style.command = key;
}
}
for (const key in self.tiptapTypes) {
if (self.tiptapTypes[key].includes(style.tag)) {
style.type = key;
}
}
// Set heading level
if (style.type === 'heading') {
const level = parseInt(style.tag.split('h')[1]);
style.options.level = level;
}
// Handle custom attributes
if (style.class) {
style.options.class = style.class;
}
if (!style.type) {
apos.notify('apostrophe:richTextStyleConfigWarning', {
type: 'warning',
dismiss: true,
icon: 'text-box-remove-icon',
interpolate: {
label: style.label,
tag: style.tag
}
});
}
});
// ensure a default so we can rely on it throughout
const hasDefault = !!styles.find(style => style.def);
if (!hasDefault && styles.length) {
// If no dev set default, use the first paragraph we can find
if (styles.filter(style => style.type === 'paragraph').length) {
styles.filter(style => style.type === 'paragraph')[0].def = true;
} else {
// Otherwise, set the first style
styles[0].def = true;
}
}
return styles;
},
localizeStyle(style) {
style.label = this.$t(style.label);
return {
...style,
label: this.$t(style.label)
};
}
}
};
</script>
<style lang="scss" scoped>
.apos-rich-text-toolbar.editor-menu-bubble {
z-index: $z-index-manager-toolbar;
position: absolute;
transform: translate3d(-50%, -50%, 0);
}
.apos-rich-text-toolbar.editor-menu-bar {
display: inline-block;
margin-bottom: 10px;
}
.apos-rich-text-toolbar__inner {
display: flex;
align-items: stretch;
height: 35px;
background-color: var(--a-background-primary);
color: var(--a-text-primary);
border-radius: var(--a-border-radius);
}
.apos-rich-text-toolbar ::v-deep .apos-is-active {
background-color: var(--a-base-9);
}
.apos-rich-text-editor__editor ::v-deep .ProseMirror:focus {
outline: none;
}
.apos-rich-text-editor__editor ::v-deep .ProseMirror p.is-empty:first-child::before {
content: attr(data-placeholder);
float: left;
pointer-events: none;
height: 0;
color: var(--a-base-4);
}
.apos-rich-text-editor__editor {
@include apos-transition();
position: relative;
border-radius: var(--a-border-radius);
box-shadow: 0 0 0 1px transparent;
}
.apos-rich-text-editor__editor.apos-is-visually-empty {
box-shadow: 0 0 0 1px var(--a-primary-transparent-50);
}
.apos-rich-text-editor__editor_after {
@include type-small;
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
display: block;
width: 200px;
height: 10px;
margin: auto;
margin-top: 7.5px;
margin-bottom: 7.5px;
color: var(--a-base-5);
opacity: 0;
visibility: hidden;
pointer-events: none;
font-weight: 700;
text-transform: uppercase;
letter-spacing: 1px;
text-align: center;
&.apos-is-visually-empty {
opacity: 1;
visibility: visible;
}
}
.apos-rich-text-toolbar__inner ::v-deep > .apos-rich-text-editor__control {
/* Addresses a Safari-only situation where it inherits the
`::-webkit-scrollbar-button` 2px margin. */
margin: 0;
}
</style>