@tiptap/extensions 3.0.0-beta.0

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 (74) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +18 -0
  3. package/dist/character-count/index.cjs +129 -0
  4. package/dist/character-count/index.cjs.map +1 -0
  5. package/dist/character-count/index.d.cts +62 -0
  6. package/dist/character-count/index.d.ts +62 -0
  7. package/dist/character-count/index.js +102 -0
  8. package/dist/character-count/index.js.map +1 -0
  9. package/dist/drop-cursor/index.cjs +47 -0
  10. package/dist/drop-cursor/index.cjs.map +1 -0
  11. package/dist/drop-cursor/index.d.cts +31 -0
  12. package/dist/drop-cursor/index.d.ts +31 -0
  13. package/dist/drop-cursor/index.js +20 -0
  14. package/dist/drop-cursor/index.js.map +1 -0
  15. package/dist/focus/index.cjs +95 -0
  16. package/dist/focus/index.cjs.map +1 -0
  17. package/dist/focus/index.d.cts +28 -0
  18. package/dist/focus/index.d.ts +28 -0
  19. package/dist/focus/index.js +68 -0
  20. package/dist/focus/index.js.map +1 -0
  21. package/dist/gap-cursor/index.cjs +51 -0
  22. package/dist/gap-cursor/index.cjs.map +1 -0
  23. package/dist/gap-cursor/index.d.cts +25 -0
  24. package/dist/gap-cursor/index.d.ts +25 -0
  25. package/dist/gap-cursor/index.js +24 -0
  26. package/dist/gap-cursor/index.js.map +1 -0
  27. package/dist/index.cjs +421 -0
  28. package/dist/index.cjs.map +1 -0
  29. package/dist/index.d.cts +272 -0
  30. package/dist/index.d.ts +272 -0
  31. package/dist/index.js +387 -0
  32. package/dist/index.js.map +1 -0
  33. package/dist/placeholder/index.cjs +88 -0
  34. package/dist/placeholder/index.cjs.map +1 -0
  35. package/dist/placeholder/index.d.cts +59 -0
  36. package/dist/placeholder/index.d.ts +59 -0
  37. package/dist/placeholder/index.js +61 -0
  38. package/dist/placeholder/index.js.map +1 -0
  39. package/dist/selection/index.cjs +63 -0
  40. package/dist/selection/index.cjs.map +1 -0
  41. package/dist/selection/index.d.cts +17 -0
  42. package/dist/selection/index.d.ts +17 -0
  43. package/dist/selection/index.js +36 -0
  44. package/dist/selection/index.js.map +1 -0
  45. package/dist/trailing-node/index.cjs +78 -0
  46. package/dist/trailing-node/index.cjs.map +1 -0
  47. package/dist/trailing-node/index.d.cts +28 -0
  48. package/dist/trailing-node/index.d.ts +28 -0
  49. package/dist/trailing-node/index.js +51 -0
  50. package/dist/trailing-node/index.js.map +1 -0
  51. package/dist/undo-redo/index.cjs +66 -0
  52. package/dist/undo-redo/index.cjs.map +1 -0
  53. package/dist/undo-redo/index.d.cts +44 -0
  54. package/dist/undo-redo/index.d.ts +44 -0
  55. package/dist/undo-redo/index.js +39 -0
  56. package/dist/undo-redo/index.js.map +1 -0
  57. package/package.json +114 -0
  58. package/src/character-count/character-count.ts +195 -0
  59. package/src/character-count/index.ts +1 -0
  60. package/src/drop-cursor/drop-cursor.ts +47 -0
  61. package/src/drop-cursor/index.ts +1 -0
  62. package/src/focus/focus.ts +110 -0
  63. package/src/focus/index.ts +1 -0
  64. package/src/gap-cursor/gap-cursor.ts +47 -0
  65. package/src/gap-cursor/index.ts +1 -0
  66. package/src/index.ts +8 -0
  67. package/src/placeholder/index.ts +1 -0
  68. package/src/placeholder/placeholder.ts +129 -0
  69. package/src/selection/index.ts +1 -0
  70. package/src/selection/selection.ts +51 -0
  71. package/src/trailing-node/index.ts +1 -0
  72. package/src/trailing-node/trailing-node.ts +84 -0
  73. package/src/undo-redo/index.ts +1 -0
  74. package/src/undo-redo/undo-redo.ts +86 -0
@@ -0,0 +1,51 @@
1
+ import { Extension, isNodeSelection } from '@tiptap/core'
2
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
3
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
4
+
5
+ export type SelectionOptions = {
6
+ /**
7
+ * The class name that should be added to the selected text.
8
+ * @default 'selection'
9
+ * @example 'is-selected'
10
+ */
11
+ className: string
12
+ }
13
+
14
+ /**
15
+ * This extension allows you to add a class to the selected text.
16
+ * @see https://www.tiptap.dev/api/extensions/selection
17
+ */
18
+ export const Selection = Extension.create({
19
+ name: 'selection',
20
+
21
+ addOptions() {
22
+ return {
23
+ className: 'selection',
24
+ }
25
+ },
26
+
27
+ addProseMirrorPlugins() {
28
+ const { editor, options } = this
29
+
30
+ return [
31
+ new Plugin({
32
+ key: new PluginKey('selection'),
33
+ props: {
34
+ decorations(state) {
35
+ if (state.selection.empty || editor.isFocused || !editor.isEditable || isNodeSelection(state.selection)) {
36
+ return null
37
+ }
38
+
39
+ return DecorationSet.create(state.doc, [
40
+ Decoration.inline(state.selection.from, state.selection.to, {
41
+ class: options.className,
42
+ }),
43
+ ])
44
+ },
45
+ },
46
+ }),
47
+ ]
48
+ },
49
+ })
50
+
51
+ export default Selection
@@ -0,0 +1 @@
1
+ export * from './trailing-node.js'
@@ -0,0 +1,84 @@
1
+ import { Extension } from '@tiptap/core'
2
+ import type { Node, NodeType } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
+
5
+ function nodeEqualsType({ types, node }: { types: NodeType | NodeType[]; node: Node | null | undefined }) {
6
+ return (node && Array.isArray(types) && types.includes(node.type)) || node?.type === types
7
+ }
8
+
9
+ /**
10
+ * Extension based on:
11
+ * - https://github.com/ueberdosis/tiptap/blob/v1/packages/tiptap-extensions/src/extensions/TrailingNode.js
12
+ * - https://github.com/remirror/remirror/blob/e0f1bec4a1e8073ce8f5500d62193e52321155b9/packages/prosemirror-trailing-node/src/trailing-node-plugin.ts
13
+ */
14
+
15
+ export interface TrailingNodeOptions {
16
+ /**
17
+ * The node type that should be inserted at the end of the document.
18
+ * @note the node will always be added to the `notAfter` lists to
19
+ * prevent an infinite loop.
20
+ * @default 'paragraph'
21
+ */
22
+ node: string
23
+ /**
24
+ * The node types after which the trailing node should not be inserted.
25
+ * @default ['paragraph']
26
+ */
27
+ notAfter?: string | string[]
28
+ }
29
+
30
+ /**
31
+ * This extension allows you to add an extra node at the end of the document.
32
+ * @see https://www.tiptap.dev/api/extensions/trailing-node
33
+ */
34
+ export const TrailingNode = Extension.create<TrailingNodeOptions>({
35
+ name: 'trailingNode',
36
+
37
+ addOptions() {
38
+ return {
39
+ node: 'paragraph',
40
+ notAfter: [],
41
+ }
42
+ },
43
+
44
+ addProseMirrorPlugins() {
45
+ const plugin = new PluginKey(this.name)
46
+ const disabledNodes = Object.entries(this.editor.schema.nodes)
47
+ .map(([, value]) => value)
48
+ .filter(node => (this.options.notAfter || []).concat(this.options.node).includes(node.name))
49
+
50
+ return [
51
+ new Plugin({
52
+ key: plugin,
53
+ appendTransaction: (_, __, state) => {
54
+ const { doc, tr, schema } = state
55
+ const shouldInsertNodeAtEnd = plugin.getState(state)
56
+ const endPosition = doc.content.size
57
+ const type = schema.nodes[this.options.node]
58
+
59
+ if (!shouldInsertNodeAtEnd) {
60
+ return
61
+ }
62
+
63
+ return tr.insert(endPosition, type.create())
64
+ },
65
+ state: {
66
+ init: (_, state) => {
67
+ const lastNode = state.tr.doc.lastChild
68
+
69
+ return !nodeEqualsType({ node: lastNode, types: disabledNodes })
70
+ },
71
+ apply: (tr, value) => {
72
+ if (!tr.docChanged) {
73
+ return value
74
+ }
75
+
76
+ const lastNode = tr.doc.lastChild
77
+
78
+ return !nodeEqualsType({ node: lastNode, types: disabledNodes })
79
+ },
80
+ },
81
+ }),
82
+ ]
83
+ },
84
+ })
@@ -0,0 +1 @@
1
+ export * from './undo-redo.js'
@@ -0,0 +1,86 @@
1
+ import { Extension } from '@tiptap/core'
2
+ import { history, redo, undo } from '@tiptap/pm/history'
3
+
4
+ export interface UndoRedoOptions {
5
+ /**
6
+ * The amount of history events that are collected before the oldest events are discarded.
7
+ * @default 100
8
+ * @example 50
9
+ */
10
+ depth: number
11
+
12
+ /**
13
+ * The delay (in milliseconds) between changes after which a new group should be started.
14
+ * @default 500
15
+ * @example 1000
16
+ */
17
+ newGroupDelay: number
18
+ }
19
+
20
+ declare module '@tiptap/core' {
21
+ interface Commands<ReturnType> {
22
+ undoRedo: {
23
+ /**
24
+ * Undo recent changes
25
+ * @example editor.commands.undo()
26
+ */
27
+ undo: () => ReturnType
28
+ /**
29
+ * Reapply reverted changes
30
+ * @example editor.commands.redo()
31
+ */
32
+ redo: () => ReturnType
33
+ }
34
+ }
35
+ }
36
+
37
+ /**
38
+ * This extension allows you to undo and redo recent changes.
39
+ * @see https://www.tiptap.dev/api/extensions/undo-redo
40
+ *
41
+ * **Important**: If the `@tiptap/extension-collaboration` package is used, make sure to remove
42
+ * the `undo-redo` extension, as it is not compatible with the `collaboration` extension.
43
+ *
44
+ * `@tiptap/extension-collaboration` uses its own history implementation.
45
+ */
46
+ export const UndoRedo = Extension.create<UndoRedoOptions>({
47
+ name: 'undoRedo',
48
+
49
+ addOptions() {
50
+ return {
51
+ depth: 100,
52
+ newGroupDelay: 500,
53
+ }
54
+ },
55
+
56
+ addCommands() {
57
+ return {
58
+ undo:
59
+ () =>
60
+ ({ state, dispatch }) => {
61
+ return undo(state, dispatch)
62
+ },
63
+ redo:
64
+ () =>
65
+ ({ state, dispatch }) => {
66
+ return redo(state, dispatch)
67
+ },
68
+ }
69
+ },
70
+
71
+ addProseMirrorPlugins() {
72
+ return [history(this.options)]
73
+ },
74
+
75
+ addKeyboardShortcuts() {
76
+ return {
77
+ 'Mod-z': () => this.editor.commands.undo(),
78
+ 'Shift-Mod-z': () => this.editor.commands.redo(),
79
+ 'Mod-y': () => this.editor.commands.redo(),
80
+
81
+ // Russian keyboard layouts
82
+ 'Mod-я': () => this.editor.commands.undo(),
83
+ 'Shift-Mod-я': () => this.editor.commands.redo(),
84
+ }
85
+ },
86
+ })