From eb1feaa1823bf5fee719b8bbdc51ab769ec10a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Fri, 21 Jul 2023 23:04:27 +0200 Subject: [PATCH] Delete image button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- app/soapbox/features/compose/editor/index.tsx | 2 +- .../compose/editor/nodes/image-component.tsx | 36 ++++++++++++++----- .../compose/editor/nodes/image-node.tsx | 10 +++--- .../floating-block-type-toolbar-plugin.tsx | 2 +- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/app/soapbox/features/compose/editor/index.tsx b/app/soapbox/features/compose/editor/index.tsx index 8ebd07321..45d715584 100644 --- a/app/soapbox/features/compose/editor/index.tsx +++ b/app/soapbox/features/compose/editor/index.tsx @@ -152,7 +152,7 @@ const ComposeEditor = React.forwardRef(({ contentEditable={
(null); const activeEditorRef = useRef(null); + const deleteNode = useCallback( + () => { + editor.update(() => { + const node = $getNodeByKey(nodeKey); + if ($isImageNode(node)) { + node.remove(); + } + }); + }, + [nodeKey], + ); + const onDelete = useCallback( (payload: KeyboardEvent) => { if (isSelected && $isNodeSelection($getSelection())) { const event: KeyboardEvent = payload; event.preventDefault(); - const node = $getNodeByKey(nodeKey); - if ($isImageNode(node)) { - node.remove(); - } + deleteNode(); } return false; }, @@ -235,12 +247,20 @@ export default function ImageComponent({ return ( <> -
+
+ { if (domNode instanceof HTMLImageElement) { const { alt: altText, src } = domNode; const node = $createImageNode({ altText, src }); return { node }; } return null; -} +}; type SerializedImageNode = Spread< { @@ -146,11 +146,11 @@ class ImageNode extends DecoratorNode { } -function $createImageNode({ +const $createImageNode = ({ altText = '', src, key, -}: ImagePayload): ImageNode { +}: ImagePayload): ImageNode => { return $applyNodeReplacement( new ImageNode( src, @@ -158,7 +158,7 @@ function $createImageNode({ key, ), ); -} +}; const $isImageNode = ( node: LexicalNode | null | undefined, diff --git a/app/soapbox/features/compose/editor/plugins/floating-block-type-toolbar-plugin.tsx b/app/soapbox/features/compose/editor/plugins/floating-block-type-toolbar-plugin.tsx index 95d3fe6d7..cd8afed94 100644 --- a/app/soapbox/features/compose/editor/plugins/floating-block-type-toolbar-plugin.tsx +++ b/app/soapbox/features/compose/editor/plugins/floating-block-type-toolbar-plugin.tsx @@ -183,7 +183,7 @@ const BlockTypeFloatingToolbar = ({ const selection = $getSelection(); if ($isRangeSelection(selection) || DEPRECATED_$isGridSelection(selection)) { const selectionNode = selection.anchor.getNode(); - selectionNode.replace($createImageNode({ src })); + selectionNode.replace($createImageNode({ altText: '', src })); } }); };