Skip to content

Commit

Permalink
fix(notebook): Disable public/private button
Browse files Browse the repository at this point in the history
Won't be able to implement feature until NotebookEdit is finalised

Signed-off-by: Gordon Smith <GordonJSmith@gmail.com>
  • Loading branch information
GordonSmith committed Aug 19, 2022
1 parent 72ffebc commit cee7265
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,17 @@
"icon": {
"dark": "./resources/dark/eye.svg",
"light": "./resources/light/eye.svg"
}
},
"enablement": "false"
},
{
"command": "notebook.cell.private",
"title": "%Private%",
"icon": {
"dark": "./resources/dark/eye-closed.svg",
"light": "./resources/light/eye-closed.svg"
}
},
"enablement": "false"
}
],
"menus": {
Expand Down Expand Up @@ -1258,4 +1260,4 @@
}
]
}
}
}
2 changes: 2 additions & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"Pin launch configuration": "Pin launch configuration",
"Pin launch configuration to current document": "Pin launch configuration to current document",
"Pinned launch configurations": "Pinned launch configurations",
"Private": "Private",
"Public": "Public",
"Refresh": "Refresh",
"Refresh Tree": "Refresh Tree",
"Reject unauthorized calls e.g. SSL certificate errors": "Reject unauthorized calls e.g. SSL certificate errors",
Expand Down
1 change: 0 additions & 1 deletion src/notebook-renderers/WUOutputTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,3 @@ export const WUOutputTables: React.FunctionComponent<WUOutput> = (output: WUOutp
}
</Pivot>;
};

2 changes: 1 addition & 1 deletion src/notebook/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class Controller {
readonly label = "ECL Notebook";
readonly supportedLanguages = ["ecl", "ojs"];

private readonly _controller: vscode.NotebookController;
readonly _controller: vscode.NotebookController;
private _executionOrder = 0;

constructor() {
Expand Down
30 changes: 27 additions & 3 deletions src/notebook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { Controller } from "./controller";
import { Serializer } from "./serializer";

export function activate(ctx: vscode.ExtensionContext) {
ctx.subscriptions.push(new Controller());
const controller = new Controller();
ctx.subscriptions.push(controller);
ctx.subscriptions.push(vscode.workspace.registerNotebookSerializer("ecl-notebook", new Serializer()));

let publicCell: boolean = true;
Expand All @@ -12,14 +13,14 @@ export function activate(ctx: vscode.ExtensionContext) {
vscode.commands.executeCommand("setContext", "isPrivate", !publicCell);

vscode.commands.registerCommand("notebook.cell.public", (cell: vscode.NotebookCell) => {
// cell.metadata["private"] = true;
cell.metadata["custom"]["public"] = false;
publicCell = false;
vscode.commands.executeCommand("setContext", "isPublic", publicCell);
vscode.commands.executeCommand("setContext", "isPrivate", !publicCell);
});

vscode.commands.registerCommand("notebook.cell.private", (cell: vscode.NotebookCell) => {
// cell.metadata["private"] = false;
cell.metadata["custom"]["public"] = true;
publicCell = true;
vscode.commands.executeCommand("setContext", "isPublic", publicCell);
vscode.commands.executeCommand("setContext", "isPrivate", !publicCell);
Expand All @@ -31,4 +32,27 @@ export function activate(ctx: vscode.ExtensionContext) {
console.log(JSON.stringify(cell.metadata));
}
});

vscode.workspace.onDidChangeNotebookDocument(onDidChangeNotebookCells, undefined, ctx.subscriptions);
}

function onDidChangeNotebookCells(e: vscode.NotebookDocumentChangeEvent) {
e.contentChanges.forEach(change => {
change.addedCells.forEach(cell => {
// const cellMetadata = getCellMetadata(cell);
// const edit = new vscode.WorkspaceEdit();
// // Don't edit the metadata directly, always get a clone (prevents accidental singletons and directly editing the objects).
// const updatedMetadata: CellMetadata = { ...JSON.parse(JSON.stringify(cellMetadata || {})) };
// edit.set(cell.notebook.uri, [vscode.NotebookEdit.updateCellMetadata(cell.index, { ...(cell.metadata), custom: updatedMetadata })]);
// vscode.workspace.applyEdit(edit);
});
});
}

export interface CellMetadata {
public: boolean;
}

function getCellMetadata(cell: vscode.NotebookCell | vscode.NotebookCellData) {
return cell.metadata?.custom as CellMetadata | undefined;
}
11 changes: 9 additions & 2 deletions src/notebook/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface RawNotebookCell {
kind: vscode.NotebookCellKind;
language: string;
value: string;
metadata: { [key: string]: any };
outputs: RawNotebookOutput[];
}

Expand All @@ -27,6 +28,8 @@ export class Serializer implements vscode.NotebookSerializer {

const cells = raw.map(item => {
const retVal = new vscode.NotebookCellData(item.kind, item.value, item.language);
retVal.metadata = retVal.metadata || {};
retVal.metadata.custom = retVal.metadata.custom || {};
if (item.outputs) {
const cellOutput = new vscode.NotebookCellOutput(item.outputs.map(item => new vscode.NotebookCellOutputItem(Uint8Array.from(item.data), item.mime)));
retVal.outputs = [cellOutput];
Expand All @@ -38,6 +41,7 @@ export class Serializer implements vscode.NotebookSerializer {
}

serializeNotebook(data: vscode.NotebookData, _token: vscode.CancellationToken): Uint8Array {

const contents: RawNotebookCell[] = [];

for (const cell of data.cells) {
Expand All @@ -49,12 +53,15 @@ export class Serializer implements vscode.NotebookSerializer {

});

contents.push({
const sCell = {
kind: cell.kind,
language: cell.languageId,
value: cell.value,
metadata: cell.metadata || {},
outputs
});
};
sCell.metadata.custom = sCell.metadata.custom || {};
contents.push(sCell);
}

return new TextEncoder().encode(JSON.stringify(contents));
Expand Down

0 comments on commit cee7265

Please sign in to comment.