Skip to content

Commit c93752b

Browse files
committed
fix(richtext-lexical): order of add/drag handles was inconsistent between gutter and no-gutter mode
1 parent 7a4dd58 commit c93752b

File tree

3 files changed

+17
-31
lines changed

3 files changed

+17
-31
lines changed

packages/richtext-lexical/src/field/lexical/plugins/handles/AddBlockHandlePlugin/index.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,33 +113,14 @@ function useAddBlockHandle(
113113

114114
useEffect(() => {
115115
if (menuRef.current && hoveredElement?.node) {
116-
editor.getEditorState().read(() => {
117-
// Check if blockNode is an empty text node
118-
let isEmptyParagraph = true
119-
if (
120-
hoveredElement.node.getType() !== 'paragraph' ||
121-
hoveredElement.node.getTextContent() !== ''
122-
) {
123-
isEmptyParagraph = false
124-
}
125-
126-
setHandlePosition(
127-
hoveredElement?.elem,
128-
menuRef.current,
129-
anchorElem,
130-
isEmptyParagraph
131-
? blockHandleHorizontalOffset
132-
: blockHandleHorizontalOffset - (editorConfig?.admin?.hideGutter ? 20 : 0),
133-
)
134-
})
116+
setHandlePosition(
117+
hoveredElement?.elem,
118+
menuRef.current,
119+
anchorElem,
120+
blockHandleHorizontalOffset,
121+
)
135122
}
136-
}, [
137-
anchorElem,
138-
hoveredElement,
139-
editor,
140-
blockHandleHorizontalOffset,
141-
editorConfig?.admin?.hideGutter,
142-
])
123+
}, [anchorElem, hoveredElement, blockHandleHorizontalOffset])
143124

144125
const handleAddClick = useCallback(
145126
(event) => {

packages/richtext-lexical/src/field/lexical/plugins/handles/DraggableBlockPlugin/index.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function useDraggableBlockMenu(
8080
const [lastTargetBlockElem, setLastTargetBlockElem] = useState<HTMLElement | null>(null)
8181
const { editorConfig } = useEditorConfigContext()
8282

83-
const blockHandleHorizontalOffset = editorConfig?.admin?.hideGutter ? -24 : -8
83+
const blockHandleHorizontalOffset = editorConfig?.admin?.hideGutter ? -44 : -8
8484

8585
useEffect(() => {
8686
/**
@@ -212,7 +212,11 @@ function useDraggableBlockMenu(
212212

213213
if (draggableBlockElem !== targetBlockElem) {
214214
setTargetLine(
215-
blockHandleHorizontalOffset,
215+
editorConfig?.admin?.hideGutter ? '0px' : '3rem',
216+
blockHandleHorizontalOffset +
217+
(editorConfig?.admin?.hideGutter
218+
? menuRef?.current?.getBoundingClientRect()?.width ?? 0
219+
: -menuRef?.current?.getBoundingClientRect()?.width ?? 0),
216220
targetLineElem,
217221
targetBlockElem,
218222
lastTargetBlockElem,

packages/richtext-lexical/src/field/lexical/plugins/handles/DraggableBlockPlugin/setTargetLine.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const TEXT_BOX_HORIZONTAL_PADDING = -24
66
const DEBUG = false
77

88
export function setTargetLine(
9-
SPACE: number,
9+
offsetWidth: string,
10+
offsetLeft: number,
1011
targetLineElem: HTMLElement,
1112
targetBlockElem: HTMLElement,
1213
lastTargetBlockElem: HTMLElement | null,
@@ -51,10 +52,10 @@ export function setTargetLine(
5152
if (!isBelow) {
5253
top -= TARGET_LINE_HALF_HEIGHT * 2
5354
}
54-
const left = TEXT_BOX_HORIZONTAL_PADDING - SPACE
55+
const left = TEXT_BOX_HORIZONTAL_PADDING - offsetLeft
5556

5657
targetLineElem.style.transform = `translate(${left}px, ${top}px)`
57-
targetLineElem.style.width = `${anchorWidth - (TEXT_BOX_HORIZONTAL_PADDING - SPACE) * 2}px`
58+
targetLineElem.style.width = `calc(${anchorWidth}px - ${offsetWidth})`
5859
targetLineElem.style.opacity = '.4'
5960

6061
/**

0 commit comments

Comments
 (0)