Skip to content

Commit 13ddccb

Browse files
authored
Reorder less eagerly (#57)
Update findGap to search for gaps closer to the original position. Previously, we always searched towards the top of the document for gaps, which meant that you would reorder _up_ as soon as you overlapped with the node above you. Now, we weight searches by how far into the parent node the position is, so you don't reorder up until you've moved halfway up the node (by position). Also we were checking `isTextblock` in findGap but we wanted `isBlock`.
1 parent 6182c7b commit 13ddccb

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

.yarn/versions/49353831.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
releases:
2+
"@pitter-patter/shuffle": patch

packages/shuffle/src/transform/reorder.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,20 @@ export function findGap(doc: Node, pos: number, nodeType: NodeType) {
6262
const $pos = doc.resolve(pos);
6363

6464
let d = $pos.depth;
65-
while (!$pos.node(d).isTextblock && d > 0) {
65+
while (!$pos.node(d).isBlock && d > 0) {
6666
d--;
6767
}
6868

69-
const start = d === 0 ? pos : $pos.before(d);
69+
if (d === 0) {
70+
return pos;
71+
}
72+
73+
const textblock = $pos.node(d);
74+
const textblockPos = $pos.before(d);
75+
76+
const isInFirstHalf = pos <= textblockPos + textblock.nodeSize / 2;
77+
78+
const start = isInFirstHalf ? textblockPos : $pos.after(d);
7079

7180
const gap = start ? insertPoint(doc, start, nodeType) : start;
7281

0 commit comments

Comments
 (0)