2023-07-21 11:24:28 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
|
|
import { useLexicalNodeSelection } from '@lexical/react/useLexicalNodeSelection';
|
|
|
|
import { mergeRegister } from '@lexical/utils';
|
2023-07-21 14:04:27 -07:00
|
|
|
import clsx from 'clsx';
|
2023-07-21 11:24:28 -07:00
|
|
|
import {
|
|
|
|
$getNodeByKey,
|
|
|
|
$getSelection,
|
|
|
|
$isNodeSelection,
|
|
|
|
$setSelection,
|
|
|
|
CLICK_COMMAND,
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
DRAGSTART_COMMAND,
|
|
|
|
KEY_BACKSPACE_COMMAND,
|
|
|
|
KEY_DELETE_COMMAND,
|
|
|
|
KEY_ENTER_COMMAND,
|
|
|
|
KEY_ESCAPE_COMMAND,
|
|
|
|
SELECTION_CHANGE_COMMAND,
|
|
|
|
} from 'lexical';
|
|
|
|
import * as React from 'react';
|
|
|
|
import { Suspense, useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
|
2023-07-21 14:04:27 -07:00
|
|
|
import { IconButton } from 'soapbox/components/ui';
|
|
|
|
|
2023-07-21 11:24:28 -07:00
|
|
|
import { $isImageNode } from './image-node';
|
|
|
|
|
|
|
|
import type {
|
|
|
|
GridSelection,
|
|
|
|
LexicalEditor,
|
|
|
|
NodeKey,
|
|
|
|
NodeSelection,
|
|
|
|
RangeSelection,
|
|
|
|
} from 'lexical';
|
|
|
|
|
|
|
|
const imageCache = new Set();
|
|
|
|
|
|
|
|
function useSuspenseImage(src: string) {
|
|
|
|
if (!imageCache.has(src)) {
|
|
|
|
throw new Promise((resolve) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = src;
|
|
|
|
img.onload = () => {
|
|
|
|
imageCache.add(src);
|
|
|
|
resolve(null);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function LazyImage({
|
|
|
|
altText,
|
|
|
|
className,
|
|
|
|
imageRef,
|
|
|
|
src,
|
|
|
|
}: {
|
|
|
|
altText: string
|
|
|
|
className: string | null
|
|
|
|
imageRef: {current: null | HTMLImageElement}
|
|
|
|
src: string
|
|
|
|
}): JSX.Element {
|
|
|
|
useSuspenseImage(src);
|
|
|
|
return (
|
|
|
|
<img
|
|
|
|
className={className || undefined}
|
|
|
|
src={src}
|
|
|
|
alt={altText}
|
|
|
|
ref={imageRef}
|
|
|
|
draggable='false'
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ImageComponent({
|
|
|
|
src,
|
|
|
|
altText,
|
|
|
|
nodeKey,
|
|
|
|
}: {
|
|
|
|
altText: string
|
|
|
|
nodeKey: NodeKey
|
|
|
|
src: string
|
|
|
|
}): JSX.Element {
|
|
|
|
const imageRef = useRef<null | HTMLImageElement>(null);
|
|
|
|
const buttonRef = useRef<HTMLButtonElement | null>(null);
|
|
|
|
const [isSelected, setSelected, clearSelection] =
|
|
|
|
useLexicalNodeSelection(nodeKey);
|
|
|
|
const [editor] = useLexicalComposerContext();
|
|
|
|
const [selection, setSelection] = useState<
|
|
|
|
RangeSelection | NodeSelection | GridSelection | null
|
|
|
|
>(null);
|
|
|
|
const activeEditorRef = useRef<LexicalEditor | null>(null);
|
|
|
|
|
2023-07-21 14:04:27 -07:00
|
|
|
const deleteNode = useCallback(
|
|
|
|
() => {
|
|
|
|
editor.update(() => {
|
|
|
|
const node = $getNodeByKey(nodeKey);
|
|
|
|
if ($isImageNode(node)) {
|
|
|
|
node.remove();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[nodeKey],
|
|
|
|
);
|
|
|
|
|
2023-07-21 11:24:28 -07:00
|
|
|
const onDelete = useCallback(
|
|
|
|
(payload: KeyboardEvent) => {
|
|
|
|
if (isSelected && $isNodeSelection($getSelection())) {
|
|
|
|
const event: KeyboardEvent = payload;
|
|
|
|
event.preventDefault();
|
2023-07-21 14:04:27 -07:00
|
|
|
deleteNode();
|
2023-07-21 11:24:28 -07:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[isSelected, nodeKey],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onEnter = useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
const latestSelection = $getSelection();
|
|
|
|
const buttonElem = buttonRef.current;
|
|
|
|
if (
|
|
|
|
isSelected &&
|
|
|
|
$isNodeSelection(latestSelection) &&
|
|
|
|
latestSelection.getNodes().length === 1
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
buttonElem !== null &&
|
|
|
|
buttonElem !== document.activeElement
|
|
|
|
) {
|
|
|
|
event.preventDefault();
|
|
|
|
buttonElem.focus();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[isSelected],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onEscape = useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
|
|
|
if (buttonRef.current === event.target) {
|
|
|
|
$setSelection(null);
|
|
|
|
editor.update(() => {
|
|
|
|
setSelected(true);
|
|
|
|
const parentRootElement = editor.getRootElement();
|
|
|
|
if (parentRootElement !== null) {
|
|
|
|
parentRootElement.focus();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
[editor, setSelected],
|
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
let isMounted = true;
|
|
|
|
const unregister = mergeRegister(
|
|
|
|
editor.registerUpdateListener(({ editorState }) => {
|
|
|
|
if (isMounted) {
|
|
|
|
setSelection(editorState.read(() => $getSelection()));
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
editor.registerCommand(
|
|
|
|
SELECTION_CHANGE_COMMAND,
|
|
|
|
(_, activeEditor) => {
|
|
|
|
activeEditorRef.current = activeEditor;
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
editor.registerCommand<MouseEvent>(
|
|
|
|
CLICK_COMMAND,
|
|
|
|
(payload) => {
|
|
|
|
const event = payload;
|
|
|
|
|
|
|
|
if (event.target === imageRef.current) {
|
|
|
|
if (event.shiftKey) {
|
|
|
|
setSelected(!isSelected);
|
|
|
|
} else {
|
|
|
|
clearSelection();
|
|
|
|
setSelected(true);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
editor.registerCommand(
|
|
|
|
DRAGSTART_COMMAND,
|
|
|
|
(event) => {
|
|
|
|
if (event.target === imageRef.current) {
|
|
|
|
// TODO This is just a temporary workaround for FF to behave like other browsers.
|
|
|
|
// Ideally, this handles drag & drop too (and all browsers).
|
|
|
|
event.preventDefault();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
editor.registerCommand(
|
|
|
|
KEY_DELETE_COMMAND,
|
|
|
|
onDelete,
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
editor.registerCommand(
|
|
|
|
KEY_BACKSPACE_COMMAND,
|
|
|
|
onDelete,
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
editor.registerCommand(KEY_ENTER_COMMAND, onEnter, COMMAND_PRIORITY_LOW),
|
|
|
|
editor.registerCommand(
|
|
|
|
KEY_ESCAPE_COMMAND,
|
|
|
|
onEscape,
|
|
|
|
COMMAND_PRIORITY_LOW,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return () => {
|
|
|
|
isMounted = false;
|
|
|
|
unregister();
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
clearSelection,
|
|
|
|
editor,
|
|
|
|
isSelected,
|
|
|
|
nodeKey,
|
|
|
|
onDelete,
|
|
|
|
onEnter,
|
|
|
|
onEscape,
|
|
|
|
setSelected,
|
|
|
|
]);
|
|
|
|
|
|
|
|
const draggable = isSelected && $isNodeSelection(selection);
|
|
|
|
const isFocused = isSelected;
|
|
|
|
return (
|
|
|
|
<Suspense fallback={null}>
|
|
|
|
<>
|
2023-07-21 14:04:27 -07:00
|
|
|
<div className='relative' draggable={draggable}>
|
|
|
|
<IconButton
|
|
|
|
onClick={deleteNode}
|
|
|
|
src={require('@tabler/icons/x.svg')}
|
|
|
|
theme='dark'
|
|
|
|
className='absolute right-2 top-2 z-10 hover:scale-105 hover:bg-gray-900'
|
|
|
|
iconClassName='h-5 w-5'
|
|
|
|
/>
|
2023-07-21 11:24:28 -07:00
|
|
|
<LazyImage
|
|
|
|
className={
|
2023-07-21 14:04:27 -07:00
|
|
|
clsx('cursor-default', {
|
|
|
|
'select-none': isFocused,
|
|
|
|
'cursor-grab active:cursor-grabbing': isFocused && $isNodeSelection(selection),
|
|
|
|
})
|
2023-07-21 11:24:28 -07:00
|
|
|
}
|
|
|
|
src={src}
|
|
|
|
altText={altText}
|
|
|
|
imageRef={imageRef}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
</Suspense>
|
|
|
|
);
|
|
|
|
}
|