Skip to content

Commit 75c3c77

Browse files
authored
Add an argument to transformPasted indicating if paste is plain text
FEATURE: Pass an argument to `transformPasted` that tells the handler whether the paste is plain text.
1 parent f56c444 commit 75c3c77

File tree

3 files changed

+6
-5
lines changed

3 files changed

+6
-5
lines changed

src/clipboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export function parseFromClipboard(view: EditorView, text: string, html: string
4444
let inCode = $context.parent.type.spec.code
4545
let dom: HTMLElement | undefined, slice: Slice | undefined
4646
if (!html && !text) return null
47-
let asText = text && (plainText || inCode || !html)
47+
let asText: boolean = !!text && (plainText || inCode || !html)
4848
if (asText) {
4949
view.someProp("transformPastedText", f => { text = f(text, inCode || plainText, view) })
5050
if (inCode) return text ? new Slice(Fragment.from(view.state.schema.text(text.replace(/\r\n?/g, "\n"))), 0, 0) : Slice.empty
@@ -101,7 +101,7 @@ export function parseFromClipboard(view: EditorView, text: string, html: string
101101
}
102102
}
103103

104-
view.someProp("transformPasted", f => { slice = f(slice!, view) })
104+
view.someProp("transformPasted", f => { slice = f(slice!, view, asText) })
105105
return slice
106106
}
107107

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,9 @@ export interface EditorProps<P = any> {
714714
clipboardTextParser?: (this: P, text: string, $context: ResolvedPos, plain: boolean, view: EditorView) => Slice
715715

716716
/// Can be used to transform pasted or dragged-and-dropped content
717-
/// before it is applied to the document.
718-
transformPasted?: (this: P, slice: Slice, view: EditorView) => Slice
717+
/// before it is applied to the document. The `plain` flag will be
718+
/// true when the text is pasted as plain text.
719+
transformPasted?: (this: P, slice: Slice, view: EditorView, plain: boolean) => Slice
719720

720721
/// Can be used to transform copied or cut content before it is
721722
/// serialized to the clipboard.

src/input.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ editHandlers.drop = (view, _event) => {
718718
let $mouse = view.state.doc.resolve(eventPos.pos)
719719
let slice = dragging && dragging.slice
720720
if (slice) {
721-
view.someProp("transformPasted", f => { slice = f(slice!, view) })
721+
view.someProp("transformPasted", f => { slice = f(slice!, view, false) })
722722
} else {
723723
slice = parseFromClipboard(view, getText(event.dataTransfer),
724724
brokenClipboardAPI ? null : event.dataTransfer.getData("text/html"), false, $mouse)

0 commit comments

Comments
 (0)