-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditor.jsx
More file actions
628 lines (568 loc) · 19.3 KB
/
Editor.jsx
File metadata and controls
628 lines (568 loc) · 19.3 KB
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
import { useEffect, useRef, forwardRef, useImperativeHandle } from 'react'
import { useTranslation } from 'react-i18next'
import { Editor as MilkdownEditor, rootCtx, defaultValueCtx, commandsCtx } from '@milkdown/kit/core'
import { commonmark, headingSchema, blockquoteSchema, hrSchema, bulletListSchema, orderedListSchema, codeBlockSchema, insertImageCommand } from '@milkdown/kit/preset/commonmark'
import { clearTextInCurrentBlockCommand, setBlockTypeCommand, wrapInBlockTypeCommand, addBlockTypeCommand } from '@milkdown/kit/preset/commonmark'
import { gfm } from '@milkdown/kit/preset/gfm'
import { listener, listenerCtx } from '@milkdown/kit/plugin/listener'
import { clipboard } from '@milkdown/kit/plugin/clipboard'
import { history } from '@milkdown/kit/plugin/history'
import { slashFactory, SlashProvider } from '@milkdown/kit/plugin/slash'
import { TextSelection, Plugin, PluginKey } from '@milkdown/kit/prose/state'
import { $prose } from '@milkdown/kit/utils'
import api from '../../api/client'
import { wikilink } from './wikilink'
import { math, mathBlockSchema } from './math'
const SLASH_ITEM_DEFS = [
{ id: 'h1', icon: 'H1' },
{ id: 'h2', icon: 'H2' },
{ id: 'h3', icon: 'H3' },
{ id: 'bullet', icon: '\u2022' },
{ id: 'ordered', icon: '1.' },
{ id: 'quote', icon: '\u275D' },
{ id: 'code', icon: '</>' },
{ id: 'hr', icon: '\u2014' },
{ id: 'callout-info', icon: '\u2139' },
{ id: 'callout-warning', icon: '\u26A0' },
{ id: 'callout-tip', icon: '\u2713' },
{ id: 'callout-danger', icon: '\u2715' },
{ id: 'mermaid', icon: '\u25C7' },
{ id: 'math', icon: '\u03A3' },
{ id: 'drawio', icon: '\u25A1' },
{ id: 'media', icon: '\u{1F5BC}' },
]
function buildSlashItems(t) {
return SLASH_ITEM_DEFS.map((d) => ({
id: d.id,
icon: d.icon,
label: t(`slash.items.${d.id}.label`),
desc: t(`slash.items.${d.id}.desc`),
}))
}
function executeSlashCommand(ctx, id, view, drawioHandlerRef, mediaHandlerRef) {
const commands = ctx.get(commandsCtx)
commands.call(clearTextInCurrentBlockCommand.key)
if (id.startsWith('callout-')) {
const type = id.replace('callout-', '')
if (view) {
const { state, dispatch } = view
const { from } = state.selection
const text = `\n:::${type}\n\n:::\n`
dispatch(state.tr.insertText(text, from))
}
return
}
if (id === 'mermaid' && view) {
const { state, dispatch } = view
const { from } = state.selection
const text = '\n```mermaid\ngraph TD\n A[Start] --> B[End]\n```\n'
dispatch(state.tr.insertText(text, from))
return
}
if (id === 'math' && view) {
const { state, dispatch } = view
const mathType = mathBlockSchema.type(ctx)
const node = mathType.create({ value: 'E = mc^2' })
dispatch(state.tr.replaceSelectionWith(node))
return
}
if (id === 'drawio') {
if (drawioHandlerRef?.current) drawioHandlerRef.current()
return
}
if (id === 'media') {
if (mediaHandlerRef?.current) mediaHandlerRef.current()
return
}
switch (id) {
case 'h1':
commands.call(setBlockTypeCommand.key, { nodeType: headingSchema.type(ctx), attrs: { level: 1 } })
break
case 'h2':
commands.call(setBlockTypeCommand.key, { nodeType: headingSchema.type(ctx), attrs: { level: 2 } })
break
case 'h3':
commands.call(setBlockTypeCommand.key, { nodeType: headingSchema.type(ctx), attrs: { level: 3 } })
break
case 'bullet':
commands.call(wrapInBlockTypeCommand.key, { nodeType: bulletListSchema.type(ctx) })
break
case 'ordered':
commands.call(wrapInBlockTypeCommand.key, { nodeType: orderedListSchema.type(ctx) })
break
case 'quote':
commands.call(wrapInBlockTypeCommand.key, { nodeType: blockquoteSchema.type(ctx) })
break
case 'code':
commands.call(setBlockTypeCommand.key, { nodeType: codeBlockSchema.type(ctx) })
break
case 'hr':
commands.call(addBlockTypeCommand.key, { nodeType: hrSchema.type(ctx) })
break
}
}
class SlashMenuView {
constructor(ctx, view, drawioHandlerRef, editorViewRef, mediaHandlerRef, items) {
this.ctx = ctx
this.view = view
this.drawioHandlerRef = drawioHandlerRef
this.mediaHandlerRef = mediaHandlerRef
this.editorViewRef = editorViewRef
this.items = items
editorViewRef.current = view
this.selectedIndex = 0
this.filteredItems = [...items]
this.isVisible = false
this.content = document.createElement('div')
this.content.className = 'slash-menu'
this.content.style.position = 'fixed'
this.content.style.zIndex = '100'
this.content.dataset.show = 'false'
this.renderItems()
this.provider = new SlashProvider({
content: this.content,
debounce: 50,
shouldShow: (view) => {
if (view.composing) return false
const currentText = this.provider.getContent(
view,
(node) => ['paragraph', 'heading'].includes(node.type.name)
)
if (currentText == null) return false
const { selection } = view.state
if (!(selection instanceof TextSelection)) return false
const { $head } = selection
if ($head.parentOffset !== $head.parent.content.size) return false
if (!currentText.startsWith('/')) return false
const filter = currentText.slice(1).toLowerCase()
this.filteredItems = this.items.filter(
(item) => item.label.toLowerCase().includes(filter) || item.id.includes(filter)
)
this.selectedIndex = 0
this.renderItems()
return this.filteredItems.length > 0
},
offset: 10,
})
this.provider.onShow = () => { this.isVisible = true }
this.provider.onHide = () => { this.isVisible = false }
this.update(view)
}
handleKey(event) {
if (!this.isVisible) return false
if (event.isComposing || event.keyCode === 229) return false
if (event.key === 'ArrowDown') {
event.preventDefault()
this.selectedIndex = Math.min(this.selectedIndex + 1, this.filteredItems.length - 1)
this.updateActiveItem()
return true
} else if (event.key === 'ArrowUp') {
event.preventDefault()
this.selectedIndex = Math.max(this.selectedIndex - 1, 0)
this.updateActiveItem()
return true
} else if (event.key === 'Enter') {
event.preventDefault()
const item = this.filteredItems[this.selectedIndex]
if (item) {
executeSlashCommand(this.ctx, item.id, this.view, this.drawioHandlerRef, this.mediaHandlerRef)
this.provider.hide()
}
return true
} else if (event.key === 'Escape') {
event.preventDefault()
this.provider.hide()
return true
}
return false
}
renderItems() {
this.content.innerHTML = ''
this.filteredItems.forEach((item, i) => {
const el = document.createElement('div')
el.className = 'slash-menu-item' + (i === this.selectedIndex ? ' active' : '')
el.innerHTML = `
<span class="slash-menu-icon">${item.icon}</span>
<div>
<div class="slash-menu-label">${item.label}</div>
<div class="slash-menu-desc">${item.desc}</div>
</div>
`
el.addEventListener('mouseenter', () => {
this.selectedIndex = i
this.updateActiveItem()
})
el.addEventListener('mousedown', (e) => {
e.preventDefault()
e.stopPropagation()
executeSlashCommand(this.ctx, item.id, this.view, this.drawioHandlerRef, this.mediaHandlerRef)
this.provider.hide()
})
this.content.appendChild(el)
})
}
updateActiveItem() {
const items = this.content.querySelectorAll('.slash-menu-item')
items.forEach((el, i) => {
el.classList.toggle('active', i === this.selectedIndex)
})
// Scroll selected item into view
items[this.selectedIndex]?.scrollIntoView({ block: 'nearest' })
}
update(view) {
this.view = view
this.editorViewRef.current = view
// Don't query or mutate menu state mid-IME — coordsAtPos / DOM writes
// triggered here can drop in-flight composition characters on Windows.
if (view.composing) return
this.provider.update(view)
}
destroy() {
this.provider.destroy()
this.content.remove()
}
}
// ── Wikilink [[ autocomplete ──
class WikilinkMenu {
constructor(emptyLabel) {
this.emptyLabel = emptyLabel
this.el = document.createElement('div')
this.el.className = 'wikilink-menu'
this.el.style.display = 'none'
document.body.appendChild(this.el)
this.pages = []
this.filtered = []
this.selectedIndex = 0
this.active = false
this.triggerPos = null // doc position where [[ starts
// Cache pages list
this.loadPages()
}
async loadPages() {
try {
const res = await api.get('/pages')
this.pages = (res.data.pages || res.data || []).map((p) => ({
slug: p.slug,
title: p.title,
}))
} catch { /* ignore */ }
}
show(view, from, query) {
this.active = true
this.triggerPos = from
this.filter(query)
// Position near cursor
const coords = view.coordsAtPos(view.state.selection.head)
this.el.style.position = 'fixed'
this.el.style.left = coords.left + 'px'
this.el.style.top = (coords.bottom + 4) + 'px'
this.el.style.display = ''
this.el.style.zIndex = '200'
}
hide() {
this.active = false
this.el.style.display = 'none'
this.triggerPos = null
}
filter(query) {
const q = query.toLowerCase()
this.filtered = this.pages.filter(
(p) => p.title.toLowerCase().includes(q) || p.slug.toLowerCase().includes(q)
).slice(0, 8)
this.selectedIndex = 0
this.render()
}
render() {
this.el.innerHTML = ''
if (this.filtered.length === 0) {
const empty = document.createElement('div')
empty.className = 'wikilink-menu-empty'
empty.textContent = this.emptyLabel
this.el.appendChild(empty)
return
}
this.filtered.forEach((page, i) => {
const item = document.createElement('div')
item.className = 'wikilink-menu-item' + (i === this.selectedIndex ? ' active' : '')
item.innerHTML = `
<span class="wikilink-menu-title">${page.title}</span>
<span class="wikilink-menu-slug">/${page.slug}</span>
`
item.addEventListener('mouseenter', () => {
this.selectedIndex = i
this.render()
})
item.addEventListener('mousedown', (e) => {
e.preventDefault()
this.select(page)
})
this.el.appendChild(item)
})
}
select(page) {
if (!this._view || this.triggerPos == null) return
const { state, dispatch } = this._view
const to = state.selection.head
const nodeType = state.schema.nodes.wikilink
if (!nodeType) {
// Schema not registered yet — fall back to plain text so typing still works.
const text = `[[${page.slug}|${page.title}]]`
dispatch(state.tr.replaceWith(this.triggerPos, to, state.schema.text(text)))
} else {
const node = nodeType.create({
slug: page.slug,
display: page.title,
transclusion: false,
})
dispatch(state.tr.replaceWith(this.triggerPos, to, node))
}
this.hide()
}
handleKeyDown(view, event) {
if (!this.active) return false
if (event.isComposing || event.keyCode === 229) return false
if (event.key === 'ArrowDown') {
event.preventDefault()
this.selectedIndex = Math.min(this.selectedIndex + 1, this.filtered.length - 1)
this.render()
return true
}
if (event.key === 'ArrowUp') {
event.preventDefault()
this.selectedIndex = Math.max(this.selectedIndex - 1, 0)
this.render()
return true
}
if (event.key === 'Enter' || event.key === 'Tab') {
event.preventDefault()
const page = this.filtered[this.selectedIndex]
if (page) this.select(page)
return true
}
if (event.key === 'Escape') {
event.preventDefault()
this.hide()
return true
}
return false
}
destroy() {
this.el.remove()
}
}
const wikilinkPluginKey = new PluginKey('wikilink-autocomplete')
const Editor = forwardRef(function Editor({ defaultValue = '', onChange, onDrawioOpen, onMediaPickerOpen, onTabOut }, ref) {
const { t } = useTranslation()
const editorRef = useRef(null)
const containerRef = useRef(null)
const onChangeRef = useRef(onChange)
const drawioHandlerRef = useRef(onDrawioOpen)
const mediaHandlerRef = useRef(onMediaPickerOpen)
const onTabOutRef = useRef(onTabOut)
const editorViewRef = useRef(null)
// The slash menu and wikilink menu both render plain DOM (not React),
// so they cannot read t() from context. Snapshot translated strings into
// refs before the editor's init effect runs.
const slashItemsRef = useRef(null)
const wikilinkEmptyRef = useRef('')
useEffect(() => {
slashItemsRef.current = buildSlashItems(t)
wikilinkEmptyRef.current = t('slash.wikilinkEmpty')
}, [t])
useEffect(() => {
onChangeRef.current = onChange
})
useEffect(() => {
drawioHandlerRef.current = onDrawioOpen || null
}, [onDrawioOpen])
useEffect(() => {
mediaHandlerRef.current = onMediaPickerOpen || null
}, [onMediaPickerOpen])
useEffect(() => {
onTabOutRef.current = onTabOut || null
}, [onTabOut])
useImperativeHandle(ref, () => ({
insertText(text) {
const view = editorViewRef.current
if (!view) return
const { state, dispatch } = view
const pos = state.selection.from
dispatch(state.tr.insertText(text, pos))
},
focus() {
const view = editorViewRef.current
if (!view) return
view.focus()
}
}), [])
useEffect(() => {
if (!containerRef.current) return
let cancelled = false
const slash = slashFactory('slash-menu')
// Create wikilink plugin per editor instance to avoid stale DOM references
const wikilinkMenu = new WikilinkMenu(wikilinkEmptyRef.current)
const wikilinkPlugin = $prose(() => {
return new Plugin({
key: wikilinkPluginKey,
props: {
handleKeyDown(view, event) {
return wikilinkMenu.handleKeyDown(view, event)
},
},
view(editorView) {
wikilinkMenu._view = editorView
return {
update(view) {
wikilinkMenu._view = view
// Bail during IME composition: coordsAtPos + DOM writes from
// show()/hide() can disturb the active composition on Windows,
// making characters disappear and the cursor jump.
if (view.composing) return
const { state } = view
const { selection } = state
if (!(selection instanceof TextSelection)) {
wikilinkMenu.hide()
return
}
const { $head } = selection
const textBefore = $head.parent.textContent.slice(0, $head.parentOffset)
const lastOpen = textBefore.lastIndexOf('[[')
if (lastOpen === -1) {
wikilinkMenu.hide()
return
}
const afterOpen = textBefore.slice(lastOpen + 2)
if (afterOpen.includes(']]')) {
wikilinkMenu.hide()
return
}
const query = afterOpen.split('|')[0]
const blockStart = $head.pos - $head.parentOffset
const triggerPos = blockStart + lastOpen
wikilinkMenu.show(view, triggerPos, query)
},
destroy() {
wikilinkMenu.destroy()
},
}
},
})
})
const tabOutPlugin = $prose(() => {
return new Plugin({
props: {
handleKeyDown(view, event) {
if (event.key !== 'Tab' || event.shiftKey) return false
if (event.isComposing || event.keyCode === 229) return false
if (!onTabOutRef.current) return false
const { $from } = view.state.selection
for (let depth = $from.depth; depth >= 0; depth--) {
const name = $from.node(depth).type.name
if (['list_item', 'bullet_list', 'ordered_list', 'code_block', 'table'].includes(name)) {
return false
}
}
event.preventDefault()
onTabOutRef.current()
return true
},
},
})
})
const init = async () => {
const editor = await MilkdownEditor.make()
.config((ctx) => {
ctx.set(rootCtx, containerRef.current)
ctx.set(defaultValueCtx, defaultValue)
ctx.get(listenerCtx).markdownUpdated((_ctx, markdown) => {
// Skip parent state updates while IME composition is active —
// the React re-render they trigger can interrupt composition on
// Windows. ProseMirror dispatches a final transaction at
// compositionend, which fires the listener again with the
// committed text, so no input is lost.
if (editorViewRef.current?.composing) return
onChangeRef.current?.(markdown)
})
let slashMenuView = null
ctx.set(slash.key, {
view: (editorView) => {
slashMenuView = new SlashMenuView(ctx, editorView, drawioHandlerRef, editorViewRef, mediaHandlerRef, slashItemsRef.current)
return slashMenuView
},
props: {
handleKeyDown(view, event) {
return slashMenuView?.handleKey(event) ?? false
},
},
})
})
.use(commonmark)
.use(gfm)
.use(math)
.use(listener)
.use(clipboard)
.use(history)
.use(slash)
.use(wikilink)
.use(wikilinkPlugin)
.use(tabOutPlugin)
.create()
// StrictMode: if cleanup ran while we were awaiting, destroy immediately
if (cancelled) {
editor.destroy()
return
}
editorRef.current = editor
}
init()
return () => {
cancelled = true
if (editorRef.current) {
editorRef.current.destroy()
editorRef.current = null
}
editorViewRef.current = null
// Clear leftover DOM from any editor
if (containerRef.current) {
containerRef.current.innerHTML = ''
}
}
}, [])
// Image paste handler
useEffect(() => {
const container = containerRef.current
if (!container) return
const handlePaste = async (e) => {
const items = e.clipboardData?.items
if (!items) return
for (const item of items) {
if (item.type.startsWith('image/')) {
e.preventDefault()
const file = item.getAsFile()
if (!file) continue
const formData = new FormData()
formData.append('file', file)
try {
const res = await api.post('/media/upload', formData)
const url = res.data.url
const editor = editorRef.current
if (editor) {
editor.action((ctx) => {
const commands = ctx.get(commandsCtx)
commands.call(insertImageCommand.key, { src: url, alt: 'image' })
})
}
} catch (err) {
console.error('Image upload failed:', err)
}
}
}
}
container.addEventListener('paste', handlePaste)
return () => container.removeEventListener('paste', handlePaste)
}, [])
return (
<div className="milkdown" ref={containerRef} />
)
})
export default Editor