Skip to content

Commit

Permalink
Merge pull request #60 from RRosio/refactor_copyPath
Browse files Browse the repository at this point in the history
Separate and clean up copy and clipboard logic
  • Loading branch information
RRosio authored Dec 12, 2024
2 parents 1dc7f64 + ef0076f commit 43adc75
Showing 1 changed file with 29 additions and 21 deletions.
50 changes: 29 additions & 21 deletions src/treeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ export class FssContextMenu {
root: any;
clicked = false;
model: any;
copiedPath: string;
notebookTracker: any;

constructor(model: any, notebookTracker: INotebookTracker) {
const root = document.createElement('div');
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');
Expand Down Expand Up @@ -48,26 +46,46 @@ export class FssContextMenu {
if (protocol) {
const canonical =
protocol + '/' + this.root.dataset.fss.replace(/^\/+/, () => '');
this.copiedPath = canonical;
navigator.clipboard.writeText(canonical).then(
return canonical;
}
}

copyPathToClipboard() {
const path = this.copyPath();

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.copyPath();

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');
Expand All @@ -80,17 +98,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();
Expand All @@ -100,7 +108,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();
}
Expand Down

0 comments on commit 43adc75

Please sign in to comment.