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

feature(core): add onPaste and onDrop events to editor #4843

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

bdbch
Copy link
Contributor

@bdbch bdbch commented Jan 31, 2024

Please describe your changes

This message on our Discord server made me realize that there's apparently no easy way to just add a simple paste or drag event handler to the actual editor instance. This user tried to add it to the editors DOM element which works sometimes (doesn't work properly if there's no content inside the contenteditable yet).

How did you accomplish your changes

I created two very simple plugins to handle paste and drop events passing up their relative events to the option passed through.

Those can be configured on the editor constructor.

How have you tested your changes

I tried to add onPaste and onDrop on a local demo.

How can we verify your changes

See above

Remarks

No remarks from my side

Checklist

  • The changes are not breaking the editor
  • Added tests where possible
  • Followed the guidelines
  • Fixed linting issues

Related issues

No related issues

@bdbch bdbch self-assigned this Jan 31, 2024
Copy link

netlify bot commented Jan 31, 2024

Deploy Preview for tiptap-embed ready!

Name Link
🔨 Latest commit caa5f99
🔍 Latest deploy log https://app.netlify.com/sites/tiptap-embed/deploys/65ba09d73ef13c000890ca11
😎 Deploy Preview https://deploy-preview-4843--tiptap-embed.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.


export const DropPlugin = (onPaste: (e: DragEvent) => void) => {
return new Plugin({
key: new PluginKey('tiptap__drop'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe keep this in lowerCamelCase (tiptapDrop), like we do in other plugins?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe call it dropEvent, analog to focusEvents?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about tiptap__dropEvent - I'm using the tiptap__ prefix to make sure to never accidentially conflict with other pluginKeys

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PluginKeys are symbols by default no? So this should only ever exist once.


export const PastePlugin = (onPaste: (e: ClipboardEvent) => void) => {
return new Plugin({
key: new PluginKey('tiptap__paste'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about tiptap__pasteEvent - I'm using the tiptap__ prefix to make sure to never accidentially conflict with other pluginKeys

@@ -11,6 +11,8 @@ export * from './NodePos.js'
export * from './NodeView.js'
export * from './PasteRule.js'
export * from './pasteRules/index.js'
export * from './plugins/DropPlugin.js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep it in extensions folder, just like extensions/focusEvents.ts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I just realized focusEvents is a wrapped plugin (as a Tiptap extension). But maybe we keep it the same style?

Copy link
Contributor

@svenadlung svenadlung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check my comments.


props: {
handleDrop: (_, e) => {
onPaste(e)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
onPaste(e)
onDrop(e)

@bdbch bdbch changed the base branch from main to develop June 26, 2024 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Ready to Merge
Development

Successfully merging this pull request may close these issues.

None yet

3 participants