-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathmarkdownClipboard.spec.ts
More file actions
246 lines (205 loc) 路 6.54 KB
/
Copy pathmarkdownClipboard.spec.ts
File metadata and controls
246 lines (205 loc) 路 6.54 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { Editor } from '@tiptap/core'
import { Markdown } from '@tiptap/markdown'
import { AllSelection } from '@tiptap/pm/state'
import StarterKit from '@tiptap/starter-kit'
import { Marked } from 'marked'
import type { marked as markedDefault } from 'marked'
import { describe, expect, it } from 'vitest'
import { createLinkExtension } from '../../../../src/editor/extensions/link'
import { createMarkdownClipboardExtension } from '../../../../src/editor/extensions/markdownClipboard'
function createEditor(): Editor {
const marked = new Marked() as unknown as typeof markedDefault
return new Editor({
extensions: [
StarterKit.configure({ link: false }),
Markdown.configure({ marked }),
createMarkdownClipboardExtension(),
createLinkExtension()
]
})
}
function copySelectionAsText(editor: Editor) {
return editor.view.serializeForClipboard(editor.state.selection.content()).text
}
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 pasteClipboardEvent(editor: Editor, event: ClipboardEvent) {
editor.view.dom.dispatchEvent(event)
return event.defaultPrevented
}
function findTextRange(editor: Editor, text: string) {
let range: { from: number; to: number } | undefined
editor.state.doc.descendants((node, pos) => {
const index = node.text?.indexOf(text) ?? -1
if (index < 0) {
return true
}
range = { from: pos + index, to: pos + index + text.length }
return false
})
if (!range) {
throw new Error(`Text not found: ${text}`)
}
return range
}
function selectText(editor: Editor, text: string) {
editor.commands.setTextSelection(findTextRange(editor, text))
}
describe('markdown clipboard extension', () => {
it('copies selected rich editor content as markdown text', () => {
const editor = createEditor()
try {
editor.commands.insertContent(
'# Heading\n\nThis is **bold** and [linked](https://opencloud.eu).',
{ contentType: 'markdown' }
)
editor.view.dispatch(editor.state.tr.setSelection(new AllSelection(editor.state.doc)))
const clipboardText = copySelectionAsText(editor)
expect(clipboardText).toContain('# Heading')
expect(clipboardText).toContain('**bold**')
expect(clipboardText).toContain('[linked](https://opencloud.eu)')
} finally {
editor.destroy()
}
})
it('copies selected text inside a list item with the list marker', () => {
const editor = createEditor()
try {
editor.commands.setContent({
type: 'doc',
content: [
{
type: 'bulletList',
content: [
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [{ type: 'text', text: 'lorem ipsum' }]
}
]
}
]
}
]
})
selectText(editor, 'ipsum')
const clipboardText = copySelectionAsText(editor)
expect(clipboardText.trim()).toBe('- ipsum')
} finally {
editor.destroy()
}
})
it('copies selected marked text inside a list item with the list marker', () => {
const editor = createEditor()
try {
editor.commands.setContent({
type: 'doc',
content: [
{
type: 'bulletList',
content: [
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'lorem ipsum',
marks: [{ type: 'bold' }]
}
]
}
]
}
]
}
]
})
selectText(editor, 'ipsum')
const clipboardText = copySelectionAsText(editor)
expect(clipboardText.trim()).toBe('- **ipsum**')
} finally {
editor.destroy()
}
})
it('copies selected text inside a code block with code fences', () => {
const editor = createEditor()
try {
editor.commands.setContent({
type: 'doc',
content: [
{
type: 'codeBlock',
attrs: { language: null },
content: [{ type: 'text', text: 'const value = 1' }]
}
]
})
selectText(editor, 'value')
const clipboardText = copySelectionAsText(editor)
expect(clipboardText.trim()).toBe('```\nvalue\n```')
} finally {
editor.destroy()
}
})
it('pastes markdown text as structured editor content', () => {
const editor = createEditor()
try {
const handled = editor.view.pasteText('# Heading\n\nThis is **bold**.')
expect(handled).toBe(true)
expect(editor.state.doc.firstChild?.type.name).toBe('heading')
expect(editor.markdown?.serialize(editor.getJSON())).toContain('# Heading')
expect(editor.markdown?.serialize(editor.getJSON())).toContain('**bold**')
} finally {
editor.destroy()
}
})
it('pastes markdown text instead of html when both clipboard formats exist', () => {
const editor = createEditor()
const event = createClipboardEvent(
'# Heading\n\nThis is **bold**.',
'<html><body><p>Plain html content</p></body></html>'
)
try {
const handled = pasteClipboardEvent(editor, event)
expect(handled).toBe(true)
expect(editor.state.doc.firstChild?.type.name).toBe('heading')
expect(editor.markdown?.serialize(editor.getJSON())).toContain('# Heading')
expect(editor.markdown?.serialize(editor.getJSON())).toContain('**bold**')
} finally {
editor.destroy()
}
})
it('does not paste into a read-only editor', () => {
const editor = createEditor()
const event = createClipboardEvent('# Heading', '')
try {
editor.commands.setContent('original', { contentType: 'markdown' })
editor.setEditable(false)
const handled = pasteClipboardEvent(editor, event)
expect(handled).toBe(false)
expect(editor.markdown?.serialize(editor.getJSON()).trim()).toBe('original')
} finally {
editor.destroy()
}
})
})