@tiptap/core 2.9.0 → 2.10.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 (66) hide show
  1. package/dist/Editor.d.ts.map +1 -1
  2. package/dist/EventEmitter.d.ts +1 -0
  3. package/dist/EventEmitter.d.ts.map +1 -1
  4. package/dist/Extension.d.ts +2 -2
  5. package/dist/Extension.d.ts.map +1 -1
  6. package/dist/InputRule.d.ts.map +1 -1
  7. package/dist/Mark.d.ts +2 -2
  8. package/dist/Mark.d.ts.map +1 -1
  9. package/dist/Node.d.ts +18 -2
  10. package/dist/Node.d.ts.map +1 -1
  11. package/dist/PasteRule.d.ts +1 -1
  12. package/dist/PasteRule.d.ts.map +1 -1
  13. package/dist/commands/insertContent.d.ts +2 -2
  14. package/dist/commands/insertContent.d.ts.map +1 -1
  15. package/dist/commands/insertContentAt.d.ts +2 -2
  16. package/dist/commands/insertContentAt.d.ts.map +1 -1
  17. package/dist/commands/setContent.d.ts +2 -2
  18. package/dist/commands/setContent.d.ts.map +1 -1
  19. package/dist/commands/setNode.d.ts.map +1 -1
  20. package/dist/commands/updateAttributes.d.ts.map +1 -1
  21. package/dist/helpers/createDocument.d.ts +2 -2
  22. package/dist/helpers/createDocument.d.ts.map +1 -1
  23. package/dist/helpers/createNodeFromContent.d.ts +1 -1
  24. package/dist/helpers/createNodeFromContent.d.ts.map +1 -1
  25. package/dist/helpers/getMarkRange.d.ts +17 -1
  26. package/dist/helpers/getMarkRange.d.ts.map +1 -1
  27. package/dist/helpers/getSchemaByResolvedExtensions.d.ts.map +1 -1
  28. package/dist/index.cjs +158 -44
  29. package/dist/index.cjs.map +1 -1
  30. package/dist/index.js +159 -45
  31. package/dist/index.js.map +1 -1
  32. package/dist/index.umd.js +158 -44
  33. package/dist/index.umd.js.map +1 -1
  34. package/dist/inputRules/markInputRule.d.ts +1 -1
  35. package/dist/inputRules/nodeInputRule.d.ts +1 -1
  36. package/dist/inputRules/textInputRule.d.ts +1 -1
  37. package/dist/inputRules/textblockTypeInputRule.d.ts +1 -1
  38. package/dist/inputRules/wrappingInputRule.d.ts +1 -1
  39. package/dist/pasteRules/markPasteRule.d.ts +1 -1
  40. package/dist/pasteRules/nodePasteRule.d.ts +1 -1
  41. package/dist/pasteRules/textPasteRule.d.ts +1 -1
  42. package/package.json +2 -2
  43. package/src/Editor.ts +5 -8
  44. package/src/EventEmitter.ts +9 -0
  45. package/src/Extension.ts +2 -2
  46. package/src/InputRule.ts +45 -30
  47. package/src/Mark.ts +2 -2
  48. package/src/Node.ts +21 -2
  49. package/src/PasteRule.ts +67 -42
  50. package/src/commands/insertContent.ts +9 -9
  51. package/src/commands/insertContentAt.ts +11 -1
  52. package/src/commands/setContent.ts +10 -14
  53. package/src/commands/setNode.ts +9 -2
  54. package/src/commands/updateAttributes.ts +72 -12
  55. package/src/helpers/createDocument.ts +4 -2
  56. package/src/helpers/createNodeFromContent.ts +4 -1
  57. package/src/helpers/getMarkRange.ts +29 -5
  58. package/src/helpers/getSchemaByResolvedExtensions.ts +1 -0
  59. package/src/inputRules/markInputRule.ts +1 -1
  60. package/src/inputRules/nodeInputRule.ts +1 -1
  61. package/src/inputRules/textInputRule.ts +1 -1
  62. package/src/inputRules/textblockTypeInputRule.ts +1 -1
  63. package/src/inputRules/wrappingInputRule.ts +1 -1
  64. package/src/pasteRules/markPasteRule.ts +1 -1
  65. package/src/pasteRules/nodePasteRule.ts +1 -1
  66. package/src/pasteRules/textPasteRule.ts +1 -1
