@tiptap/extension-drag-handle 2.24.1 → 3.0.0-beta.10

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.
Files changed (37) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/index.cjs +463 -457
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +68 -0
  5. package/dist/index.d.ts +68 -5
  6. package/dist/index.js +441 -450
  7. package/dist/index.js.map +1 -1
  8. package/package.json +20 -18
  9. package/src/drag-handle-plugin.ts +233 -236
  10. package/src/drag-handle.ts +42 -28
  11. package/src/helpers/dragHandler.ts +8 -8
  12. package/src/helpers/findNextElementFromCursor.ts +3 -5
  13. package/src/helpers/getInnerCoords.ts +3 -7
  14. package/src/helpers/getOuterNode.ts +1 -1
  15. package/dist/drag-handle-plugin.d.ts +0 -20
  16. package/dist/drag-handle-plugin.d.ts.map +0 -1
  17. package/dist/drag-handle.d.ts +0 -44
  18. package/dist/drag-handle.d.ts.map +0 -1
  19. package/dist/helpers/cloneElement.d.ts +0 -2
  20. package/dist/helpers/cloneElement.d.ts.map +0 -1
  21. package/dist/helpers/dragHandler.d.ts +0 -3
  22. package/dist/helpers/dragHandler.d.ts.map +0 -1
  23. package/dist/helpers/findNextElementFromCursor.d.ts +0 -14
  24. package/dist/helpers/findNextElementFromCursor.d.ts.map +0 -1
  25. package/dist/helpers/getComputedStyle.d.ts +0 -2
  26. package/dist/helpers/getComputedStyle.d.ts.map +0 -1
  27. package/dist/helpers/getInnerCoords.d.ts +0 -6
  28. package/dist/helpers/getInnerCoords.d.ts.map +0 -1
  29. package/dist/helpers/getOuterNode.d.ts +0 -4
  30. package/dist/helpers/getOuterNode.d.ts.map +0 -1
  31. package/dist/helpers/minMax.d.ts +0 -2
  32. package/dist/helpers/minMax.d.ts.map +0 -1
  33. package/dist/helpers/removeNode.d.ts +0 -2
  34. package/dist/helpers/removeNode.d.ts.map +0 -1
  35. package/dist/index.d.ts.map +0 -1
  36. package/dist/index.umd.js +0 -499
  37. package/dist/index.umd.js.map +0 -1
@@ -1,26 +1,32 @@
1
- import { Editor, Extension } from '@tiptap/core'
2
- import { Node } from '@tiptap/pm/model'
3
- import { Props } from 'tippy.js'
1
+ import type { ComputePositionConfig } from '@floating-ui/dom'
2
+ import { type Editor, Extension } from '@tiptap/core'
3
+ import type { Node } from '@tiptap/pm/model'
4
4
 
5
5
  import { DragHandlePlugin } from './drag-handle-plugin.js'
6
6
 
7
+ export const defaultComputePositionConfig: ComputePositionConfig = {
8
+ placement: 'left-start',
9
+ strategy: 'absolute',
10
+ }
11
+
7
12
  export interface DragHandleOptions {
8
13
  /**
9
- * Renders an element that is positioned with tippy.js
14
+ * Renders an element that is positioned with the floating-ui/dom package
10
15
  */
11
- render (): HTMLElement,
16
+ render(): HTMLElement
12
17
  /**
13
- * Options for tippy.js
18
+ * Configuration for position computation of the drag handle
19
+ * using the floating-ui/dom package
14
20
  */
15
- tippyOptions?: Partial<Props>,
21
+ computePositionConfig?: ComputePositionConfig
16
22
  /**
17
23
  * Locks the draghandle in place and visibility
18
24
  */
19
- locked?: boolean,
25
+ locked?: boolean
20
26
  /**
21
27
  * Returns a node or null when a node is hovered over
22
28
  */
23
- onNodeChange?: (options: { node: Node | null, editor: Editor }) => void,
29
+ onNodeChange?: (options: { node: Node | null; editor: Editor }) => void
24
30
  }
25
31
 
