-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathplainTextClipboard.spec.ts
More file actions
182 lines (152 loc) 路 4.73 KB
/
Copy pathplainTextClipboard.spec.ts
File metadata and controls
182 lines (152 loc) 路 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Editor } from '@tiptap/core'
import { AllSelection } from '@tiptap/pm/state'
import StarterKit from '@tiptap/starter-kit'
import { describe, expect, it } from 'vitest'
import { createPlainTextClipboardExtension } from '../../../../src/editor/extensions/plainTextClipboard'
function createEditor(): Editor {
return new Editor({
extensions: [
StarterKit.configure({
blockquote: false,
bold: false,
bulletList: false,
code: false,
codeBlock: false,
heading: false,
italic: false,
link: false,
listItem: false,
listKeymap: false,
orderedList: false,
strike: false
}),
createPlainTextClipboardExtension()
]
})
}
function createClipboardEvent(text: string, html: string): ClipboardEvent {
const event = new Event('paste', { bubbles: true, cancelable: true }) as ClipboardEvent
Object.defineProperty(event, 'clipboardData', {
value: {
getData: (type: string) => {
if (type === 'text/plain') {
return text
}
if (type === 'text/html') {
return html
}
return ''
}
}
})
return event
}
function copyAll(editor: Editor) {
editor.view.dispatch(editor.state.tr.setSelection(new AllSelection(editor.state.doc)))
return editor.view.serializeForClipboard(editor.state.selection.content()).text
}
function getLines(editor: Editor) {
const lines: string[] = []
editor.state.doc.forEach((node) => lines.push(node.textContent))
return lines
}
describe('plain text clipboard extension', () => {
it('pastes the text/plain payload instead of html when both clipboard formats exist', () => {
const editor = createEditor()
const event = createClipboardEvent(
'# Heading\n\n- item **bold**',
'<h1>Heading</h1><ul><li><p>item <strong>bold</strong></p></li></ul>'
)
try {
editor.view.dom.dispatchEvent(event)
expect(event.defaultPrevented).toBe(true)
expect(getLines(editor)).toEqual(['# Heading', '', '- item **bold**'])
} finally {
editor.destroy()
}
})
it('keeps empty lines as empty paragraphs', () => {
const editor = createEditor()
try {
editor.view.pasteText('first\n\n\nsecond')
expect(getLines(editor)).toEqual(['first', '', '', 'second'])
} finally {
editor.destroy()
}
})
it('pastes a single line inline into the current paragraph', () => {
const editor = createEditor()
try {
editor.commands.setContent({
type: 'doc',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'hello ' }] }]
})
editor.commands.focus('end')
editor.view.pasteText('world')
expect(getLines(editor)).toEqual(['hello world'])
} finally {
editor.destroy()
}
})
it('falls back to the default paste handling when no text/plain payload exists', () => {
const editor = createEditor()
const event = createClipboardEvent('', '<p>html only</p>')
try {
editor.view.dom.dispatchEvent(event)
expect(getLines(editor)).toEqual(['html only'])
} finally {
editor.destroy()
}
})
it('does not paste into a read-only editor', () => {
const editor = createEditor()
const event = createClipboardEvent('pasted', '')
try {
editor.commands.setContent({
type: 'doc',
content: [{ type: 'paragraph', content: [{ type: 'text', text: 'original' }] }]
})
editor.setEditable(false)
editor.view.dom.dispatchEvent(event)
expect(event.defaultPrevented).toBe(false)
expect(getLines(editor)).toEqual(['original'])
} finally {
editor.destroy()
}
})
it('copies one line per paragraph, including empty ones and hard breaks', () => {
const editor = createEditor()
try {
editor.commands.setContent({
type: 'doc',
content: [
{
type: 'paragraph',
content: [
{ type: 'text', text: 'first' },
{ type: 'hardBreak' },
{ type: 'text', text: 'second' }
]
},
{ type: 'paragraph' },
{ type: 'paragraph', content: [{ type: 'text', text: 'third' }] }
]
})
expect(copyAll(editor)).toBe('first\nsecond\n\nthird')
} finally {
editor.destroy()
}
})
it('round-trips copied content without changing the lines', () => {
const editor = createEditor()
try {
editor.view.pasteText('first\n\nsecond\nthird')
const clipboardText = copyAll(editor)
editor.view.pasteText(clipboardText)
expect(clipboardText).toBe('first\n\nsecond\nthird')
expect(getLines(editor)).toEqual(['first', '', 'second', 'third'])
} finally {
editor.destroy()
}
})
})