Skip to content

Commit

Permalink
Fix event targets returned by paste eventListeners (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skalakid authored Feb 14, 2024
1 parent 6ccd3b8 commit 870e2f6
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,27 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
divRef.current.style.maxHeight = elementHeight;
}, [numberOfLines]);

useEffect(() => {
// update event listeners events objects
const originalAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function (eventName, callback) {
if (eventName === 'paste' && typeof callback === 'function') {
originalAddEventListener.call(this, eventName, function (event) {
try {
if (divRef.current && divRef.current.contains(event.target as Node)) {
// pasting returns styled span elements as event.target instead of the contentEditable div. We want to keep the div as the target
Object.defineProperty(event, 'target', {writable: false, value: divRef.current});
}
callback(event);
// eslint-disable-next-line no-empty
} catch (e) {}
});
} else {
originalAddEventListener.call(this, eventName, callback);
}
};
}, []);

return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
Expand Down

0 comments on commit 870e2f6

Please sign in to comment.