Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: right click select images #5056

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move away from core
  • Loading branch information
bcarleton3 committed Oct 13, 2023
commit d7eff7229fdfd6b18fbccf44c6558b31dd0b0ae2
87 changes: 52 additions & 35 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 Down Expand Up @@ -35,7 +36,7 @@ import {
$setSelection,
CLICK_COMMAND,
COMMAND_PRIORITY_LOW,
CONTEXT_MENU_COMMAND,
createCommand,
DRAGSTART_COMMAND,
KEY_BACKSPACE_COMMAND,
KEY_DELETE_COMMAND,
Expand All @@ -61,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 @@ -208,8 +212,46 @@ 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 => {
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 @@ -226,43 +268,12 @@ export default function ImageComponent({
),
editor.registerCommand<MouseEvent>(
CLICK_COMMAND,
(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;
},
onClick,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand<MouseEvent>(
CONTEXT_MENU_COMMAND,
(payload: MouseEvent) => {
const event = payload;
const latestSelection = $getSelection();
const domElement = event.target as HTMLElement;

if (
domElement.tagName === 'IMG' &&
$isRangeSelection(latestSelection) &&
latestSelection.getNodes().length === 1
) {
clearSelection();
setSelected(true);
}
return false;
},
RIGHT_CLICK_IMAGE_COMMAND,
onClick,
COMMAND_PRIORITY_LOW,
),
editor.registerCommand(
Expand Down Expand Up @@ -295,9 +306,13 @@ export default function ImageComponent({
COMMAND_PRIORITY_LOW,
),
);

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

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

Expand Down
3 changes: 0 additions & 3 deletions packages/lexical/src/LexicalCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export const SELECTION_CHANGE_COMMAND: LexicalCommand<void> = createCommand(
);
export const CLICK_COMMAND: LexicalCommand<MouseEvent> =
createCommand('CLICK_COMMAND');
export const CONTEXT_MENU_COMMAND: LexicalCommand<MouseEvent> = createCommand(
'CONTEXT_MENU_COMMAND',
);
export const DELETE_CHARACTER_COMMAND: LexicalCommand<boolean> = createCommand(
'DELETE_CHARACTER_COMMAND',
);
Expand Down
9 changes: 0 additions & 9 deletions packages/lexical/src/LexicalEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
$setCompositionKey,
BLUR_COMMAND,
CLICK_COMMAND,
CONTEXT_MENU_COMMAND,
CONTROLLED_TEXT_INSERTION_COMMAND,
COPY_COMMAND,
CUT_COMMAND,
Expand Down Expand Up @@ -141,7 +140,6 @@ const rootElementEvents: RootElementEvents = [
['compositionend', onCompositionEnd],
['input', onInput],
['click', onClick],
['contextmenu', PASS_THROUGH_COMMAND],
['cut', PASS_THROUGH_COMMAND],
['copy', PASS_THROUGH_COMMAND],
['dragstart', PASS_THROUGH_COMMAND],
Expand Down Expand Up @@ -1168,13 +1166,6 @@ export function addRootElementEvents(
stopLexicalPropagation(event);
if (editor.isEditable()) {
switch (eventName) {
case 'contextmenu':
return dispatchCommand(
editor,
CONTEXT_MENU_COMMAND,
event as MouseEvent,
);

case 'cut':
return dispatchCommand(
editor,
Expand Down
1 change: 0 additions & 1 deletion packages/lexical/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export {
CLEAR_EDITOR_COMMAND,
CLEAR_HISTORY_COMMAND,
CLICK_COMMAND,
CONTEXT_MENU_COMMAND,
CONTROLLED_TEXT_INSERTION_COMMAND,
COPY_COMMAND,
createCommand,
Expand Down