@@ -21,6 +21,13 @@ declare module '@tiptap/core' {
21
21
  export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) => ({ state, dispatch, chain }) => {
22
22
  const type = getNodeType(typeOrName, state.schema)
23
23
 
24
+ let attributesToCopy: Record<string, any> | undefined
25
+
26
+ if (state.selection.$anchor.sameParent(state.selection.$head)) {
27
+ // only copy attributes if the selection is pointing to a node of the same type
28
+ attributesToCopy = state.selection.$anchor.parent.attrs
29
+ }
30
+
24
31
  // TODO: use a fallback like insertContent?
25
32
  if (!type.isTextblock) {
26
33
  console.warn('[tiptap warn]: Currently "setNode()" only supports text block nodes.')
@@ -32,7 +39,7 @@ export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) =>
32
39
  chain()
33
40
  // try to convert node to default node if needed
34
41
  .command(({ commands }) => {
35
- const canSetBlock = setBlockType(type, attributes)(state)
42
+ const canSetBlock = setBlockType(type, { ...attributesToCopy, ...attributes })(state)
36
43
 
37
44
  if (canSetBlock) {
38
45
  return true
@@ -41,7 +48,7 @@ export const setNode: RawCommands['setNode'] = (typeOrName, attributes = {}) =>
41
48
  return commands.clearNodes()
42
49
  })
43
50
  .command(({ state: updatedState }) => {
44
- return setBlockType(type, attributes)(updatedState, dispatch)
51
+ return setBlockType(type, { ...attributesToCopy, ...attributes })(updatedState, dispatch)
45
52
  })
46
53
  .run()
47
54
  )
@@ -1,4 +1,7 @@
1
- import { MarkType, NodeType } from '@tiptap/pm/model'
1
+ import {
2
+ Mark, MarkType, Node, NodeType,
3
+ } from '@tiptap/pm/model'
4
+ import { SelectionRange } from '@tiptap/pm/state'
2
5
 
3
6
  import { getMarkType } from '../helpers/getMarkType.js'
4
7
  import { getNodeType } from '../helpers/getNodeType.js'
@@ -30,6 +33,7 @@ declare module '@tiptap/core' {
30
33
  }
31
34
 
32
35
  export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, attributes = {}) => ({ tr, state, dispatch }) => {
36
+
33
37
  let nodeType: NodeType | null = null
34
38
  let markType: MarkType | null = null
35
39
 
@@ -51,24 +55,80 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
51
55
  }
52
56
 
