This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 829
Allow image pasting in plain mode in RTE #11056
Merged
artcodespace
merged 17 commits into
develop
from
alunturner/allow-image-pasting-plain-mode-RTE
Jun 12, 2023
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
393359e
get rough funcitonality working
18542a0
try to tidy up types
7c1746d
fix merge error
4fac2c9
fix signature change error
880dbcd
type wrangling
cadc518
use onBeforeInput listener
da53c26
add onBeforeInput handler, add logic to onPaste
efb9bfd
fix type error
55f4429
Merge remote-tracking branch 'origin/develop' into alunturner/allow-i…
1e55f9e
bring plain text listeners in line with useInputEventProcessor
5fcb69d
extract common function to util file, move tests
a693a22
tidy comment
f74a287
tidy comments
25c365a
fix typo
d9c73e8
add util tests
d6a0ece
Merge branch 'develop' into alunturner/allow-image-pasting-plain-mode…
a5642e2
add text paste test
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
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 |
---|---|---|
|
@@ -33,10 +33,7 @@ import { isCaretAtEnd, isCaretAtStart } from "../utils/selection"; | |
import { getEventsFromEditorStateTransfer, getEventsFromRoom } from "../utils/event"; | ||
import { endEditing } from "../utils/editing"; | ||
import Autocomplete from "../../Autocomplete"; | ||
import { handleEventWithAutocomplete } from "./utils"; | ||
import ContentMessages from "../../../../../ContentMessages"; | ||
import { getBlobSafeMimeType } from "../../../../../utils/blobs"; | ||
import { isNotNull } from "../../../../../Typeguards"; | ||
import { handleClipboardEvent, handleEventWithAutocomplete, isEventToHandleAsClipboardEvent } from "./utils"; | ||
|
||
export function useInputEventProcessor( | ||
onSend: () => void, | ||
|
@@ -61,17 +58,8 @@ export function useInputEventProcessor( | |
onSend(); | ||
}; | ||
|
||
// this is required to handle edge case image pasting in Safari, see | ||
// https://github.com/vector-im/element-web/issues/25327 and it is caught by the | ||
// `beforeinput` listener attached to the composer | ||
const isInputEventForClipboard = | ||
event instanceof InputEvent && event.inputType === "insertFromPaste" && isNotNull(event.dataTransfer); | ||
const isClipboardEvent = event instanceof ClipboardEvent; | ||
|
||
const shouldHandleAsClipboardEvent = isClipboardEvent || isInputEventForClipboard; | ||
|
||
if (shouldHandleAsClipboardEvent) { | ||
const data = isClipboardEvent ? event.clipboardData : event.dataTransfer; | ||
if (isEventToHandleAsClipboardEvent(event)) { | ||
const data = event instanceof ClipboardEvent ? event.clipboardData : event.dataTransfer; | ||
const handled = handleClipboardEvent(event, data, roomContext, mxClient, eventRelation); | ||
return handled ? null : event; | ||
} | ||
|
@@ -244,88 +232,3 @@ function handleInputEvent(event: InputEvent, send: Send, isCtrlEnterToSend: bool | |
|
||
return event; | ||
} | ||
|
||
/** | ||
* Takes an event and handles image pasting. Returns a boolean to indicate if it has handled | ||
* the event or not. Must accept either clipboard or input events in order to prevent issue: | ||
* https://github.com/vector-im/element-web/issues/25327 | ||
* | ||
* @param event - event to process | ||
* @param roomContext - room in which the event occurs | ||
* @param mxClient - current matrix client | ||
* @param eventRelation - used to send the event to the correct place eg timeline vs thread | ||
* @returns - boolean to show if the event was handled or not | ||
*/ | ||
export function handleClipboardEvent( | ||
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. Extracted to common util file with no changes |
||
event: ClipboardEvent | InputEvent, | ||
data: DataTransfer | null, | ||
roomContext: IRoomState, | ||
mxClient: MatrixClient, | ||
eventRelation?: IEventRelation, | ||
): boolean { | ||
// Logic in this function follows that of `SendMessageComposer.onPaste` | ||
const { room, timelineRenderingType, replyToEvent } = roomContext; | ||
|
||
function handleError(error: unknown): void { | ||
if (error instanceof Error) { | ||
console.log(error.message); | ||
} else if (typeof error === "string") { | ||
console.log(error); | ||
} | ||
} | ||
|
||
if (event.type !== "paste" || data === null || room === undefined) { | ||
return false; | ||
} | ||
|
||
// Prioritize text on the clipboard over files if RTF is present as Office on macOS puts a bitmap | ||
// in the clipboard as well as the content being copied. Modern versions of Office seem to not do this anymore. | ||
// We check text/rtf instead of text/plain as when copy+pasting a file from Finder or Gnome Image Viewer | ||
// it puts the filename in as text/plain which we want to ignore. | ||
if (data.files.length && !data.types.includes("text/rtf")) { | ||
ContentMessages.sharedInstance() | ||
.sendContentListToRoom(Array.from(data.files), room.roomId, eventRelation, mxClient, timelineRenderingType) | ||
.catch(handleError); | ||
return true; | ||
} | ||
|
||
// Safari `Insert from iPhone or iPad` | ||
// data.getData("text/html") returns a string like: <img src="blob:https://..."> | ||
if (data.types.includes("text/html")) { | ||
const imgElementStr = data.getData("text/html"); | ||
const parser = new DOMParser(); | ||
const imgDoc = parser.parseFromString(imgElementStr, "text/html"); | ||
|
||
if ( | ||
imgDoc.getElementsByTagName("img").length !== 1 || | ||
!imgDoc.querySelector("img")?.src.startsWith("blob:") || | ||
imgDoc.childNodes.length !== 1 | ||
) { | ||
handleError("Failed to handle pasted content as Safari inserted content"); | ||
return false; | ||
} | ||
const imgSrc = imgDoc.querySelector("img")!.src; | ||
|
||
fetch(imgSrc) | ||
.then((response) => { | ||
response | ||
.blob() | ||
.then((imgBlob) => { | ||
const type = imgBlob.type; | ||
const safetype = getBlobSafeMimeType(type); | ||
const ext = type.split("/")[1]; | ||
const parts = response.url.split("/"); | ||
const filename = parts[parts.length - 1]; | ||
const file = new File([imgBlob], filename + "." + ext, { type: safetype }); | ||
ContentMessages.sharedInstance() | ||
.sendContentToRoom(file, room.roomId, eventRelation, mxClient, replyToEvent) | ||
.catch(handleError); | ||
}) | ||
.catch(handleError); | ||
}) | ||
.catch(handleError); | ||
return true; | ||
} | ||
|
||
return false; | ||
} |
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
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
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
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.
Logic extracted to common typeguard util
isEventToHandleAsClipboardEvent