Add an argument to transformPasted indicating if paste is plain text #183

Merged
ocavue merged 2 commits from ocavue/transformPasted-plain into master 2025-08-15 13:29:08 +02:00
ocavue commented 2025-08-14 15:27:44 +02:00 (Migrated from github.com)

This PR introduces a new argument plain: boolean to the transformPasted⁠ callback, indicating whether the pasted content should be treated as plain text.

The existing transformPastedText and clipboardTextParser also include this plain flag. However, these two functions have the argument text: string, while transformPasted does not. Therefore, I'm not entirely sure if the new API added in this PR aligns with the convention.

In my case, I'd want to perform different transformations depending on whether the Shift key is held down while pasting. While I can add a custom keydown handler myself, it would be much more convenient if ProseMirror could simply export the plain information.

This PR introduces a new argument `plain: boolean` to the [`transformPasted`](https://prosemirror.net/docs/ref/#view.EditorProps.transformPasted)⁠ callback, indicating whether the pasted content should be treated as plain text. The existing [`transformPastedText`](https://prosemirror.net/docs/ref/#view.EditorProps.transformPastedText) and [`clipboardTextParser`](https://prosemirror.net/docs/ref/#view.EditorProps.clipboardTextParser) also include this `plain` flag. However, these two functions have the argument `text: string`, while `transformPasted` does not. Therefore, I'm not entirely sure if the new API added in this PR aligns with the convention. In my case, I'd want to perform different transformations depending on whether the `Shift` key is held down while pasting. While I can add a custom keydown handler myself, it would be much more convenient if ProseMirror could simply export the `plain` information.
marijnh (Migrated from github.com) reviewed 2025-08-15 12:17:20 +02:00
@ -101,7 +101,7 @@ export function parseFromClipboard(view: EditorView, text: string, html: string
}
marijnh (Migrated from github.com) commented 2025-08-15 12:17:20 +02:00

I think it would be more correct to pass asText here, since that reflects whether the content is actually treated as plain text, not just whether shift is held. Does that make sense?

I think it would be more correct to pass `asText` here, since that reflects whether the content is actually treated as plain text, not just whether shift is held. Does that make sense?
ocavue (Migrated from github.com) reviewed 2025-08-15 13:00:07 +02:00
@ -101,7 +101,7 @@ export function parseFromClipboard(view: EditorView, text: string, html: string
}
ocavue (Migrated from github.com) commented 2025-08-15 13:00:06 +02:00

I've switched to using asText in commit https://github.com/ProseMirror/prosemirror-view/pull/183/commits/85255cde184f2695c6fcf5d34a3530e8cdd12ba7.

There are very subtle differences between asText and plainText, so I believe it's fine to make the switch. The only situation where these two variables differ, IMHO, is when pasting something into a code block without holding the Shift key. In this case, plainText is false, and asText is true. However, the function parseFromClipboard would easily return at line 50 (code link), meaning that transformPasted on line 104 (code link) won't be called at all, so it doesn't matter which variable we pass in this case.

I've switched to using `asText` in commit https://github.com/ProseMirror/prosemirror-view/pull/183/commits/85255cde184f2695c6fcf5d34a3530e8cdd12ba7. There are very subtle differences between `asText` and `plainText`, so I believe it's fine to make the switch. The only situation where these two variables differ, IMHO, is when pasting something into a code block without holding the `Shift` key. In this case, `plainText` is false, and `asText` is true. However, the function `parseFromClipboard` would easily return at line 50 ([code link](https://github.com/ProseMirror/prosemirror-view/blob/1.40.1/src/clipboard.ts#L50)), meaning that `transformPasted` on line 104 ([code link](https://github.com/ProseMirror/prosemirror-view/blob/1.40.1/src/clipboard.ts#L104)) won't be called at all, so it doesn't matter which variable we pass in this case.
ocavue (Migrated from github.com) reviewed 2025-08-15 13:04:59 +02:00
@ -101,7 +101,7 @@ export function parseFromClipboard(view: EditorView, text: string, html: string
}
ocavue (Migrated from github.com) commented 2025-08-15 13:04:58 +02:00

I'm not sure if the behavior of transformPasted not being called when pasting into a code block is a bug or intentional design.

I'm not sure if the behavior of _`transformPasted` not being called when pasting into a code block_ is a bug or intentional design.
marijnh commented 2025-08-15 13:50:00 +02:00 (Migrated from github.com)

The only situation where these two variables differ

There's also the situation where there's text on the clipboard but no HTML.

Not calling transformPasted in the code block case does sound like a bug. See patch ad55444.

> The only situation where these two variables differ There's also the situation where there's text on the clipboard but no HTML. Not calling `transformPasted` in the code block case does sound like a bug. See patch ad55444.
ocavue commented 2025-08-15 14:00:51 +02:00 (Migrated from github.com)

Thanks for the review and merge!

Thanks for the review and merge!
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
prosemirror/prosemirror-view!183
No description provided.