26
32
  declare module '@tiptap/core' {
@@ -29,15 +35,15 @@ declare module '@tiptap/core' {
29
35
  /**
30
36
  * Locks the draghandle in place and visibility
31
37
  */
32
- lockDragHandle: () => ReturnType,
38
+ lockDragHandle: () => ReturnType
33
39
  /**
34
40
  * Unlocks the draghandle
35
41
  */
36
- unlockDragHandle: () => ReturnType,
42
+ unlockDragHandle: () => ReturnType
37
43
  /**
38
44
  * Toggle draghandle lock state
39
45
  */
40
- toggleDragHandle: () => ReturnType,
46
+ toggleDragHandle: () => ReturnType
41
47
  }
42
48
  }
43
49
  }
@@ -54,26 +60,34 @@ export const DragHandle = Extension.create<DragHandleOptions>({
54
60
 
55
61
  return element
56
62
  },
57
- tippyOptions: {},
63
+ computePositionConfig: {},
58
64
  locked: false,
59
- onNodeChange: () => { return null },
65
+ onNodeChange: () => {
66
+ return null
67
+ },
60
68
  }
61
69
  },
62
70
 
63
71
  addCommands() {
64
72
  return {
65
- lockDragHandle: () => ({ editor }) => {
66
- this.options.locked = true
67
- return editor.commands.setMeta('lockDragHandle', this.options.locked)
68
- },
69
- unlockDragHandle: () => ({ editor }) => {
70
- this.options.locked = false
71
- return editor.commands.setMeta('lockDragHandle', this.options.locked)
72
- },
73
- toggleDragHandle: () => ({ editor }) => {
74
- this.options.locked = !this.options.locked
75
- return editor.commands.setMeta('lockDragHandle', this.options.locked)
76
- },
73
+ lockDragHandle:
74
+ () =>
75
+ ({ editor }) => {
76
+ this.options.locked = true
77
+ return editor.commands.setMeta('lockDragHandle', this.options.locked)
78
+ },
79
+ unlockDragHandle:
80
+ () =>
81
+ ({ editor }) => {
82
+ this.options.locked = false
83
+ return editor.commands.setMeta('lockDragHandle', this.options.locked)
84
+ },
85
+ toggleDragHandle:
86
+ () =>
87
+ ({ editor }) => {
88
+ this.options.locked = !this.options.locked
89
+ return editor.commands.setMeta('lockDragHandle', this.options.locked)
90
+ },
77
91
  }
78
92
  },
79
93
 
@@ -82,11 +96,11 @@ export const DragHandle = Extension.create<DragHandleOptions>({
82
96
 
83
97
  return [
84
98
  DragHandlePlugin({
85
- tippyOptions: this.options.tippyOptions,
99
+ computePositionConfig: { ...defaultComputePositionConfig, ...this.options.computePositionConfig },
86
100
  element,
87
101
  editor: this.editor,
88
102
  onNodeChange: this.options.onNodeChange,
89
- }),
103
+ }).plugin,
90
104
  ]
91
105
  },
92
106
  })
@@ -1,6 +1,6 @@
1
- import { Editor } from '@tiptap/core'
1
+ import type { Editor } from '@tiptap/core'
2
2
  import { getSelectionRanges, NodeRangeSelection } from '@tiptap/extension-node-range'
3
- import { SelectionRange } from '@tiptap/pm/state'
3
+ import type { SelectionRange } from '@tiptap/pm/state'
4
4
 
5
5
  import { cloneElement } from './cloneElement.js'
6
6
  import { findElementNextToCoords } from './findNextElementFromCursor.js'
@@ -11,7 +11,10 @@ function getDragHandleRanges(event: DragEvent, editor: Editor): SelectionRange[]
11
11
  const { doc } = editor.view.state
12
12
 
13
13
  const result = findElementNextToCoords({
14
- editor, x: event.clientX, y: event.clientY, direction: 'right',
14
+ editor,
15
+ x: event.clientX,
16
+ y: event.clientY,
17
+ direction: 'right',
15
18
  })
16
19
 
