Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(markdown): move dragged images within a document instead of copying (#9088)

teo68c594922aaf
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/component/decorator/MarkdownImage.tsx b/apps/web/src/lib/core/component/LexicalMarkdown/component/decorator/MarkdownImage.tsx
index b2f2b4d7ac..633d1dda6b 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/component/decorator/MarkdownImage.tsx
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/component/decorator/MarkdownImage.tsx
@@ -34,6 +34,7 @@ import {
useContext,
} from 'solid-js';
import { LexicalWrapperContext } from '../../context/LexicalWrapperContext';
+import { DRAG_DATA_FORMAT } from '../../plugins/draggable-block/draggableBlockPlugin';
import {
$upgradeDSSMediaUrl,
getMediaUrl,
@@ -345,6 +346,12 @@ export function MarkdownImage(props: ImageDecoratorProps) {
)}
draggable={true}
use:internalDrag={true}
+ onDragStart={(e) => {
+ // Dropped in this editor the image moves; anywhere else it is
+ // copied from its URL.
+ if (interactable() && editor()?.isEditable())
+ e.dataTransfer?.setData(DRAG_DATA_FORMAT, props.key);
+ }}
ref={imageRef}
src={imageUrl()}
style={{
diff --git a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts
index ec1ca89fff..a13e8970eb 100644
--- a/apps/web/src/lib/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts
+++ b/apps/web/src/lib/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts
@@ -14,6 +14,7 @@ import {
COMMAND_PRIORITY_HIGH,
KEY_DOWN_COMMAND,
type LexicalEditor,
+ type NodeKey,
SELECTION_CHANGE_COMMAND,
} from 'lexical';
import { createStore, type SetStoreFunction } from 'solid-js/store';
@@ -242,6 +243,10 @@ function registerDraggableBlock(
) {
const { setState } = props;
let isDraggingBlock = false;
+ // The node being dragged, when the drag started in this editor. A block
+ // dragged in from another editor (an image dropped into a second document)
+ // is left to the editor's other drop handlers, which copy it.
+ let draggedKey: NodeKey | null = null;
let isKeyboardMode = false;
let hasNonCollapsedSelection = false;
let rafId: number | null = null;
@@ -346,8 +351,19 @@ function registerDraggableBlock(
return false;
}
+ // Listens on `document`: the drag handle is portaled outside the editor.
+ function handleDragStart(event: DragEvent) {
+ const key = event.dataTransfer?.getData(DRAG_DATA_FORMAT);
+ draggedKey =
+ key && editor.read(() => $getNodeByKey(key) !== null) ? key : null;
+ // Media blocks start their drag from the block itself, not the handle,
+ // so the drop line needs the drag state set here too.
+ if (draggedKey) setState({ isDragging: true });
+ }
+
function handleDragOver(event: DragEvent) {
- if (!event.dataTransfer?.types.includes(DRAG_DATA_FORMAT)) return;
+ if (!draggedKey || !event.dataTransfer?.types.includes(DRAG_DATA_FORMAT))
+ return;
isDraggingBlock = true;
event.preventDefault();
@@ -371,9 +387,12 @@ function registerDraggableBlock(
function handleDrop(event: DragEvent) {
const dragData = event.dataTransfer?.getData(DRAG_DATA_FORMAT);
- if (!dragData) return;
+ if (!draggedKey || dragData !== draggedKey) return;
isDraggingBlock = false;
+ // Moving the node can detach the drag source, whose dragend then never
+ // reaches this editor.
+ draggedKey = null;
event.preventDefault();
event.stopImmediatePropagation();
@@ -536,6 +555,7 @@ function registerDraggableBlock(
}
function handleDragEnd() {
+ draggedKey = null;
if (isDraggingBlock) {
isDraggingBlock = false;
resetState();
@@ -570,6 +590,7 @@ function registerDraggableBlock(
}
document.addEventListener('mousemove', onMouseMove);
+ document.addEventListener('dragstart', handleDragStart);
window.addEventListener('scroll', onScroll, true);
// Hide drag handle when any key is pressed
@@ -603,6 +624,7 @@ function registerDraggableBlock(
rafId = null;
}
document.removeEventListener('mousemove', onMouseMove);
+ document.removeEventListener('dragstart', handleDragStart);
window.removeEventListener('scroll', onScroll, true);
unregisterKeyDown();
unregisterSelectionChange();