@tiptap/extensions 3.31.2 → 3.31.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiptap/extensions",
3
- "version": "3.31.2",
3
+ "version": "3.31.4",
4
4
  "description": "various extensions for tiptap",
5
5
  "keywords": [
6
6
  "tiptap",
@@ -103,12 +103,12 @@
103
103
  }
104
104
  },
105
105
  "devDependencies": {
106
- "@tiptap/core": "^3.31.2",
107
- "@tiptap/pm": "^3.31.2"
106
+ "@tiptap/core": "^3.31.4",
107
+ "@tiptap/pm": "^3.31.4"
108
108
  },
109
109
  "peerDependencies": {
110
- "@tiptap/core": "3.31.2",
111
- "@tiptap/pm": "3.31.2"
110
+ "@tiptap/core": "3.31.4",
111
+ "@tiptap/pm": "3.31.4"
112
112
  },
113
113
  "scripts": {
114
114
  "build": "vp pack"
@@ -0,0 +1,78 @@
1
+ import { Editor, getChangedRanges } from '@tiptap/core'
2
+ import Document from '@tiptap/extension-document'
3
+ import Paragraph from '@tiptap/extension-paragraph'
4
+ import Text from '@tiptap/extension-text'
5
+ import { describe, expect, it } from 'vite-plus/test'
6
+
7
+ import { getTopLevelBlocksInRange, toContentRelativeRange } from './resolveTopLevelRange.js'
8
+
9
+ function createHeadlessEditor(content: string) {
10
+ return new Editor({
11
+ element: null,
12
+ extensions: [Document, Paragraph, Text],
13
+ content,
14
+ })
15
+ }
16
+
17
+ describe('placeholder utility: getTopLevelBlocksInRange', () => {
18
+ it('returns content-relative ranges aligned with nodesBetween positions', () => {
19
+ const editor = createHeadlessEditor('<p></p><p></p><p></p>')
20
+
21
+ const doc = editor.state.doc
22
+ const nodesBetweenPositions: number[] = []
23
+
24
+ doc.nodesBetween(0, doc.content.size, (node, pos) => {
25
+ if (node.type.name === 'paragraph') {
26
+ nodesBetweenPositions.push(pos)
27
+ }
28
+ })
29
+
30
+ const blocks = getTopLevelBlocksInRange(doc, 1, 2)
31
+
32
+ expect(blocks).toEqual([{ from: 0, to: 2 }])
33
+ expect(blocks[0]?.from).toBe(nodesBetweenPositions[0])
34
+
35
+ editor.destroy()
36
+ })
37
+
38
+ it('aligns resolveTopLevelRange + toContentRelativeRange with getTopLevelBlocksInRange', () => {
39
+ const editor = createHeadlessEditor('<p></p><p></p><p></p>')
40
+
41
+ const doc = editor.state.doc
42
+
43
+ doc.forEach((node, offset) => {
44
+ const contentRange = { from: offset, to: offset + node.nodeSize }
45
+ const absoluteRange = { from: offset + 1, to: offset + node.nodeSize + 1 }
46
+
47
+ expect(toContentRelativeRange(doc, absoluteRange)).toEqual(contentRange)
48
+ expect(getTopLevelBlocksInRange(doc, absoluteRange.from, absoluteRange.to)).toEqual([
49
+ contentRange,
50
+ ])
51
+ })
52
+
53
+ editor.destroy()
54
+ })
55
+
56
+ it('collects only the touched top-level block for a single-paragraph edit', () => {
57
+ const editor = createHeadlessEditor('<p></p><p></p><p></p>')
58
+
59
+ const changes: Array<{ from: number; to: number }> = []
60
+
61
+ editor.on('transaction', ({ transaction }) => {
62
+ if (transaction.docChanged) {
63
+ for (const change of getChangedRanges(transaction)) {
64
+ changes.push(change.newRange)
65
+ }
66
+ }
67
+ })
68
+
69
+ editor.commands.insertContent('Hello')
70
+
71
+ const doc = editor.state.doc
72
+ const blocks = changes.flatMap(change => getTopLevelBlocksInRange(doc, change.from, change.to))
73
+
74
+ expect(blocks).toEqual([{ from: 0, to: 7 }])
75
+
76
+ editor.destroy()
77
+ })
78
+ })
@@ -0,0 +1,97 @@
1
+ import { Editor } from '@tiptap/core'
2
+ import Document from '@tiptap/extension-document'
3
+ import Paragraph from '@tiptap/extension-paragraph'
4
+ import Text from '@tiptap/extension-text'
5
+ import { afterEach, describe, expect, it, vi } from 'vite-plus/test'
6
+
7
+ import { Selection } from './selection.js'
8
+
9
+ describe('extension-selection', () => {
10
+ let editor: Editor | null = null
11
+
12
+ afterEach(() => {
13
+ editor?.destroy()
14
+ editor = null
15
+ vi.restoreAllMocks()
16
+ })
17
+
18
+ it('clears the native selection on blur', () => {
19
+ editor = new Editor({
20
+ extensions: [Document, Paragraph, Text, Selection],
21
+ content: '<p>Hello world</p>',
22
+ })
23
+
24
+ editor.commands.setTextSelection({ from: 1, to: 6 })
25
+
26
+ const button = document.createElement('button')
27
+
28
+ document.body.append(button)
29
+
30
+ editor.view.dom.blur()
31
+ button.focus()
32
+
33
+ const domSelection = window.getSelection()
34
+
35
+ expect(domSelection?.isCollapsed ?? true).toBe(true)
36
+
37
+ button.remove()
38
+ })
39
+
40
+ it('adds selection decorations when blurred with a non-empty selection', () => {
41
+ editor = new Editor({
42
+ extensions: [Document, Paragraph, Text, Selection],
43
+ content: '<p>Hello world</p>',
44
+ })
45
+
46
+ editor.commands.setTextSelection({ from: 1, to: 6 })
47
+
48
+ const button = document.createElement('button')
49
+
50
+ document.body.append(button)
51
+
52
+ editor.view.dom.blur()
53
+ button.focus()
54
+
55
+ expect(editor.view.dom.querySelector('.selection')).not.toBeNull()
56
+
57
+ button.remove()
58
+ })
59
+
60
+ it('does not clear or restore the DOM selection when the editor is not editable', async () => {
61
+ editor = new Editor({
62
+ extensions: [Document, Paragraph, Text, Selection],
63
+ content: '<p>Hello world</p>',
64
+ editable: false,
65
+ })
66
+
67
+ const removeAllRanges = vi.fn()
68
+ const focusSpy = vi.spyOn(editor.view, 'focus')
69
+
70
+ vi.spyOn(window, 'getSelection').mockReturnValue({
71
+ removeAllRanges,
72
+ } as unknown as Selection)
73
+
74
+ editor.commands.setTextSelection({ from: 1, to: 6 })
75
+
76
+ const button = document.createElement('button')
77
+
78
+ document.body.append(button)
79
+
80
+ editor.view.dom.blur()
81
+ button.focus()
82
+
83
+ expect(removeAllRanges).not.toHaveBeenCalled()
84
+
85
+ editor.view.dom.focus()
86
+
87
+ await new Promise<void>(resolve => {
88
+ requestAnimationFrame(() => {
89
+ requestAnimationFrame(() => resolve())
90
+ })
91
+ })
92
+
93
+ expect(focusSpy).not.toHaveBeenCalled()
94
+
95
+ button.remove()
96
+ })
97
+ })
@@ -0,0 +1,90 @@
1
+ import { Editor } from '@tiptap/core'
2
+ import Document from '@tiptap/extension-document'
3
+ import Heading from '@tiptap/extension-heading'
4
+ import Paragraph from '@tiptap/extension-paragraph'
5
+ import Text from '@tiptap/extension-text'
6
+ import { afterEach, describe, expect, it } from 'vite-plus/test'
7
+
8
+ import { skipTrailingNodeMeta, TrailingNode } from './trailing-node.js'
9
+
10
+ const headingDocument = {
11
+ type: 'doc',
12
+ content: [
13
+ {
14
+ type: 'heading',
15
+ attrs: { level: 1 },
16
+ content: [{ type: 'text', text: 'Title' }],
17
+ },
18
+ ],
19
+ }
20
+
21
+ const headingWithTrailingParagraphDocument = {
22
+ type: 'doc',
23
+ content: [
24
+ {
25
+ type: 'heading',
26
+ attrs: { level: 1 },
27
+ content: [{ type: 'text', text: 'Title' }],
28
+ },
29
+ {
30
+ type: 'paragraph',
31
+ },
32
+ ],
33
+ }
34
+
35
+ describe('extension-trailing-node', () => {
36
+ let editor: Editor | null = null
37
+
38
+ const createEditor = () => {
39
+ editor = new Editor({
40
+ extensions: [Document, Paragraph, Text, Heading, TrailingNode],
41
+ content: '<h1>Title</h1><p>End</p>',
42
+ })
43
+ }
44
+
45
+ const deleteTrailingParagraph = (meta?: { key: string; value: unknown }) => {
46
+ const headingSize = editor!.state.doc.firstChild!.nodeSize
47
+ const transaction = editor!.state.tr.delete(headingSize, editor!.state.doc.content.size)
48
+
49
+ if (meta) {
50
+ transaction.setMeta(meta.key, meta.value)
51
+ }
52
+
53
+ editor!.view.dispatch(transaction)
54
+ }
55
+
56
+ afterEach(() => {
57
+ if (editor) {
58
+ editor.destroy()
59
+ editor = null
60
+ }
61
+ })
62
+
63
+ it('adds a trailing paragraph when the document ends with a heading', () => {
64
+ createEditor()
65
+
66
+ deleteTrailingParagraph()
67
+
68
+ expect(editor!.getJSON()).toEqual(headingWithTrailingParagraphDocument)
69
+ })
70
+
71
+ it('skips trailing node insertion for unique id transactions', () => {
72
+ createEditor()
73
+
74
+ deleteTrailingParagraph({ key: '__uniqueIDTransaction', value: true })
75
+
76
+ expect(editor!.getJSON()).toEqual(headingDocument)
77
+ })
78
+
79
+ it('only skips trailing node insertion for the current transaction cycle', () => {
80
+ createEditor()
81
+
82
+ deleteTrailingParagraph({ key: skipTrailingNodeMeta, value: true })
83
+
84
+ expect(editor!.getJSON()).toEqual(headingDocument)
85
+
86
+ editor!.commands.setTextSelection(1)
87
+
88
+ expect(editor!.getJSON()).toEqual(headingWithTrailingParagraphDocument)
89
+ })
90
+ })