-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProsemirrorEditor.svelte
361 lines (289 loc) · 7.53 KB
/
ProsemirrorEditor.svelte
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
<script>
import { onMount, onDestroy, createEventDispatcher } from 'svelte'
import { createSingleLineEditor } from './state'
import { EditorView } from "prosemirror-view"
import { EditorState } from "prosemirror-state"
const dispatch = createEventDispatcher()
/** @type string */
export let className = "ui-editor"
/** @type EditorState */
export let editorState = createSingleLineEditor()
/** @type string */
export let placeholder = ''
/** Reference to the editor view
* @type EditorView|null */
export let view = null
/** Debounce change events (set to zero for immediate updates) */
export let debounceChangeEventsInterval = 50
/** Reference to the editor component
* @type HTMLDivElement */
export let editor = null
/** Focus the content-editable div */
export function focus() {
view && view.focus()
}
/** Blur the content-editable div */
export function blur() {
editor && editor.blur()
}
/** Tracks the timeout id of the last time the change event was dispatched */
let dispatchLastEditTimeout
/** Tracks whether changes to editor state were not yet dispatched */
let isDirty = false
$: if (view && editorState && !isDirty) {
view.updateState(editorState) // necessary to keep the DOM in sync with the editor state on external updates
}
/** Tracks whether the editor is empty (i.e. has a content size of 0) */
let editorIsEmpty
$: editorIsEmpty = editorState ? editorState.doc.content.size === 0
|| (editorState.doc.textContent === "" && editorState.doc.content.size < 3) : true
/** Dispatches a change event and resets whether the editor state is dirty */
const dispatchChangeEvent = () => {
if (isDirty) {
dispatch('change', {editorState})
isDirty = false
}
}
/**
* Captures custom events from plugins and dispatches them with a new event type (based on event.detail.type)
* @param event {CustomEvent}
*/
const onCustomEvent = event => {
if (event.detail) {
const {type, ...detail} = event.detail
dispatch(type || 'custom', detail)
}
}
onMount(() => {
view = new EditorView({mount: editor}, {
state: editorState,
dispatchTransaction: (transaction) => {
editorState = view.state.apply(transaction)
const contentHasChanged = !editorState.doc.eq(view.state.doc)
if (contentHasChanged) {
isDirty = true
if (debounceChangeEventsInterval > 0) {
if (dispatchLastEditTimeout) clearTimeout(dispatchLastEditTimeout)
dispatchLastEditTimeout = setTimeout(dispatchChangeEvent, 50)
} else {
setTimeout(dispatchChangeEvent, 0)
}
}
view.updateState(editorState)
dispatch('transaction', {view, editorState, isDirty, contentHasChanged})
},
})
})
onDestroy(() => {
view.destroy()
})
</script>
<div class={className}
class:ProseMirror={true}
class:editor_empty={editorIsEmpty}
data-placeholder={placeholder}
bind:this={editor}
on:focus
on:blur
on:keydown
on:custom={onCustomEvent}
></div>
<style>
:global(body) {
--ui-color-placeholder: #AAAAAA;
}
:global(.ProseMirror) {
position: relative;
}
:global(.ProseMirror) {
word-wrap: break-word;
white-space: pre-wrap;
-webkit-font-variant-ligatures: none;
font-variant-ligatures: none;
}
/* :global(.ProseMirror) pre {
white-space: pre-wrap;
}
:global(.ProseMirror) li {
position: relative;
} */
:global(.ProseMirror-hideselection *::selection) {
background: transparent;
}
:global(.ProseMirror-hideselection *::-moz-selection) {
background: transparent;
}
:global(.ProseMirror-hideselection) {
caret-color: transparent;
}
:global(.ProseMirror-selectednode) {
outline: 2px solid #8cf;
}
/* Make sure li selections wrap around markers */
:global(li.ProseMirror-selectednode) {
outline: none;
}
:global(li.ProseMirror-selectednode:after) {
content: "";
position: absolute;
left: -32px;
right: -2px;
top: -2px;
bottom: -2px;
border: 2px solid #8cf;
pointer-events: none;
}
:global(.ProseMirror .empty-node::before) {
position: absolute;
color: #aaa;
cursor: text;
}
:global(.ProseMirror .empty-node:hover::before) {
color: #777;
}
:global(.ProseMirror.editor_empty::before) {
position: absolute;
content: attr(data-placeholder);
pointer-events: none;
color: var(--ui-color-placeholder);
}
:global(.conversation) {
margin-block-end: 1em;
margin-block-start: 1em;
margin-inline-end: 16px;
margin-inline-start: 16px;
}
:global(.conversation:last-child) {
margin-block-end: 1em;
}
:global(ol.doc li[value]::marker) {
display: list-item;
}
:global(ol.doc li[value]) {
list-style-type:decimal;
}
:global(ol.doc li) {
list-style-type:none;
}
:global(ol.doc li p, ol.doc li p.conversation) {
margin-block-start: 0px;
margin-block-end: 0px;
}
:global(ol.doc-special li::marker) {
content: none;
}
:global(ol.doc-special li[data-id]::marker) {
content: attr(data-id) ". ";
}
:global(ol.doc-three-rows) {
padding-inline-start: 0px;
}
:global(ol.doc-three-rows li) {
margin-top: 1em;
margin-block-end: 1em;
margin-block-start: 1em;
margin-inline-start: 163px;
}
:global(ol.doc-three-rows li[data-id]::before) {
content: attr(data-id) ". ";
position: absolute;
left: 1em;
padding-top: 2px;
padding-bottom: 2px;
}
:global(ol.doc-three-rows li[data-name]::marker) {
content: attr(data-name);
width: 100px;
font-variant-numeric: normal;
}
:global(ol.doc-three-rows li::marker) {
content: none;
}
:global(ol.year li p:first-child) {
text-indent: -44px;
margin-top: 1em;
margin-block-end: 1em;
margin-block-start: 1em;
}
:global(ol.year li) {
list-style-type: none;
}
:global(span.year-width) {
width: 44px;
}
:global(.puce) {
display: list-item;
list-style-type: disc;
margin-top: 1em;
margin-block-end: 1em;
margin-block-start: 1em;
margin-inline-start: 40px;
}
:global(p.puce::marker) {
display: list-item;
}
:global(p.puce:last-child) {
margin-top: 1em;
margin-bottom: 1em;
margin-block-end: 1em;
}
:global(p.puce:first-child) {
margin-top: 1em;
margin-bottom: 1em;
margin-block-end: 1em;
}
:global(.no-puce) {
margin-top: 1em;
margin-block-end: 1em;
margin-block-start: 1em;
margin-inline-start: 40px;
}
:global(p.no-puce:last-child) {
margin-top: 1em;
margin-bottom: 1em;
margin-block-end: 1em;
}
:global(p.no-puce:first-child) {
margin-top: 1em;
margin-bottom: 1em;
margin-block-end: 1em;
}
:global(p.single-li) {
display: list-item;
list-style-type: none;
margin-top: 1em;
margin-block-end: 1em;
margin-block-start: 0px;
margin-inline-start: 40px;
}
:global(p.single-li::marker) {
content: attr(data-id) ". "
}
:global(h4.note) {
display: list-item;
counter-increment: h4;
margin-top: 1em;
margin-block-end: 0em;
margin-block-start: 0em;
margin-inline-start: 30px;
padding-inline-start: 10px;
}
:global(h4::marker) {
display: list-item;
content: counter(h4);
}
:global(h3) {
counter-reset: h4;
}
:global(address) {
display: inline;
}
:global(.top-space) {
margin-top: 1em;
}
:global(img) {
max-width: 100%;
width: 100%;
height: auto;
}
</style>