17
20
  if (!result.resultNode || result.pos === null) {
@@ -55,14 +58,11 @@ export function dragHandler(event: DragEvent, editor: Editor) {
55
58
  const selectionRanges = getSelectionRanges($from, $to, 0)
56
59
  const isDragHandleWithinSelection = selectionRanges.some(range => {
57
60
  return dragHandleRanges.find(dragHandleRange => {
58
- return dragHandleRange.$from === range.$from
59
- && dragHandleRange.$to === range.$to
61
+ return dragHandleRange.$from === range.$from && dragHandleRange.$to === range.$to
60
62
  })
61
63
  })
62
64
 
63
- const ranges = empty || !isDragHandleWithinSelection
64
- ? dragHandleRanges
65
- : selectionRanges
65
+ const ranges = empty || !isDragHandleWithinSelection ? dragHandleRanges : selectionRanges
66
66
 
67
67
  if (!ranges.length) {
68
68
  return
@@ -1,5 +1,5 @@
1
- import { Editor } from '@tiptap/core'
2
- import { Node } from '@tiptap/pm/model'
1
+ import type { Editor } from '@tiptap/core'
2
+ import type { Node } from '@tiptap/pm/model'
3
3
 
4
4
  export type FindElementNextToCoords = {
5
5
  x: number
@@ -9,9 +9,7 @@ export type FindElementNextToCoords = {
9
9
  }
10
10
 
11
11
  export const findElementNextToCoords = (options: FindElementNextToCoords) => {
12
- const {
13
- x, y, direction, editor,
14
- } = options
12
+ const { x, y, direction, editor } = options
15
13
  let resultElement: HTMLElement | null = null
16
14
  let resultNode: Node | null = null
17
15
  let pos: number | null = null
@@ -1,20 +1,16 @@
1
- import { EditorView } from '@tiptap/pm/view'
1
+ import type { EditorView } from '@tiptap/pm/view'
2
2
 
3
3
  import { getComputedStyle } from './getComputedStyle.js'
4
4
  import { minMax } from './minMax.js'
5
5
 
6
- export function getInnerCoords(view: EditorView, x: number, y: number): { left: number, top: number } {
6
+ export function getInnerCoords(view: EditorView, x: number, y: number): { left: number; top: number } {
7
7
  const paddingLeft = parseInt(getComputedStyle(view.dom, 'paddingLeft'), 10)
8
8
  const paddingRight = parseInt(getComputedStyle(view.dom, 'paddingRight'), 10)
9
9
  const borderLeft = parseInt(getComputedStyle(view.dom, 'borderLeftWidth'), 10)
10
10
  const borderRight = parseInt(getComputedStyle(view.dom, 'borderLeftWidth'), 10)
11
11
  const bounds = view.dom.getBoundingClientRect()
12
12
  const coords = {
13
- left: minMax(
14
- x,
15
- bounds.left + paddingLeft + borderLeft,
16
- bounds.right - paddingRight - borderRight,
17
- ),
13
+ left: minMax(x, bounds.left + paddingLeft + borderLeft, bounds.right - paddingRight - borderRight),
18
14
  top: y,
19
15
  }
20
16
 
@@ -1,4 +1,4 @@
1
- import { Node } from '@tiptap/pm/model'
1
+ import type { Node } from '@tiptap/pm/model'
2
2
 
3
3
  export const getOuterNodePos = (doc: Node, pos: number): number => {
4
4
  const resolvedPos = doc.resolve(pos)
@@ -1,20 +0,0 @@
1
- import { Editor } from '@tiptap/core';
2
- import { Node } from '@tiptap/pm/model';
3
- import { Plugin, PluginKey } from '@tiptap/pm/state';
4
- import { Props as TippyProps } from 'tippy.js';
5
- export interface DragHandlePluginProps {
6
- pluginKey?: PluginKey | string;
7
- editor: Editor;
8
- element: HTMLElement;
9
- onNodeChange?: (data: {
10
- editor: Editor;
11
- node: Node | null;
12
- pos: number;
13
- }) => void;
14
- tippyOptions?: Partial<TippyProps>;
15
- }
16
- export declare const dragHandlePluginDefaultKey: PluginKey<any>;
17
- export declare const DragHandlePlugin: ({ pluginKey, element, editor, tippyOptions, onNodeChange, }: DragHandlePluginProps) => Plugin<{
18
- locked: boolean;
19
- }>;
20
- //# sourceMappingURL=drag-handle-plugin.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"drag-handle-plugin.d.ts","sourceRoot":"","sources":["../src/drag-handle-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAErC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EACQ,MAAM,EAAE,SAAS,EAE/B,MAAM,kBAAkB,CAAA;AAEzB,OAAc,EAAY,KAAK,IAAI,UAAU,EAAE,MAAM,UAAU,CAAA;AA+C/D,MAAM,WAAW,qBAAqB;IACpC,SAAS,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC;IAC/B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,WAAW,CAAC;IACrB,YAAY,CAAC,EAAE,CAAC,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;QAAC,GAAG,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IAClF,YAAY,CAAC,EAAE,OAAO,CAAC,UAAU,CAAC,CAAC;CACpC;AAED,eAAO,MAAM,0BAA0B,gBAA8B,CAAA;AAErE,eAAO,MAAM,gBAAgB,gEAM1B,qBAAqB;;EA0SvB,CAAA"}
@@ -1,44 +0,0 @@
1
- import { Editor, Extension } from '@tiptap/core';
2
- import { Node } from '@tiptap/pm/model';
3
- import { Props } from 'tippy.js';
4
- export interface DragHandleOptions {
5
- /**
6
- * Renders an element that is positioned with tippy.js
7
- */
8
- render(): HTMLElement;
9
- /**
10
- * Options for tippy.js
11
- */
12
- tippyOptions?: Partial<Props>;
13
- /**
14
- * Locks the draghandle in place and visibility
15
- */
16
- locked?: boolean;
17
- /**
18
- * Returns a node or null when a node is hovered over
19
- */
20
- onNodeChange?: (options: {
21
- node: Node | null;
22
- editor: Editor;
23
- }) => void;
24
- }
25
- declare module '@tiptap/core' {
26
- interface Commands<ReturnType> {
27
- dragHandle: {
28
- /**
29
- * Locks the draghandle in place and visibility
30
- */
31
- lockDragHandle: () => ReturnType;
32
- /**
33
- * Unlocks the draghandle
34
- */
35
- unlockDragHandle: () => ReturnType;
36
- /**
37
- * Toggle draghandle lock state
38
- */
39
- toggleDragHandle: () => ReturnType;
40
- };
41
- }
42
- }
43
- export declare const DragHandle: Extension<DragHandleOptions, any>;
44
- //# sourceMappingURL=drag-handle.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"drag-handle.d.ts","sourceRoot":"","sources":["../src/drag-handle.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AACvC,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAIhC,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,MAAM,IAAK,WAAW,CAAC;IACvB;;OAEG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;IAC9B;;OAEG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB;;OAEG;IACH,YAAY,CAAC,EAAE,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,IAAI,GAAG,IAAI,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;CACzE;AAED,OAAO,QAAQ,cAAc,CAAC;IAC5B,UAAU,QAAQ,CAAC,UAAU;QAC3B,UAAU,EAAE;YACV;;eAEG;YACH,cAAc,EAAE,MAAM,UAAU,CAAC;YACjC;;eAEG;YACH,gBAAgB,EAAE,MAAM,UAAU,CAAC;YACnC;;eAEG;YACH,gBAAgB,EAAE,MAAM,UAAU,CAAC;SACpC,CAAA;KACF;CACF;AAED,eAAO,MAAM,UAAU,mCA+CrB,CAAA"}
@@ -1,2 +0,0 @@
1
- export declare function cloneElement(node: HTMLElement): HTMLElement;
2
- //# sourceMappingURL=cloneElement.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"cloneElement.d.ts","sourceRoot":"","sources":["../../src/helpers/cloneElement.ts"],"names":[],"mappings":"AAWA,wBAAgB,YAAY,CAAC,IAAI,EAAE,WAAW,eAU7C"}
@@ -1,3 +0,0 @@
1
- import { Editor } from '@tiptap/core';
2
- export declare function dragHandler(event: DragEvent, editor: Editor): void;
3
- //# sourceMappingURL=dragHandler.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"dragHandler.d.ts","sourceRoot":"","sources":["../../src/helpers/dragHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AA2CrC,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,QA0D3D"}
@@ -1,14 +0,0 @@
1
- import { Editor } from '@tiptap/core';
2
- import { Node } from '@tiptap/pm/model';
3
- export type FindElementNextToCoords = {
4
- x: number;
5
- y: number;
6
- direction?: 'left' | 'right';
7
- editor: Editor;
8
- };
9
- export declare const findElementNextToCoords: (options: FindElementNextToCoords) => {
10
- resultElement: HTMLElement | null;
11
- resultNode: Node | null;
12
- pos: number | null;
13
- };
14
- //# sourceMappingURL=findNextElementFromCursor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"findNextElementFromCursor.d.ts","sourceRoot":"","sources":["../../src/helpers/findNextElementFromCursor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AACrC,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAEvC,MAAM,MAAM,uBAAuB,GAAG;IACpC,CAAC,EAAE,MAAM,CAAA;IACT,CAAC,EAAE,MAAM,CAAA;IACT,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IAC5B,MAAM,EAAE,MAAM,CAAA;CACf,CAAA;AAED,eAAO,MAAM,uBAAuB,YAAa,uBAAuB;;;;CA4CvE,CAAA"}
@@ -1,2 +0,0 @@
1
- export declare function getComputedStyle(node: Element, property: keyof CSSStyleDeclaration): any;
2
- //# sourceMappingURL=getComputedStyle.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getComputedStyle.d.ts","sourceRoot":"","sources":["../../src/helpers/getComputedStyle.ts"],"names":[],"mappings":"AAAA,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,GAAG,GAAG,CAIxF"}
@@ -1,6 +0,0 @@
1
- import { EditorView } from '@tiptap/pm/view';
2
- export declare function getInnerCoords(view: EditorView, x: number, y: number): {
3
- left: number;
4
- top: number;
5
- };
6
- //# sourceMappingURL=getInnerCoords.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getInnerCoords.d.ts","sourceRoot":"","sources":["../../src/helpers/getInnerCoords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAK5C,wBAAgB,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAgBpG"}
@@ -1,4 +0,0 @@
1
- import { Node } from '@tiptap/pm/model';
2
- export declare const getOuterNodePos: (doc: Node, pos: number) => number;
3
- export declare const getOuterNode: (doc: Node, pos: number) => Node | null;
4
- //# sourceMappingURL=getOuterNode.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"getOuterNode.d.ts","sourceRoot":"","sources":["../../src/helpers/getOuterNode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAA;AAEvC,eAAO,MAAM,eAAe,QAAS,IAAI,OAAO,MAAM,KAAG,MAWxD,CAAA;AAED,eAAO,MAAM,YAAY,QAAS,IAAI,OAAO,MAAM,KAAG,IAAI,GAAG,IAkB5D,CAAA"}
@@ -1,2 +0,0 @@
1
- export declare function minMax(value?: number, min?: number, max?: number): number;
2
- //# sourceMappingURL=minMax.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"minMax.d.ts","sourceRoot":"","sources":["../../src/helpers/minMax.ts"],"names":[],"mappings":"AAAA,wBAAgB,MAAM,CAAC,KAAK,SAAI,EAAE,GAAG,SAAI,EAAE,GAAG,SAAI,GAAG,MAAM,CAE1D"}
@@ -1,2 +0,0 @@
1
- export declare function removeNode(node: HTMLElement): void;
2
- //# sourceMappingURL=removeNode.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"removeNode.d.ts","sourceRoot":"","sources":["../../src/helpers/removeNode.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,IAAI,EAAE,WAAW,QAE3C"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAE7C,cAAc,kBAAkB,CAAA;AAChC,cAAc,yBAAyB,CAAA;AAEvC,eAAe,UAAU,CAAA"}