|
| 1 | +import { injectable } from 'inversify'; |
| 2 | +import { |
| 3 | + env, |
| 4 | + NotebookEdit, |
| 5 | + NotebookEditor, |
| 6 | + NotebookRendererMessaging, |
| 7 | + notebooks, |
| 8 | + workspace, |
| 9 | + WorkspaceEdit |
| 10 | +} from 'vscode'; |
| 11 | + |
| 12 | +import { IExtensionSyncActivationService } from '../../../platform/activation/types'; |
| 13 | +import { IDisposable } from '../../../platform/common/types'; |
| 14 | +import { dispose } from '../../../platform/common/utils/lifecycle'; |
| 15 | +import { logger } from '../../../platform/logging'; |
| 16 | + |
| 17 | +type SelectPageSizeCommand = { |
| 18 | + command: 'selectPageSize'; |
| 19 | + cellIndex: number; |
| 20 | + size: number; |
| 21 | +}; |
| 22 | + |
| 23 | +type GoToPageCommand = { |
| 24 | + command: 'goToPage'; |
| 25 | + cellIndex: number; |
| 26 | + page: number; |
| 27 | +}; |
| 28 | + |
| 29 | +type CopyTableDataCommand = { |
| 30 | + command: 'copyTableData'; |
| 31 | + data: string; |
| 32 | +}; |
| 33 | + |
| 34 | +type ExportDataframeCommand = { |
| 35 | + command: 'exportDataframe'; |
| 36 | + cellIndex: number; |
| 37 | +}; |
| 38 | + |
| 39 | +type DataframeCommand = SelectPageSizeCommand | GoToPageCommand | CopyTableDataCommand | ExportDataframeCommand; |
| 40 | + |
| 41 | +@injectable() |
| 42 | +export class DataframeRendererComms implements IExtensionSyncActivationService { |
| 43 | + private readonly disposables: IDisposable[] = []; |
| 44 | + |
| 45 | + public dispose() { |
| 46 | + dispose(this.disposables); |
| 47 | + } |
| 48 | + |
| 49 | + activate() { |
| 50 | + const comms = notebooks.createRendererMessaging('deepnote-dataframe-renderer'); |
| 51 | + comms.onDidReceiveMessage(this.onDidReceiveMessage.bind(this, comms), this, this.disposables); |
| 52 | + } |
| 53 | + |
| 54 | + private onDidReceiveMessage( |
| 55 | + _comms: NotebookRendererMessaging, |
| 56 | + { editor, message }: { editor: NotebookEditor; message: DataframeCommand } |
| 57 | + ) { |
| 58 | + logger.info('DataframeRendererComms received message', message); |
| 59 | + |
| 60 | + if (!message || typeof message !== 'object') { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + switch (message.command) { |
| 65 | + case 'selectPageSize': |
| 66 | + this.handleSelectPageSize(editor, message); |
| 67 | + break; |
| 68 | + case 'goToPage': |
| 69 | + this.handleGoToPage(editor, message); |
| 70 | + break; |
| 71 | + case 'copyTableData': |
| 72 | + this.handleCopyTableData(message); |
| 73 | + break; |
| 74 | + case 'exportDataframe': |
| 75 | + this.handleExportDataframe(editor, message); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private async handleSelectPageSize(editor: NotebookEditor, message: SelectPageSizeCommand) { |
| 81 | + const cell = editor.notebook.cellAt(message.cellIndex); |
| 82 | + logger.info( |
| 83 | + `[DataframeRenderer] selectPageSize called for cell ${ |
| 84 | + message.cellIndex |
| 85 | + } (${cell?.document.uri.toString()}), size=${message.size}` |
| 86 | + ); |
| 87 | + |
| 88 | + // Store page size in cell metadata |
| 89 | + if (cell) { |
| 90 | + const edit = new WorkspaceEdit(); |
| 91 | + const notebookEdit = NotebookEdit.updateCellMetadata(message.cellIndex, { |
| 92 | + ...cell.metadata, |
| 93 | + dataframePageSize: message.size |
| 94 | + }); |
| 95 | + edit.set(editor.notebook.uri, [notebookEdit]); |
| 96 | + await workspace.applyEdit(edit); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private handleGoToPage(editor: NotebookEditor, message: GoToPageCommand) { |
| 101 | + const cell = editor.notebook.cellAt(message.cellIndex); |
| 102 | + logger.info( |
| 103 | + `[DataframeRenderer] goToPage called for cell ${ |
| 104 | + message.cellIndex |
| 105 | + } (${cell?.document.uri.toString()}), page=${message.page}` |
| 106 | + ); |
| 107 | + // Could store current page in cell metadata if needed |
| 108 | + } |
| 109 | + |
| 110 | + private async handleCopyTableData(message: CopyTableDataCommand) { |
| 111 | + logger.info(`[DataframeRenderer] copyTableData called, data length=${message.data.length} characters`); |
| 112 | + await env.clipboard.writeText(message.data); |
| 113 | + } |
| 114 | + |
| 115 | + private handleExportDataframe(editor: NotebookEditor, message: ExportDataframeCommand) { |
| 116 | + const cell = editor.notebook.cellAt(message.cellIndex); |
| 117 | + logger.info( |
| 118 | + `[DataframeRenderer] exportDataframe called for cell ${ |
| 119 | + message.cellIndex |
| 120 | + } (${cell?.document.uri.toString()})` |
| 121 | + ); |
| 122 | + // TODO: Implement dataframe export functionality |
| 123 | + } |
| 124 | +} |
0 commit comments