Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Add onBeforeInput listener #710

Merged
merged 1 commit into from
Jun 8, 2023
Merged

Conversation

artcodespace
Copy link
Contributor

@artcodespace artcodespace commented Jun 8, 2023

To handle a special case in element web (regarding pasting stuff from an iPhone to a Mac, see issue element-hq/element-web#25327) we require a before input listener to be added to the rich text editor.

This PR adds that listener and ensures it passes the event through when required to handle the case listed in the issue above. This listener will be required anyway to implement IME behaviour correctly (ie allowing composition of characters in certain foreign languages)

@artcodespace artcodespace requested a review from a team June 8, 2023 12:40
@sonarqubecloud
Copy link

sonarqubecloud bot commented Jun 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Comment on lines +126 to +142
// Can be called by onPaste or onBeforeInput
const onPaste = (e: ClipboardEvent | InputEvent) => {
// this is required to handle edge case image pasting in Safari, see
// https://github.com/vector-im/element-web/issues/25327
const isSpecialCaseInputEvent =
isInputEvent(e) &&
e.inputType === 'insertFromPaste' &&
e.dataTransfer !== null;

const isEventToHandle =
isClipboardEvent(e) || isSpecialCaseInputEvent;

if (isEventToHandle) {
e.preventDefault();
e.stopPropagation();

_handleInput(e);
Copy link
Contributor Author

@artcodespace artcodespace Jun 8, 2023

Choose a reason for hiding this comment

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

Before we only used to call this when an event occurred and returned early (ie did nothing) if it was not an event from the clipboard.

Now we need to handle a special case as a clipboard event, so the check has become "check if it is a clipboard event or the special case, and if it's either of those, do stuff. Otherwise, do nothing"

@@ -185,6 +199,7 @@ export function useListeners(
editorNode.removeEventListener('paste', onPaste);
editorNode.removeEventListener('wysiwygInput', onWysiwygInput);
editorNode.removeEventListener('keydown', onKeyDown);
editorNode.removeEventListener('beforeinput', onBeforeInput);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These lines create a reference to the onPaste function which will be called whenever a beforeInput event occurs. The addEventListener call attaches that function when the composer is created, the removeEventListener call removes it when the composer is removed from the page

@codecov-commenter
Copy link

Codecov Report

Patch coverage has no change and project coverage change: +1.50 🎉

Comparison is base (cfda115) 88.28% compared to head (22d1916) 89.79%.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #710      +/-   ##
============================================
+ Coverage     88.28%   89.79%   +1.50%     
============================================
  Files           145       79      -66     
  Lines         16652    13932    -2720     
  Branches        769        0     -769     
============================================
- Hits          14702    12510    -2192     
+ Misses         1765     1422     -343     
+ Partials        185        0     -185     
Flag Coverage Δ
uitests ?
uitests-android ?
uitests-ios ?
unittests 89.79% <ø> (+1.98%) ⬆️
unittests-android ?
unittests-ios ?
unittests-rust 89.79% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 66 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@aringenbach aringenbach left a comment

Choose a reason for hiding this comment

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

LGTM

@artcodespace artcodespace merged commit ce80769 into main Jun 8, 2023
@artcodespace artcodespace deleted the alunturner/add-beforeinput-listener branch June 8, 2023 13:16
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants