Skip to content

Commit

Permalink
Fix: right click select images (facebook#5056)
Browse files Browse the repository at this point in the history
Co-authored-by: bcarleton3 <[email protected]>
  • Loading branch information
bencarletonn and bcarleton3 authored Oct 13, 2023
1 parent ca45b1d commit 5ccde0d
Showing 1 changed file with 61 additions and 18 deletions.
79 changes: 61 additions & 18 deletions packages/lexical-playground/src/nodes/ImageComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import type {
GridSelection,
LexicalCommand,
LexicalEditor,
NodeKey,
NodeSelection,
Expand All @@ -31,9 +32,11 @@ import {
$getNodeByKey,
$getSelection,
$isNodeSelection,
$isRangeSelection,
$setSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_LOW,
createCommand,
DRAGSTART_COMMAND,
KEY_BACKSPACE_COMMAND,
KEY_DELETE_COMMAND,
Expand All @@ -59,6 +62,9 @@ import {$isImageNode} from './ImageNode';

const imageCache = new Set();

export const RIGHT_CLICK_IMAGE_COMMAND: LexicalCommand<MouseEvent> =
createCommand('RIGHT_CLICK_IMAGE_COMMAND');

function useSuspenseImage(src: string) {
if (!imageCache.has(src)) {
throw new Promise((resolve) => {
Expand Down Expand Up @@ -206,8 +212,51 @@ export default function ImageComponent({
[caption, editor, setSelected],
);

const onClick = useCallback(
(payload: MouseEvent) => {
const event = payload;

if (isResizing) {
return true;
}
if (event.target === imageRef.current) {
if (event.shiftKey) {
setSelected(!isSelected);
} else {
clearSelection();
setSelected(true);
}
return true;
}

return false;
},
[isResizing, isSelected, setSelected, clearSelection],
);

const onRightClick = useCallback(
(event: MouseEvent): void => {
editor.getEditorState().read(() => {
const latestSelection = $getSelection();
const domElement = event.target as HTMLElement;
if (
domElement.tagName === 'IMG' &&
$isRangeSelection(latestSelection) &&
latestSelection.getNodes().length === 1
) {
editor.dispatchCommand(
RIGHT_CLICK_IMAGE_COMMAND,
event as MouseEvent,
);
}
});
},
[editor],
);

useEffect(() => {
let isMounted = true;
const rootElement = editor.getRootElement();
const unregister = mergeRegister(
editor.registerUpdateListener(({editorState}) => {
if (isMounted) {
Expand All @@ -224,24 +273,12 @@ export default function ImageComponent({
),
editor.registerCommand<MouseEvent>(
CLICK_COMMAND,
(payload) => {
const event = payload;

if (isResizing) {
return true;
}
if (event.target === imageRef.current) {
if (event.shiftKey) {
setSelected(!isSelected);
} else {
clearSelection();
setSelected(true);
}
return true;
}

return false;
},
onClick,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand<MouseEvent>(
RIGHT_CLICK_IMAGE_COMMAND,
onClick,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
Expand Down Expand Up @@ -274,9 +311,13 @@ export default function ImageComponent({
COMMAND_PRIORITY_LOW,
),
);

rootElement?.addEventListener('contextmenu', onRightClick);

return () => {
isMounted = false;
unregister();
rootElement?.removeEventListener('contextmenu', onRightClick);
};
}, [
clearSelection,
Expand All @@ -287,6 +328,8 @@ export default function ImageComponent({
onDelete,
onEnter,
onEscape,
onClick,
onRightClick,
setSelected,
]);

Expand Down

0 comments on commit 5ccde0d

Please sign in to comment.