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.

feat(md): drag to rearrange nodes (#2597)

Seamusd450806e3d72
diff --git a/js/app/packages/block-md/component/MarkdownEditor.tsx b/js/app/packages/block-md/component/MarkdownEditor.tsx
index b578e5d66b..6583c74e70 100644
--- a/js/app/packages/block-md/component/MarkdownEditor.tsx
+++ b/js/app/packages/block-md/component/MarkdownEditor.tsx
@@ -32,6 +32,7 @@ import TableActionMenu, {
tableCellNodeKeySignal,
} from '@core/component/LexicalMarkdown/component/menu/TableActionMenu';
import { DragInsertIndicator } from '@core/component/LexicalMarkdown/component/misc/DragInsertIndicator';
+import { DraggableBlockMenu } from '@core/component/LexicalMarkdown/component/misc/DraggableBlockMenu';
import { TableCellResizer } from '@core/component/LexicalMarkdown/component/misc/TableCellResizer';
import { Wordcount } from '@core/component/LexicalMarkdown/component/status/Wordcount';
import {
@@ -45,11 +46,13 @@ import {
} from '@core/component/LexicalMarkdown/context/LexicalWrapperContext';
import {
CLOSE_INLINE_SEARCH_COMMAND,
+ createDraggableBlockStore,
createDragInsertStore,
createWordcountStatsStore,
DefaultShortcuts,
diffPlugin,
documentMetadataPlugin,
+ draggableBlockPlugin,
dragInsertPlugin,
filePastePlugin,
generatePlugin,
@@ -292,6 +295,10 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
// store for the drag insert pluign.
const [dragInsertStore, setDragInsertStore] = createDragInsertStore();
+ // store for the draggable block (drag-to-rearrange) plugin.
+ const [draggableBlockStore, setDraggableBlockStore] =
+ createDraggableBlockStore();
+
// set up the solid-dnd stuff
const droppable = createDroppable(editor._config.namespace, {
type: 'markdown-editor',
@@ -665,13 +672,19 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
setMdStore('selection', lexicalWrapper.selection);
editor.setRootElement(el);
- // Register this plugin once we have the ref.
+ // Register plugins that require the container ref.
plugins.use(
dragInsertPlugin({
setState: setDragInsertStore,
dragListenerRef: editorContainerRef,
})
);
+ plugins.use(
+ draggableBlockPlugin({
+ setState: setDraggableBlockStore,
+ anchorElem: editorContainerRef,
+ })
+ );
const editorRefObserver = new ResizeObserver(observeClickTargetHeight);
@@ -981,6 +994,12 @@ export function MarkdownEditor(props: { autoFocusOnMount?: boolean } = {}) {
active={canEdit() ?? false}
/>
+ <DraggableBlockMenu
+ state={draggableBlockStore}
+ setState={setDraggableBlockStore}
+ active={canEdit() ?? false}
+ />
+
<EmojiMenu
editor={editor}
menu={emojiMenuOperations}
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownConfigBuilder.ts b/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownConfigBuilder.ts
index 3d35d50b37..c5340f2ae2 100644
--- a/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownConfigBuilder.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/builder/MarkdownConfigBuilder.ts
@@ -120,6 +120,11 @@ export class EditorConfigBuilder implements EditorBuilder {
return this;
}
+ withDraggableBlocks(): this {
+ this.state.draggableBlocks = true;
+ return this;
+ }
+
singleLine(): this {
this.state.singleLine = true;
return this;
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts b/js/app/packages/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts
index 3a81e0ed7a..296b5953b9 100644
--- a/js/app/packages/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/builder/buildHandleFromConfig.ts
@@ -3,7 +3,9 @@ import { createLexicalWrapper } from '../context/LexicalWrapperContext';
import {
codePlugin,
createAccessoryStore,
+ createDraggableBlockStore,
createDragInsertStore,
+ draggableBlockPlugin,
dragInsertPlugin,
emojisPlugin,
filePastePlugin,
@@ -174,6 +176,18 @@ export function buildHandleFromConfig(config: EditorConfig): EditorHandle {
plugins.use(dragInsertPlugin({ setState: setDragInsertStore }));
}
+ // Drag-to-rearrange blocks (uses root element fallback since no anchor ref
+ // is available during builder-time configuration).
+ const draggableBlockStoreResult = config.draggableBlocks
+ ? createDraggableBlockStore()
+ : undefined;
+ const draggableBlockStore = draggableBlockStoreResult?.[0];
+ const setDraggableBlockStore = draggableBlockStoreResult?.[1];
+
+ if (config.draggableBlocks && setDraggableBlockStore) {
+ plugins.use(draggableBlockPlugin({ setState: setDraggableBlockStore }));
+ }
+
// File clipboard paste — auto-register when fileDrop is enabled, since
// dragInsertPlugin blocks DRAG_DROP_PASTE (Lexical's built-in paste-files
// path) without processing the files. A custom filePaste config from
@@ -272,6 +286,7 @@ export function buildHandleFromConfig(config: EditorConfig): EditorHandle {
emojisMenuOps,
accessoryStore,
dragInsertStore,
+ draggableBlockStore,
fileDropConfig,
},
};
diff --git a/js/app/packages/core/component/LexicalMarkdown/builder/types.ts b/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
index 3f5006361c..02ee84fd1e 100644
--- a/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/builder/types.ts
@@ -5,6 +5,7 @@ import type { Store } from 'solid-js/store';
import type { createLexicalWrapper } from '../context/LexicalWrapperContext';
import type {
createAccessoryStore,
+ createDraggableBlockStore,
createDragInsertStore,
ItemMention,
PluginManager,
@@ -127,6 +128,8 @@ export interface EditorConfig {
actions: ActionsOptions | false;
/** When true, decorator components skip backend fetches (e.g. preview API). */
skipPreviewFetch: boolean;
+ /** Enable drag-to-rearrange handles on top-level blocks. */
+ draggableBlocks?: boolean;
}
/** @internal consumed by MarkdownShell; do not access directly */
@@ -143,6 +146,9 @@ export interface EditorInternals {
emojisMenuOps: ReturnType<typeof createMenuOperations> | undefined;
accessoryStore: ReturnType<typeof createAccessoryStore>[0] | undefined;
dragInsertStore: ReturnType<typeof createDragInsertStore>[0] | undefined;
+ draggableBlockStore:
+ | ReturnType<typeof createDraggableBlockStore>[0]
+ | undefined;
fileDropConfig: MediaDropOptions | undefined;
}
diff --git a/js/app/packages/core/component/LexicalMarkdown/component/misc/DraggableBlockMenu.tsx b/js/app/packages/core/component/LexicalMarkdown/component/misc/DraggableBlockMenu.tsx
new file mode 100644
index 0000000000..54d5b73562
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/component/misc/DraggableBlockMenu.tsx
@@ -0,0 +1,183 @@
+import { $isListItemNode, $isListNode, type ListItemNode } from '@lexical/list';
+import { $getNearestNodeFromDOMNode } from 'lexical';
+import { Show, useContext } from 'solid-js';
+import type { SetStoreFunction, Store } from 'solid-js/store';
+import { LexicalWrapperContext } from '../../context/LexicalWrapperContext';
+import {
+ $collectNestedGroup,
+ DRAG_DATA_FORMAT,
+ type DraggableBlockState,
+} from '../../plugins/draggable-block/draggableBlockPlugin';
+import Dots from '@icon/bold/dots-six-vertical-bold.svg';
+import { ScopedPortal } from '@core/component/ScopedPortal';
+
+const HANDLE_SIZE = 20;
+const HANDLE_GAP = 4;
+
+export function DraggableBlockMenu(props: {
+ state: Store<DraggableBlockState>;
+ setState: SetStoreFunction<DraggableBlockState>;
+ active: boolean;
+}) {
+ const lexicalWrapper = useContext(LexicalWrapperContext);
+ const editor = () => lexicalWrapper?.editor;
+
+ const handlePosition = () => {
+ const elem = props.state.hoveredElement;
+ if (!elem) return null;
+
+ const rect = elem.getBoundingClientRect();
+ const rootRect = editor()?.getRootElement()?.getBoundingClientRect();
+ if (!rootRect) return null;
+
+ const effectiveHeight = Math.min(rect.height, 24);
+
+ return {
+ top: rect.top + (effectiveHeight - HANDLE_SIZE) / 2,
+ left: rootRect.left - HANDLE_SIZE - HANDLE_GAP,
+ };
+ };
+
+ const targetLinePosition = () => {
+ const elem = props.state.targetElement;
+ const position = props.state.targetPosition;
+ if (!elem || !position) return null;
+
+ const rect = elem.getBoundingClientRect();
+ const rootRect = editor()?.getRootElement()?.getBoundingClientRect();
+ if (!rootRect) return null;
+
+ const padding = 4;
+ return {
+ top: position === 'before' ? rect.top - padding : rect.bottom + padding,
+ left: rootRect.left,
+ width: rootRect.width,
+ };
+ };
+
+ function onDragStart(event: DragEvent) {
+ const elem = props.state.hoveredElement;
+ const ed = editor();
+ if (!elem || !event.dataTransfer || !ed) return;
+
+ let nodeKey = '';
+ let dragImageElem: HTMLElement = elem;
+ // Elements to temporarily hide so the parent list can be used as drag
+ // image showing only the dragged group.
+ let hideElems: HTMLElement[] = [];
+ let listElemForImage: HTMLElement | null = null;
+
+ ed.read(() => {
+ const node = $getNearestNodeFromDOMNode(elem);
+ if (!node) return;
+
+ if ($isListItemNode(node)) {
+ const parent = node.getParent();
+
+ // First item of a list → drag the entire list.
+ if (parent && $isListNode(parent) && parent.getFirstChild() === node) {
+ nodeKey = parent.getKey();
+ const le = ed.getElementByKey(nodeKey);
+ if (le) dragImageElem = le;
+ return;
+ }
+
+ // Non-first item with nested children: figure out which sibling
+ // <li>s to temporarily hide so the parent list preview only shows
+ // the dragged group.
+ if (parent && $isListNode(parent)) {
+ const group = $collectNestedGroup(node as ListItemNode);
+ if (group.length > 1) {
+ const groupKeys = new Set(group.map((n) => n.getKey()));
+ for (const child of parent.getChildren()) {
+ if (!groupKeys.has(child.getKey())) {
+ const el = ed.getElementByKey(child.getKey());
+ if (el) hideElems.push(el);
+ }
+ }
+ listElemForImage = ed.getElementByKey(parent.getKey());
+ }
+ }
+ }
+
+ nodeKey = node.getKey();
+ });
+
+ if (!nodeKey) return;
+
+ event.dataTransfer.effectAllowed = 'move';
+
+ if (hideElems.length > 0 && listElemForImage) {
+ const listImg: HTMLElement = listElemForImage;
+ // Temporarily hide non-group siblings so the parent list element
+ // only shows the items being dragged. The browser captures the
+ // drag image snapshot synchronously, so restoring in setTimeout is
+ // safe.
+ for (const el of hideElems) el.style.display = 'none';
+ listImg.style.transform = 'translateZ(0)';
+ event.dataTransfer.setDragImage(listImg, 0, 0);
+ setTimeout(() => {
+ for (const el of hideElems) el.style.display = '';
+ listImg.style.transform = '';
+ });
+ } else {
+ const { transform } = dragImageElem.style;
+ dragImageElem.style.transform = 'translateZ(0)';
+ event.dataTransfer.setDragImage(dragImageElem, 0, 0);
+ setTimeout(() => {
+ dragImageElem.style.transform = transform;
+ });
+ }
+
+ event.dataTransfer.setData(DRAG_DATA_FORMAT, nodeKey);
+ props.setState({ isDragging: true });
+ }
+
+ function onDragEnd() {
+ props.setState({
+ isDragging: false,
+ targetElement: null,
+ targetPosition: null,
+ hoveredElement: null,
+ });
+ }
+
+ return (
+ <Show when={props.active}>
+ <ScopedPortal scope="block">
+ <div
+ class="draggable-block-menu fixed z-10 flex items-center justify-center cursor-grab rounded transition-opacity duration-100"
+ classList={{
+ 'opacity-0 pointer-events-none': !handlePosition(),
+ 'opacity-100': !!handlePosition(),
+ }}
+ style={{
+ top: (handlePosition()?.top ?? -9999) + 'px',
+ left: (handlePosition()?.left ?? -9999) + 'px',
+ width: HANDLE_SIZE + 'px',
+ height: HANDLE_SIZE + 'px',
+ }}
+ draggable={!!handlePosition()}
+ onDragStart={onDragStart}
+ onDragEnd={onDragEnd}
+ >
+ <Dots class="size-5 text-ink-extra-muted opacity-50 pointer-events-none" />
+ </div>
+
+ <Show when={props.state.isDragging && targetLinePosition()}>
+ {(pos) => (
+ <div
+ class="fixed bg-accent/60 pointer-events-none rounded-full ring-6 ring-accent/10"
+ style={{
+ top: pos().top - 1 + 'px',
+ left: pos().left + 'px',
+ width: pos().width + 'px',
+ height: '2px',
+ }}
+ />
+ )}
+ </Show>
+ </ScopedPortal>
+ </Show>
+ );
+}
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts
new file mode 100644
index 0000000000..4fb75677e9
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/draggableBlockPlugin.ts
@@ -0,0 +1,566 @@
+import {
+ $createListNode,
+ $isListItemNode,
+ $isListNode,
+ type ListItemNode,
+} from '@lexical/list';
+import { mergeRegister } from '@lexical/utils';
+import {
+ $getNearestNodeFromDOMNode,
+ $getNodeByKey,
+ $getRoot,
+ COMMAND_PRIORITY_HIGH,
+ KEY_DOWN_COMMAND,
+ type LexicalEditor,
+} from 'lexical';
+import { createStore, type SetStoreFunction } from 'solid-js/store';
+
+export const DRAG_DATA_FORMAT = 'application/x-lexical-drag-block';
+
+export type DraggableBlockState = {
+ hoveredElement: HTMLElement | null;
+ isDragging: boolean;
+ targetElement: HTMLElement | null;
+ targetPosition: 'before' | 'after' | null;
+};
+
+export const createDraggableBlockStore = () => {
+ return createStore<DraggableBlockState>({
+ hoveredElement: null,
+ isDragging: false,
+ targetElement: null,
+ targetPosition: null,
+ });
+};
+
+function getTopLevelNodeKeys(editor: LexicalEditor): string[] {
+ return editor.read(() => $getRoot().getChildrenKeys());
+}
+
+function getCollapsedMargins(elem: HTMLElement): {
+ marginTop: number;
+ marginBottom: number;
+} {
+ const getMargin = (
+ element: Element | null,
+ margin: 'marginTop' | 'marginBottom'
+ ): number =>
+ element ? parseFloat(window.getComputedStyle(element)[margin]) : 0;
+
+ const { marginTop, marginBottom } = window.getComputedStyle(elem);
+ const prevSiblingMarginBottom = getMargin(
+ elem.previousElementSibling,
+ 'marginBottom'
+ );
+ const nextSiblingMarginTop = getMargin(elem.nextElementSibling, 'marginTop');
+
+ return {
+ marginTop: Math.max(parseFloat(marginTop), prevSiblingMarginBottom),
+ marginBottom: Math.max(parseFloat(marginBottom), nextSiblingMarginTop),
+ };
+}
+
+/**
+ * Given a list DOM element, find the `<li>` child closest to clientY. Uses
+ * midpoint-distance so there are no dead zones between items.
+ */
+function $getListItemElement(
+ editor: LexicalEditor,
+ listElem: HTMLElement,
+ clientY: number
+): HTMLElement | null {
+ const listNode = $getNearestNodeFromDOMNode(listElem);
+ if (!listNode || !$isListNode(listNode)) return null;
+
+ let closest: HTMLElement | null = null;
+ let closestDist = Infinity;
+ // Track the last "real" item (one with actual content, not just a nesting
+ // wrapper) so we can normalize wrapper hits.
+ let lastRealLi: HTMLElement | null = null;
+ let closestIsWrapper = false;
+
+ for (const child of listNode.getChildren()) {
+ if (!$isListItemNode(child)) continue;
+ const li = editor.getElementByKey(child.getKey());
+ if (!li) continue;
+
+ const isWrapper =
+ child.getChildren().length > 0 &&
+ child.getChildren().every((c) => $isListNode(c));
+
+ const rect = li.getBoundingClientRect();
+ const mid = rect.top + rect.height / 2;
+ const dist = Math.abs(clientY - mid);
+
+ if (!isWrapper) lastRealLi = li;
+
+ if (dist < closestDist) {
+ closestDist = dist;
+ closest = li;
+ closestIsWrapper = isWrapper;
+ }
+ }
+
+ // If the closest hit is a nesting wrapper, return the preceding real item
+ // instead so the handle targets the logical owner of that nested content.
+ if (closestIsWrapper && lastRealLi) return lastRealLi;
+ return closest;
+}
+
+/**
+ * Find the draggable element at the given clientY coordinate.
+ *
+ * For most blocks this is the top-level element. For lists we drill into the
+ * `<li>` items so individual list items can be reordered.
+ *
+ * When useEdgeAsDefault is true, positions above the first block or
+ * below the last block snap to those blocks respectively.
+ */
+function getBlockElement(
+ editor: LexicalEditor,
+ event: { clientY: number },
+ useEdgeAsDefault = false
+): HTMLElement | null {
+ const topLevelNodeKeys = getTopLevelNodeKeys(editor);
+ if (topLevelNodeKeys.length === 0) return null;
+
+ let blockElem: HTMLElement | null = null;
+
+ editor.read(() => {
+ if (useEdgeAsDefault) {
+ const firstElem = editor.getElementByKey(topLevelNodeKeys[0]);
+ const lastElem = editor.getElementByKey(
+ topLevelNodeKeys[topLevelNodeKeys.length - 1]
+ );
+ if (firstElem && event.clientY < firstElem.getBoundingClientRect().top) {
+ blockElem = firstElem;
+ return;
+ }
+ if (lastElem && event.clientY > lastElem.getBoundingClientRect().bottom) {
+ blockElem = lastElem;
+ return;
+ }
+ }
+
+ for (const key of topLevelNodeKeys) {
+ const elem = editor.getElementByKey(key);
+ if (!elem) continue;
+
+ const rect = elem.getBoundingClientRect();
+ const { marginTop, marginBottom } = getCollapsedMargins(elem);
+
+ if (
+ event.clientY >= rect.top - marginTop &&
+ event.clientY <= rect.bottom + marginBottom
+ ) {
+ // If this is a list, drill into its items.
+ const node = $getNodeByKey(key);
+ if (node && $isListNode(node)) {
+ const li = $getListItemElement(editor, elem, event.clientY);
+ if (li) {
+ blockElem = li;
+ return;
+ }
+ }
+ blockElem = elem;
+ return;
+ }
+ }
+ });
+
+ return blockElem;
+}
+
+/**
+ * Collect a ListItemNode together with any immediately following siblings that
+ * represent nested content. Exported for use by the drag-handle component.
+ *
+ * Handles two nesting strategies:
+ *
+ * 1. **Flat-indent** — siblings with a higher `getIndent()` value.
+ * 2. **Tree-nesting** — a sibling ListItemNode whose only children are
+ * ListNodes (a "nesting wrapper" with no text of its own).
+ *
+ * Must be called inside an editor.update() context.
+ */
+export function $collectNestedGroup(item: ListItemNode): ListItemNode[] {
+ const indent = item.getIndent();
+ const group: ListItemNode[] = [item];
+ let sibling = item.getNextSibling();
+
+ while (sibling && $isListItemNode(sibling)) {
+ const sib = sibling as ListItemNode;
+
+ // Flat-indent: higher indent means nested under us.
+ if (sib.getIndent() > indent) {
+ group.push(sib);
+ sibling = sib.getNextSibling();
+ continue;
+ }
+
+ // Tree-nesting: a ListItemNode whose only children are ListNodes is a
+ // wrapper that holds nested content for the previous item.
+ const children = sib.getChildren();
+ if (children.length > 0 && children.every((c) => $isListNode(c))) {
+ group.push(sib);
+ sibling = sib.getNextSibling();
+ continue;
+ }
+
+ break;
+ }
+ return group;
+}
+
+export type DraggableBlockPluginProps = {
+ setState: SetStoreFunction<DraggableBlockState>;
+ /**
+ * Element to listen for mouse / drag events on.
+ * Falls back to the Lexical root element when omitted.
+ */
+ anchorElem?: HTMLElement;
+};
+
+function registerDraggableBlock(
+ editor: LexicalEditor,
+ props: DraggableBlockPluginProps
+) {
+ const { setState } = props;
+ let isDraggingBlock = false;
+ let isKeyboardMode = false;
+ let rafId: number | null = null;
+ let latestMouseEvent: MouseEvent | null = null;
+
+ // Attached to `document` so movement in the left margin (where the drag
+ // handle sits) is still tracked. We use the editor root rect to scope
+ // the horizontal range: from 60 px left of the content to its right edge.
+ const HORIZONTAL_BUFFER = 60;
+
+ function processMouseMove(event: MouseEvent) {
+ const target = event.target;
+ if (!(target instanceof HTMLElement)) {
+ clearHover();
+ return;
+ }
+ // Ignore mouse-moves while a button is held (text selection, etc.)
+ if (event.buttons > 0) return;
+
+ // Keep the current hover while the cursor sits on the drag handle itself.
+ if (target.closest('.draggable-block-menu')) return;
+
+ // Only react when the cursor is horizontally near the editor.
+ const rootRect = editor.getRootElement()?.getBoundingClientRect();
+ if (rootRect) {
+ const inXRange =
+ event.clientX >= rootRect.left - HORIZONTAL_BUFFER &&
+ event.clientX <= rootRect.right;
+ const inYRange =
+ event.clientY >= rootRect.top && event.clientY <= rootRect.bottom;
+ if (!inXRange || !inYRange) {
+ clearHover();
+ return;
+ }
+ }
+
+ const blockElem = getBlockElement(editor, event);
+ setState({ hoveredElement: blockElem });
+ }
+
+ function onMouseMove(event: MouseEvent) {
+ if (isKeyboardMode) {
+ isKeyboardMode = false;
+ }
+
+ latestMouseEvent = event;
+
+ if (rafId !== null) return;
+
+ rafId = requestAnimationFrame(() => {
+ rafId = null;
+ if (latestMouseEvent) {
+ processMouseMove(latestMouseEvent);
+ latestMouseEvent = null;
+ }
+ });
+ }
+
+ function clearHover() {
+ if (isDraggingBlock) return;
+ setState({ hoveredElement: null });
+ }
+
+ // Keyboard handlers (hide on typing)
+ function handleKeyboardInput() {
+ if (!isKeyboardMode) {
+ isKeyboardMode = true;
+ setState({ hoveredElement: null });
+ }
+ return false;
+ }
+
+ function handleDragOver(event: DragEvent) {
+ if (!event.dataTransfer?.types.includes(DRAG_DATA_FORMAT)) return;
+ isDraggingBlock = true;
+
+ event.preventDefault();
+ event.stopImmediatePropagation();
+ if (event.dataTransfer) event.dataTransfer.dropEffect = 'move';
+
+ const targetBlockElem = getBlockElement(editor, event, true);
+ if (!targetBlockElem) {
+ setState({ targetElement: null, targetPosition: null });
+ return;
+ }
+
+ const targetRect = targetBlockElem.getBoundingClientRect();
+ const insertBefore = event.clientY < targetRect.top + targetRect.height / 2;
+
+ setState({
+ targetElement: targetBlockElem,
+ targetPosition: insertBefore ? 'before' : 'after',
+ });
+ }
+
+ function handleDrop(event: DragEvent) {
+ const dragData = event.dataTransfer?.getData(DRAG_DATA_FORMAT);
+ if (!dragData) return;
+
+ isDraggingBlock = false;
+ event.preventDefault();
+ event.stopImmediatePropagation();
+
+ const targetBlockElem = getBlockElement(editor, event, true);
+ if (!targetBlockElem) {
+ resetState();
+ return;
+ }
+
+ const targetRect = targetBlockElem.getBoundingClientRect();
+ const insertBefore = event.clientY < targetRect.top + targetRect.height / 2;
+
+ editor.update(() => {
+ const draggedNode = $getNodeByKey(dragData);
+ if (!draggedNode) return;
+
+ let targetNode = $getNearestNodeFromDOMNode(targetBlockElem);
+ if (!targetNode) return;
+
+ if (targetNode === draggedNode) return;
+
+ const draggedParent = draggedNode.getParent();
+ const targetParent = $isListItemNode(targetNode)
+ ? targetNode.getParent()
+ : null;
+
+ // Same-list reorder. Both nodes are ListItemNodes sharing the same
+ // ListNode parent.
+ if (
+ $isListItemNode(draggedNode) &&
+ $isListItemNode(targetNode) &&
+ draggedParent &&
+ draggedParent === targetParent
+ ) {
+ const group = $collectNestedGroup(draggedNode as ListItemNode);
+
+ // Dropping onto a member of the dragged group is a no-op —
+ // prevents detaching nested subtrees from their logical parent.
+ const groupKeys = new Set(group.map((n) => n.getKey()));
+ if (groupKeys.has(targetNode.getKey())) return;
+
+ if (insertBefore) {
+ // Insert the whole group before the target, preserving order.
+ for (const item of group) {
+ targetNode.insertBefore(item);
+ }
+ } else {
+ // Insert the whole group after the target, preserving order.
+ // Walk backwards so each insertAfter stacks correctly.
+ let anchor = targetNode;
+ for (const item of group) {
+ anchor.insertAfter(item);
+ anchor = item;
+ }
+ }
+ return;
+ }
+
+ // List-item extracted to root level. A ListItemNode dragged outside its
+ // parent list needs to be wrapped in a fresh ListNode of the same type.
+ if (
+ $isListItemNode(draggedNode) &&
+ draggedParent &&
+ $isListNode(draggedParent)
+ ) {
+ const group = $collectNestedGroup(draggedNode as ListItemNode);
+ const listType = draggedParent.getListType();
+ const newList = $createListNode(listType);
+ for (const item of group) {
+ newList.append(item);
+ }
+
+ // Resolve the insertion target to a top-level node when the
+ // cursor is over a list item in a *different* list.
+ let insertTarget = targetNode;
+ if (
+ $isListItemNode(targetNode) &&
+ targetParent &&
+ $isListNode(targetParent)
+ ) {
+ insertTarget = targetParent;
+ }
+
+ if (insertBefore) {
+ insertTarget.insertBefore(newList);
+ } else {
+ insertTarget.insertAfter(newList);
+ }
+
+ // Remove the source list if it's now empty.
+ if (draggedParent.getChildrenSize() === 0) {
+ draggedParent.remove();
+ }
+ return;
+ }
+
+ // Non-list block dropped onto a list item → split the list
+ if (
+ !$isListItemNode(draggedNode) &&
+ $isListItemNode(targetNode) &&
+ targetParent &&
+ $isListNode(targetParent)
+ ) {
+ const listNode = targetParent;
+ const children = listNode.getChildren();
+ const listType = listNode.getListType();
+
+ // Determine the split index. When inserting after the target,
+ // include any nesting-wrapper siblings that belong to it.
+ const targetGroup = $collectNestedGroup(targetNode as ListItemNode);
+ const lastInGroup = targetGroup[targetGroup.length - 1];
+ const splitIndex = insertBefore
+ ? targetNode.getIndexWithinParent()
+ : lastInGroup.getIndexWithinParent() + 1;
+
+ const beforeItems = children.slice(0, splitIndex);
+ const afterItems = children.slice(splitIndex);
+
+ // Build the "before" list (items above the split).
+ if (beforeItems.length > 0) {
+ const beforeList = $createListNode(listType);
+ for (const item of beforeItems) beforeList.append(item);
+ listNode.insertBefore(beforeList);
+ }
+
+ // Insert the dragged block at the split point.
+ listNode.insertBefore(draggedNode);
+
+ // Build the "after" list (items below the split).
+ if (afterItems.length > 0) {
+ const afterList = $createListNode(listType);
+ for (const item of afterItems) afterList.append(item);
+ listNode.insertBefore(afterList);
+ }
+
+ // Remove the now-empty original list.
+ listNode.remove();
+ return;
+ }
+
+ // Default: top-level block move
+ if (
+ $isListItemNode(targetNode) &&
+ targetParent &&
+ $isListNode(targetParent)
+ ) {
+ targetNode = targetParent;
+ }
+
+ if (targetNode === draggedNode) return;
+
+ if (insertBefore) {
+ targetNode.insertBefore(draggedNode);
+ } else {
+ targetNode.insertAfter(draggedNode);
+ }
+ });
+
+ resetState();
+ }
+
+ function handleDragEnd() {
+ if (isDraggingBlock) {
+ isDraggingBlock = false;
+ resetState();
+ }
+ }
+
+ function onScroll() {
+ if (!isDraggingBlock) {
+ setState({ hoveredElement: null });
+ }
+ }
+
+ function resetState() {
+ setState({
+ isDragging: false,
+ targetElement: null,
+ targetPosition: null,
+ hoveredElement: null,
+ });
+ }
+
+ // -- Listener management -------------------------------------------------
+
+ function attachDragListeners(elem: HTMLElement) {
+ elem.addEventListener('dragover', handleDragOver, true);
+ elem.addEventListener('drop', handleDrop, true);
+ elem.addEventListener('dragend', handleDragEnd, true);
+ }
+
+ function detachDragListeners(elem: HTMLElement) {
+ elem.removeEventListener('dragover', handleDragOver, true);
+ elem.removeEventListener('drop', handleDrop, true);
+ elem.removeEventListener('dragend', handleDragEnd, true);
+ }
+
+ document.addEventListener('mousemove', onMouseMove);
+ window.addEventListener('scroll', onScroll, true);
+
+ // Hide drag handle when any key is pressed
+ const unregisterKeyDown = editor.registerCommand(
+ KEY_DOWN_COMMAND,
+ () => {
+ handleKeyboardInput();
+ return false;
+ },
+ COMMAND_PRIORITY_HIGH
+ );
+
+ function cleanup() {
+ if (rafId !== null) {
+ cancelAnimationFrame(rafId);
+ rafId = null;
+ }
+ document.removeEventListener('mousemove', onMouseMove);
+ window.removeEventListener('scroll', onScroll, true);
+ unregisterKeyDown();
+ }
+
+ if (props.anchorElem) {
+ attachDragListeners(props.anchorElem);
+ return mergeRegister(() => detachDragListeners(props.anchorElem!), cleanup);
+ }
+
+ // Fallback: attach drag listeners to the editor root element.
+ return mergeRegister(
+ editor.registerRootListener((root, prevRoot) => {
+ if (root) attachDragListeners(root);
+ if (prevRoot) detachDragListeners(prevRoot);
+ }),
+ cleanup
+ );
+}
+
+export const draggableBlockPlugin = (props: DraggableBlockPluginProps) => {
+ return (editor: LexicalEditor) => registerDraggableBlock(editor, props);
+};
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/index.ts
new file mode 100644
index 0000000000..26f8b0332c
--- /dev/null
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/draggable-block/index.ts
@@ -0,0 +1 @@
+export * from './draggableBlockPlugin';
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
index cde5212be9..1c20065c21 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/index.ts
@@ -7,6 +7,7 @@ export * from './custom-delete';
export * from './diff';
export * from './document-metadata';
export * from './drag-insert';
+export * from './draggable-block';
export * from './element-event';
export * from './emojis';
export * from './file-paste';