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): let people delete a table from a notebook (#124184)

Kyle Swank51800ea78256
diff --git a/frontend/src/lib/components/MarkdownNotebook/EditableTableBlock.tsx b/frontend/src/lib/components/MarkdownNotebook/EditableTableBlock.tsx
index ec7ed81152b1..9860211b4c08 100644
--- a/frontend/src/lib/components/MarkdownNotebook/EditableTableBlock.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/EditableTableBlock.tsx
@@ -12,7 +12,7 @@ import {
useState,
} from 'react'
-import { IconMinus, IconPlus } from '@posthog/icons'
+import { IconMinus, IconPlus, IconTrash } from '@posthog/icons'
import { LemonButton } from '@posthog/lemon-ui'
import { shouldUseMarkdownPaste } from './documentModel'
@@ -111,6 +111,7 @@ export function EditableTableBlock({
setBlockRef,
setTableCellRef,
updateNode,
+ deleteNode,
handleSelectionChange,
startTextSelectionPointer,
restoreSelectionRef,
@@ -120,6 +121,7 @@ export function EditableTableBlock({
setBlockRef: (element: HTMLElement | null) => void
setTableCellRef: (position: TableCellPosition, element: HTMLElement | null) => void
updateNode: (nodeId: string, updater: (node: NotebookBlockNode) => NotebookBlockNode | null) => void
+ deleteNode: () => void
handleSelectionChange: () => void
startTextSelectionPointer: (event: TextSelectionPointerStartEvent) => void
restoreSelectionRef: MutableRefObject<RestoreSelectionRequest | null>
@@ -481,6 +483,7 @@ export function EditableTableBlock({
label={control.label}
tooltip={control.tooltip}
icon={<IconPlus />}
+ dataAttr="markdown-notebook-table-add-row"
onClick={() => addTableRowAfter(control.rowIndex, 0)}
/>
</div>
@@ -499,6 +502,7 @@ export function EditableTableBlock({
label={control.label}
tooltip={control.tooltip}
icon={<IconPlus />}
+ dataAttr="markdown-notebook-table-add-column"
onClick={() => addTableColumnAfter(control.columnIndex)}
/>
</div>
@@ -519,6 +523,7 @@ export function EditableTableBlock({
label={`Remove row ${rowIndex + 1}`}
tooltip="Remove row"
icon={<IconMinus />}
+ dataAttr="markdown-notebook-table-remove-row"
onClick={() => removeTableRow(rowIndex)}
/>
</div>
@@ -540,6 +545,7 @@ export function EditableTableBlock({
label={`Remove column ${columnIndex + 1}`}
tooltip="Remove column"
icon={<IconMinus />}
+ dataAttr="markdown-notebook-table-remove-column"
disabledReason={
columnCount <= 1 ? 'Tables need at least one column' : undefined
}
@@ -548,6 +554,21 @@ export function EditableTableBlock({
</div>
)
})}
+ <div
+ className="MarkdownNotebook__table-remove-zone MarkdownNotebook__table-delete-zone"
+ style={tableControlStyle({
+ '--table-control-left': `${tableLeft}px`,
+ '--table-control-top': `${tableTop}px`,
+ })}
+ >
+ <TableStructureControlButton
+ label="Delete table"
+ tooltip="Delete table"
+ icon={<IconTrash />}
+ dataAttr="markdown-notebook-table-delete"
+ onClick={deleteNode}
+ />
+ </div>
</div>
) : null}
</div>
@@ -560,12 +581,14 @@ export function TableStructureControlButton({
label,
tooltip,
icon,
+ dataAttr,
disabledReason,
onClick,
}: {
label: string
tooltip: string
icon: JSX.Element
+ dataAttr: string
disabledReason?: string
onClick: () => void
}): JSX.Element {
@@ -573,6 +596,7 @@ export function TableStructureControlButton({
<LemonButton
aria-label={label}
className="MarkdownNotebook__table-structure-control"
+ data-attr={dataAttr}
disabledReason={disabledReason}
icon={icon}
noPadding
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
index 22d62af228d3..67b1af761b0b 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.scss
@@ -1453,6 +1453,19 @@ h3.MarkdownNotebook__text-block--heading {
transform: translateY(-50%);
}
+// Sits inside the top-left corner, because the scroll container clips anything outside the grid
+.MarkdownNotebook__table-delete-zone {
+ top: var(--table-control-top);
+ left: var(--table-control-left);
+ width: 1.5rem;
+ height: 1.5rem;
+}
+
+// Keep header text out from under the delete zone, so a click meant for the caret never deletes the table
+.MarkdownNotebook__table-block--editable th:first-child .MarkdownNotebook__table-cell-content {
+ padding-left: 1.75rem;
+}
+
.MarkdownNotebook__table-structure-control {
--lemon-button-height: 1.25rem;
--lemon-button-icon-size: 0.875rem;
@@ -1478,6 +1491,12 @@ h3.MarkdownNotebook__text-block--heading {
.MarkdownNotebook__table-add-zone:focus-within .MarkdownNotebook__table-structure-control,
.MarkdownNotebook__table-remove-zone:hover .MarkdownNotebook__table-structure-control,
.MarkdownNotebook__table-remove-zone:focus-within .MarkdownNotebook__table-structure-control,
+.MarkdownNotebook__table-block--editable:hover
+ .MarkdownNotebook__table-delete-zone
+ .MarkdownNotebook__table-structure-control,
+.MarkdownNotebook__table-block--editable:focus-within
+ .MarkdownNotebook__table-delete-zone
+ .MarkdownNotebook__table-structure-control,
.MarkdownNotebook__table-structure-control:focus-visible {
opacity: 1;
transform: scale(1);
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
index 193054e87ec2..ced2a5fe2382 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.test.ts
@@ -4539,6 +4539,47 @@ aXbc
expect(onChange).not.toHaveBeenCalled()
})
+ it.each([
+ [
+ 'deletes an empty table',
+ '| | |\n| --- | --- |\n| | |',
+ `${TEST_NOTEBOOK_TITLE_MARKDOWN}\n\nIntro paragraph`,
+ ],
+ ['keeps a table with content', '| A | |\n| --- | --- |\n| | |', undefined],
+ ])('%s on backspace', (_, tableMarkdown, expectedMarkdown) => {
+ const onChange = jest.fn()
+ const { container } = render(
+ createElement(MarkdownNotebook, {
+ value: withNotebookTitle(`Intro paragraph\n\n${tableMarkdown}`),
+ onChange,
+ })
+ )
+ const lastCell = Array.from(
+ container.querySelectorAll<HTMLElement>('.MarkdownNotebook__table-cell-content')
+ ).at(-1) as HTMLElement
+
+ placeCaretInElement(lastCell)
+ fireEvent.keyDown(lastCell, { key: 'Backspace' })
+
+ expect(container.querySelector('.MarkdownNotebook__table-block') === null).toEqual(!!expectedMarkdown)
+ expect(onChange.mock.calls.at(-1)?.[0]).toEqual(expectedMarkdown)
+ })
+
+ it('deletes a table with content from its delete control', () => {
+ const onChange = jest.fn()
+ const { container } = render(
+ createElement(MarkdownNotebook, {
+ value: withNotebookTitle('Intro paragraph\n\n| A | B |\n| --- | --- |\n| 1 | 2 |'),
+ onChange,
+ })
+ )
+
+ fireEvent.click(container.querySelector('button[aria-label="Delete table"]') as HTMLButtonElement)
+
+ expect(container.querySelector('.MarkdownNotebook__table-block')).toBeNull()
+ expect(onChange).toHaveBeenLastCalledWith(`${TEST_NOTEBOOK_TITLE_MARKDOWN}\n\nIntro paragraph`)
+ })
+
it('keeps newly inserted components active for keyboard row actions', () => {
const onChange = jest.fn()
const { container } = render(createElement(MarkdownNotebook, { value: withNotebookTitle(' '), onChange }))
diff --git a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
index 78fef0d2897f..972c93cab307 100644
--- a/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/MarkdownNotebook.tsx
@@ -234,6 +234,7 @@ import {
getTableCellRefKey,
getTableColumnCount,
getTableEdgeCellPosition,
+ isTableEmpty,
makeEmptyTableRow,
normalizeTableRow,
tableCellPositionsEqual,
@@ -4736,6 +4737,27 @@ function MarkdownNotebookEditor({
return true
}, [commitDocument, requestFocusAfterRemovingNode])
+ const deleteEmptyTableAtCurrentSelection = useCallback((): boolean => {
+ const element = getSelectedInlineEditableElementOfType(
+ notebookRef.current,
+ 'MarkdownNotebook__table-cell-content'
+ )
+ const nodeId = element?.dataset.markdownNotebookNodeId
+ if (!element || !nodeId) {
+ return false
+ }
+
+ const nodes = documentRef.current.nodes.length ? documentRef.current.nodes : [emptyNodeRef.current]
+ const node = nodes.find((currentNode) => currentNode.id === nodeId)
+ if (!node || node.type !== 'table' || !isTableEmpty(node)) {
+ return false
+ }
+
+ requestFocusAfterRemovingNode(nodeId)
+ deleteNodeWithRefCleanup(nodeId)
+ return true
+ }, [deleteNodeWithRefCleanup, requestFocusAfterRemovingNode])
+
const insertParagraphBelowTrailingCodeBlockAtCurrentSelection = useCallback((): boolean => {
const element = getSelectedInlineEditableElementOfType(notebookRef.current, 'MarkdownNotebook__code-block')
const nodeId = element?.dataset.markdownNotebookNodeId
@@ -6774,7 +6796,8 @@ function MarkdownNotebookEditor({
!event.ctrlKey &&
(deleteListItemRangeAtCurrentSelection() ||
deleteListItemAtCurrentSelection(event.key === 'Backspace' ? 'backward' : 'forward') ||
- deleteEmptyCodeBlockAtCurrentSelection())
+ deleteEmptyCodeBlockAtCurrentSelection() ||
+ deleteEmptyTableAtCurrentSelection())
) {
event.preventDefault()
event.stopPropagation()
diff --git a/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx b/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
index 346a783b0ed5..55f9c2c6e063 100644
--- a/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
+++ b/frontend/src/lib/components/MarkdownNotebook/renderNode.tsx
@@ -235,6 +235,7 @@ export function renderNode({
setBlockRef={setBlockRef}
setTableCellRef={setTableCellRef}
updateNode={updateNode}
+ deleteNode={deleteNodeAndFocusAdjacent}
handleSelectionChange={handleSelectionChange}
startTextSelectionPointer={startTextSelectionPointer}
restoreSelectionRef={restoreSelectionRef}
diff --git a/frontend/src/lib/components/MarkdownNotebook/tableModel.ts b/frontend/src/lib/components/MarkdownNotebook/tableModel.ts
index 0546d539e819..754b6efdd653 100644
--- a/frontend/src/lib/components/MarkdownNotebook/tableModel.ts
+++ b/frontend/src/lib/components/MarkdownNotebook/tableModel.ts
@@ -1,5 +1,6 @@
import { InsertMenuSelectionDirection, TableCellPosition } from './editorTypes'
import { NotebookTableBlockNode, NotebookTableCell } from './types'
+import { getInlineText } from './utils'
export function getTableCellRefKey(nodeId: string, position: TableCellPosition): string {
return `${nodeId}:${position.section}:${String(position.rowIndex)}:${String(position.columnIndex)}`
@@ -17,6 +18,10 @@ export function makeEmptyTableRow(columnCount: number): NotebookTableCell[] {
return Array.from({ length: columnCount }, () => ({ children: [] }))
}
+export function isTableEmpty(node: NotebookTableBlockNode): boolean {
+ return [node.headers, ...node.rows].every((row) => row.every((cell) => !getInlineText(cell.children).length))
+}
+
export function getTableCellPositions(node: NotebookTableBlockNode): TableCellPosition[] {
const columnCount = getTableColumnCount(node)
return [