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

fix(core): onDrop and onPaste only registered when defined #5681 #5684

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
17 changes: 6 additions & 11 deletions packages/core/src/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import { CommandManager } from './CommandManager.js'
import { EventEmitter } from './EventEmitter.js'
import { ExtensionManager } from './ExtensionManager.js'
import {
ClipboardTextSerializer, Commands, Editable, FocusEvents, Keymap, Tabindex,
ClipboardTextSerializer, Commands, Drop, Editable, FocusEvents, Keymap, Paste,
Tabindex,
} from './extensions/index.js'
import { createDocument } from './helpers/createDocument.js'
import { getAttributes } from './helpers/getAttributes.js'
Expand All @@ -24,8 +25,6 @@ import { isActive } from './helpers/isActive.js'
import { isNodeEmpty } from './helpers/isNodeEmpty.js'
import { resolveFocusPosition } from './helpers/resolveFocusPosition.js'
import { NodePos } from './NodePos.js'
import { DropPlugin } from './plugins/DropPlugin.js'
import { PastePlugin } from './plugins/PastePlugin.js'
import { style } from './style.js'
import {
CanCommands,
Expand Down Expand Up @@ -112,14 +111,8 @@ export class Editor extends EventEmitter<EditorEvents> {
this.on('focus', this.options.onFocus)
this.on('blur', this.options.onBlur)
this.on('destroy', this.options.onDestroy)

if (this.options.onPaste) {
this.registerPlugin(PastePlugin(this.options.onPaste))
}

if (this.options.onDrop) {
this.registerPlugin(DropPlugin(this.options.onDrop))
}
this.on('drop', ({ event, slice, moved }) => this.options.onDrop(event, slice, moved))
this.on('paste', ({ event, slice }) => this.options.onPaste(event, slice))
Comment on lines +114 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I figured, that while I was here, I may as well promote these to be actual editor events that follow the same pattern as others. This allows flexibility to register multiple handlers to listen to the same event

Copy link
Contributor

Choose a reason for hiding this comment

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

@nperez0111 This resolves this, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@svenadlung it does 😉


window.setTimeout(() => {
if (this.isDestroyed) {
Expand Down Expand Up @@ -279,6 +272,8 @@ export class Editor extends EventEmitter<EditorEvents> {
FocusEvents,
Keymap,
Tabindex,
Drop,
Paste,
].filter(ext => {
if (typeof this.options.enableCoreExtensions === 'object') {
return this.options.enableCoreExtensions[ext.name as keyof typeof this.options.enableCoreExtensions] !== false
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/extensions/drop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Plugin, PluginKey } from '@tiptap/pm/state'

import { Extension } from '../Extension.js'

export const Drop = Extension.create({
name: 'drop',

addProseMirrorPlugins() {
return [
new Plugin({
key: new PluginKey('tiptapDrop'),

props: {
handleDrop: (_, e, slice, moved) => {
this.editor.emit('drop', {
editor: this.editor, event: e, slice, moved,
})
},
},
}),
]
},
})
2 changes: 2 additions & 0 deletions packages/core/src/extensions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
export { ClipboardTextSerializer } from './clipboardTextSerializer.js'
export { Commands } from './commands.js'
export { Drop } from './drop.js'
export { Editable } from './editable.js'
export { FocusEvents } from './focusEvents.js'
export { Keymap } from './keymap.js'
export { Paste } from './paste.js'
export { Tabindex } from './tabindex.js'
26 changes: 26 additions & 0 deletions packages/core/src/extensions/paste.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Plugin, PluginKey } from '@tiptap/pm/state'

import { Extension } from '../Extension.js'

export const Paste = Extension.create({
name: 'paste',

addProseMirrorPlugins() {

return [
new Plugin({
key: new PluginKey('tiptapPaste'),

props: {
handlePaste: (_view, e, slice) => {
this.editor.emit('paste', {
editor: this.editor,
event: e,
slice,
})
},
},
}),
]
},
})
2 changes: 0 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export * from './NodePos.js'
export * from './NodeView.js'
export * from './PasteRule.js'
export * from './pasteRules/index.js'
export * from './plugins/DropPlugin.js'
export * from './plugins/PastePlugin.js'
export * from './Tracker.js'
export * from './types.js'
export * from './utilities/index.js'
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/plugins/DropPlugin.ts

This file was deleted.

14 changes: 0 additions & 14 deletions packages/core/src/plugins/PastePlugin.ts

This file was deleted.

6 changes: 5 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export interface EditorEvents {
focus: { editor: Editor; event: FocusEvent; transaction: Transaction };
blur: { editor: Editor; event: FocusEvent; transaction: Transaction };
destroy: void;
paste: { editor: Editor; event: ClipboardEvent; slice: Slice };
drop: { editor: Editor; event: DragEvent; slice: Slice; moved: boolean };
}

export type EnableRules = (AnyExtension | string)[] | boolean;
Expand Down Expand Up @@ -110,7 +112,9 @@ export interface EditorOptions {
| 'commands'
| 'focusEvents'
| 'keymap'
| 'tabindex',
| 'tabindex'
| 'drop'
| 'paste',
false
>
>;
Expand Down