codex-team / codex-team/editor.js

[Bug] Order of events is wrong for paste

Open
#2,065 6 comments 7 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
31.9k
Forks
2.2k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

`onChange` event is fired before pasted content becomes visible through `editor.save()`.

Steps to reproduce:
1. Create an `editor.onChange` handler that prints the results of `await editor.save()`
2. Paste multiple paragraphs into the editor
3. Notice how the last paragraph is missing from the saved state

Expected behavior:

All paragraphs are present.

Screenshots:

N/A

Device, Browser, OS:

N/A

Editor.js version:
`next` from GitHub but the bug is also present in the current live demo page

Plugins you use with their versions:

Default plugins from `example-dev.html`

------

Here's what's happening:

1. When content is pasted, `handlePasteEvent` is called in the `Paste` component.
2. This in turn calls `processText`
3. Since multiple paragraphs are inserted, they are mapped to `insertBlock` calls
1. Each one calls `paste` in the `BlockManager`
2. `paste` first calls `insert` on the `BlockManager`
a. At this point an empty block is created and inserted into DOM
b. `blockDidMutated` is triggered
c. This in turn leads to the `onChange` handler being called
d. Since the block is still empty, the Paragraph tool [returns nothing](https://github.com/editor-js/paragraph/blob/21cbdea6e5e61094b046f47e8cb423a817cec3ed/src/index.js#L208-L214) from its `data` getter
3. `paste` then invokes `block.call(BlockToolAPI.ON_PASTE, pasteEvent)`
a. This [sets proper data](https://github.com/editor-js/paragraph/blob/21cbdea6e5e61094b046f47e8cb423a817cec3ed/src/index.js#L224-L228) on the newly inserted paragraph
b. `blockDidMutated` is not retriggered so `onChange` signal is not invoked

I think it would be cleaner if tool's `onPaste` was called before calling `BlockManager.insert` and if it returned the initial `data` to pass to `BlockManager.insert`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.