Skip to content

Commit e50c0ce

Browse files
committed
refactor: only report moves, not indents & outdents
1 parent 998c737 commit e50c0ce

7 files changed

+38
-61
lines changed

packages/core/src/api/__snapshots__/blocks-indented-changed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@
124124
"source": {
125125
"type": "local",
126126
},
127-
"type": "indent",
127+
"type": "move",
128128
},
129129
]

packages/core/src/api/__snapshots__/blocks-moved-deeper-into-nesting.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,6 @@
159159
"source": {
160160
"type": "local",
161161
},
162-
"type": "indent",
162+
"type": "move",
163163
},
164164
]

packages/core/src/api/__snapshots__/blocks-moved-multiple-in-same-transaction.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"source": {
9191
"type": "local",
9292
},
93-
"type": "outdent",
93+
"type": "move",
9494
},
9595
{
9696
"block": {
@@ -183,6 +183,6 @@
183183
"source": {
184184
"type": "local",
185185
},
186-
"type": "outdent",
186+
"type": "move",
187187
},
188188
]

packages/core/src/api/__snapshots__/blocks-moved-to-different-parent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@
7373
"source": {
7474
"type": "local",
7575
},
76-
"type": "outdent",
76+
"type": "move",
7777
},
7878
]

packages/core/src/api/__snapshots__/blocks-moved-to-root-level.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@
7373
"source": {
7474
"type": "local",
7575
},
76-
"type": "outdent",
76+
"type": "move",
7777
},
7878
]

packages/core/src/api/__snapshots__/blocks-outdented-changed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,6 @@
124124
"source": {
125125
"type": "local",
126126
},
127-
"type": "outdent",
127+
"type": "move",
128128
},
129129
]

packages/core/src/api/nodeUtil.ts

Lines changed: 31 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ export type BlocksChanged<
104104
prevBlock: undefined;
105105
}
106106
| {
107-
type: "update" | "move";
107+
type: "update";
108108
/**
109109
* The previous block.
110110
*/
111111
prevBlock: Block<BSchema, ISchema, SSchema>;
112112
}
113113
| {
114-
type: "indent" | "outdent";
114+
type: "move";
115115
/**
116116
* The previous block.
117117
*/
@@ -169,30 +169,33 @@ function determineChangeSource(transaction: Transaction): BlockChangeSource {
169169
return { type: "local" };
170170
}
171171

172-
type BlockInfo<
173-
BSchema extends BlockSchema,
174-
ISchema extends InlineContentSchema,
175-
SSchema extends StyleSchema,
176-
> = {
177-
block: Block<BSchema, ISchema, SSchema>;
178-
parentId: string | undefined;
179-
depth: number;
180-
};
181-
182172
function collectAllBlocks<
183173
BSchema extends BlockSchema,
184174
ISchema extends InlineContentSchema,
185175
SSchema extends StyleSchema,
186-
>(doc: Node): Record<string, BlockInfo<BSchema, ISchema, SSchema>> {
187-
const blocks: Record<string, BlockInfo<BSchema, ISchema, SSchema>> = {};
176+
>(
177+
doc: Node,
178+
): Record<
179+
string,
180+
{
181+
block: Block<BSchema, ISchema, SSchema>;
182+
parentId: string | undefined;
183+
}
184+
> {
185+
const blocks: Record<
186+
string,
187+
{
188+
block: Block<BSchema, ISchema, SSchema>;
189+
parentId: string | undefined;
190+
}
191+
> = {};
188192
const pmSchema = getPmSchema(doc);
189193
doc.descendants((node, pos) => {
190194
if (isNodeBlock(node)) {
191195
const parentId = getParentBlockId(doc, pos);
192196
blocks[node.attrs.id] = {
193197
block: nodeToBlock(node, pmSchema),
194198
parentId,
195-
depth: doc.resolve(pos).depth,
196199
};
197200
}
198201
return true;
@@ -257,46 +260,20 @@ export function getBlocksChangedByTransaction<
257260
const prev = prevBlocks[id];
258261
const next = nextBlocks[id];
259262
const isParentDifferent = prev.parentId !== next.parentId;
260-
const isDepthDifferent = prev.depth !== next.depth;
261263

262-
if (isParentDifferent || isDepthDifferent) {
263-
const isDepthIncrease = next.depth > prev.depth;
264-
const isDepthDecrease = next.depth < prev.depth;
265-
266-
if (isDepthIncrease) {
267-
changes.push({
268-
type: "indent",
269-
block: next.block,
270-
prevBlock: prev.block,
271-
source,
272-
prevParent: prev.parentId
273-
? prevBlocks[prev.parentId]?.block
274-
: undefined,
275-
currentParent: next.parentId
276-
? nextBlocks[next.parentId]?.block
277-
: undefined,
278-
});
279-
} else if (isDepthDecrease) {
280-
changes.push({
281-
type: "outdent",
282-
block: next.block,
283-
prevBlock: prev.block,
284-
source,
285-
prevParent: prev.parentId
286-
? prevBlocks[prev.parentId]?.block
287-
: undefined,
288-
currentParent: next.parentId
289-
? nextBlocks[next.parentId]?.block
290-
: undefined,
291-
});
292-
} else {
293-
changes.push({
294-
type: "move",
295-
block: next.block,
296-
prevBlock: prev.block,
297-
source,
298-
});
299-
}
264+
if (isParentDifferent) {
265+
changes.push({
266+
type: "move",
267+
block: next.block,
268+
prevBlock: prev.block,
269+
source,
270+
prevParent: prev.parentId
271+
? prevBlocks[prev.parentId]?.block
272+
: undefined,
273+
currentParent: next.parentId
274+
? nextBlocks[next.parentId]?.block
275+
: undefined,
276+
});
300277
} else if (areBlocksDifferentExcludingChildren(prev.block, next.block)) {
301278
changes.push({
302279
type: "update",

0 commit comments

Comments
 (0)