basecamp / basecamp/trix

Content Not Refreshed Correctly for pasteEventHasPlainTextOnly Special Case

Open
#1,103 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
20k
Forks
1.1k
Avg merge
3d 12h
Merged PRs (30d)
13

Description

Say I create a paste handler that is going to change the incoming text like:

```javascript
elem = document.querySelector('trix-editor')
elem.addEventListener('trix-paste', function() {
length = elem.editor.getDocument().toString().length
elem.editor.setSelectedRange([0, length])
elem.editor.activateAttribute('bold')
})
```

If I paste text from an application like Microsoft Word, the `clipboardData` types will include "text/plain", "text/html", "text/rtf" and "Files". The Trix editor will show the text in bold after pasting.

If I paste text from a plain text editor like TextEdit, the `clipboardData` types will only include "text/plain". We'll get into the special [`pasteEventHasPlainTextOnly`](https://github.com/basecamp/trix/blob/main/src/trix/controllers/level_2_input_controller.js#L48) handling. The Trix editor won't show the text in bold after pasting. It will switch the pasted content to bold if you type another character into the editor which seems to indicate that this special handling isn't correctly refreshing the editor after the `trix-paste` event is done.

This doesn't affect Trix version 1.3.1 that is running at https://trix-editor.org/.

Do we still need the workaround that was added back in 2019 [here](https://github.com/basecamp/trix/commit/f01ac664f3c0e31be9279d2a2b35d2594717e3ed)? The code comment near the `pasteEventHasPlainTextOnly` handling says:

```
// Handle paste event to work around beforeinput.insertFromPaste browser bugs.
// Safe to remove each condition once fixed upstream.
```

The Chromium [bug report](https://bugs.chromium.org/p/chromium/issues/detail?id=934448) is now marked as fixed. Can the workaround be removed now since it's breaking the paste behavior for plain text editors?

##### Details

* Trix version: 2.0.5

Contributor guide

No contributing guide indexed for this repository

Research direction

Start at src/trix/controllers/level_2_input_controller.js, especially the pasteEventHasPlainTextOnly handling, and reproduce issue 1103 with a plain-text paste followed by the shown trix-paste handler. Compare the refresh behavior with rich clipboard input and Trix 1.3.1; done means the bold selection is visible immediately after pasting without requiring another typed character.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.