-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from aguingand/parse-clipboard
Handle pasting / copying markdown
- Loading branch information
Showing
8 changed files
with
168 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { Editor } from "@tiptap/core"; | ||
import StarterKit from "@tiptap/starter-kit"; | ||
import { Markdown } from "../src"; | ||
import { clipboardEvent } from "./utils/dom"; | ||
|
||
|
||
describe('clipboard', () => { | ||
describe('paste', () => { | ||
test('transform', () => { | ||
const editor = new Editor({ | ||
extensions: [ | ||
StarterKit, | ||
Markdown.configure({ | ||
transformPastedText: true, | ||
}), | ||
], | ||
}); | ||
|
||
const event = clipboardEvent('paste'); | ||
event.clipboardData.setData('text/plain', `# My title`); | ||
|
||
editor.view.dom.dispatchEvent(event); | ||
|
||
expect(editor.getHTML()).toContain('<h1>My title</h1>') | ||
}); | ||
|
||
test('does not transform', () => { | ||
const editor = new Editor({ | ||
extensions: [ | ||
StarterKit, | ||
Markdown.configure({ | ||
transformPastedText: false, | ||
}), | ||
], | ||
}); | ||
|
||
const event = clipboardEvent('paste'); | ||
event.clipboardData.setData('text/plain', `# My title`); | ||
|
||
editor.view.dom.dispatchEvent(event); | ||
|
||
expect(editor.getHTML()).not.toContain('<h1>My title</h1>') | ||
}); | ||
}); | ||
|
||
describe('copy', () => { | ||
test('transform', () => { | ||
const editor = new Editor({ | ||
content: '# My title', | ||
extensions: [ | ||
StarterKit, | ||
Markdown.configure({ | ||
transformCopiedText: true, | ||
}), | ||
], | ||
}); | ||
|
||
const event = clipboardEvent('copy'); | ||
|
||
editor.commands.selectAll(); | ||
editor.view.dom.dispatchEvent(event); | ||
|
||
expect(event.clipboardData.getData('text/plain')).toBe('# My title'); | ||
}); | ||
|
||
test('does not transform', () => { | ||
const editor = new Editor({ | ||
content: '# My title', | ||
extensions: [ | ||
StarterKit, | ||
Markdown.configure({ | ||
transformCopiedText: false, | ||
}), | ||
], | ||
}); | ||
|
||
const event = clipboardEvent('copy'); | ||
|
||
editor.commands.selectAll(); | ||
editor.view.dom.dispatchEvent(event); | ||
|
||
expect(event.clipboardData.getData('text/plain')).toBe('My title'); | ||
}); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
|
||
export function clipboardEvent(name) { | ||
const clipboardData = { | ||
data: {}, | ||
getData(format) { return this.data[format] }, | ||
setData(format, content) { return this.data[format] = content }, | ||
clearData(format) { delete this.data[format] }, | ||
}; | ||
const event = new Event(name); | ||
event.clipboardData = clipboardData; | ||
|
||
return event; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
document.createRange = () => { | ||
return Object.assign(new Range(), { | ||
getClientRects: () => [], | ||
getBoundingClientRect: () => ({}), | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Extension } from "@tiptap/core"; | ||
import { Plugin, PluginKey } from '@tiptap/pm/state'; | ||
import { DOMParser } from '@tiptap/pm/model'; | ||
import { elementFromString } from "../../util/dom"; | ||
|
||
export const MarkdownClipboard = Extension.create({ | ||
name: 'markdownClipboard', | ||
addOptions() { | ||
return { | ||
transformPastedText: false, | ||
transformCopiedText: false, | ||
} | ||
}, | ||
addProseMirrorPlugins() { | ||
return [ | ||
new Plugin({ | ||
key: new PluginKey('markdownClipboard'), | ||
props: { | ||
clipboardTextParser: (text, context, plainText) => { | ||
if(plainText || !this.options.transformPastedText) { | ||
return null; // pasting with shift key prevents formatting | ||
} | ||
const parsed = this.editor.storage.markdown.parser.parse(text, { inline: true }); | ||
return DOMParser.fromSchema(this.editor.schema) | ||
.parseSlice(elementFromString(parsed), { preserveWhitespace: true }); | ||
}, | ||
/** | ||
* @param {import('prosemirror-model').Slice} slice | ||
*/ | ||
clipboardTextSerializer: (slice) => { | ||
if(!this.options.transformCopiedText) { | ||
return null; | ||
} | ||
return this.editor.storage.markdown.serializer.serialize(slice.content); | ||
}, | ||
}, | ||
}) | ||
] | ||
} | ||
}) |