Skip to content

Commit 4d594e3

Browse files
committed
fix: improve drag handle block insertion and deletion
1 parent 90a0e52 commit 4d594e3

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/shared/components/control/block-editor/DragHandle.tsx

+20-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { useInteractions, useFloating, useClick } from '@floating-ui/react';
1818
import { useEditor } from 'novel';
1919
import { useState } from 'react';
2020

21+
import type { EditorInstance} from 'novel';
2122
import type { ReactNode } from 'react';
2223

2324
function Item({ children, onClick }: { children: ReactNode; onClick: () => void }) {
@@ -41,15 +42,25 @@ function DragHandle() {
4142
const click = useClick(context);
4243
const { getReferenceProps, getFloatingProps } = useInteractions([click]);
4344

45+
const getDragHandlePos = (editor: EditorInstance) => {
46+
const { left, top } = refs.reference.current!.getBoundingClientRect();
47+
const posAtCoords = editor.view.posAtCoords({ left: left + 50, top: top + 1 });
48+
49+
return posAtCoords;
50+
};
51+
4452
const insertNewBlockBelow = () => {
4553
if (!editor) return;
4654

4755
return editor
4856
.chain()
4957
.focus()
5058
.command(({ tr }) => {
51-
const { $from } = tr.selection;
52-
const insertPos = $from.after($from.depth);
59+
const posAtCoords = getDragHandlePos(editor);
60+
if (!posAtCoords) return false;
61+
62+
const $pos = editor.state.doc.resolve(posAtCoords.pos);
63+
const insertPos = $pos.after($pos.depth);
5364
tr.insert(insertPos, editor.schema.nodes.paragraph.create());
5465

5566
setIsOpen(false);
@@ -64,8 +75,13 @@ function DragHandle() {
6475
.chain()
6576
.focus()
6677
.command(({ tr }) => {
67-
const { $from } = tr.selection;
68-
tr.delete($from.before($from.depth), $from.after($from.depth));
78+
const posAtCoords = getDragHandlePos(editor);
79+
if (!posAtCoords) return false;
80+
81+
const $pos = editor.state.doc.resolve(posAtCoords.pos);
82+
const from = $pos.before($pos.depth);
83+
const to = $pos.after($pos.depth);
84+
tr.delete(from, to);
6985

7086
setIsOpen(false);
7187
return true;

0 commit comments

Comments
 (0)