Skip to content

Commit f5fb095

Browse files
committed
feat(richtext-lexical): improve draggable block indicator style and animations
1 parent b94a265 commit f5fb095

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const Indeterminate = 0
1515

1616
type Props = {
1717
anchorElem: HTMLElement
18-
cache_treshold?: number
18+
cache_threshold?: number
1919
editor: LexicalEditor
2020
/** fuzzy makes the search not exact. If no exact match found, find the closes node instead of returning null */
2121
fuzzy?: boolean
@@ -51,7 +51,7 @@ function isPointClose(previous: Point, current: Point, threshold: number = 20):
5151
export function getNodeCloseToPoint(props: Props): Output {
5252
const {
5353
anchorElem,
54-
cache_treshold = 20,
54+
cache_threshold = 20,
5555
editor,
5656
fuzzy = false,
5757
horizontalOffset = 0,
@@ -63,13 +63,13 @@ export function getNodeCloseToPoint(props: Props): Output {
6363

6464
// Use cache
6565
if (
66-
cache_treshold > 0 &&
66+
cache_threshold > 0 &&
6767
cache.props &&
6868
cache.result &&
6969
cache.props.fuzzy === props.fuzzy &&
7070
cache.props.horizontalOffset === props.horizontalOffset &&
7171
cache.props.useEdgeAsDefault === props.useEdgeAsDefault &&
72-
isPointClose(cache.props.point, props.point, cache_treshold)
72+
isPointClose(cache.props.point, props.point, cache_threshold)
7373
) {
7474
if (verbose) {
7575
//console.log('Returning cached result')

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,11 @@
5353
transition: none;
5454
}
5555
}
56+
57+
58+
.rich-text-lexical {
59+
.ContentEditable__root > * {
60+
will-change: margin-top, margin-bottom;
61+
transition: margin-top 0.08s, margin-bottom 0.08s;
62+
}
63+
}

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ function hideTargetLine(
5555
targetLineElem.style.opacity = '0'
5656
}
5757
if (lastTargetBlockElem) {
58-
lastTargetBlockElem.style.opacity = '1'
59-
lastTargetBlockElem.style.transform = 'translate(0, 0)'
58+
lastTargetBlockElem.style.opacity = ''
59+
lastTargetBlockElem.style.transform = ''
60+
// Delete marginBottom and marginTop values we set
61+
lastTargetBlockElem.style.marginBottom = ''
62+
lastTargetBlockElem.style.marginTop = ''
6063
//lastTargetBlockElem.style.border = 'none'
6164
}
6265
}
@@ -139,7 +142,7 @@ function useDraggableBlockMenu(
139142
isFoundNodeEmptyParagraph,
140143
} = getNodeCloseToPoint({
141144
anchorElem,
142-
cache_treshold: 0,
145+
cache_threshold: 0,
143146
editor,
144147
horizontalOffset: -distanceFromScrollerElem,
145148
point: new Point(event.x, event.y),

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,39 @@ export function setTargetLine(
3636
lineTop += targetBlockElemHeight / 2
3737
}
3838

39-
const top = lineTop - anchorTop - TARGET_LINE_HALF_HEIGHT
39+
const targetElemTranslate = 0
40+
let targetElemTranslate2 = 0
41+
42+
if (!isFoundNodeEmptyParagraph) {
43+
if (isBelow) {
44+
targetElemTranslate2 = -TARGET_LINE_HALF_HEIGHT
45+
} else {
46+
targetElemTranslate2 = TARGET_LINE_HALF_HEIGHT
47+
}
48+
}
49+
50+
let top = lineTop - anchorTop + targetElemTranslate2
51+
if (!isBelow) {
52+
top -= TARGET_LINE_HALF_HEIGHT * 2
53+
}
4054
const left = TEXT_BOX_HORIZONTAL_PADDING - SPACE
4155

4256
targetLineElem.style.transform = `translate(${left}px, ${top}px)`
4357
targetLineElem.style.width = `${anchorWidth - (TEXT_BOX_HORIZONTAL_PADDING - SPACE) * 2}px`
4458
targetLineElem.style.opacity = '.4'
4559

46-
targetBlockElem.style.opacity = '0.4'
60+
/**
61+
* Move around element below or above the line (= the target / targetBlockElem)
62+
*/
63+
//targetBlockElem.style.opacity = '0.4'
4764
if (!isFoundNodeEmptyParagraph) {
4865
// move lastTargetBlockElem down 50px to make space for targetLineElem (which is 50px height)
66+
targetBlockElem.style.transform = `translate(0, ${targetElemTranslate}px)`
4967
if (isBelow) {
50-
targetBlockElem.style.transform = `translate(0, ${-TARGET_LINE_HALF_HEIGHT / 1.9}px)`
68+
// add to existing marginBottom plus the height of targetLineElem
69+
targetBlockElem.style.marginBottom = TARGET_LINE_HALF_HEIGHT * 2 + 'px'
5170
} else {
52-
targetBlockElem.style.transform = `translate(0, ${TARGET_LINE_HALF_HEIGHT / 1.9}px)`
71+
targetBlockElem.style.marginTop = TARGET_LINE_HALF_HEIGHT * 2 + 'px'
5372
}
5473
}
5574

@@ -59,8 +78,12 @@ export function setTargetLine(
5978
}
6079

6180
if (lastTargetBlockElem && lastTargetBlockElem !== targetBlockElem) {
62-
lastTargetBlockElem.style.opacity = '1'
63-
lastTargetBlockElem.style.transform = 'translate(0, 0)'
81+
lastTargetBlockElem.style.opacity = ''
82+
lastTargetBlockElem.style.transform = ''
83+
84+
// Delete marginBottom and marginTop values we set
85+
lastTargetBlockElem.style.marginBottom = ''
86+
lastTargetBlockElem.style.marginTop = ''
6487
//lastTargetBlockElem.style.border = 'none'
6588
}
6689
}

0 commit comments

Comments
 (0)