@prosekit/web 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/get-default-state-CIEy7xrl.js +2 -1
- package/dist/get-default-state-CIEy7xrl.js.map +1 -0
- package/dist/get-safe-editor-view-DENm4avv.js +2 -1
- package/dist/get-safe-editor-view-DENm4avv.js.map +1 -0
- package/dist/{inject-style-RwRNoINh.js → inject-style-BJQNFufI.js} +12 -13
- package/dist/inject-style-BJQNFufI.js.map +1 -0
- package/dist/prosekit-web-autocomplete.d.ts +2 -1
- package/dist/prosekit-web-autocomplete.d.ts.map +1 -0
- package/dist/prosekit-web-autocomplete.js +5 -7
- package/dist/prosekit-web-autocomplete.js.map +1 -0
- package/dist/prosekit-web-block-handle.d.ts +2 -1
- package/dist/prosekit-web-block-handle.d.ts.map +1 -0
- package/dist/prosekit-web-block-handle.js +33 -20
- package/dist/prosekit-web-block-handle.js.map +1 -0
- package/dist/prosekit-web-drop-indicator.d.ts +2 -1
- package/dist/prosekit-web-drop-indicator.d.ts.map +1 -0
- package/dist/prosekit-web-drop-indicator.js +2 -1
- package/dist/prosekit-web-drop-indicator.js.map +1 -0
- package/dist/prosekit-web-inline-popover.d.ts +2 -1
- package/dist/prosekit-web-inline-popover.d.ts.map +1 -0
- package/dist/prosekit-web-inline-popover.js +4 -5
- package/dist/prosekit-web-inline-popover.js.map +1 -0
- package/dist/prosekit-web-popover.d.ts +2 -1
- package/dist/prosekit-web-popover.d.ts.map +1 -0
- package/dist/prosekit-web-popover.js +2 -1
- package/dist/prosekit-web-popover.js.map +1 -0
- package/dist/prosekit-web-resizable.d.ts +2 -1
- package/dist/prosekit-web-resizable.d.ts.map +1 -0
- package/dist/prosekit-web-resizable.js +2 -1
- package/dist/prosekit-web-resizable.js.map +1 -0
- package/dist/prosekit-web-table-handle.d.ts +2 -1
- package/dist/prosekit-web-table-handle.d.ts.map +1 -0
- package/dist/prosekit-web-table-handle.js +38 -75
- package/dist/prosekit-web-table-handle.js.map +1 -0
- package/dist/prosekit-web-tooltip.d.ts +2 -1
- package/dist/prosekit-web-tooltip.d.ts.map +1 -0
- package/dist/prosekit-web-tooltip.js +2 -1
- package/dist/prosekit-web-tooltip.js.map +1 -0
- package/dist/prosekit-web.d.ts +1 -1
- package/dist/prosekit-web.js +1 -0
- package/dist/use-editor-extension-Cc7ZG7uj.js +2 -1
- package/dist/use-editor-extension-Cc7ZG7uj.js.map +1 -0
- package/dist/use-scrolling-BNfsQs3S.js +2 -1
- package/dist/use-scrolling-BNfsQs3S.js.map +1 -0
- package/package.json +21 -20
- package/src/components/autocomplete/autocomplete-empty/element.gen.ts +18 -0
- package/src/components/autocomplete/autocomplete-empty/setup.ts +6 -0
- package/src/components/autocomplete/autocomplete-empty/types.ts +16 -0
- package/src/components/autocomplete/autocomplete-item/element.gen.ts +18 -0
- package/src/components/autocomplete/autocomplete-item/setup.ts +38 -0
- package/src/components/autocomplete/autocomplete-item/types.ts +31 -0
- package/src/components/autocomplete/autocomplete-list/element.gen.ts +18 -0
- package/src/components/autocomplete/autocomplete-list/setup.ts +140 -0
- package/src/components/autocomplete/autocomplete-list/types.ts +30 -0
- package/src/components/autocomplete/autocomplete-popover/element.gen.ts +18 -0
- package/src/components/autocomplete/autocomplete-popover/helpers.spec.ts +21 -0
- package/src/components/autocomplete/autocomplete-popover/helpers.ts +7 -0
- package/src/components/autocomplete/autocomplete-popover/setup.ts +185 -0
- package/src/components/autocomplete/autocomplete-popover/types.ts +103 -0
- package/src/components/autocomplete/context.ts +19 -0
- package/src/components/autocomplete/index.gen.ts +17 -0
- package/src/components/block-handle/block-handle-add/element.gen.ts +18 -0
- package/src/components/block-handle/block-handle-add/setup.ts +37 -0
- package/src/components/block-handle/block-handle-add/types.ts +26 -0
- package/src/components/block-handle/block-handle-draggable/element.gen.ts +18 -0
- package/src/components/block-handle/block-handle-draggable/set-drag-preview.ts +88 -0
- package/src/components/block-handle/block-handle-draggable/setup.ts +133 -0
- package/src/components/block-handle/block-handle-draggable/types.ts +26 -0
- package/src/components/block-handle/block-handle-popover/element.gen.ts +18 -0
- package/src/components/block-handle/block-handle-popover/pointer-move.ts +243 -0
- package/src/components/block-handle/block-handle-popover/setup.ts +88 -0
- package/src/components/block-handle/block-handle-popover/types.ts +84 -0
- package/src/components/block-handle/context.ts +34 -0
- package/src/components/block-handle/index.gen.ts +13 -0
- package/src/components/drop-indicator/drop-indicator/element.gen.ts +18 -0
- package/src/components/drop-indicator/drop-indicator/setup.ts +87 -0
- package/src/components/drop-indicator/drop-indicator/types.ts +34 -0
- package/src/components/drop-indicator/index.gen.ts +5 -0
- package/src/components/inline-popover/index.gen.ts +5 -0
- package/src/components/inline-popover/inline-popover/element.gen.ts +18 -0
- package/src/components/inline-popover/inline-popover/setup.ts +97 -0
- package/src/components/inline-popover/inline-popover/types.ts +115 -0
- package/src/components/inline-popover/inline-popover/virtual-selection-element.ts +75 -0
- package/src/components/popover/index.gen.ts +13 -0
- package/src/components/popover/popover-content/element.gen.ts +18 -0
- package/src/components/popover/popover-content/setup.ts +1 -0
- package/src/components/popover/popover-content/types.ts +12 -0
- package/src/components/popover/popover-root/element.gen.ts +18 -0
- package/src/components/popover/popover-root/setup.ts +1 -0
- package/src/components/popover/popover-root/types.ts +12 -0
- package/src/components/popover/popover-trigger/element.gen.ts +18 -0
- package/src/components/popover/popover-trigger/setup.ts +1 -0
- package/src/components/popover/popover-trigger/types.ts +12 -0
- package/src/components/resizable/context.ts +45 -0
- package/src/components/resizable/index.gen.ts +9 -0
- package/src/components/resizable/resizable-handle/calc-resize.spec.ts +280 -0
- package/src/components/resizable/resizable-handle/calc-resize.ts +121 -0
- package/src/components/resizable/resizable-handle/element.gen.ts +18 -0
- package/src/components/resizable/resizable-handle/setup.ts +112 -0
- package/src/components/resizable/resizable-handle/types.ts +32 -0
- package/src/components/resizable/resizable-root/element.gen.ts +18 -0
- package/src/components/resizable/resizable-root/setup.ts +93 -0
- package/src/components/resizable/resizable-root/types.ts +28 -0
- package/src/components/table-handle/context.ts +49 -0
- package/src/components/table-handle/dnd.ts +135 -0
- package/src/components/table-handle/hooks/use-drop.ts +94 -0
- package/src/components/table-handle/hooks/use-empty-image.ts +30 -0
- package/src/components/table-handle/index.gen.ts +37 -0
- package/src/components/table-handle/table-handle-column-root/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-column-root/setup.ts +71 -0
- package/src/components/table-handle/table-handle-column-root/types.ts +76 -0
- package/src/components/table-handle/table-handle-column-trigger/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-column-trigger/setup.ts +75 -0
- package/src/components/table-handle/table-handle-column-trigger/types.ts +23 -0
- package/src/components/table-handle/table-handle-drag-preview/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-drag-preview/render-preview.ts +80 -0
- package/src/components/table-handle/table-handle-drag-preview/setup.ts +67 -0
- package/src/components/table-handle/table-handle-drag-preview/types.ts +17 -0
- package/src/components/table-handle/table-handle-drag-preview/updater.ts +101 -0
- package/src/components/table-handle/table-handle-drop-indicator/calc-drag-over.ts +44 -0
- package/src/components/table-handle/table-handle-drop-indicator/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-drop-indicator/setup.ts +56 -0
- package/src/components/table-handle/table-handle-drop-indicator/types.ts +18 -0
- package/src/components/table-handle/table-handle-drop-indicator/updater.ts +110 -0
- package/src/components/table-handle/table-handle-popover-content/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-popover-content/setup.ts +90 -0
- package/src/components/table-handle/table-handle-popover-content/types.ts +40 -0
- package/src/components/table-handle/table-handle-popover-item/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-popover-item/setup.ts +23 -0
- package/src/components/table-handle/table-handle-popover-item/types.ts +24 -0
- package/src/components/table-handle/table-handle-root/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-root/setup.ts +93 -0
- package/src/components/table-handle/table-handle-root/types.ts +26 -0
- package/src/components/table-handle/table-handle-row-root/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-row-root/setup.ts +77 -0
- package/src/components/table-handle/table-handle-row-root/types.ts +75 -0
- package/src/components/table-handle/table-handle-row-trigger/element.gen.ts +18 -0
- package/src/components/table-handle/table-handle-row-trigger/setup.ts +74 -0
- package/src/components/table-handle/table-handle-row-trigger/types.ts +26 -0
- package/src/components/table-handle/utils.ts +107 -0
- package/src/components/tooltip/index.gen.ts +13 -0
- package/src/components/tooltip/tooltip-content/element.gen.ts +18 -0
- package/src/components/tooltip/tooltip-content/setup.ts +1 -0
- package/src/components/tooltip/tooltip-content/types.ts +12 -0
- package/src/components/tooltip/tooltip-root/element.gen.ts +18 -0
- package/src/components/tooltip/tooltip-root/setup.ts +1 -0
- package/src/components/tooltip/tooltip-root/types.ts +12 -0
- package/src/components/tooltip/tooltip-trigger/element.gen.ts +18 -0
- package/src/components/tooltip/tooltip-trigger/setup.ts +1 -0
- package/src/components/tooltip/tooltip-trigger/types.ts +12 -0
- package/src/hooks/use-editor-extension.ts +19 -0
- package/src/hooks/use-editor-focus-event.ts +23 -0
- package/src/hooks/use-editor-typing.ts +36 -0
- package/src/hooks/use-editor-update-event.ts +23 -0
- package/src/hooks/use-first-rendering.ts +20 -0
- package/src/hooks/use-keymap.ts +20 -0
- package/src/hooks/use-scrolling.ts +33 -0
- package/src/hooks/use-selecting.ts +63 -0
- package/src/index.ts +1 -0
- package/src/utils/assign-styles.ts +14 -0
- package/src/utils/clone-element.ts +110 -0
- package/src/utils/css-feature-detection.ts +9 -0
- package/src/utils/fade-color.ts +15 -0
- package/src/utils/get-box-element.ts +20 -0
- package/src/utils/get-client-rect.ts +35 -0
- package/src/utils/get-default-state.spec.ts +50 -0
- package/src/utils/get-default-state.ts +22 -0
- package/src/utils/get-effective-background-color.ts +21 -0
- package/src/utils/get-safe-editor-view.ts +10 -0
- package/src/utils/inject-style.ts +11 -0
- package/src/utils/is-finite-positive-number.ts +3 -0
- package/src/utils/max-z-index.ts +3 -0
- package/src/utils/throttle.ts +17 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import type { VirtualElement } from '@floating-ui/dom'
|
|
2
|
+
import {
|
|
3
|
+
isElement,
|
|
4
|
+
isHTMLElement,
|
|
5
|
+
isTextNode,
|
|
6
|
+
} from '@ocavue/utils'
|
|
7
|
+
import {
|
|
8
|
+
defineDOMEventHandler,
|
|
9
|
+
union,
|
|
10
|
+
type PlainExtension,
|
|
11
|
+
} from '@prosekit/core'
|
|
12
|
+
import type { ProseMirrorNode } from '@prosekit/pm/model'
|
|
13
|
+
import type { EditorView } from '@prosekit/pm/view'
|
|
14
|
+
|
|
15
|
+
import { getClientRect } from '../../../utils/get-client-rect'
|
|
16
|
+
import { throttle } from '../../../utils/throttle'
|
|
17
|
+
import type { HoverState } from '../context'
|
|
18
|
+
|
|
19
|
+
export type ElementHoverHandler = (
|
|
20
|
+
reference: VirtualElement | null,
|
|
21
|
+
hoverState: HoverState | null,
|
|
22
|
+
) => void
|
|
23
|
+
|
|
24
|
+
export function defineElementHoverHandler(handler: ElementHoverHandler): PlainExtension {
|
|
25
|
+
const handleElement = (
|
|
26
|
+
node: ProseMirrorNode,
|
|
27
|
+
pos: number,
|
|
28
|
+
element: HTMLElement,
|
|
29
|
+
parentElement?: Node | null,
|
|
30
|
+
) => {
|
|
31
|
+
const reference: VirtualElement = {
|
|
32
|
+
contextElement: element,
|
|
33
|
+
|
|
34
|
+
getBoundingClientRect: () => {
|
|
35
|
+
const rect = findFirstLineRect(parentElement, element)
|
|
36
|
+
return rect ? fulfillRect(rect) : fallbackRect
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
handler(reference, { node, pos })
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const handlePointerEvent = (view: EditorView, event: PointerEvent) => {
|
|
44
|
+
const { x, y } = event
|
|
45
|
+
const block = findBlockByCoords(view, x, y)
|
|
46
|
+
if (!block) {
|
|
47
|
+
handler(null, null)
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const { node, pos } = block
|
|
52
|
+
const element = view.nodeDOM(pos)
|
|
53
|
+
if (!element || !isHTMLElement(element)) {
|
|
54
|
+
handler(null, null)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// If `node` is the first child of another non-doc block node, for example a
|
|
59
|
+
// list node or a blockquote node, we want to put the block handle agains
|
|
60
|
+
// the parent node.
|
|
61
|
+
const $pos = view.state.doc.resolve(pos)
|
|
62
|
+
if ($pos.depth > 0 && $pos.index($pos.depth) === 0) {
|
|
63
|
+
const parentPos = $pos.before($pos.depth)
|
|
64
|
+
const parentNode = $pos.parent
|
|
65
|
+
const parentElement = view.nodeDOM(parentPos)
|
|
66
|
+
handleElement(parentNode, parentPos, element, parentElement)
|
|
67
|
+
} else {
|
|
68
|
+
handleElement(node, pos, element)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return union(
|
|
73
|
+
defineDOMEventHandler('pointermove', throttle(handlePointerEvent, 200)),
|
|
74
|
+
defineDOMEventHandler('pointerout', handlePointerEvent),
|
|
75
|
+
defineDOMEventHandler('keypress', () => handler(null, null)),
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function findBlockByCoords(view: EditorView, x: number, y: number): { node: ProseMirrorNode; pos: number } | undefined {
|
|
80
|
+
const rect = getClientRect(view.dom)
|
|
81
|
+
if (!isWithinRect(rect, x, y)) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let parent: ProseMirrorNode | undefined = view.state.doc
|
|
86
|
+
let pos = -1
|
|
87
|
+
|
|
88
|
+
while (parent) {
|
|
89
|
+
if (parent.isBlock && (parent.isTextblock || parent.isAtom || parent.type.spec.isolating)) {
|
|
90
|
+
return { node: parent, pos }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Collect all children and their positions
|
|
94
|
+
const children: ProseMirrorNode[] = []
|
|
95
|
+
const positions: number[] = []
|
|
96
|
+
parent.forEach((child, offset) => {
|
|
97
|
+
children.push(child)
|
|
98
|
+
positions.push(offset + pos + 1)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
let lo = 0
|
|
102
|
+
let hi = children.length - 1
|
|
103
|
+
|
|
104
|
+
while (lo <= hi) {
|
|
105
|
+
const i = hi - ((hi - lo) >> 1)
|
|
106
|
+
const childDOM = view.nodeDOM(positions[i])
|
|
107
|
+
const childRect = getNodeRect(childDOM)
|
|
108
|
+
if (!childRect) {
|
|
109
|
+
console.warn(`[prosekit] Unable to get rect at position: ${positions[i]}`)
|
|
110
|
+
return
|
|
111
|
+
}
|
|
112
|
+
if (childRect.top > y) {
|
|
113
|
+
hi = i - 1
|
|
114
|
+
} else if (childRect.bottom < y) {
|
|
115
|
+
lo = i + 1
|
|
116
|
+
} else {
|
|
117
|
+
lo = i
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (lo > hi) {
|
|
123
|
+
return
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
parent = children[lo]
|
|
127
|
+
pos = positions[lo]
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function getNodeRect(node: Node | null | undefined): Rect | undefined {
|
|
132
|
+
if (node && isElement(node) && node.isConnected) {
|
|
133
|
+
return getClientRect(node)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isWithinRect(rect: Rect, x: number, y: number) {
|
|
138
|
+
return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
interface Rect {
|
|
142
|
+
top: number
|
|
143
|
+
right: number
|
|
144
|
+
bottom: number
|
|
145
|
+
left: number
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function findFirstLineRect(outer?: Node | null, inner?: Node | null): Rect | undefined {
|
|
149
|
+
if (outer && !outer.isConnected) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
if (inner && !inner.isConnected) {
|
|
153
|
+
return
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
if (outer && inner) {
|
|
157
|
+
const outerRect = findOuterRect(outer)
|
|
158
|
+
const innerRect = findFirstLineRectInNode(inner)
|
|
159
|
+
if (outerRect && innerRect) {
|
|
160
|
+
const { top, bottom } = innerRect
|
|
161
|
+
const { left, right } = outerRect
|
|
162
|
+
return { top, bottom, left, right }
|
|
163
|
+
} else {
|
|
164
|
+
return outerRect || innerRect
|
|
165
|
+
}
|
|
166
|
+
} else if (outer) {
|
|
167
|
+
return findFirstLineRectInNode(outer)
|
|
168
|
+
} else if (inner) {
|
|
169
|
+
return findFirstLineRectInNode(inner)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function findOuterRect(node: Node): Rect | undefined {
|
|
174
|
+
if (!isElement(node)) {
|
|
175
|
+
return
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const rect = getClientRect(node)
|
|
179
|
+
const style = node.ownerDocument.defaultView?.getComputedStyle(node)
|
|
180
|
+
const marginLeft = style && Number.parseInt(style.marginLeft, 10) || 0
|
|
181
|
+
const marginRight = style && Number.parseInt(style.marginRight, 10) || 0
|
|
182
|
+
const left = rect.left - marginLeft
|
|
183
|
+
const right = rect.right + marginRight
|
|
184
|
+
|
|
185
|
+
return { top: rect.top, bottom: rect.bottom, left, right }
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function findFirstLineRectInNode(node: Node): Rect | undefined {
|
|
189
|
+
if (isElement(node)) {
|
|
190
|
+
return findFirstLineRectInElement(node)
|
|
191
|
+
} else if (isTextNode(node)) {
|
|
192
|
+
return findFirstLineRectInTextNode(node)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function findFirstLineRectInTextNode(node: Text): Rect | undefined {
|
|
197
|
+
const ownerDocument = node.ownerDocument
|
|
198
|
+
if (!ownerDocument) {
|
|
199
|
+
return
|
|
200
|
+
}
|
|
201
|
+
const range = ownerDocument.createRange()
|
|
202
|
+
range.setStart(node, 0)
|
|
203
|
+
range.setEnd(node, 0)
|
|
204
|
+
const rects = range.getClientRects()
|
|
205
|
+
return rects[0]
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function findFirstLineRectInElement(element: Element): Rect | undefined {
|
|
209
|
+
if (element.nodeName === 'BR') {
|
|
210
|
+
return element.getBoundingClientRect()
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const rect = getClientRect(element)
|
|
214
|
+
const style = element.ownerDocument.defaultView?.getComputedStyle(element)
|
|
215
|
+
const marginLeft = style && Number.parseInt(style.marginLeft, 10) || 0
|
|
216
|
+
const marginRight = style && Number.parseInt(style.marginRight, 10) || 0
|
|
217
|
+
const left = rect.left - marginLeft
|
|
218
|
+
const right = rect.right + marginRight
|
|
219
|
+
|
|
220
|
+
const lineHeight = style && Number.parseInt(style.lineHeight, 10) || 24
|
|
221
|
+
const paddingTop = style && Number.parseInt(style.paddingTop, 10) || 0
|
|
222
|
+
const borderTop = style && Number.parseInt(style.borderTopWidth, 10) || 0
|
|
223
|
+
const top = rect.top + paddingTop + borderTop
|
|
224
|
+
const bottom = top + lineHeight
|
|
225
|
+
|
|
226
|
+
return { top, bottom, left, right }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function fulfillRect({ top, right, bottom, left }: Rect) {
|
|
230
|
+
return { top, right, bottom, left, width: right - left, height: bottom - top, x: left, y: top }
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// A fallback rect that is far away from the screen. It should not be used through.
|
|
234
|
+
const fallbackRect = Object.freeze({
|
|
235
|
+
top: -9999,
|
|
236
|
+
right: -9999,
|
|
237
|
+
bottom: -9999,
|
|
238
|
+
left: -9999,
|
|
239
|
+
width: 0,
|
|
240
|
+
height: 0,
|
|
241
|
+
x: -9999,
|
|
242
|
+
y: -9999,
|
|
243
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createComputed,
|
|
3
|
+
createSignal,
|
|
4
|
+
useAttribute,
|
|
5
|
+
type ConnectableElement,
|
|
6
|
+
type ReadonlySignal,
|
|
7
|
+
type SetupOptions,
|
|
8
|
+
} from '@aria-ui/core'
|
|
9
|
+
import { useOverlayPositionerState } from '@aria-ui/overlay/elements'
|
|
10
|
+
import { usePresence } from '@aria-ui/presence'
|
|
11
|
+
import type { VirtualElement } from '@floating-ui/dom'
|
|
12
|
+
import type { Editor } from '@prosekit/core'
|
|
13
|
+
|
|
14
|
+
import { useEditorExtension } from '../../../hooks/use-editor-extension'
|
|
15
|
+
import { useScrolling } from '../../../hooks/use-scrolling'
|
|
16
|
+
import {
|
|
17
|
+
blockPopoverContext,
|
|
18
|
+
draggingContext,
|
|
19
|
+
type BlockPopoverContext,
|
|
20
|
+
type HoverState,
|
|
21
|
+
} from '../context'
|
|
22
|
+
|
|
23
|
+
import {
|
|
24
|
+
defineElementHoverHandler,
|
|
25
|
+
type ElementHoverHandler,
|
|
26
|
+
} from './pointer-move'
|
|
27
|
+
import type {
|
|
28
|
+
BlockHandlePopoverEvents,
|
|
29
|
+
BlockHandlePopoverProps,
|
|
30
|
+
} from './types'
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
export function useBlockHandlePopover(
|
|
36
|
+
host: ConnectableElement,
|
|
37
|
+
{ state, emit }: SetupOptions<BlockHandlePopoverProps, BlockHandlePopoverEvents>,
|
|
38
|
+
): void {
|
|
39
|
+
const { editor, ...overlayState } = state
|
|
40
|
+
const reference = createSignal<VirtualElement | null>(null)
|
|
41
|
+
useOverlayPositionerState(host, overlayState, { reference })
|
|
42
|
+
|
|
43
|
+
const context = createSignal<BlockPopoverContext>(null)
|
|
44
|
+
blockPopoverContext.provide(host, context)
|
|
45
|
+
|
|
46
|
+
const dragging = createSignal(false)
|
|
47
|
+
draggingContext.provide(host, dragging)
|
|
48
|
+
|
|
49
|
+
const scrolling = useScrolling(host)
|
|
50
|
+
const open = createComputed(() => {
|
|
51
|
+
return !!context.get() && !scrolling.get()
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
useHoverExtension(host, editor, (referenceValue, hoverState) => {
|
|
55
|
+
reference.set(referenceValue)
|
|
56
|
+
context.set(hoverState)
|
|
57
|
+
const stateChangeDetails = hoverState ? { node: hoverState.node, pos: hoverState.pos } : null
|
|
58
|
+
emit('stateChange', stateChangeDetails)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
useAttribute(host, 'data-state', () => (open.get() ? 'open' : 'closed'))
|
|
62
|
+
usePresence(host, open)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function useHoverExtension(
|
|
66
|
+
host: ConnectableElement,
|
|
67
|
+
editor: ReadonlySignal<Editor | null>,
|
|
68
|
+
handler: ElementHoverHandler,
|
|
69
|
+
) {
|
|
70
|
+
let prevHoverState: HoverState | null = null
|
|
71
|
+
|
|
72
|
+
const extension = defineElementHoverHandler((reference, hoverState) => {
|
|
73
|
+
if (isHoverStateEqual(prevHoverState, hoverState)) {
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
prevHoverState = hoverState
|
|
78
|
+
handler(reference, hoverState)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
useEditorExtension(host, editor, extension)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function isHoverStateEqual(a: HoverState | null, b: HoverState | null) {
|
|
85
|
+
if (!a && !b) return true
|
|
86
|
+
if (!a || !b) return false
|
|
87
|
+
return a.pos === b.pos && a.node.eq(b.node)
|
|
88
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventDeclarations,
|
|
3
|
+
PropDeclarations,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
import {
|
|
6
|
+
overlayPositionerEvents,
|
|
7
|
+
overlayPositionerProps,
|
|
8
|
+
type OverlayPositionerEvents,
|
|
9
|
+
type OverlayPositionerProps,
|
|
10
|
+
} from '@aria-ui/overlay/elements'
|
|
11
|
+
import type { Placement } from '@floating-ui/dom'
|
|
12
|
+
import type { Editor } from '@prosekit/core'
|
|
13
|
+
import type { ProseMirrorNode } from '@prosekit/pm/model'
|
|
14
|
+
|
|
15
|
+
export interface BlockHandlePopoverProps extends Omit<OverlayPositionerProps, 'placement' | 'hoist' | 'flip' | 'shift' | 'hide'> {
|
|
16
|
+
/**
|
|
17
|
+
* The ProseKit editor instance.
|
|
18
|
+
*
|
|
19
|
+
* @default null
|
|
20
|
+
* @hidden
|
|
21
|
+
*/
|
|
22
|
+
editor: Editor | null
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* The placement of the popover, relative to the hovered block.
|
|
26
|
+
*
|
|
27
|
+
* @default "left"
|
|
28
|
+
*/
|
|
29
|
+
placement: Placement
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Whether to use the browser [Popover API](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API)
|
|
33
|
+
* to place the floating element on top of other page content.
|
|
34
|
+
*
|
|
35
|
+
* @default false
|
|
36
|
+
*/
|
|
37
|
+
hoist: boolean
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @default false
|
|
41
|
+
* @hidden
|
|
42
|
+
*/
|
|
43
|
+
flip: boolean
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @default false
|
|
47
|
+
* @hidden
|
|
48
|
+
*/
|
|
49
|
+
shift: boolean
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @default true
|
|
53
|
+
* @hidden
|
|
54
|
+
*/
|
|
55
|
+
hide: boolean
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @internal */
|
|
59
|
+
export const blockHandlePopoverProps: PropDeclarations<BlockHandlePopoverProps> = {
|
|
60
|
+
...overlayPositionerProps,
|
|
61
|
+
editor: { default: null },
|
|
62
|
+
placement: { default: 'left' },
|
|
63
|
+
|
|
64
|
+
// Enabling `hoist` will cause the popover to have a small delay when
|
|
65
|
+
// scrolling the page.
|
|
66
|
+
hoist: { default: false },
|
|
67
|
+
|
|
68
|
+
flip: { default: false },
|
|
69
|
+
shift: { default: false },
|
|
70
|
+
hide: { default: true },
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface BlockHandlePopoverEvents extends OverlayPositionerEvents {
|
|
74
|
+
/**
|
|
75
|
+
* Fired when the hovered block changes.
|
|
76
|
+
*/
|
|
77
|
+
stateChange: CustomEvent<{ node: ProseMirrorNode; pos: number } | null>
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** @internal */
|
|
81
|
+
export const blockHandlePopoverEvents: EventDeclarations<BlockHandlePopoverEvents> = {
|
|
82
|
+
...overlayPositionerEvents,
|
|
83
|
+
stateChange: {},
|
|
84
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
type Context,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
import type { ProseMirrorNode } from '@prosekit/pm/model'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export interface HoverState {
|
|
11
|
+
node: ProseMirrorNode
|
|
12
|
+
pos: number
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
export type BlockPopoverContext = HoverState | null
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export const blockPopoverContext: Context<BlockPopoverContext> = createContext(
|
|
24
|
+
'prosekit-block-popover-context',
|
|
25
|
+
null,
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export const draggingContext: Context<boolean> = createContext(
|
|
32
|
+
'prosekit-block-handle-dragging-context',
|
|
33
|
+
false,
|
|
34
|
+
)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// This file is generated by packages/dev/src/gen-components.ts
|
|
2
|
+
|
|
3
|
+
export { BlockHandleAddElement } from './block-handle-add/element.gen'
|
|
4
|
+
export { blockHandleAddEvents, blockHandleAddProps, type BlockHandleAddEvents, type BlockHandleAddProps } from './block-handle-add/types'
|
|
5
|
+
export { useBlockHandleAdd } from './block-handle-add/setup'
|
|
6
|
+
|
|
7
|
+
export { BlockHandleDraggableElement } from './block-handle-draggable/element.gen'
|
|
8
|
+
export { blockHandleDraggableEvents, blockHandleDraggableProps, type BlockHandleDraggableEvents, type BlockHandleDraggableProps } from './block-handle-draggable/types'
|
|
9
|
+
export { useBlockHandleDraggable } from './block-handle-draggable/setup'
|
|
10
|
+
|
|
11
|
+
export { BlockHandlePopoverElement } from './block-handle-popover/element.gen'
|
|
12
|
+
export { blockHandlePopoverEvents, blockHandlePopoverProps, type BlockHandlePopoverEvents, type BlockHandlePopoverProps } from './block-handle-popover/types'
|
|
13
|
+
export { useBlockHandlePopover } from './block-handle-popover/setup'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineCustomElement, registerCustomElement, type BaseElementConstructor } from "@aria-ui/core"
|
|
2
|
+
|
|
3
|
+
import { useDropIndicator } from "./setup"
|
|
4
|
+
import { dropIndicatorEvents, dropIndicatorProps, type DropIndicatorEvents, type DropIndicatorProps } from "./types"
|
|
5
|
+
|
|
6
|
+
const DropIndicatorElementBase: BaseElementConstructor<DropIndicatorProps> = defineCustomElement<
|
|
7
|
+
DropIndicatorProps,
|
|
8
|
+
DropIndicatorEvents
|
|
9
|
+
>({
|
|
10
|
+
props: dropIndicatorProps,
|
|
11
|
+
events: dropIndicatorEvents,
|
|
12
|
+
setup: useDropIndicator,
|
|
13
|
+
})
|
|
14
|
+
class DropIndicatorElement extends DropIndicatorElementBase {}
|
|
15
|
+
|
|
16
|
+
registerCustomElement('prosekit-drop-indicator', DropIndicatorElement)
|
|
17
|
+
|
|
18
|
+
export { DropIndicatorElement }
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createComputed,
|
|
3
|
+
createSignal,
|
|
4
|
+
useEffect,
|
|
5
|
+
type ConnectableElement,
|
|
6
|
+
type SetupOptions,
|
|
7
|
+
} from '@aria-ui/core'
|
|
8
|
+
import { usePresence } from '@aria-ui/presence'
|
|
9
|
+
import {
|
|
10
|
+
defineDropIndicator,
|
|
11
|
+
type ShowHandlerOptions,
|
|
12
|
+
} from '@prosekit/extensions/drop-indicator'
|
|
13
|
+
|
|
14
|
+
import { useEditorExtension } from '../../../hooks/use-editor-extension'
|
|
15
|
+
import { useScrolling } from '../../../hooks/use-scrolling'
|
|
16
|
+
import { assignStyles } from '../../../utils/assign-styles'
|
|
17
|
+
|
|
18
|
+
import type {
|
|
19
|
+
DropIndicatorEvents,
|
|
20
|
+
DropIndicatorProps,
|
|
21
|
+
} from './types'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export function useDropIndicator(
|
|
27
|
+
host: ConnectableElement,
|
|
28
|
+
{ state }: SetupOptions<DropIndicatorProps, DropIndicatorEvents>,
|
|
29
|
+
): void {
|
|
30
|
+
type DropIndicatorContext = ShowHandlerOptions | null
|
|
31
|
+
const context = createSignal<DropIndicatorContext>(null)
|
|
32
|
+
|
|
33
|
+
const extension = defineDropIndicator({
|
|
34
|
+
onShow: (options) => context.set(options),
|
|
35
|
+
onHide: () => context.set(null),
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
useEditorExtension(host, state.editor, extension)
|
|
39
|
+
|
|
40
|
+
const line = createComputed(() => context.get()?.line)
|
|
41
|
+
const scrolling = useScrolling(host)
|
|
42
|
+
const presence = createComputed(() => {
|
|
43
|
+
return !!context.get() && !scrolling.get()
|
|
44
|
+
})
|
|
45
|
+
usePresence(host, presence)
|
|
46
|
+
|
|
47
|
+
useEffect(
|
|
48
|
+
host,
|
|
49
|
+
() => {
|
|
50
|
+
const lineValue = line.get()
|
|
51
|
+
const lineWidth = state.width.get()
|
|
52
|
+
|
|
53
|
+
if (!lineValue) return
|
|
54
|
+
|
|
55
|
+
const { p1: { x: x1, y: y1 }, p2: { x: x2, y: y2 } } = lineValue
|
|
56
|
+
const horizontal = y1 === y2
|
|
57
|
+
|
|
58
|
+
let width: number
|
|
59
|
+
let height: number
|
|
60
|
+
let top: number = y1
|
|
61
|
+
let left: number = x1
|
|
62
|
+
|
|
63
|
+
if (horizontal) {
|
|
64
|
+
width = x2 - x1
|
|
65
|
+
height = lineWidth
|
|
66
|
+
top -= lineWidth / 2
|
|
67
|
+
} else {
|
|
68
|
+
width = lineWidth
|
|
69
|
+
height = y2 - y1
|
|
70
|
+
left -= lineWidth / 2
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
top = Math.round(top)
|
|
74
|
+
left = Math.round(left)
|
|
75
|
+
|
|
76
|
+
assignStyles(host, {
|
|
77
|
+
position: 'fixed',
|
|
78
|
+
pointerEvents: 'none',
|
|
79
|
+
width: `${width}px`,
|
|
80
|
+
height: `${height}px`,
|
|
81
|
+
transform: `translate(${left}px, ${top}px)`,
|
|
82
|
+
left: '0px',
|
|
83
|
+
top: '0px',
|
|
84
|
+
})
|
|
85
|
+
},
|
|
86
|
+
)
|
|
87
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventDeclarations,
|
|
3
|
+
PropDeclarations,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
import type { Editor } from '@prosekit/core'
|
|
6
|
+
|
|
7
|
+
export interface DropIndicatorProps {
|
|
8
|
+
/**
|
|
9
|
+
* The ProseKit editor instance.
|
|
10
|
+
*
|
|
11
|
+
* @default null
|
|
12
|
+
* @hidden
|
|
13
|
+
*/
|
|
14
|
+
editor: Editor | null
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The line width in pixels.
|
|
18
|
+
*
|
|
19
|
+
* @default 2
|
|
20
|
+
*/
|
|
21
|
+
width: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
export const dropIndicatorProps: PropDeclarations<DropIndicatorProps> = Object.freeze({
|
|
26
|
+
editor: { default: null },
|
|
27
|
+
width: { default: 2 },
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
export interface DropIndicatorEvents {
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @internal */
|
|
34
|
+
export const dropIndicatorEvents: EventDeclarations<DropIndicatorEvents> = {}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// This file is generated by packages/dev/src/gen-components.ts
|
|
2
|
+
|
|
3
|
+
export { DropIndicatorElement } from './drop-indicator/element.gen'
|
|
4
|
+
export { dropIndicatorEvents, dropIndicatorProps, type DropIndicatorEvents, type DropIndicatorProps } from './drop-indicator/types'
|
|
5
|
+
export { useDropIndicator } from './drop-indicator/setup'
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// This file is generated by packages/dev/src/gen-components.ts
|
|
2
|
+
|
|
3
|
+
export { InlinePopoverElement } from './inline-popover/element.gen'
|
|
4
|
+
export { inlinePopoverEvents, inlinePopoverProps, type InlinePopoverEvents, type InlinePopoverProps } from './inline-popover/types'
|
|
5
|
+
export { useInlinePopover } from './inline-popover/setup'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineCustomElement, registerCustomElement, type BaseElementConstructor } from "@aria-ui/core"
|
|
2
|
+
|
|
3
|
+
import { useInlinePopover } from "./setup"
|
|
4
|
+
import { inlinePopoverEvents, inlinePopoverProps, type InlinePopoverEvents, type InlinePopoverProps } from "./types"
|
|
5
|
+
|
|
6
|
+
const InlinePopoverElementBase: BaseElementConstructor<InlinePopoverProps> = defineCustomElement<
|
|
7
|
+
InlinePopoverProps,
|
|
8
|
+
InlinePopoverEvents
|
|
9
|
+
>({
|
|
10
|
+
props: inlinePopoverProps,
|
|
11
|
+
events: inlinePopoverEvents,
|
|
12
|
+
setup: useInlinePopover,
|
|
13
|
+
})
|
|
14
|
+
class InlinePopoverElement extends InlinePopoverElementBase {}
|
|
15
|
+
|
|
16
|
+
registerCustomElement('prosekit-inline-popover', InlinePopoverElement)
|
|
17
|
+
|
|
18
|
+
export { InlinePopoverElement }
|