@prosekit/web 0.7.3 → 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-D5jj7cme.js → inject-style-BJQNFufI.js} +4 -5
- 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 +9 -13
- 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 +17 -37
- 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 +17 -17
- 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,88 @@
|
|
|
1
|
+
import { assignStyles } from '../../../utils/assign-styles'
|
|
2
|
+
import { deepCloneElement } from '../../../utils/clone-element'
|
|
3
|
+
import { getClientRect } from '../../../utils/get-client-rect'
|
|
4
|
+
import { injectStyle } from '../../../utils/inject-style'
|
|
5
|
+
import { maxZIndex } from '../../../utils/max-z-index'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Sets a drag preview image for the given element and ensures the preview position
|
|
9
|
+
* relative to the pointer is correct.
|
|
10
|
+
*
|
|
11
|
+
* This function does the following:
|
|
12
|
+
*
|
|
13
|
+
* - Creates a temporary container element.
|
|
14
|
+
* - Puts the container at the end of the document body.
|
|
15
|
+
* - Sets event's drag image.
|
|
16
|
+
* - Removes the container from the document body after the next frame.
|
|
17
|
+
*/
|
|
18
|
+
export function setDragPreview(event: DragEvent, element: HTMLElement): void {
|
|
19
|
+
const { top, bottom, left, right } = getClientRect(element)
|
|
20
|
+
const width = right - left
|
|
21
|
+
const height = bottom - top
|
|
22
|
+
const elementX = left
|
|
23
|
+
const elementY = top
|
|
24
|
+
|
|
25
|
+
const { clientX, clientY } = event
|
|
26
|
+
|
|
27
|
+
const document = element.ownerDocument
|
|
28
|
+
|
|
29
|
+
const container = document.createElement('div')
|
|
30
|
+
|
|
31
|
+
// If outsideX is positive, the point is at the left side of the element.
|
|
32
|
+
const outsideX = Math.round(elementX - clientX)
|
|
33
|
+
// If outsideY is positive, the point is above the element.
|
|
34
|
+
const outsideY = Math.round(elementY - clientY)
|
|
35
|
+
|
|
36
|
+
const borderX = Math.max(outsideX, 0)
|
|
37
|
+
const borderY = Math.max(outsideY, 0)
|
|
38
|
+
assignStyles(container, {
|
|
39
|
+
// Ensuring we don't cause reflow when adding the element to the page using
|
|
40
|
+
// `position:fixed` rather than `position:absolute` so we are positioned on
|
|
41
|
+
// the current viewport. `position:fixed` also creates a new stacking
|
|
42
|
+
// context, so we don't need to do that here.
|
|
43
|
+
// https://github.com/atlassian/pragmatic-drag-and-drop/blob/56276552/packages/core/src/public-utils/element/custom-native-drag-preview/set-custom-native-drag-preview.ts#L60
|
|
44
|
+
position: 'fixed',
|
|
45
|
+
|
|
46
|
+
// The element is positioned off-screen to avoid capturing the content of
|
|
47
|
+
// the page on Safari when the dragging element has a transparent background
|
|
48
|
+
// on Safari. See https://github.com/prosekit/prosekit/issues/1153 for more
|
|
49
|
+
// details.
|
|
50
|
+
top: '-1000vh',
|
|
51
|
+
left: '-1000vw',
|
|
52
|
+
|
|
53
|
+
// The element should not be interactive.
|
|
54
|
+
pointerEvents: 'none',
|
|
55
|
+
|
|
56
|
+
zIndex: maxZIndex,
|
|
57
|
+
|
|
58
|
+
// Only reliable cross browser technique found to push a drag preview away
|
|
59
|
+
// from the cursor is to use transparent borders on the container.
|
|
60
|
+
// https://github.com/atlassian/pragmatic-drag-and-drop/blob/56276552/packages/core/src/public-utils/element/custom-native-drag-preview/pointer-outside-of-preview.ts#L13-L18
|
|
61
|
+
borderLeft: `${borderX}px solid transparent`,
|
|
62
|
+
borderTop: `${borderY}px solid transparent`,
|
|
63
|
+
|
|
64
|
+
boxSizing: 'border-box',
|
|
65
|
+
width: `${width + borderX}px`,
|
|
66
|
+
height: `${height + borderY}px`,
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
const [clonedElement, styleText] = deepCloneElement(element, true)
|
|
70
|
+
|
|
71
|
+
// A hardcoded opacity.
|
|
72
|
+
clonedElement.style.setProperty('opacity', '0.5', 'important')
|
|
73
|
+
// The bounding client rect doesn't include the margin, so we need to remove
|
|
74
|
+
// the margin too from the cloned element so that it can fit the container.
|
|
75
|
+
clonedElement.style.setProperty('margin', '0', 'important')
|
|
76
|
+
// Hide the outline of the cloned element.
|
|
77
|
+
clonedElement.style.setProperty('outline-color', 'transparent', 'important')
|
|
78
|
+
|
|
79
|
+
document.body.appendChild(container)
|
|
80
|
+
container.appendChild(clonedElement)
|
|
81
|
+
injectStyle(container, styleText)
|
|
82
|
+
|
|
83
|
+
event.dataTransfer?.setDragImage(container, Math.max(-outsideX, 0), Math.max(-outsideY, 0))
|
|
84
|
+
|
|
85
|
+
requestAnimationFrame(() => {
|
|
86
|
+
container.remove()
|
|
87
|
+
})
|
|
88
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useAttribute,
|
|
3
|
+
useEffect,
|
|
4
|
+
useEventListener,
|
|
5
|
+
type ConnectableElement,
|
|
6
|
+
type ReadonlySignal,
|
|
7
|
+
type SignalState,
|
|
8
|
+
} from '@aria-ui/core'
|
|
9
|
+
import { isHTMLElement } from '@ocavue/utils'
|
|
10
|
+
import type { Editor } from '@prosekit/core'
|
|
11
|
+
import type { ViewDragging } from '@prosekit/extensions/drop-indicator'
|
|
12
|
+
import {
|
|
13
|
+
Fragment,
|
|
14
|
+
Slice,
|
|
15
|
+
} from '@prosekit/pm/model'
|
|
16
|
+
import { NodeSelection } from '@prosekit/pm/state'
|
|
17
|
+
import type { EditorView } from '@prosekit/pm/view'
|
|
18
|
+
|
|
19
|
+
import { getBoxElement } from '../../../utils/get-box-element'
|
|
20
|
+
import { getSafeEditorView } from '../../../utils/get-safe-editor-view'
|
|
21
|
+
import {
|
|
22
|
+
blockPopoverContext,
|
|
23
|
+
draggingContext,
|
|
24
|
+
type BlockPopoverContext,
|
|
25
|
+
type HoverState,
|
|
26
|
+
} from '../context'
|
|
27
|
+
|
|
28
|
+
import { setDragPreview } from './set-drag-preview'
|
|
29
|
+
import type { BlockHandleDraggableProps } from './types'
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
export function useBlockHandleDraggable(
|
|
35
|
+
host: ConnectableElement,
|
|
36
|
+
{ state }: { state: SignalState<BlockHandleDraggableProps> },
|
|
37
|
+
): void {
|
|
38
|
+
const context = blockPopoverContext.consume(host)
|
|
39
|
+
const dragging = draggingContext.consume(host)
|
|
40
|
+
|
|
41
|
+
useEffect(host, () => {
|
|
42
|
+
host.draggable = true
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
usePointerDownHandler(host, context, state.editor)
|
|
46
|
+
|
|
47
|
+
useEventListener(host, 'dragstart', (event) => {
|
|
48
|
+
dragging.set(true)
|
|
49
|
+
|
|
50
|
+
const view = getSafeEditorView(state.editor.get())
|
|
51
|
+
const hoverState = context.get()
|
|
52
|
+
|
|
53
|
+
if (view && hoverState) {
|
|
54
|
+
view.dom.classList.add('prosekit-dragging')
|
|
55
|
+
createDraggingPreview(view, hoverState, event)
|
|
56
|
+
setViewDragging(view, hoverState)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
useEventListener(host, 'dragend', () => {
|
|
61
|
+
dragging.set(false)
|
|
62
|
+
|
|
63
|
+
const view = getSafeEditorView(state.editor.get())
|
|
64
|
+
if (view) {
|
|
65
|
+
view.dom.classList.remove('prosekit-dragging')
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
useAttribute(host, 'data-dragging', () => (dragging.get() ? '' : undefined))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function usePointerDownHandler(
|
|
73
|
+
host: ConnectableElement,
|
|
74
|
+
context: ReadonlySignal<BlockPopoverContext>,
|
|
75
|
+
editor: ReadonlySignal<Editor | null>,
|
|
76
|
+
) {
|
|
77
|
+
useEventListener(host, 'pointerdown', () => {
|
|
78
|
+
const { pos } = context.get() ?? {}
|
|
79
|
+
const { view } = editor.get() ?? {}
|
|
80
|
+
|
|
81
|
+
if (pos == null || view == null) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
view.dispatch(
|
|
86
|
+
view.state.tr.setSelection(NodeSelection.create(view.state.doc, pos)),
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
// Clicking the handle will blur the editor, so we need to focus it again.
|
|
90
|
+
// We cannot call `event.preventDefault()` here to prevent the blur
|
|
91
|
+
// because it will prevent the drag event from firing.
|
|
92
|
+
requestAnimationFrame(() => {
|
|
93
|
+
view.focus()
|
|
94
|
+
})
|
|
95
|
+
})
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function createDraggingPreview(view: EditorView, hoverState: HoverState, event: DragEvent): void {
|
|
99
|
+
if (!event.dataTransfer) {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const { pos } = hoverState
|
|
104
|
+
|
|
105
|
+
const element = view.nodeDOM(pos)
|
|
106
|
+
if (!element || !isHTMLElement(element)) {
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const boxElement = getBoxElement(element)
|
|
111
|
+
if (!boxElement || !isHTMLElement(boxElement)) {
|
|
112
|
+
return
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
event.dataTransfer.clearData()
|
|
116
|
+
event.dataTransfer.setData('text/html', boxElement.outerHTML)
|
|
117
|
+
event.dataTransfer.effectAllowed = 'copyMove'
|
|
118
|
+
setDragPreview(event, boxElement)
|
|
119
|
+
|
|
120
|
+
return
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function setViewDragging(view: EditorView, hoverState: HoverState): void {
|
|
124
|
+
const { node, pos } = hoverState
|
|
125
|
+
|
|
126
|
+
const dragging: ViewDragging = {
|
|
127
|
+
slice: new Slice(Fragment.from(node), 0, 0),
|
|
128
|
+
move: true,
|
|
129
|
+
node: NodeSelection.create(view.state.doc, pos),
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
view.dragging = dragging
|
|
133
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventDeclarations,
|
|
3
|
+
PropDeclarations,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
import type { Editor } from '@prosekit/core'
|
|
6
|
+
|
|
7
|
+
export interface BlockHandleDraggableProps {
|
|
8
|
+
/**
|
|
9
|
+
* The ProseKit editor instance.
|
|
10
|
+
*
|
|
11
|
+
* @default null
|
|
12
|
+
* @hidden
|
|
13
|
+
*/
|
|
14
|
+
editor: Editor | null
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @internal */
|
|
18
|
+
export const blockHandleDraggableProps: PropDeclarations<BlockHandleDraggableProps> = {
|
|
19
|
+
editor: { default: null },
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** @internal */
|
|
23
|
+
export interface BlockHandleDraggableEvents {}
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export const blockHandleDraggableEvents: EventDeclarations<BlockHandleDraggableEvents> = {}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineCustomElement, registerCustomElement, type BaseElementConstructor } from "@aria-ui/core"
|
|
2
|
+
|
|
3
|
+
import { useBlockHandlePopover } from "./setup"
|
|
4
|
+
import { blockHandlePopoverEvents, blockHandlePopoverProps, type BlockHandlePopoverEvents, type BlockHandlePopoverProps } from "./types"
|
|
5
|
+
|
|
6
|
+
const BlockHandlePopoverElementBase: BaseElementConstructor<BlockHandlePopoverProps> = defineCustomElement<
|
|
7
|
+
BlockHandlePopoverProps,
|
|
8
|
+
BlockHandlePopoverEvents
|
|
9
|
+
>({
|
|
10
|
+
props: blockHandlePopoverProps,
|
|
11
|
+
events: blockHandlePopoverEvents,
|
|
12
|
+
setup: useBlockHandlePopover,
|
|
13
|
+
})
|
|
14
|
+
class BlockHandlePopoverElement extends BlockHandlePopoverElementBase {}
|
|
15
|
+
|
|
16
|
+
registerCustomElement('prosekit-block-handle-popover', BlockHandlePopoverElement)
|
|
17
|
+
|
|
18
|
+
export { BlockHandlePopoverElement }
|
|
@@ -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
|
+
}
|