trix icon indicating copy to clipboard operation
trix copied to clipboard

Content Not Refreshed Correctly for pasteEventHasPlainTextOnly Special Case

Open chadrschroeder opened this issue 1 year ago • 3 comments

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

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 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? 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 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

chadrschroeder avatar Oct 19 '23 19:10 chadrschroeder

If it helps any, the issue appears to be fixed if I change this statement from render() to requestRender() which matches what is done here.

chadrschroeder avatar Oct 19 '23 21:10 chadrschroeder

@chadrschroeder any workaround until this get fixed? Is there a way to manually call requestRender after trix-paste event is called?

danielnc avatar Apr 12 '24 00:04 danielnc

Unfortunately, I don't think there's a clean way to work around this other than to serve a customized version of the Trix JavaScript where the Trix.controllers.Level2InputController.events.paste function has been adjusted. It would be nice if https://github.com/basecamp/trix/pull/1107 were reviewed.

chadrschroeder avatar Apr 12 '24 13:04 chadrschroeder