@tiptap/extensions 3.0.0-next.4 → 3.0.0-next.6

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 (39) hide show
  1. package/LICENSE.md +1 -1
  2. package/dist/character-count/index.cjs +129 -0
  3. package/dist/character-count/index.cjs.map +1 -0
  4. package/dist/character-count/index.d.cts +62 -0
  5. package/dist/character-count/index.d.ts +62 -0
  6. package/dist/character-count/index.js +102 -0
  7. package/dist/character-count/index.js.map +1 -0
  8. package/dist/drop-cursor/index.cjs +47 -0
  9. package/dist/drop-cursor/index.cjs.map +1 -0
  10. package/dist/drop-cursor/index.d.cts +31 -0
  11. package/dist/drop-cursor/index.d.ts +31 -0
  12. package/dist/drop-cursor/index.js +20 -0
  13. package/dist/drop-cursor/index.js.map +1 -0
  14. package/dist/history/index.cjs +66 -0
  15. package/dist/history/index.cjs.map +1 -0
  16. package/dist/history/index.d.cts +44 -0
  17. package/dist/history/index.d.ts +44 -0
  18. package/dist/history/index.js +39 -0
  19. package/dist/history/index.js.map +1 -0
  20. package/dist/index.cjs +222 -23
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +157 -2
  23. package/dist/index.d.ts +157 -2
  24. package/dist/index.js +218 -22
  25. package/dist/index.js.map +1 -1
  26. package/dist/placeholder/index.cjs +88 -0
  27. package/dist/placeholder/index.cjs.map +1 -0
  28. package/dist/placeholder/index.d.cts +59 -0
  29. package/dist/placeholder/index.d.ts +59 -0
  30. package/dist/placeholder/index.js +61 -0
  31. package/dist/placeholder/index.js.map +1 -0
  32. package/package.json +27 -3
  33. package/src/character-count/character-count.ts +195 -0
  34. package/src/character-count/index.ts +1 -0
  35. package/src/history/history.ts +86 -0
  36. package/src/history/index.ts +1 -0
  37. package/src/index.ts +3 -0
  38. package/src/placeholder/index.ts +1 -0
  39. package/src/placeholder/placeholder.ts +128 -0