53
57
  if (dispatch) {
54
- tr.selection.ranges.forEach(range => {
58
+ tr.selection.ranges.forEach((range: SelectionRange) => {
59
+
55
60
  const from = range.$from.pos
56
61
  const to = range.$to.pos
57
62
 
58
- state.doc.nodesBetween(from, to, (node, pos) => {
59
- if (nodeType && nodeType === node.type) {
60
- tr.setNodeMarkup(pos, undefined, {
61
- ...node.attrs,
63
+ let lastPos: number | undefined
64
+ let lastNode: Node | undefined
65
+ let trimmedFrom: number
66
+ let trimmedTo: number
67
+
68
+ if (tr.selection.empty) {
69
+ state.doc.nodesBetween(from, to, (node: Node, pos: number) => {
70
+
71
+ if (nodeType && nodeType === node.type) {
72
+ trimmedFrom = Math.max(pos, from)
73
+ trimmedTo = Math.min(pos + node.nodeSize, to)
74
+ lastPos = pos
75
+ lastNode = node
76
+ }
77
+ })
78
+ } else {
79
+ state.doc.nodesBetween(from, to, (node: Node, pos: number) => {
80
+
81
+ if (pos < from && nodeType && nodeType === node.type) {
82
+ trimmedFrom = Math.max(pos, from)
83
+ trimmedTo = Math.min(pos + node.nodeSize, to)
84
+ lastPos = pos
85
+ lastNode = node
86
+ }
87
+
88
+ if (pos >= from && pos <= to) {
89
+
90
+ if (nodeType && nodeType === node.type) {
91
+ tr.setNodeMarkup(pos, undefined, {
92
+ ...node.attrs,
93
+ ...attributes,
94
+ })
95
+ }
96
+
97
+ if (markType && node.marks.length) {
98
+ node.marks.forEach((mark: Mark) => {
99
+
100
+ if (markType === mark.type) {
101
+ const trimmedFrom2 = Math.max(pos, from)
102
+ const trimmedTo2 = Math.min(pos + node.nodeSize, to)
103
+
104
+ tr.addMark(
105
+ trimmedFrom2,
106
+ trimmedTo2,
107
+ markType.create({
108
+ ...mark.attrs,
109
+ ...attributes,
110
+ }),
111
+ )
112
+ }
113
+ })
114
+ }
115
+ }
116
+ })
117
+ }
118
+
119
+ if (lastNode) {
120
+
121
+ if (lastPos !== undefined) {
122
+ tr.setNodeMarkup(lastPos, undefined, {
123
+ ...lastNode.attrs,
62
124
  ...attributes,
63
125
  })
64
126
  }
65
127
 
66
- if (markType && node.marks.length) {
67
- node.marks.forEach(mark => {
68
- if (markType === mark.type) {
69
- const trimmedFrom = Math.max(pos, from)
70
- const trimmedTo = Math.min(pos + node.nodeSize, to)
128
+ if (markType && lastNode.marks.length) {
129
+ lastNode.marks.forEach((mark: Mark) => {
71
130
 
131
+ if (markType === mark.type) {
72
132
  tr.addMark(
73
133
  trimmedFrom,
74
134
  trimmedTo,
@@ -80,7 +140,7 @@ export const updateAttributes: RawCommands['updateAttributes'] = (typeOrName, at
80
140
  }
81
141
  })
82
142
  }
83
- })
143
+ }
84
144
  })
85
145
  }
86
146
 
@@ -1,4 +1,6 @@
1
- import { Node as ProseMirrorNode, ParseOptions, Schema } from '@tiptap/pm/model'
1
+ import {
2
+ Fragment, Node as ProseMirrorNode, ParseOptions, Schema,
3
+ } from '@tiptap/pm/model'
2
4
 
3
5
  import { Content } from '../types.js'
4
6
  import { createNodeFromContent } from './createNodeFromContent.js'
@@ -11,7 +13,7 @@ import { createNodeFromContent } from './createNodeFromContent.js'
11
13
  * @returns The created Prosemirror document node
12
14
  */
13
15
  export function createDocument(
14
- content: Content,
16
+ content: Content | ProseMirrorNode | Fragment,
15
17
  schema: Schema,
16
18
  parseOptions: ParseOptions = {},
17
19
  options: { errorOnInvalidContent?: boolean } = {},
@@ -23,10 +23,13 @@ export type CreateNodeFromContentOptions = {
23
23
  * @returns The created Prosemirror node or fragment
24
24
  */
25
25
  export function createNodeFromContent(
26
- content: Content,
26
+ content: Content | ProseMirrorNode | Fragment,
27
27
  schema: Schema,
28
28
  options?: CreateNodeFromContentOptions,
29
29
  ): ProseMirrorNode | Fragment {
30
+ if (content instanceof ProseMirrorNode || content instanceof Fragment) {
31
+ return content
32
+ }
30
33
  options = {
31
34
  slice: true,
32
35
  parseOptions: {},
@@ -9,7 +9,14 @@ function findMarkInSet(
9
9
  attributes: Record<string, any> = {},
10
10
  ): ProseMirrorMark | undefined {
11
11
  return marks.find(item => {
12
- return item.type === type && objectIncludes(item.attrs, attributes)
12
+ return (
13
+ item.type === type
14
+ && objectIncludes(
15
+ // Only check equality for the attributes that are provided
16
+ Object.fromEntries(Object.keys(attributes).map(k => [k, item.attrs[k]])),
17
+ attributes,
18
+ )
19
+ )
13
20
  })
14
21
  }
15
22
 
@@ -21,10 +28,23 @@ function isMarkInSet(
21
28
  return !!findMarkInSet(marks, type, attributes)
22
29
  }
23
30
 
31
+ /**
32
+ * Get the range of a mark at a resolved position.
33
+ */
24
34
  export function getMarkRange(
35
+ /**
36
+ * The position to get the mark range for.
37
+ */
25
38
  $pos: ResolvedPos,
39
+ /**
40
+ * The mark type to get the range for.
41
+ */
26
42
  type: MarkType,
27
- attributes: Record<string, any> = {},
43
+ /**
44
+ * The attributes to match against.
45
+ * If not provided, only the first mark at the position will be matched.
46
+ */
47
+ attributes?: Record<string, any>,
28
48
  ): Range | void {
29
49
  if (!$pos || !type) {
30
50
  return
@@ -41,6 +61,9 @@ export function getMarkRange(
41
61
  return
42
62
  }
43
63
 
64
+ // Default to only matching against the first mark's attributes
65
+ attributes = attributes || start.node.marks[0]?.attrs
66
+
44
67
  // We now know that the cursor is either at the start, middle or end of a text node with the specified mark
45
68
  // so we can look it up on the targeted mark
46
69
  const mark = findMarkInSet([...start.node.marks], type, attributes)
@@ -54,9 +77,10 @@ export function getMarkRange(
54
77
  let endIndex = startIndex + 1
55
78
  let endPos = startPos + start.node.nodeSize
56
79
 
57
- findMarkInSet([...start.node.marks], type, attributes)
58
-
59
- while (startIndex > 0 && mark.isInSet($pos.parent.child(startIndex - 1).marks)) {
80
+ while (
81
+ startIndex > 0
82
+ && isMarkInSet([...$pos.parent.child(startIndex - 1).marks], type, attributes)
83
+ ) {
60
84
  startIndex -= 1
61
85
  startPos -= $pos.parent.child(startIndex).nodeSize
62
86
  }
@@ -78,6 +78,7 @@ export function getSchemaByResolvedExtensions(extensions: Extensions, editor?: E
78
78
  ),
79
79
  code: callOrReturn(getExtensionField<NodeConfig['code']>(extension, 'code', context)),
80
80
  whitespace: callOrReturn(getExtensionField<NodeConfig['whitespace']>(extension, 'whitespace', context)),
81
+ linebreakReplacement: callOrReturn(getExtensionField<NodeConfig['linebreakReplacement']>(extension, 'linebreakReplacement', context)),
81
82
  defining: callOrReturn(
82
83
  getExtensionField<NodeConfig['defining']>(extension, 'defining', context),
83
84
  ),
@@ -8,7 +8,7 @@ import { callOrReturn } from '../utilities/callOrReturn.js'
8
8
  /**
9
9
  * Build an input rule that adds a mark when the
10
10
  * matched text is typed into it.
11
- * @see https://tiptap.dev/guide/custom-extensions/#input-rules
11
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
12
12
  */
13
13
  export function markInputRule(config: {
14
14
  find: InputRuleFinder
@@ -7,7 +7,7 @@ import { callOrReturn } from '../utilities/callOrReturn.js'
7
7
  /**
8
8
  * Build an input rule that adds a node when the
9
9
  * matched text is typed into it.
10
- * @see https://tiptap.dev/guide/custom-extensions/#input-rules
10
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
11
11
  */
12
12
  export function nodeInputRule(config: {
13
13
  /**
@@ -3,7 +3,7 @@ import { InputRule, InputRuleFinder } from '../InputRule.js'
3
3
  /**
4
4
  * Build an input rule that replaces text when the
5
5
  * matched text is typed into it.
6
- * @see https://tiptap.dev/guide/custom-extensions/#input-rules
6
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
7
7
  */
8
8
  export function textInputRule(config: {
9
9
  find: InputRuleFinder,
@@ -9,7 +9,7 @@ import { callOrReturn } from '../utilities/callOrReturn.js'
9
9
  * matched text is typed into it. When using a regular expresion you’ll
10
10
  * probably want the regexp to start with `^`, so that the pattern can
11
11
  * only occur at the start of a textblock.
12
- * @see https://tiptap.dev/guide/custom-extensions/#input-rules
12
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
13
13
  */
14
14
  export function textblockTypeInputRule(config: {
15
15
  find: InputRuleFinder
@@ -19,7 +19,7 @@ import { callOrReturn } from '../utilities/callOrReturn.js'
19
19
  * two nodes. You can pass a join predicate, which takes a regular
20
20
  * expression match and the node before the wrapped node, and can
21
21
  * return a boolean to indicate whether a join should happen.
22
- * @see https://tiptap.dev/guide/custom-extensions/#input-rules
22
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#input-rules
23
23
  */
24
24
  export function wrappingInputRule(config: {
25
25
  find: InputRuleFinder,
@@ -8,7 +8,7 @@ import { callOrReturn } from '../utilities/callOrReturn.js'
8
8
  /**
9
9
  * Build an paste rule that adds a mark when the
10
10
  * matched text is pasted into it.
11
- * @see https://tiptap.dev/guide/custom-extensions/#paste-rules
11
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#paste-rules
12
12
  */
13
13
  export function markPasteRule(config: {
14
14
  find: PasteRuleFinder
@@ -7,7 +7,7 @@ import { callOrReturn } from '../utilities/index.js'
7
7
  /**
8
8
  * Build an paste rule that adds a node when the
9
9
  * matched text is pasted into it.
10
- * @see https://tiptap.dev/guide/custom-extensions/#paste-rules
10
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#paste-rules
11
11
  */
12
12
  export function nodePasteRule(config: {
13
13
  find: PasteRuleFinder
@@ -3,7 +3,7 @@ import { PasteRule, PasteRuleFinder } from '../PasteRule.js'
3
3
  /**
4
4
  * Build an paste rule that replaces text when the
5
5
  * matched text is pasted into it.
6
- * @see https://tiptap.dev/guide/custom-extensions/#paste-rules
6
+ * @see https://tiptap.dev/docs/editor/extensions/custom-extensions/extend-existing#paste-rules
7
7
  */
8
8
  export function textPasteRule(config: {
9
9
  find: PasteRuleFinder,