This repository has been archived by the owner on Sep 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Add onBeforeInput listener #710
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,15 +123,24 @@ export function useListeners( | |
const onInput = (e: Event) => isInputEvent(e) && _handleInput(e); | ||
editorNode.addEventListener('input', onInput); | ||
|
||
const onPaste = (e: Event) => { | ||
if (!isClipboardEvent(e)) { | ||
return; | ||
// 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); | ||
} | ||
|
||
e.preventDefault(); | ||
e.stopPropagation(); | ||
|
||
_handleInput(e); | ||
}; | ||
editorNode.addEventListener('paste', onPaste); | ||
|
||
|
@@ -177,6 +186,11 @@ export function useListeners( | |
}; | ||
document.addEventListener('selectionchange', onSelectionChange); | ||
|
||
// this is required to handle edge case image pasting in Safari, see | ||
// https://github.com/vector-im/element-web/issues/25327 | ||
const onBeforeInput = onPaste; | ||
editorNode.addEventListener('beforeinput', onBeforeInput); | ||
|
||
setAreListenersReady(true); | ||
|
||
return () => { | ||
|
@@ -185,6 +199,7 @@ export function useListeners( | |
editorNode.removeEventListener('paste', onPaste); | ||
editorNode.removeEventListener('wysiwygInput', onWysiwygInput); | ||
editorNode.removeEventListener('keydown', onKeyDown); | ||
editorNode.removeEventListener('beforeinput', onBeforeInput); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines create a reference to the |
||
document.removeEventListener('selectionchange', onSelectionChange); | ||
}; | ||
}, [ | ||
|
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.
There was a problem hiding this comment.
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"