@@ -0,0 +1,86 @@
1
+ import { Extension } from '@tiptap/core'
2
+ import { history, redo, undo } from '@tiptap/pm/history'
3
+
4
+ export interface HistoryOptions {
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
+ history: {
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/history
40
+ *
41
+ * **Important**: If the `@tiptap/extension-collaboration` package is used, make sure to remove
42
+ * the `history` extension, as it is not compatible with the `collaboration` extension.
43
+ *
44
+ * `@tiptap/extension-collaboration` uses its own history implementation.
45
+ */
46
+ export const History = Extension.create<HistoryOptions>({
47
+ name: 'history',
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
+ })
@@ -0,0 +1 @@
1
+ export * from './history.js'
package/src/index.ts CHANGED
@@ -1,5 +1,8 @@
1
+ export * from './character-count/index.js'
1
2
  export * from './drop-cursor/index.js'
2
3
  export * from './focus/index.js'
3
4
  export * from './gap-cursor/index.js'
5
+ export * from './history/index.js'
6
+ export * from './placeholder/index.js'
4
7
  export * from './selection/index.js'
5
8
  export * from './trailing-node/index.js'
@@ -0,0 +1 @@
1
+ export * from './placeholder.js'
@@ -0,0 +1,128 @@
1
+ import { Editor, Extension, isNodeEmpty } from '@tiptap/core'
2
+ import { Node as ProsemirrorNode } from '@tiptap/pm/model'
3
+ import { Plugin, PluginKey } from '@tiptap/pm/state'
4
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
5
+
6
+ export interface PlaceholderOptions {
7
+ /**
8
+ * **The class name for the empty editor**
9
+ * @default 'is-editor-empty'
10
+ */
11
+ emptyEditorClass: string
12
+
13
+ /**
14
+ * **The class name for empty nodes**
15
+ * @default 'is-empty'
16
+ */
17
+ emptyNodeClass: string
18
+
19
+ /**
20
+ * **The placeholder content**
21
+ *
22
+ * You can use a function to return a dynamic placeholder or a string.
23
+ * @default 'Write something …'
24
+ */
25
+ placeholder:
26
+ | ((PlaceholderProps: { editor: Editor; node: ProsemirrorNode; pos: number; hasAnchor: boolean }) => string)
27
+ | string
28
+
29
+ /**
30
+ * **Checks if the placeholder should be only shown when the editor is editable.**
31
+ *
32
+ * If true, the placeholder will only be shown when the editor is editable.
33
+ * If false, the placeholder will always be shown.
34
+ * @default true
35
+ */
36
+ showOnlyWhenEditable: boolean
37
+
38
+ /**
39
+ * **Checks if the placeholder should be only shown when the current node is empty.**
40
+ *
41
+ * If true, the placeholder will only be shown when the current node is empty.
42
+ * If false, the placeholder will be shown when any node is empty.
43
+ * @default true
44
+ */
45
+ showOnlyCurrent: boolean
46
+
47
+ /**
48
+ * **Controls if the placeholder should be shown for all descendents.**
49
+ *
50
+ * If true, the placeholder will be shown for all descendents.
51
+ * If false, the placeholder will only be shown for the current node.
52
+ * @default false
53
+ */
54
+ includeChildren: boolean
55
+ }
56
+
57
+ /**
58
+ * This extension allows you to add a placeholder to your editor.
59
+ * A placeholder is a text that appears when the editor or a node is empty.
60
+ * @see https://www.tiptap.dev/api/extensions/placeholder
61
+ */
62
+ export const Placeholder = Extension.create<PlaceholderOptions>({
63
+ name: 'placeholder',
64
+
65
+ addOptions() {
66
+ return {
67
+ emptyEditorClass: 'is-editor-empty',
68
+ emptyNodeClass: 'is-empty',
69
+ placeholder: 'Write something …',
70
+ showOnlyWhenEditable: true,
71
+ showOnlyCurrent: true,
72
+ includeChildren: false,
73
+ }
74
+ },
75
+
76
+ addProseMirrorPlugins() {
77
+ return [
78
+ new Plugin({
79
+ key: new PluginKey('placeholder'),
80
+ props: {
81
+ decorations: ({ doc, selection }) => {
82
+ const active = this.editor.isEditable || !this.options.showOnlyWhenEditable
83
+ const { anchor } = selection
84
+ const decorations: Decoration[] = []
85
+
86
+ if (!active) {
87
+ return null
88
+ }
89
+
90
+ const isEmptyDoc = this.editor.isEmpty
91
+
92
+ doc.descendants((node, pos) => {
93
+ const hasAnchor = anchor >= pos && anchor <= pos + node.nodeSize
94
+ const isEmpty = !node.isLeaf && isNodeEmpty(node)
95
+
96
+ if ((hasAnchor || !this.options.showOnlyCurrent) && isEmpty) {
97
+ const classes = [this.options.emptyNodeClass]
98
+
99
+ if (isEmptyDoc) {
100
+ classes.push(this.options.emptyEditorClass)
101
+ }
102
+
103
+ const decoration = Decoration.node(pos, pos + node.nodeSize, {
104
+ class: classes.join(' '),
105
+ 'data-placeholder':
106
+ typeof this.options.placeholder === 'function'
107
+ ? this.options.placeholder({
108
+ editor: this.editor,
109
+ node,
110
+ pos,
111
+ hasAnchor,
112
+ })
113
+ : this.options.placeholder,
114
+ })
115
+
116
+ decorations.push(decoration)
117
+ }
118
+
119
+ return this.options.includeChildren
120
+ })
121
+
122
+ return DecorationSet.create(doc, decorations)
123
+ },
124
+ },
125
+ }),
126
+ ]
127
+ },
128
+ })