Skip to content

Commit ba2c76d

Browse files
authored
fix order list counter (TypeCellOS#42)
Co-authored-by: Richard Meng <richard@owl3d.ai>
1 parent 84895a3 commit ba2c76d

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

src/extensions/Blocks/OrderedListPlugin.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@ export const OrderedListPlugin = () => {
88
appendTransaction: (_transactions, _oldState, newState) => {
99
const newTr = newState.tr;
1010
let modified = false;
11-
let count = 1;
1211
let skip = 0;
13-
newState.doc.descendants((node, pos) => {
12+
13+
let countPerBlockGroup = new Map();
14+
15+
newState.doc.descendants((node, pos, parent) => {
1416
if (node.type.name === "block" && !node.attrs.listType) {
15-
count = 1;
17+
// reset for non consecutive oli blocks
18+
countPerBlockGroup.set(parent, 1);
1619
}
1720
if (
1821
skip === 0 &&
1922
node.type.name === "block" &&
2023
node.attrs.listType === "oli"
2124
) {
2225
skip = node.content.childCount;
26+
27+
// create count for block group if not exist
28+
if (!countPerBlockGroup.has(parent)) {
29+
countPerBlockGroup.set(parent, 1);
30+
}
31+
32+
let count = countPerBlockGroup.get(parent);
33+
2334
// This assumes that the content node is always the first child of the oli block,
2435
// as the content model grows this assumption may need to change
2536
if (node.content.child(0).attrs.position !== `${count}.`) {
@@ -32,7 +43,7 @@ export const OrderedListPlugin = () => {
3243
modified = true;
3344
}
3445

35-
count++;
46+
countPerBlockGroup.set(parent, count + 1);
3647
} else if (skip > 0) {
3748
skip--;
3849
}

0 commit comments

Comments
 (0)