Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added transformCopiedHTML callback #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export function serializeForClipboard(view: EditorView, slice: Slice) {
let text = view.someProp("clipboardTextSerializer", f => f(slice, view)) ||
slice.content.textBetween(0, slice.content.size, "\n\n")

// Call transformCopiedHTML callback to allow user to manipulate the HTML content before passing to clipboard
wrap = view.someProp("transformCopiedHTML", f => f(wrap!, view)) || wrap

return {dom: wrap, text}
}

Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ export interface EditorProps<P = any> {
/// for example to clean it up.
transformPastedHTML?: (this: P, html: string, view: EditorView) => string

/// Can be used to transform copied HTML, _before_ it is,
/// passed to the clipboard.
transformCopiedHTML?: (this: P, html: HTMLDivElement, view: EditorView) => HTMLDivElement

/// The [parser](#model.DOMParser) to use when reading content from
/// the clipboard. When not given, the value of the
/// [`domParser`](#view.EditorProps.domParser) prop is used.
Expand Down
10 changes: 10 additions & 0 deletions test/webtest-clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ describe("Clipboard interface", () => {
new Slice(p("abc").content, 0, 0), eq)
})

it("will call transformCopiedHTML", () => {
let d = doc(blockquote(blockquote(p("foo"))))
let view = tempEditor({doc: d, transformCopiedHTML(html) {
html.innerHTML = '<p>bar</p>'
return html
},})
let {dom} = serializeForClipboard(view,new Slice(doc(p("foo")).content, 1, 0), eq)
ist(dom.innerHTML, '<p>bar</p>')
})

it("will call transformPastedText", () => {
let view = tempEditor({transformPastedText(_) { return "abc" }})
ist(parseFromClipboard(view, "def", null, false, view.state.doc.resolve(1)),
Expand Down