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

[lexical-playground][lexical-react] Feature: Push Draggable Element to Parent #7338

Merged
merged 7 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
left: 0;
top: 0;
will-change: transform;
display: flex;
gap: 2px;
}

.draggable-block-menu .icon {
Expand All @@ -16,11 +18,16 @@
background-image: url(../../images/icons/draggable-block-menu.svg);
}

.draggable-block-menu .icon-plus {
cursor: pointer;
background-image: url(../../images/icons/plus.svg);
}

.draggable-block-menu:active {
cursor: grabbing;
}

.draggable-block-menu:hover {
.draggable-block-menu .icon:hover {
background-color: #efefef;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import './index.css';

import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
import {DraggableBlockPlugin_EXPERIMENTAL} from '@lexical/react/LexicalDraggableBlockPlugin';
import {useRef} from 'react';
import {$createParagraphNode, $getNearestNodeFromDOMNode} from 'lexical';
import {useRef, useState} from 'react';

const DRAGGABLE_BLOCK_MENU_CLASSNAME = 'draggable-block-menu';

Expand All @@ -23,8 +25,33 @@
}: {
anchorElem?: HTMLElement;
}): JSX.Element {
const [editor] = useLexicalComposerContext();
const menuRef = useRef<HTMLDivElement>(null);
const targetLineRef = useRef<HTMLDivElement>(null);
const [draggableElement, setDraggableElement] = useState<HTMLElement | null>(
null,
);

function insertBlock(e: React.MouseEvent) {
if (!draggableElement || !editor) {
return;
}

editor.update(() => {
const node = $getNearestNodeFromDOMNode(draggableElement);
if (!node) {
return;
}

const pNode = $createParagraphNode();
if (e.altKey || e.ctrlKey) {
node.insertBefore(pNode);
} else {
node.insertAfter(pNode);
}
pNode.select();
});
}

return (
<DraggableBlockPlugin_EXPERIMENTAL
Expand All @@ -33,13 +60,15 @@
targetLineRef={targetLineRef}
menuComponent={
<div ref={menuRef} className="icon draggable-block-menu">
<div className="icon icon-plus" onClick={insertBlock} />

Check warning on line 63 in packages/lexical-playground/src/plugins/DraggableBlockPlugin/index.tsx

View workflow job for this annotation

GitHub Actions / core-tests / integrity (20.11.0)

Avoid non-native interactive elements. If using native HTML is not possible, add an appropriate role and support for tabbing, mouse, keyboard, and touch inputs to an interactive content element
<div className="icon" />
</div>
}
targetLineComponent={
<div ref={targetLineRef} className="draggable-block-target-line" />
}
isOnMenu={isOnMenu}
onElementChanged={setDraggableElement}
/>
);
}
4 changes: 2 additions & 2 deletions packages/lexical-playground/src/ui/ContentEditable.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
display: block;
position: relative;
outline: 0;
padding: 8px 28px 40px;
padding: 8px 46px 40px;
min-height: 150px;
}
@media (max-width: 1025px) {
Expand All @@ -29,7 +29,7 @@
position: absolute;
text-overflow: ellipsis;
top: 8px;
left: 28px;
left: 46px;
right: 28px;
user-select: none;
white-space: nowrap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Props = $ReadOnly<{
menuComponent: React.Node,
targetLineComponent: React.Node,
isOnMenu: (element: HTMLElement) => boolean,
onElementChanged?: (element: HTMLElement | null) => void
}>;

declare export function DraggableBlockPlugin_EXPERIMENTAL(
Expand Down
21 changes: 18 additions & 3 deletions packages/lexical-react/src/LexicalDraggableBlockPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import {
DragEvent as ReactDragEvent,
ReactNode,
useCallback,
useEffect,
useRef,
useState,
Expand Down Expand Up @@ -269,13 +270,24 @@ function useDraggableBlockMenu(
menuComponent: ReactNode,
targetLineComponent: ReactNode,
isOnMenu: (element: HTMLElement) => boolean,
onElementChanged?: (element: HTMLElement | null) => void,
): JSX.Element {
const scrollerElem = anchorElem.parentElement;

const isDraggingBlockRef = useRef<boolean>(false);
const [draggableBlockElem, setDraggableBlockElem] =
const [draggableBlockElem, setDraggableBlockElemState] =
useState<HTMLElement | null>(null);

const setDraggableBlockElem = useCallback(
(elem: HTMLElement | null) => {
setDraggableBlockElemState(elem);
if (onElementChanged) {
onElementChanged(elem);
}
},
[onElementChanged],
);

useEffect(() => {
function onMouseMove(event: MouseEvent) {
const target = event.target;
Expand Down Expand Up @@ -308,7 +320,7 @@ function useDraggableBlockMenu(
scrollerElem.removeEventListener('mouseleave', onMouseLeave);
}
};
}, [scrollerElem, anchorElem, editor, isOnMenu]);
}, [scrollerElem, anchorElem, editor, isOnMenu, setDraggableBlockElem]);

useEffect(() => {
if (menuRef.current) {
Expand Down Expand Up @@ -401,7 +413,7 @@ function useDraggableBlockMenu(
COMMAND_PRIORITY_HIGH,
),
);
}, [anchorElem, editor, targetLineRef]);
}, [anchorElem, editor, targetLineRef, setDraggableBlockElem]);

function onDragStart(event: ReactDragEvent<HTMLDivElement>): void {
const dataTransfer = event.dataTransfer;
Expand Down Expand Up @@ -442,13 +454,15 @@ export function DraggableBlockPlugin_EXPERIMENTAL({
menuComponent,
targetLineComponent,
isOnMenu,
onElementChanged,
}: {
anchorElem?: HTMLElement;
menuRef: React.RefObject<HTMLElement>;
targetLineRef: React.RefObject<HTMLElement>;
menuComponent: ReactNode;
targetLineComponent: ReactNode;
isOnMenu: (element: HTMLElement) => boolean;
onElementChanged?: (element: HTMLElement | null) => void;
}): JSX.Element {
const [editor] = useLexicalComposerContext();
return useDraggableBlockMenu(
Expand All @@ -460,5 +474,6 @@ export function DraggableBlockPlugin_EXPERIMENTAL({
menuComponent,
targetLineComponent,
isOnMenu,
onElementChanged,
);
}