: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(notebooks): copy only the selected text inside a table cell (#118119)
posthog[bot]3dc4cebe92a2
diff --git a/frontend/src/lib/components/MarkdownNotebook/domSelection.test.ts b/frontend/src/lib/components/MarkdownNotebook/domSelection.test.tsindex 39eb132f7b70..3cf811f7fd7c 100644--- a/frontend/src/lib/components/MarkdownNotebook/domSelection.test.ts+++ b/frontend/src/lib/components/MarkdownNotebook/domSelection.test.ts@@ -1,11 +1,12 @@import {findTextPosition,getSelectedCodeRanges,+ getSelectedNotebookMarkdown,getSelectionRange,getTextOffset,restoreSelection,} from './domSelection'-import { NotebookCodeBlockNode } from './types'+import { NotebookCodeBlockNode, NotebookTableBlockNode } from './types'function codeNode(language: string, text: string): NotebookCodeBlockNode {return { id: 'code-1', type: 'code', language, text }@@ -94,3 +95,51 @@ describe('getSelectedCodeRanges', () => {expect(getSelectedCodeRanges(selection, [node], { 'code-1': element })).toHaveLength(expectedRanges)})})++describe('getSelectedNotebookMarkdown in tables', () => {+ afterEach(() => {+ window.getSelection()?.removeAllRanges()+ document.body.innerHTML = ''+ })++ it.each([+ { label: 'part of one cell', endCellIndex: 0, endOffset: 3, expected: null },+ { label: 'two cells', endCellIndex: 1, endOffset: 2, expected: '| Name | Value |\n| --- | --- |' },+ ])('copies the right content for a selection across $label', ({ endCellIndex, endOffset, expected }) => {+ const table: NotebookTableBlockNode = {+ id: 'table-1',+ type: 'table',+ headers: [{ children: [{ type: 'text', text: 'Name' }] }, { children: [{ type: 'text', text: 'Value' }] }],+ rows: [],+ }+ const notebookElement = document.createElement('div')+ const tableElement = document.createElement('div')+ const cells = table.headers.map((header, columnIndex) => {+ const cell = document.createElement('div')+ cell.dataset.markdownNotebookTableSection = 'header'+ cell.dataset.markdownNotebookTableRowIndex = '0'+ cell.dataset.markdownNotebookTableColumnIndex = String(columnIndex)+ cell.textContent = header.children[0].type === 'text' ? header.children[0].text : ''+ return cell+ })+ tableElement.append(...cells)+ notebookElement.appendChild(tableElement)+ document.body.appendChild(notebookElement)++ const range = document.createRange()+ range.setStart(cells[0].firstChild!, 1)+ range.setEnd(cells[endCellIndex].firstChild!, endOffset)+ const selection = window.getSelection()!+ selection.removeAllRanges()+ selection.addRange(range)++ const markdown = getSelectedNotebookMarkdown(+ selection,+ notebookElement,+ [table],+ { [table.id]: tableElement },+ {}+ )+ expect(markdown?.trim() ?? null).toEqual(expected)+ })+})diff --git a/frontend/src/lib/components/MarkdownNotebook/domSelection.ts b/frontend/src/lib/components/MarkdownNotebook/domSelection.tsindex 736be4060f6f..cea07db3e138 100644--- a/frontend/src/lib/components/MarkdownNotebook/domSelection.ts+++ b/frontend/src/lib/components/MarkdownNotebook/domSelection.ts@@ -21,6 +21,8 @@ import {} from './types'import { getInlineText, normalizeInlineNodes } from './utils'+const TABLE_CELL_SELECTOR = '[data-markdown-notebook-table-section]'+export function getNotebookBlockElement(rootElement: HTMLElement | null, nodeId: string): HTMLElement | null {return (rootElement?.querySelector<HTMLElement>(@@ -352,6 +354,11 @@ export function getSelectedNotebookMarkdown(return null}+ // Copy and cut inside one table cell use the native clipboard, so only the selected text is copied.+ if (isRangeInsideSingleTableCell(range)) {+ return null+ }+const selectedNodes: NotebookBlockNode[] = []nodes.forEach((node) => {const element = blockRefs[node.id]@@ -390,6 +397,11 @@ export function getSelectedNotebookMarkdown(return serializeNotebookNodes(selectedNodes)}+function isRangeInsideSingleTableCell(range: Range): boolean {+ const startCell = getElementForNode(range.startContainer)?.closest(TABLE_CELL_SELECTOR)+ return !!startCell && startCell === getElementForNode(range.endContainer)?.closest(TABLE_CELL_SELECTOR)+}+export function getSelectedTextRanges(selection: Selection | null,nodes: NotebookBlockNode[],