@tiptap/extension-drag-handle 3.20.1 → 3.20.3

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/index.d.ts DELETED
@@ -1,333 +0,0 @@
1
- import { ComputePositionConfig, VirtualElement } from '@floating-ui/dom';
2
- import { Extension, Editor } from '@tiptap/core';
3
- import { Node, ResolvedPos } from '@tiptap/pm/model';
4
- import { EditorView } from '@tiptap/pm/view';
5
- import { PluginKey, Plugin } from '@tiptap/pm/state';
6
-
7
- /**
8
- * Context provided to each rule for evaluation.
9
- * Contains all information needed to make a decision.
10
- */
11
- interface RuleContext {
12
- /** The node being evaluated */
13
- node: Node;
14
- /** Absolute position of the node in the document */
15
- pos: number;
16
- /** Depth in the document tree (0 = doc root) */
17
- depth: number;
18
- /** Parent node (null if this is the doc) */
19
- parent: Node | null;
20
- /** This node's index among siblings (0-based) */
21
- index: number;
22
- /** Convenience: true if index === 0 */
23
- isFirst: boolean;
24
- /** Convenience: true if this is the last child */
25
- isLast: boolean;
26
- /** The resolved position for advanced queries */
27
- $pos: ResolvedPos;
28
- /** Editor view for DOM access if needed */
29
- view: EditorView;
30
- }
31
- /**
32
- * A rule that determines whether a node should be a drag target.
33
- */
34
- interface DragHandleRule {
35
- /**
36
- * Unique identifier for debugging and rule management.
37
- */
38
- id: string;
39
- /**
40
- * Evaluate the node and return a score deduction.
41
- *
42
- * The return value is subtracted from the node's score (which starts at 1000).
43
- * Higher deductions make the node less likely to be selected as the drag target.
44
- *
45
- * @returns A number representing the score deduction:
46
- * - `0` - No deduction, node remains fully eligible
47
- * - `1-999` - Partial deduction, node is less preferred but still eligible
48
- * - `>= 1000` - Node is excluded from being a drag target
49
- *
50
- * @example
51
- * // Exclude first child in list items
52
- * evaluate: ({ parent, isFirst }) => {
53
- * if (isFirst && parent?.type.name === 'listItem') {
54
- * return 1000 // Exclude
55
- * }
56
- * return 0
57
- * }
58
- *
59
- * @example
60
- * // Prefer shallower nodes with partial deduction
61
- * evaluate: ({ depth }) => {
62
- * // Deeper nodes get small deductions, making shallower nodes win ties
63
- * return depth * 50
64
- * }
65
- *
66
- * @example
67
- * // Context-based partial deductions
68
- * evaluate: ({ node, parent }) => {
69
- * if (parent?.type.name === 'tableCell') {
70
- * // Inside table cells, slightly prefer the cell over its content
71
- * return node.type.name === 'paragraph' ? 100 : 0
72
- * }
73
- * return 0
74
- * }
75
- */
76
- evaluate: (context: RuleContext) => number;
77
- }
78
-
79
- /**
80
- * Edge detection presets for common use cases.
81
- */
82
- type EdgeDetectionPreset = 'left' | 'right' | 'both' | 'none';
83
- /**
84
- * Advanced edge detection configuration.
85
- * Most users should use presets instead.
86
- */
87
- interface EdgeDetectionConfig {
88
- /**
89
- * Which edges trigger parent preference.
90
- * @default ['left', 'top']
91
- */
92
- edges: Array<'left' | 'right' | 'top' | 'bottom'>;
93
- /**
94
- * Distance in pixels from edge to trigger.
95
- * @default 12
96
- */
97
- threshold: number;
98
- /**
99
- * How strongly to prefer parent (higher = stronger preference).
100
- * This is multiplied by depth, so deeper nodes are affected more.
101
- * @default 500
102
- */
103
- strength: number;
104
- }
105
- /**
106
- * Configuration for nested drag handle behavior.
107
- */
108
- interface NestedOptions {
109
- /**
110
- * Additional rules to determine which nodes are draggable.
111
- * These run AFTER the default rules.
112
- *
113
- * @example
114
- * rules: [
115
- * {
116
- * id: 'onlyAlternatives',
117
- * evaluate: ({ node, parent }) => {
118
- * if (parent?.type.name === 'question') {
119
- * return node.type.name === 'alternative' ? 0 : 1000
120
- * }
121
- * return 0
122
- * },
123
- * },
124
- * ]
125
- */
126
- rules?: DragHandleRule[];
127
- /**
128
- * Set to `false` to disable default rules and use only your custom rules.
129
- * Default rules handle common cases like list items and inline content.
130
- *
131
- * @default true
132
- */
133
- defaultRules?: boolean;
134
- /**
135
- * Restrict nested drag handles to specific container types.
136
- * If set, nested dragging only works inside these node types.
137
- *
138
- * @example
139
- * // Only enable nested dragging in lists and custom question blocks
140
- * allowedContainers: ['bulletList', 'orderedList', 'questionBlock']
141
- */
142
- allowedContainers?: string[];
143
- /**
144
- * Edge detection behavior. Controls when to prefer parent over nested node.
145
- *
146
- * Presets:
147
- * - `'left'` (default) - Prefer parent near left/top edges
148
- * - `'right'` - Prefer parent near right/top edges (for RTL)
149
- * - `'both'` - Prefer parent near any horizontal edge
150
- * - `'none'` - Disable edge detection
151
- *
152
- * Or pass a partial/full config object for fine-tuned control.
153
- * Partial configs are merged with defaults.
154
- *
155
- * @default 'left'
156
- *
157
- * @example
158
- * // Only override threshold, keep default edges and strength
159
- * edgeDetection: { threshold: 20 }
160
- */
161
- edgeDetection?: EdgeDetectionPreset | Partial<EdgeDetectionConfig>;
162
- }
163
- /**
164
- * Normalized nested options with all properties resolved.
165
- */
166
- interface NormalizedNestedOptions {
167
- /** Whether nested drag handles are enabled */
168
- enabled: boolean;
169
- /** Custom rules to apply */
170
- rules: DragHandleRule[];
171
- /** Whether to include default rules */
172
- defaultRules: boolean;
173
- /** Allowed container node types (undefined means all) */
174
- allowedContainers: string[] | undefined;
175
- /** Resolved edge detection configuration */
176
- edgeDetection: EdgeDetectionConfig;
177
- }
178
-
179
- declare const defaultComputePositionConfig: ComputePositionConfig;
180
- interface DragHandleOptions {
181
- /**
182
- * Renders an element that is positioned with the floating-ui/dom package
183
- */
184
- render(): HTMLElement;
185
- /**
186
- * Configuration for position computation of the drag handle
187
- * using the floating-ui/dom package
188
- */
189
- computePositionConfig?: ComputePositionConfig;
190
- /**
191
- * A function that returns the virtual element for the drag handle.
192
- * This is useful when the menu needs to be positioned relative to a specific DOM element.
193
- */
194
- getReferencedVirtualElement?: () => VirtualElement | null;
195
- /**
196
- * Locks the draghandle in place and visibility
197
- */
198
- locked?: boolean;
199
- /**
200
- * Returns a node or null when a node is hovered over
201
- */
202
- onNodeChange?: (options: {
203
- node: Node | null;
204
- editor: Editor;
205
- }) => void;
206
- /**
207
- * The callback function that will be called when drag start.
208
- */
209
- onElementDragStart?: (e: DragEvent) => void;
210
- /**
211
- * The callback function that will be called when drag end.
212
- */
213
- onElementDragEnd?: (e: DragEvent) => void;
214
- /**
215
- * Enable drag handles for nested content (list items, blockquotes, etc.).
216
- *
217
- * When enabled, the drag handle will appear for nested blocks, not just
218
- * top-level blocks. A rule-based scoring system determines which node
219
- * to target based on cursor position and configured rules.
220
- *
221
- * **Values:**
222
- * - `false` (default): Only root-level blocks show drag handles
223
- * - `true`: Enable with sensible defaults (left edge detection, default rules)
224
- * - `NestedOptions`: Enable with custom configuration
225
- *
226
- * **Configuration options:**
227
- * - `rules`: Custom rules to determine which nodes are draggable
228
- * - `defaultRules`: Whether to include default rules (default: true)
229
- * - `allowedContainers`: Restrict nested dragging to specific container types
230
- * - `edgeDetection`: Control when to prefer parent over nested node
231
- * - `'left'` (default): Prefer parent near left/top edges
232
- * - `'right'`: Prefer parent near right/top edges (for RTL)
233
- * - `'both'`: Prefer parent near any horizontal edge
234
- * - `'none'`: Disable edge detection
235
- *
236
- * @default false
237
- *
238
- * @example
239
- * // Simple enable with sensible defaults
240
- * DragHandle.configure({
241
- * nested: true,
242
- * })
243
- *
244
- * @example
245
- * // Restrict to specific containers
246
- * DragHandle.configure({
247
- * nested: {
248
- * allowedContainers: ['bulletList', 'orderedList'],
249
- * },
250
- * })
251
- *
252
- * @example
253
- * // With custom rules
254
- * DragHandle.configure({
255
- * nested: {
256
- * rules: [{
257
- * id: 'excludeCodeBlocks',
258
- * evaluate: ({ node }) => node.type.name === 'codeBlock' ? 1000 : 0,
259
- * }],
260
- * edgeDetection: 'none',
261
- * },
262
- * })
263
- */
264
- nested?: boolean | NestedOptions;
265
- }
266
- declare module '@tiptap/core' {
267
- interface Commands<ReturnType> {
268
- dragHandle: {
269
- /**
270
- * Locks the draghandle in place and visibility
271
- */
272
- lockDragHandle: () => ReturnType;
273
- /**
274
- * Unlocks the draghandle
275
- */
276
- unlockDragHandle: () => ReturnType;
277
- /**
278
- * Toggle draghandle lock state
279
- */
280
- toggleDragHandle: () => ReturnType;
281
- };
282
- }
283
- }
284
- declare const DragHandle: Extension<DragHandleOptions, any>;
285
-
286
- interface DragHandlePluginProps {
287
- pluginKey?: PluginKey | string;
288
- editor: Editor;
289
- element: HTMLElement;
290
- onNodeChange?: (data: {
291
- editor: Editor;
292
- node: Node | null;
293
- pos: number;
294
- }) => void;
295
- onElementDragStart?: (e: DragEvent) => void;
296
- onElementDragEnd?: (e: DragEvent) => void;
297
- computePositionConfig?: ComputePositionConfig;
298
- getReferencedVirtualElement?: () => VirtualElement | null;
299
- nestedOptions: NormalizedNestedOptions;
300
- }
301
- declare const dragHandlePluginDefaultKey: PluginKey<any>;
302
- declare const DragHandlePlugin: ({ pluginKey, element, editor, computePositionConfig, getReferencedVirtualElement, onNodeChange, onElementDragStart, onElementDragEnd, nestedOptions, }: DragHandlePluginProps) => {
303
- unbind(): void;
304
- plugin: Plugin<{
305
- locked: boolean;
306
- }>;
307
- };
308
-
309
- /**
310
- * All default rules.
311
- * Users can extend these or replace them entirely.
312
- */
313
- declare const defaultRules: DragHandleRule[];
314
-
315
- /**
316
- * Normalizes the nested options input into a complete configuration object.
317
- *
318
- * @param input - The nested option (boolean, object, or undefined)
319
- * @returns A fully normalized options object
320
- *
321
- * @example
322
- * // Simple enable
323
- * normalizeNestedOptions(true)
324
- * // Returns: { enabled: true, rules: [], defaultRules: true, ... }
325
- *
326
- * @example
327
- * // Custom config
328
- * normalizeNestedOptions({ rules: [myRule], edgeDetection: 'none' })
329
- * // Returns: { enabled: true, rules: [myRule], edgeDetection: { edges: [], ... } }
330
- */
331
- declare function normalizeNestedOptions(input: boolean | NestedOptions | undefined): NormalizedNestedOptions;
332
-
333
- export { DragHandle, type DragHandleOptions, DragHandlePlugin, type DragHandlePluginProps, type DragHandleRule, type EdgeDetectionConfig, type EdgeDetectionPreset, type NestedOptions, type NormalizedNestedOptions, type RuleContext, DragHandle as default, defaultComputePositionConfig, defaultRules, dragHandlePluginDefaultKey, normalizeNestedOptions };