ProseMirror / prosemirror

The ProseMirror WYSIWYM editor
http://prosemirror.net/
MIT License
7.53k stars 334 forks source link

[prosemirror-view] Proposal: Add transformCopiedHTML callback #1447

Open ashu12chi opened 4 months ago

ashu12chi commented 4 months ago

Hi,

This is duplicate of this discussion.

We have custom styles that I want to append to the html content as <style> tag when it’s copied from editor. We also want to add a <meta> tag, so that it indicates that the source of content currently in clipboard is from my editor component along with other meta data.

We want a callback similar to transformPastedHTML which can have following signature:

transformCopiedHTML?: (this: P, html: HTMLDivElement, view: EditorView) => HTMLDivElement

This callback can be added here and it will help us in manipulating the copied HTML just before adding it to the clipboard without dealing with custom ClipboardSerializer.

marijnh commented 4 months ago

What is the problem with using a custom ClipboardSerializer?

ashu12chi commented 4 months ago

There are multiple reasons due to which we don't want to use custom ClipboardSerializer:

  1. Our use case is simple which requires the manipulation on the final generated HTML and we does not want to interfere with the default clipboard serialization of ProseMirror.
  2. The ProseMirror performs further operation on the obtained document fragment (ref), and it this final html that we want to transform before it’s passed to clipboard.

Additionally, the use case we want to handle here should be fairly common e.g. When we copy any content from MS Office apps like MS Word or MS Excel, it also add <style> and <meta> tags.

marijnh commented 4 months ago

Our use case is simple which requires the manipulation on the final generated HTML and we does not want to interfere with the default clipboard serialization of ProseMirror.

You can call to the default serializer, and then do your own transformations on the result.

The ProseMirror performs further operation on the obtained document fragment (ref), and it this final html that we want to transform before it’s passed to clipboard.

That just adds an attribute, one that you shouldn't be interfering with anyway.

Additionally, the use case we want to handle here should be fairly common e.g. When we copy any content from MS Office apps like MS Word or MS Excel, it also add