fix/feat: dispatch clipboard events without active selection #1190
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What:
Make the
copy()
andcut()
functions more compatible with actual browser behaviour by always dispatching the respective events with an emptyclipboardData
event member and only adding the default behaviour iff the event was notpreventDefault()
ed.Why:
At $WORK we are implementing custom copy and paste handlers on elements that are neither editable in the classic sense (e.g. not an
<input>
element) nor require classic selection (marking text with the cursor).These handlers convert a rich datastructure to different MIME types and add the data to the event's
clipboardData
. When pasting in our web application, we can read a custom MIME type and restore the rich data structure. When pasting externally to an application that only understands text, a reduced text version is pasted.This works perfectly in browsers!
Without this PR,
user.copy()
does not dispatch a copy event, because it only dispatches the event if there is text selected. This is not in conformance with browser behavior and also not in conformance with the Clipboard API and events spec.How:
This pull request changes the implementation of
copy()
andpaste()
functions according to the Clipboard API and events spec.Specifically, the following changes were made:
copy
/cut
event, regardless of selection state.clipboardData
property is empty and can be filled by event listenerse.preventDefault()
and only their content is written to the clipboard).Checklist:
Additional Notes:
Unfortunately, this breaks existing behaviour :-/
I doubt users rely on this, because it does not match the browser's behavior and relying that the event is not dispatched if nothing is selected or that it has the current selection content already set would break the event handlers in a real environment. Still I want to mention that this is not really backwards compatible.