From d3b733cfb8a70e6895b93640e121e6d667af8858 Mon Sep 17 00:00:00 2001 From: RRosio Date: Thu, 12 Dec 2024 10:49:18 -0800 Subject: [PATCH 1/2] separate copy and clipboard logic --- src/treeContext.ts | 46 +++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/treeContext.ts b/src/treeContext.ts index d447b02..a0b4da0 100644 --- a/src/treeContext.ts +++ b/src/treeContext.ts @@ -49,25 +49,47 @@ export class FssContextMenu { const canonical = protocol + '/' + this.root.dataset.fss.replace(/^\/+/, () => ''); this.copiedPath = canonical; - navigator.clipboard.writeText(canonical).then( + } + } + + copyPathToClipboard() { + this.copyPath(); + const path = this.copiedPath; + + if (path) { + navigator.clipboard.writeText(path).then( () => { // Success - console.log('Copy path: ' + canonical); + console.log('Copy path: ' + path); this.root.remove(); }, () => { - console.log('Copy path failed: ' + canonical); + console.log('Copy path failed: ' + path); this.root.remove(); } ); } } + insertCodeBlock(codeBlock: string) { + // Determine if there is an active notebook and cell to paste to + const notebookPanel = this.notebookTracker.currentWidget; + if (notebookPanel) { + const activeCell = notebookPanel.content.activeCell; + if (activeCell) { + const cellContent = activeCell.model.sharedModel.getSource(); + const newCellContent = cellContent + '\n' + codeBlock; + activeCell.model.sharedModel.setSource(newCellContent); + console.log('Updated cell content to: ', newCellContent); + } + } + } copyOpenCodeBlock() { this.copyPath(); + const path = this.copiedPath; - if (this.copiedPath) { - const openCodeBlock = `with fsspec.open("${this.copiedPath}", "rt") as f:\n for line in f:\n print(line)`; + if (path) { + const openCodeBlock = `with fsspec.open("${path}", "rt") as f:\n for line in f:\n print(line)`; navigator.clipboard.writeText(openCodeBlock).then( () => { console.log('Copied `open` code block'); @@ -80,17 +102,7 @@ export class FssContextMenu { } ); - // Determine if there is an active notebook and cell to paste to - const notebookPanel = this.notebookTracker.currentWidget; - if (notebookPanel) { - const activeCell = notebookPanel.content.activeCell; - if (activeCell) { - const cellContent = activeCell.model.sharedModel.getSource(); - const newCellContent = cellContent + '\n' + openCodeBlock; - activeCell.model.sharedModel.setSource(newCellContent); - console.log('Updated cell content to: ', newCellContent); - } - } + this.insertCodeBlock(openCodeBlock); } else { console.log('Failed to copy `open` code block'); this.root.remove(); @@ -100,7 +112,7 @@ export class FssContextMenu { handleItemClick(event: any) { // TODO multiple menu it if (event.target.dataset.fssContextType === 'copyPath') { - this.copyPath(); + this.copyPathToClipboard(); } else if (event.target.dataset.fssContextType === 'copyOpenCodeBlock') { this.copyOpenCodeBlock(); } From ef0076f062de1c580095d5ca6b65c035b206053b Mon Sep 17 00:00:00 2001 From: RRosio Date: Thu, 12 Dec 2024 10:54:35 -0800 Subject: [PATCH 2/2] remove unnecessary class variable logic --- src/treeContext.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/treeContext.ts b/src/treeContext.ts index a0b4da0..fe5cd8a 100644 --- a/src/treeContext.ts +++ b/src/treeContext.ts @@ -5,7 +5,6 @@ export class FssContextMenu { root: any; clicked = false; model: any; - copiedPath: string; notebookTracker: any; constructor(model: any, notebookTracker: INotebookTracker) { @@ -13,7 +12,6 @@ export class FssContextMenu { root.classList.add('jfss-tree-context-menu'); this.root = root; this.model = model; - this.copiedPath = ''; this.notebookTracker = notebookTracker; const menuItem = this.createMenuItem('Copy Path', 'copyPath'); @@ -48,13 +46,12 @@ export class FssContextMenu { if (protocol) { const canonical = protocol + '/' + this.root.dataset.fss.replace(/^\/+/, () => ''); - this.copiedPath = canonical; + return canonical; } } copyPathToClipboard() { - this.copyPath(); - const path = this.copiedPath; + const path = this.copyPath(); if (path) { navigator.clipboard.writeText(path).then( @@ -85,8 +82,7 @@ export class FssContextMenu { } } copyOpenCodeBlock() { - this.copyPath(); - const path = this.copiedPath; + const path = this.copyPath(); if (path) { const openCodeBlock = `with fsspec.open("${path}", "rt") as f:\n for line in f:\n print(line)`;