Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(notebooks): make a Tab indent its own undo step (#71609)

willwearinge045097e8e64
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
index a8db7f51526f..f4b0ec79bc36 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
@@ -1613,6 +1613,32 @@ Repeated block`),
nowSpy.mockRestore()
})
+ it('undoes an accidental Tab indent without also reverting the preceding typing', () => {
+ // A Tab indent produces a text op on the list block, indistinguishable to the differ
+ // from typing — so without a discrete undo step it folds into the typing run and one
+ // Cmd+Z reverts the typed text too. The frozen clock keeps both edits in the same
+ // coalescing window, which is exactly when the regression bites.
+ const nowSpy = jest.spyOn(Date, 'now').mockReturnValue(10_000)
+ const onChange = jest.fn()
+ const { container } = render(
+ createElement(MarkdownNotebook, { value: withNotebookTitle('- one\n- t'), onChange })
+ )
+
+ const secondItem = getEditableListItems(container)[1]
+ secondItem.focus()
+ secondItem.textContent = 'two'
+ fireEvent.input(secondItem)
+ pressTabInListItem(getEditableListItems(container)[1], 0)
+ expect(onChange).toHaveBeenLastCalledWith(withNotebookTitle('- one\n - two'))
+
+ fireEvent.keyDown(getEditableListItems(container)[1], { key: 'z', metaKey: true })
+
+ // Only the indent is undone; "two" survives as its own separate undo step.
+ expect(onChange).toHaveBeenLastCalledWith(withNotebookTitle('- one\n- two'))
+
+ nowSpy.mockRestore()
+ })
+
it('reports a block-level caret when a component block is focused', () => {
const onCaretChange = jest.fn()
const { container } = render(
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index 016700dff0e5..2194f419bb63 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -293,6 +293,10 @@ type CommitDocumentOptions = {
/** Set when the commit applies a remote merge: the notebook version being merged in.
* Remote caret pings already at this version reflect the change and must not be remapped. */
remoteMergeVersion?: number
+ /** Structural edits (e.g. a Tab indent) look like text edits to the differ, so they would
+ * otherwise fold into an adjacent typing run and stop being independently undoable. Pass
+ * false to force a discrete undo step. */
+ coalesce?: boolean
}
type RemoteCaretAnchor = {
@@ -1098,7 +1102,8 @@ function MarkdownNotebookEditor({
(
previousDocument: NotebookDocument,
nextDocument: NotebookDocument,
- historyOperations?: NotebookOperation[]
+ historyOperations?: NotebookOperation[],
+ coalesce: boolean = true
): void => {
const inverseOps = historyOperations ?? diffNotebookDocuments(nextDocument, previousDocument)
if (!inverseOps.length) {
@@ -1107,8 +1112,10 @@ function MarkdownNotebookEditor({
const now = Date.now()
const onlyOp = inverseOps.length === 1 ? inverseOps[0] : null
+ // A non-coalescing entry stays its own undo step: it never folds into the previous
+ // entry, and a null coalesceNodeId keeps the next typing run from folding into it.
const coalesceNodeId =
- onlyOp && (onlyOp.type === 'text' || onlyOp.type === 'replace_block') ? onlyOp.nodeId : null
+ coalesce && onlyOp && (onlyOp.type === 'text' || onlyOp.type === 'replace_block') ? onlyOp.nodeId : null
const lastEntry = historyRef.current.undo[historyRef.current.undo.length - 1]
if (
coalesceNodeId &&
@@ -1153,7 +1160,12 @@ function MarkdownNotebookEditor({
const editableDocument = ensureEditableNotebookDocument(nextDocument)
const previousDocument = documentRef.current
if (options.addToHistory ?? true) {
- pushHistoryEntry(previousDocument, editableDocument, options.historyOperations)
+ pushHistoryEntry(
+ previousDocument,
+ editableDocument,
+ options.historyOperations,
+ options.coalesce ?? true
+ )
}
// Rendered remote carets ride along with the text they sit in.
mapRemoteCaretAnchors(previousDocument, editableDocument, options.remoteMergeVersion)
@@ -1802,12 +1814,17 @@ function MarkdownNotebookEditor({
start: offset,
end: offset,
}
- commitDocument({
- ...currentDocument,
- nodes: nodes.map((currentNode) =>
- currentNode.id === node.id ? { ...node, items: nextItems } : currentNode
- ),
- })
+ commitDocument(
+ {
+ ...currentDocument,
+ nodes: nodes.map((currentNode) =>
+ currentNode.id === node.id ? { ...node, items: nextItems } : currentNode
+ ),
+ },
+ // A Tab indent must be its own undo step, not folded into the typing run that
+ // preceded it — otherwise Cmd+Z can't undo just the accidental indent.
+ { coalesce: false }
+ )
return true
},
[commitDocument]