-
-
Notifications
You must be signed in to change notification settings - Fork 432
/
Copy pathedit-view.ts
160 lines (139 loc) · 3.83 KB
/
edit-view.ts
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
import type { Ctx } from '@milkdown/ctx'
import { TextSelection } from '@milkdown/prose/state'
import type { PluginView } from '@milkdown/prose/state'
import type { Mark } from '@milkdown/prose/model'
import type { EditorView } from '@milkdown/prose/view'
import { TooltipProvider } from '@milkdown/plugin-tooltip'
import { editorViewCtx } from '@milkdown/core'
import { linkSchema } from '@milkdown/preset-commonmark'
import { posToDOMRect } from '@milkdown/prose'
import {
linkTooltipConfig,
linkTooltipState,
type LinkTooltipConfig,
} from '../slices'
import { createApp, ref, type App, type Ref } from 'vue'
import { EditLink } from './component'
interface Data {
from: number
to: number
mark: Mark | null
}
const defaultData: Data = {
from: -1,
to: -1,
mark: null,
}
export class LinkEditTooltip implements PluginView {
#content: HTMLElement
#provider: TooltipProvider
#data: Data = { ...defaultData }
#app: App
#config: Ref<LinkTooltipConfig>
#src = ref('')
constructor(
readonly ctx: Ctx,
view: EditorView
) {
this.#config = ref(this.ctx.get(linkTooltipConfig.key))
const content = document.createElement('div')
content.className = 'milkdown-link-edit'
const app = createApp(EditLink, {
config: this.#config,
src: this.#src,
onConfirm: this.#confirmEdit,
onCancel: this.#reset,
})
app.mount(content)
this.#app = app
this.#content = content
this.#provider = new TooltipProvider({
content,
debounce: 0,
shouldShow: () => false,
})
this.#provider.onHide = () => {
requestAnimationFrame(() => {
view.dom.focus({ preventScroll: true })
})
}
this.#provider.update(view)
}
#reset = () => {
this.#provider.hide()
this.ctx.update(linkTooltipState.key, (state) => ({
...state,
mode: 'preview' as const,
}))
this.#data = { ...defaultData }
}
#confirmEdit = (href: string) => {
const view = this.ctx.get(editorViewCtx)
const { from, to, mark } = this.#data
const type = linkSchema.type(this.ctx)
if (mark && mark.attrs.href === href) {
this.#reset()
return
}
const tr = view.state.tr
if (mark) tr.removeMark(from, to, mark)
tr.addMark(from, to, type.create({ href }))
view.dispatch(tr)
this.#reset()
}
#enterEditMode = (value: string, from: number, to: number) => {
const config = this.ctx.get(linkTooltipConfig.key)
this.#config.value = config
this.#src.value = value
this.ctx.update(linkTooltipState.key, (state) => ({
...state,
mode: 'edit' as const,
}))
const view = this.ctx.get(editorViewCtx)
view.dispatch(
view.state.tr.setSelection(TextSelection.create(view.state.doc, from, to))
)
this.#provider.show({
getBoundingClientRect: () => posToDOMRect(view, from, to),
})
requestAnimationFrame(() => {
this.#content.querySelector('input')?.focus()
})
}
update = (view: EditorView) => {
const { state } = view
const { selection } = state
if (!(selection instanceof TextSelection)) return
const { from, to } = selection
if (from === this.#data.from && to === this.#data.to) return
this.#reset()
}
destroy = () => {
this.#app.unmount()
this.#provider.destroy()
this.#content.remove()
}
addLink = (from: number, to: number) => {
this.#data = {
from,
to,
mark: null,
}
this.#enterEditMode('', from, to)
}
editLink = (mark: Mark, from: number, to: number) => {
this.#data = {
from,
to,
mark,
}
this.#enterEditMode(mark.attrs.href, from, to)
}
removeLink = (from: number, to: number) => {
const view = this.ctx.get(editorViewCtx)
const tr = view.state.tr
tr.removeMark(from, to, linkSchema.type(this.ctx))
view.dispatch(tr)
this.#reset()
}
}