Skip to content

Commit

Permalink
Merge pull request #323 from brunocroh/fix/remove-drag-handle-on-drop
Browse files Browse the repository at this point in the history
fix: remove drag-handle on drop
  • Loading branch information
andrewdoro committed Mar 5, 2024
2 parents 7e189b8 + 1ec5518 commit b62800c
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions packages/headless/src/extensions/drag-and-drop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ function nodeDOMAtCoords(coords: { x: number; y: number }) {
(elem: Element) =>
elem.parentElement?.matches?.(".ProseMirror") ||
elem.matches(
["li", "p:not(:first-child)", "pre", "blockquote", "h1, h2, h3, h4, h5, h6"].join(", ")
[
"li",
"p:not(:first-child)",
"pre",
"blockquote",
"h1, h2, h3, h4, h5, h6",
].join(", ")
)
);
}

function nodePosAtDOM(node: Element, view: EditorView, options: DragHandleOptions) {
function nodePosAtDOM(
node: Element,
view: EditorView,
options: DragHandleOptions
) {
const boundingRect = node.getBoundingClientRect();

return view.posAtCoords({
Expand All @@ -57,7 +67,9 @@ function DragHandle(options: DragHandleOptions) {
const nodePos = nodePosAtDOM(node, view, options);
if (nodePos == null || nodePos < 0) return;

view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos)));
view.dispatch(
view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos))
);

const slice = view.state.selection.content();
const { dom, text } = __serializeForClipboard(view, slice);
Expand Down Expand Up @@ -87,7 +99,9 @@ function DragHandle(options: DragHandleOptions) {
const nodePos = nodePosAtDOM(node, view, options);
if (!nodePos) return;

view.dispatch(view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos)));
view.dispatch(
view.state.tr.setSelection(NodeSelection.create(view.state.doc, nodePos))
);
}

let dragHandleElement: HTMLElement | null = null;
Expand Down Expand Up @@ -177,6 +191,7 @@ function DragHandle(options: DragHandleOptions) {
},
drop: (view) => {
view.dom.classList.remove("dragging");
hideDragHandle();
},
dragend: (view) => {
view.dom.classList.remove("dragging");
Expand Down

0 comments on commit b62800c

Please sign in to comment.