@prosekit/web 0.7.3 → 0.7.5
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 → get-default-state-BzBimBWi.js} +2 -1
- package/dist/get-default-state-BzBimBWi.js.map +1 -0
- package/dist/{get-safe-editor-view-DENm4avv.js → get-safe-editor-view-Dt9Amrcn.js} +2 -1
- package/dist/get-safe-editor-view-Dt9Amrcn.js.map +1 -0
- package/dist/{inject-style-D5jj7cme.js → inject-style-Dm6W58W3.js} +6 -9
- package/dist/inject-style-Dm6W58W3.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 +9 -13
- 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 +31 -31
- 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 +6 -6
- 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 +8 -12
- 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 +5 -7
- 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 +39 -72
- 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.js +1 -0
- package/dist/{use-editor-extension-Cc7ZG7uj.js → use-editor-extension-B2WuUfnd.js} +2 -1
- package/dist/use-editor-extension-B2WuUfnd.js.map +1 -0
- package/dist/{use-scrolling-BNfsQs3S.js → use-scrolling-BOvyjDvH.js} +2 -1
- package/dist/use-scrolling-BOvyjDvH.js.map +1 -0
- package/package.json +21 -21
- 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 +262 -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,280 @@
|
|
|
1
|
+
import {
|
|
2
|
+
expect,
|
|
3
|
+
test,
|
|
4
|
+
} from 'vitest'
|
|
5
|
+
|
|
6
|
+
import { calcResize } from './calc-resize'
|
|
7
|
+
|
|
8
|
+
test('calcBottomRightResize', () => {
|
|
9
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
10
|
+
const [w1, h1] = [200, 100]
|
|
11
|
+
const [w2, h2] = calcResize('bottom-right', w1, h1, dx, dy, null)
|
|
12
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
13
|
+
}
|
|
14
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
15
|
+
const [w1, h1] = [200, 100]
|
|
16
|
+
const [w2, h2] = calcResize('bottom-right', w1, h1, dx, dy, null)
|
|
17
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// drag to the right
|
|
21
|
+
testIncrease(10, 0)
|
|
22
|
+
|
|
23
|
+
// drag to the left
|
|
24
|
+
testDecrease(-10, 0)
|
|
25
|
+
|
|
26
|
+
// drag to the bottom
|
|
27
|
+
testIncrease(0, 10)
|
|
28
|
+
|
|
29
|
+
// drag to the top
|
|
30
|
+
testDecrease(0, -10)
|
|
31
|
+
|
|
32
|
+
// drag to the bottom-right
|
|
33
|
+
testIncrease(10, 10)
|
|
34
|
+
|
|
35
|
+
// drag to the top-left
|
|
36
|
+
testDecrease(-10, -10)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
test('calcBottomLeftResize', () => {
|
|
40
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
41
|
+
const [w1, h1] = [200, 100]
|
|
42
|
+
const [w2, h2] = calcResize('bottom-left', w1, h1, dx, dy, null)
|
|
43
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
44
|
+
}
|
|
45
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
46
|
+
const [w1, h1] = [200, 100]
|
|
47
|
+
const [w2, h2] = calcResize('bottom-left', w1, h1, dx, dy, null)
|
|
48
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// drag to the right
|
|
52
|
+
testDecrease(10, 0)
|
|
53
|
+
|
|
54
|
+
// drag to the left
|
|
55
|
+
testIncrease(-10, 0)
|
|
56
|
+
|
|
57
|
+
// drag to the bottom
|
|
58
|
+
testIncrease(0, 10)
|
|
59
|
+
|
|
60
|
+
// drag to the top
|
|
61
|
+
testDecrease(0, -10)
|
|
62
|
+
|
|
63
|
+
// drag to the bottom-left
|
|
64
|
+
testIncrease(-10, 10)
|
|
65
|
+
|
|
66
|
+
// drag to the top-right
|
|
67
|
+
testDecrease(10, -10)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('calcTopRightResize', () => {
|
|
71
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
72
|
+
const [w1, h1] = [200, 100]
|
|
73
|
+
const [w2, h2] = calcResize('top-right', w1, h1, dx, dy, null)
|
|
74
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
75
|
+
}
|
|
76
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
77
|
+
const [w1, h1] = [200, 100]
|
|
78
|
+
const [w2, h2] = calcResize('top-right', w1, h1, dx, dy, null)
|
|
79
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// drag to the right
|
|
83
|
+
testIncrease(10, 0)
|
|
84
|
+
|
|
85
|
+
// drag to the left
|
|
86
|
+
testDecrease(-10, 0)
|
|
87
|
+
|
|
88
|
+
// drag to the bottom
|
|
89
|
+
testDecrease(0, 10)
|
|
90
|
+
|
|
91
|
+
// drag to the top
|
|
92
|
+
testIncrease(0, -10)
|
|
93
|
+
|
|
94
|
+
// drag to the top-right
|
|
95
|
+
testIncrease(10, -10)
|
|
96
|
+
|
|
97
|
+
// drag to the bottom-left
|
|
98
|
+
testDecrease(-10, 10)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
test('calcTopLeftResize', () => {
|
|
102
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
103
|
+
const [w1, h1] = [200, 100]
|
|
104
|
+
const [w2, h2] = calcResize('top-left', w1, h1, dx, dy, null)
|
|
105
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
106
|
+
}
|
|
107
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
108
|
+
const [w1, h1] = [200, 100]
|
|
109
|
+
const [w2, h2] = calcResize('top-left', w1, h1, dx, dy, null)
|
|
110
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// drag to the right
|
|
114
|
+
testDecrease(10, 0)
|
|
115
|
+
|
|
116
|
+
// drag to the left
|
|
117
|
+
testIncrease(-10, 0)
|
|
118
|
+
|
|
119
|
+
// drag to the bottom
|
|
120
|
+
testDecrease(0, 10)
|
|
121
|
+
|
|
122
|
+
// drag to the top
|
|
123
|
+
testIncrease(0, -10)
|
|
124
|
+
|
|
125
|
+
// drag to the top-left
|
|
126
|
+
testIncrease(-10, -10)
|
|
127
|
+
|
|
128
|
+
// drag to the bottom-right
|
|
129
|
+
testDecrease(10, 10)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
test('calcTopResize', () => {
|
|
133
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
134
|
+
const [w1, h1] = [200, 100]
|
|
135
|
+
const [w2, h2] = calcResize('top', w1, h1, dx, dy, null)
|
|
136
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
137
|
+
}
|
|
138
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
139
|
+
const [w1, h1] = [200, 100]
|
|
140
|
+
const [w2, h2] = calcResize('top', w1, h1, dx, dy, null)
|
|
141
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
142
|
+
}
|
|
143
|
+
const testUnchanged = (dx: number, dy: number) => {
|
|
144
|
+
const [w1, h1] = [200, 100]
|
|
145
|
+
const [w2, h2] = calcResize('top', w1, h1, dx, dy, null)
|
|
146
|
+
expectAreaToUnchanged(w1, h1, w2, h2)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// drag to the right
|
|
150
|
+
testUnchanged(10, 0)
|
|
151
|
+
|
|
152
|
+
// drag to the left
|
|
153
|
+
testUnchanged(-10, 0)
|
|
154
|
+
|
|
155
|
+
// drag to the bottom
|
|
156
|
+
testDecrease(0, 10)
|
|
157
|
+
|
|
158
|
+
// drag to the top
|
|
159
|
+
testIncrease(0, -10)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('calcRightResize', () => {
|
|
163
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
164
|
+
const [w1, h1] = [100, 200]
|
|
165
|
+
const [w2, h2] = calcResize('right', w1, h1, dx, dy, null)
|
|
166
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
167
|
+
}
|
|
168
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
169
|
+
const [w1, h1] = [100, 200]
|
|
170
|
+
const [w2, h2] = calcResize('right', w1, h1, dx, dy, null)
|
|
171
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
172
|
+
}
|
|
173
|
+
const testUnchanged = (dx: number, dy: number) => {
|
|
174
|
+
const [w1, h1] = [100, 200]
|
|
175
|
+
const [w2, h2] = calcResize('right', w1, h1, dx, dy, null)
|
|
176
|
+
expectAreaToUnchanged(w1, h1, w2, h2)
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// drag to the right
|
|
180
|
+
testIncrease(10, 0)
|
|
181
|
+
|
|
182
|
+
// drag to the left
|
|
183
|
+
testDecrease(-10, 0)
|
|
184
|
+
|
|
185
|
+
// drag to the bottom
|
|
186
|
+
testUnchanged(0, 10)
|
|
187
|
+
|
|
188
|
+
// drag to the top
|
|
189
|
+
testUnchanged(0, -10)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
test('sizes are clamped to positive values', () => {
|
|
193
|
+
const [w1, h1] = [100, 200]
|
|
194
|
+
const [w2, h2] = calcResize('right', w1, h1, -500, 0, null)
|
|
195
|
+
expect(w2).toBeGreaterThan(0)
|
|
196
|
+
expect(h2).toBeGreaterThan(0)
|
|
197
|
+
})
|
|
198
|
+
|
|
199
|
+
test('calcBottomResize', () => {
|
|
200
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
201
|
+
const [w1, h1] = [100, 200]
|
|
202
|
+
const [w2, h2] = calcResize('bottom', w1, h1, dx, dy, null)
|
|
203
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
204
|
+
}
|
|
205
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
206
|
+
const [w1, h1] = [100, 200]
|
|
207
|
+
const [w2, h2] = calcResize('bottom', w1, h1, dx, dy, null)
|
|
208
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
209
|
+
}
|
|
210
|
+
const testUnchanged = (dx: number, dy: number) => {
|
|
211
|
+
const [w1, h1] = [100, 200]
|
|
212
|
+
const [w2, h2] = calcResize('bottom', w1, h1, dx, dy, null)
|
|
213
|
+
expectAreaToUnchanged(w1, h1, w2, h2)
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// drag to the right
|
|
217
|
+
testUnchanged(10, 0)
|
|
218
|
+
|
|
219
|
+
// drag to the left
|
|
220
|
+
testUnchanged(-10, 0)
|
|
221
|
+
|
|
222
|
+
// drag to the bottom
|
|
223
|
+
testIncrease(0, 10)
|
|
224
|
+
|
|
225
|
+
// drag to the top
|
|
226
|
+
testDecrease(0, -10)
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
test('calcLeftResize', () => {
|
|
230
|
+
const testIncrease = (dx: number, dy: number) => {
|
|
231
|
+
const [w1, h1] = [100, 200]
|
|
232
|
+
const [w2, h2] = calcResize('left', w1, h1, dx, dy, null)
|
|
233
|
+
expectAreaToIncrease(w1, h1, w2, h2)
|
|
234
|
+
}
|
|
235
|
+
const testDecrease = (dx: number, dy: number) => {
|
|
236
|
+
const [w1, h1] = [100, 200]
|
|
237
|
+
const [w2, h2] = calcResize('left', w1, h1, dx, dy, null)
|
|
238
|
+
expectAreaToDecrease(w1, h1, w2, h2)
|
|
239
|
+
}
|
|
240
|
+
const testUnchanged = (dx: number, dy: number) => {
|
|
241
|
+
const [w1, h1] = [100, 200]
|
|
242
|
+
const [w2, h2] = calcResize('left', w1, h1, dx, dy, null)
|
|
243
|
+
expectAreaToUnchanged(w1, h1, w2, h2)
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// drag to the right
|
|
247
|
+
testDecrease(10, 0)
|
|
248
|
+
|
|
249
|
+
// drag to the left
|
|
250
|
+
testIncrease(-10, 0)
|
|
251
|
+
|
|
252
|
+
// drag to the bottom
|
|
253
|
+
testUnchanged(0, 10)
|
|
254
|
+
|
|
255
|
+
// drag to the top
|
|
256
|
+
testUnchanged(0, -10)
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
function expectAreaToIncrease(w1: number, h1: number, w2: number, h2: number) {
|
|
260
|
+
const area1 = w1 * h1
|
|
261
|
+
const area2 = w2 * h2
|
|
262
|
+
const aspectRatio1 = w1 / h1
|
|
263
|
+
const aspectRatio2 = w2 / h2
|
|
264
|
+
expect(area2).toBeGreaterThan(area1)
|
|
265
|
+
expect(aspectRatio2).toBeCloseTo(aspectRatio1)
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function expectAreaToDecrease(w1: number, h1: number, w2: number, h2: number) {
|
|
269
|
+
const area1 = w1 * h1
|
|
270
|
+
const area2 = w2 * h2
|
|
271
|
+
const aspectRatio1 = w1 / h1
|
|
272
|
+
const aspectRatio2 = w2 / h2
|
|
273
|
+
expect(area2).toBeLessThan(area1)
|
|
274
|
+
expect(aspectRatio2).toBeCloseTo(aspectRatio1)
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function expectAreaToUnchanged(w1: number, h1: number, w2: number, h2: number) {
|
|
278
|
+
expect(w1).toBe(w2)
|
|
279
|
+
expect(h1).toBe(h2)
|
|
280
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { isFinitePositiveNumber } from '../../../utils/is-finite-positive-number'
|
|
2
|
+
|
|
3
|
+
export function calcResize(
|
|
4
|
+
position:
|
|
5
|
+
| 'top'
|
|
6
|
+
| 'right'
|
|
7
|
+
| 'bottom'
|
|
8
|
+
| 'left'
|
|
9
|
+
| 'top-left'
|
|
10
|
+
| 'top-right'
|
|
11
|
+
| 'bottom-left'
|
|
12
|
+
| 'bottom-right',
|
|
13
|
+
w: number,
|
|
14
|
+
h: number,
|
|
15
|
+
dx: number,
|
|
16
|
+
dy: number,
|
|
17
|
+
aspectRatio: number | null | undefined,
|
|
18
|
+
): [w: number, h: number] {
|
|
19
|
+
aspectRatio = aspectRatio ? aspectRatio : w / h
|
|
20
|
+
aspectRatio = isFinitePositiveNumber(aspectRatio) ? aspectRatio : 1
|
|
21
|
+
|
|
22
|
+
switch (position) {
|
|
23
|
+
case 'bottom-right':
|
|
24
|
+
return clamp(calcBottomRightResize(w, h, dx, dy, aspectRatio))
|
|
25
|
+
case 'bottom-left':
|
|
26
|
+
return clamp(calcBottomLeftResize(w, h, dx, dy, aspectRatio))
|
|
27
|
+
case 'top-right':
|
|
28
|
+
return clamp(calcTopRightResize(w, h, dx, dy, aspectRatio))
|
|
29
|
+
case 'top-left':
|
|
30
|
+
return clamp(calcTopLeftResize(w, h, dx, dy, aspectRatio))
|
|
31
|
+
case 'top':
|
|
32
|
+
return clamp(calcTopResize(w, h, dx, dy, aspectRatio))
|
|
33
|
+
case 'right':
|
|
34
|
+
return clamp(calcRightResize(w, h, dx, dy, aspectRatio))
|
|
35
|
+
case 'bottom':
|
|
36
|
+
return clamp(calcBottomResize(w, h, dx, dy, aspectRatio))
|
|
37
|
+
case 'left':
|
|
38
|
+
return clamp(calcLeftResize(w, h, dx, dy, aspectRatio))
|
|
39
|
+
default:
|
|
40
|
+
throw new RangeError(`Invalid position: ${position}`)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
type CalcResize = (
|
|
45
|
+
w: number,
|
|
46
|
+
h: number,
|
|
47
|
+
dx: number,
|
|
48
|
+
dy: number,
|
|
49
|
+
aspectRatio: number,
|
|
50
|
+
) => [w: number, h: number]
|
|
51
|
+
|
|
52
|
+
const calcBottomRightResize: CalcResize = (w, h, dx, dy, r) => {
|
|
53
|
+
w += dx
|
|
54
|
+
h += dy
|
|
55
|
+
|
|
56
|
+
const sum = w + h
|
|
57
|
+
h = sum / (r + 1)
|
|
58
|
+
w = sum - h
|
|
59
|
+
return [w, h]
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const calcBottomLeftResize: CalcResize = (w, h, dx, dy, r) => {
|
|
63
|
+
w -= dx
|
|
64
|
+
h += dy
|
|
65
|
+
|
|
66
|
+
const sum = w + h
|
|
67
|
+
h = sum / (r + 1)
|
|
68
|
+
w = sum - h
|
|
69
|
+
return [w, h]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const calcTopRightResize: CalcResize = (w, h, dx, dy, r) => {
|
|
73
|
+
w += dx
|
|
74
|
+
h -= dy
|
|
75
|
+
|
|
76
|
+
const sum = w + h
|
|
77
|
+
h = sum / (r + 1)
|
|
78
|
+
w = sum - h
|
|
79
|
+
return [w, h]
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const calcTopLeftResize: CalcResize = (w, h, dx, dy, r) => {
|
|
83
|
+
w -= dx
|
|
84
|
+
h -= dy
|
|
85
|
+
|
|
86
|
+
const sum = w + h
|
|
87
|
+
h = sum / (r + 1)
|
|
88
|
+
w = sum - h
|
|
89
|
+
return [w, h]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const calcTopResize: CalcResize = (w, h, dx, dy, r) => {
|
|
93
|
+
h -= dy
|
|
94
|
+
w = h * r
|
|
95
|
+
return [w, h]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const calcRightResize: CalcResize = (w, h, dx, dy, r) => {
|
|
99
|
+
w += dx
|
|
100
|
+
h = w / r
|
|
101
|
+
return [w, h]
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const calcBottomResize: CalcResize = (w, h, dx, dy, r) => {
|
|
105
|
+
h += dy
|
|
106
|
+
w = h * r
|
|
107
|
+
return [w, h]
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const calcLeftResize: CalcResize = (w, h, dx, dy, r) => {
|
|
111
|
+
w -= dx
|
|
112
|
+
h = w / r
|
|
113
|
+
return [w, h]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function clamp([w, h]: [number, number]): [number, number] {
|
|
117
|
+
return [
|
|
118
|
+
Math.max(w, 1),
|
|
119
|
+
Math.max(h, 1),
|
|
120
|
+
]
|
|
121
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineCustomElement, registerCustomElement, type BaseElementConstructor } from "@aria-ui/core"
|
|
2
|
+
|
|
3
|
+
import { useResizableHandle } from "./setup"
|
|
4
|
+
import { resizableHandleEvents, resizableHandleProps, type ResizableHandleEvents, type ResizableHandleProps } from "./types"
|
|
5
|
+
|
|
6
|
+
const ResizableHandleElementBase: BaseElementConstructor<ResizableHandleProps> = defineCustomElement<
|
|
7
|
+
ResizableHandleProps,
|
|
8
|
+
ResizableHandleEvents
|
|
9
|
+
>({
|
|
10
|
+
props: resizableHandleProps,
|
|
11
|
+
events: resizableHandleEvents,
|
|
12
|
+
setup: useResizableHandle,
|
|
13
|
+
})
|
|
14
|
+
class ResizableHandleElement extends ResizableHandleElementBase {}
|
|
15
|
+
|
|
16
|
+
registerCustomElement('prosekit-resizable-handle', ResizableHandleElement)
|
|
17
|
+
|
|
18
|
+
export { ResizableHandleElement }
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSignal,
|
|
3
|
+
useEffect,
|
|
4
|
+
type ConnectableElement,
|
|
5
|
+
type ReadonlySignal,
|
|
6
|
+
type SignalState,
|
|
7
|
+
} from '@aria-ui/core'
|
|
8
|
+
import { getWindow } from '@ocavue/utils'
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
onResizeContext,
|
|
12
|
+
onResizeEndContext,
|
|
13
|
+
onResizeStartContext,
|
|
14
|
+
type OnResize,
|
|
15
|
+
type OnResizeEnd,
|
|
16
|
+
type OnResizeStart,
|
|
17
|
+
} from '../context'
|
|
18
|
+
|
|
19
|
+
import { calcResize } from './calc-resize'
|
|
20
|
+
import type { ResizableHandleProps } from './types'
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
export function useResizableHandle(
|
|
26
|
+
host: ConnectableElement,
|
|
27
|
+
{ state }: { state: SignalState<ResizableHandleProps> },
|
|
28
|
+
): void {
|
|
29
|
+
const onResize = onResizeContext.consume(host)
|
|
30
|
+
const onResizeStart = onResizeStartContext.consume(host)
|
|
31
|
+
const onResizeEnd = onResizeEndContext.consume(host)
|
|
32
|
+
|
|
33
|
+
useResizableHandleState(host, state, { onResize, onResizeStart, onResizeEnd })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function useResizableHandleState(
|
|
37
|
+
host: ConnectableElement,
|
|
38
|
+
state: SignalState<ResizableHandleProps>,
|
|
39
|
+
context: {
|
|
40
|
+
onResizeStart: ReadonlySignal<OnResizeStart>
|
|
41
|
+
onResize: ReadonlySignal<OnResize>
|
|
42
|
+
onResizeEnd: ReadonlySignal<OnResizeEnd>
|
|
43
|
+
},
|
|
44
|
+
) {
|
|
45
|
+
let startX = 0
|
|
46
|
+
let startY = 0
|
|
47
|
+
let width = 0
|
|
48
|
+
let height = 0
|
|
49
|
+
let aspectRatio = 1
|
|
50
|
+
|
|
51
|
+
const pointerPressing = createSignal(false)
|
|
52
|
+
|
|
53
|
+
const handlePointerDown = (event: PointerEvent) => {
|
|
54
|
+
event.preventDefault()
|
|
55
|
+
pointerPressing.set(true)
|
|
56
|
+
|
|
57
|
+
startX = event.x
|
|
58
|
+
startY = event.y
|
|
59
|
+
|
|
60
|
+
const size = context.onResizeStart.get()?.()
|
|
61
|
+
if (size) {
|
|
62
|
+
;[width, height, aspectRatio] = size
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const handlePointerMove = (event: PointerEvent) => {
|
|
67
|
+
event.preventDefault()
|
|
68
|
+
|
|
69
|
+
const dx = event.x - startX
|
|
70
|
+
const dy = event.y - startY
|
|
71
|
+
|
|
72
|
+
const [w, h] = calcResize(
|
|
73
|
+
state.position.peek(),
|
|
74
|
+
width,
|
|
75
|
+
height,
|
|
76
|
+
dx,
|
|
77
|
+
dy,
|
|
78
|
+
aspectRatio,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
context.onResize.get()?.(w, h)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const handlePointerUp = (event: PointerEvent) => {
|
|
85
|
+
event.preventDefault()
|
|
86
|
+
pointerPressing.set(false)
|
|
87
|
+
|
|
88
|
+
context.onResizeEnd.get()?.()
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
useEffect(host, () => {
|
|
92
|
+
host.addEventListener('pointerdown', handlePointerDown)
|
|
93
|
+
return () => {
|
|
94
|
+
host.removeEventListener('pointerdown', handlePointerDown)
|
|
95
|
+
}
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
useEffect(host, () => {
|
|
99
|
+
if (!pointerPressing.get()) {
|
|
100
|
+
return
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const win = getWindow(host)
|
|
104
|
+
|
|
105
|
+
win.addEventListener('pointermove', handlePointerMove)
|
|
106
|
+
win.addEventListener('pointerup', handlePointerUp)
|
|
107
|
+
return () => {
|
|
108
|
+
win.removeEventListener('pointermove', handlePointerMove)
|
|
109
|
+
win.removeEventListener('pointerup', handlePointerUp)
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventDeclarations,
|
|
3
|
+
PropDeclarations,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
|
|
6
|
+
export interface ResizableHandleProps {
|
|
7
|
+
/**
|
|
8
|
+
* The position of the handle.
|
|
9
|
+
*
|
|
10
|
+
* @default "bottom-right"
|
|
11
|
+
*/
|
|
12
|
+
position:
|
|
13
|
+
| 'top'
|
|
14
|
+
| 'right'
|
|
15
|
+
| 'bottom'
|
|
16
|
+
| 'left'
|
|
17
|
+
| 'top-left'
|
|
18
|
+
| 'top-right'
|
|
19
|
+
| 'bottom-left'
|
|
20
|
+
| 'bottom-right'
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
export const resizableHandleProps: PropDeclarations<ResizableHandleProps> = {
|
|
25
|
+
position: { default: 'bottom-right' },
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** @internal */
|
|
29
|
+
export interface ResizableHandleEvents {}
|
|
30
|
+
|
|
31
|
+
/** @internal */
|
|
32
|
+
export const resizableHandleEvents: EventDeclarations<ResizableHandleEvents> = {}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineCustomElement, registerCustomElement, type BaseElementConstructor } from "@aria-ui/core"
|
|
2
|
+
|
|
3
|
+
import { useResizableRoot } from "./setup"
|
|
4
|
+
import { resizableRootEvents, resizableRootProps, type ResizableRootEvents, type ResizableRootProps } from "./types"
|
|
5
|
+
|
|
6
|
+
const ResizableRootElementBase: BaseElementConstructor<ResizableRootProps> = defineCustomElement<
|
|
7
|
+
ResizableRootProps,
|
|
8
|
+
ResizableRootEvents
|
|
9
|
+
>({
|
|
10
|
+
props: resizableRootProps,
|
|
11
|
+
events: resizableRootEvents,
|
|
12
|
+
setup: useResizableRoot,
|
|
13
|
+
})
|
|
14
|
+
class ResizableRootElement extends ResizableRootElementBase {}
|
|
15
|
+
|
|
16
|
+
registerCustomElement('prosekit-resizable-root', ResizableRootElement)
|
|
17
|
+
|
|
18
|
+
export { ResizableRootElement }
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSignal,
|
|
3
|
+
useAttribute,
|
|
4
|
+
useEffect,
|
|
5
|
+
type ConnectableElement,
|
|
6
|
+
type SetupOptions,
|
|
7
|
+
} from '@aria-ui/core'
|
|
8
|
+
|
|
9
|
+
import { isFinitePositiveNumber } from '../../../utils/is-finite-positive-number'
|
|
10
|
+
import {
|
|
11
|
+
onResizeContext,
|
|
12
|
+
onResizeEndContext,
|
|
13
|
+
onResizeStartContext,
|
|
14
|
+
type OnResize,
|
|
15
|
+
type OnResizeEnd,
|
|
16
|
+
type OnResizeStart,
|
|
17
|
+
} from '../context'
|
|
18
|
+
|
|
19
|
+
import type {
|
|
20
|
+
ResizableRootEvents,
|
|
21
|
+
ResizableRootProps,
|
|
22
|
+
} from './types'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export function useResizableRoot(
|
|
28
|
+
host: ConnectableElement,
|
|
29
|
+
{ state, emit }: SetupOptions<ResizableRootProps, ResizableRootEvents>,
|
|
30
|
+
): void {
|
|
31
|
+
const resizing = createSignal(false)
|
|
32
|
+
|
|
33
|
+
const onResizeStart: OnResizeStart = () => {
|
|
34
|
+
const { width, height } = host.getBoundingClientRect()
|
|
35
|
+
|
|
36
|
+
let aspectRatio: number = state.aspectRatio.peek() ?? width / height
|
|
37
|
+
|
|
38
|
+
if (!isFinitePositiveNumber(aspectRatio)) {
|
|
39
|
+
aspectRatio = 0
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
emit('resizeStart', { width, height })
|
|
43
|
+
resizing.set(true)
|
|
44
|
+
return [width, height, aspectRatio]
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const onResize: OnResize = (width, height) => {
|
|
48
|
+
state.width.set(width)
|
|
49
|
+
state.height.set(height)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const onResizeEnd: OnResizeEnd = () => {
|
|
53
|
+
const { width, height } = host.getBoundingClientRect()
|
|
54
|
+
emit('resizeEnd', { width, height })
|
|
55
|
+
resizing.set(false)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
onResizeStartContext.provide(host, createSignal(onResizeStart))
|
|
59
|
+
onResizeContext.provide(host, createSignal(onResize))
|
|
60
|
+
onResizeEndContext.provide(host, createSignal(onResizeEnd))
|
|
61
|
+
|
|
62
|
+
useEffect(host, () => {
|
|
63
|
+
updateResizableRootStyles(
|
|
64
|
+
host,
|
|
65
|
+
Math.max(state.width.get() || 0, 1),
|
|
66
|
+
Math.max(state.height.get() || 0, 1),
|
|
67
|
+
state.aspectRatio.get(),
|
|
68
|
+
)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
useAttribute(host, 'data-resizing', () => (resizing.get() ? '' : undefined))
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function updateResizableRootStyles(
|
|
75
|
+
host: ConnectableElement,
|
|
76
|
+
width: number,
|
|
77
|
+
height: number,
|
|
78
|
+
aspectRatio: number | null,
|
|
79
|
+
) {
|
|
80
|
+
host.style.width = isFinitePositiveNumber(width) ? `${width}px` : ''
|
|
81
|
+
|
|
82
|
+
host.style.height = isFinitePositiveNumber(height) ? `${height}px` : ''
|
|
83
|
+
|
|
84
|
+
if (isFinitePositiveNumber(aspectRatio)) {
|
|
85
|
+
host.style.aspectRatio = `${aspectRatio}`
|
|
86
|
+
|
|
87
|
+
if (width && width > 0 && aspectRatio >= 1) {
|
|
88
|
+
host.style.height = 'auto'
|
|
89
|
+
} else if (height && height > 0 && aspectRatio <= 1) {
|
|
90
|
+
host.style.width = 'min-content'
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventDeclarations,
|
|
3
|
+
PropDeclarations,
|
|
4
|
+
} from '@aria-ui/core'
|
|
5
|
+
|
|
6
|
+
export interface ResizableRootProps {
|
|
7
|
+
width: number | null
|
|
8
|
+
height: number | null
|
|
9
|
+
aspectRatio: number | null
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @internal */
|
|
13
|
+
export const resizableRootProps: PropDeclarations<ResizableRootProps> = {
|
|
14
|
+
width: { default: null },
|
|
15
|
+
height: { default: null },
|
|
16
|
+
aspectRatio: { default: null },
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ResizableRootEvents {
|
|
20
|
+
resizeStart: CustomEvent<{ width: number; height: number }>
|
|
21
|
+
resizeEnd: CustomEvent<{ width: number; height: number }>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @internal */
|
|
25
|
+
export const resizableRootEvents: EventDeclarations<ResizableRootEvents> = {
|
|
26
|
+
resizeStart: {},
|
|
27
|
+
resizeEnd: {},
|
|
28
|
+
}
|