-
-
Notifications
You must be signed in to change notification settings - Fork 499
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (108 loc) · 3.32 KB
/
index.ts
File metadata and controls
128 lines (108 loc) · 3.32 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
import type { Ctx } from '@milkdown/kit/ctx'
import type {
EditorState,
PluginView,
Selection,
} from '@milkdown/kit/prose/state'
import type { EditorView } from '@milkdown/kit/prose/view'
import { TooltipProvider, tooltipFactory } from '@milkdown/kit/plugin/tooltip'
import { TextSelection } from '@milkdown/kit/prose/state'
import { createApp, ref, shallowRef, type App, type ShallowRef } from 'vue'
import type { GroupBuilder } from '../../utils'
import type { DefineFeature } from '../shared'
import type { ToolbarItem } from './config'
import { crepeFeatureConfig } from '../../core/slice'
import { CrepeFeature } from '../../feature'
import { Toolbar } from './component'
interface ToolbarConfig {
boldIcon: string
codeIcon: string
italicIcon: string
linkIcon: string
strikethroughIcon: string
latexIcon: string
buildToolbar: (builder: GroupBuilder<ToolbarItem>) => void
}
export type ToolbarFeatureConfig = Partial<ToolbarConfig>
const toolbarTooltip = tooltipFactory('CREPE_TOOLBAR')
class ToolbarView implements PluginView {
#tooltipProvider: TooltipProvider
#content: HTMLElement
#app: App
#selection: ShallowRef<Selection>
#show = ref(false)
constructor(ctx: Ctx, view: EditorView, config?: ToolbarFeatureConfig) {
const content = document.createElement('div')
content.className = 'milkdown-toolbar'
this.#selection = shallowRef(view.state.selection)
const app = createApp(Toolbar, {
ctx,
hide: this.hide,
config,
selection: this.#selection,
show: this.#show,
})
app.mount(content)
this.#content = content
this.#app = app
this.#tooltipProvider = new TooltipProvider({
content: this.#content,
debounce: 20,
offset: 10,
shouldShow(view: EditorView) {
const { doc, selection } = view.state
const { empty, from, to } = selection
const isEmptyTextBlock =
!doc.textBetween(from, to).length &&
selection instanceof TextSelection
const isNotTextBlock = !(selection instanceof TextSelection)
const activeElement = (view.dom.getRootNode() as ShadowRoot | Document)
.activeElement
const isTooltipChildren = content.contains(activeElement)
const notHasFocus = !view.hasFocus() && !isTooltipChildren
const isReadonly = !view.editable
if (
notHasFocus ||
isNotTextBlock ||
empty ||
isEmptyTextBlock ||
isReadonly
)
return false
return true
},
})
this.#tooltipProvider.onShow = () => {
this.#show.value = true
}
this.#tooltipProvider.onHide = () => {
this.#show.value = false
}
this.update(view)
}
update = (view: EditorView, prevState?: EditorState) => {
this.#tooltipProvider.update(view, prevState)
this.#selection.value = view.state.selection
}
destroy = () => {
this.#tooltipProvider.destroy()
this.#app.unmount()
this.#content.remove()
}
hide = () => {
this.#tooltipProvider.hide()
}
}
export const toolbar: DefineFeature<ToolbarFeatureConfig> = (
editor,
config
) => {
editor
.config(crepeFeatureConfig(CrepeFeature.Toolbar))
.config((ctx) => {
ctx.set(toolbarTooltip.key, {
view: (view) => new ToolbarView(ctx, view, config),
})
})
.use(toolbarTooltip)
}