@oceanum/oceanumlab 4.7.1 → 4.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/lib/DatameshWidget.js +14 -5
  2. package/lib/StoredNotebooks.d.ts +9 -5
  3. package/lib/StoredNotebooks.js +49 -21
  4. package/lib/auth/serverAuth.js +1 -0
  5. package/lib/auth/startSignIn.d.ts +11 -0
  6. package/lib/auth/startSignIn.js +16 -0
  7. package/lib/auth/tokens.d.ts +7 -0
  8. package/lib/index.d.ts +1 -1
  9. package/lib/index.js +8 -6
  10. package/lib/share/client.d.ts +84 -0
  11. package/lib/share/client.js +205 -0
  12. package/lib/share/events.d.ts +7 -0
  13. package/lib/share/events.js +9 -0
  14. package/lib/share/notebook.d.ts +184 -0
  15. package/lib/share/notebook.js +277 -0
  16. package/lib/share/oceanumOnly.d.ts +49 -0
  17. package/lib/share/oceanumOnly.js +90 -0
  18. package/lib/share/plugin.d.ts +21 -0
  19. package/lib/share/plugin.js +883 -0
  20. package/lib/share/widgets.d.ts +25 -0
  21. package/lib/share/widgets.js +154 -0
  22. package/package.json +5 -1
  23. package/src/DatameshWidget.tsx +21 -2
  24. package/src/StoredNotebooks.tsx +60 -20
  25. package/src/__tests__/OceanumPanel.spec.tsx +60 -4
  26. package/src/__tests__/StoredNotebooks.spec.tsx +157 -0
  27. package/src/__tests__/oceanumOnly.spec.ts +193 -0
  28. package/src/__tests__/shareClient.spec.ts +314 -0
  29. package/src/__tests__/shareNotebook.spec.ts +451 -0
  30. package/src/__tests__/sharePlugin.spec.ts +426 -0
  31. package/src/auth/serverAuth.ts +1 -0
  32. package/src/auth/startSignIn.ts +23 -0
  33. package/src/auth/tokens.ts +7 -0
  34. package/src/index.ts +8 -6
  35. package/src/share/client.ts +288 -0
  36. package/src/share/events.ts +10 -0
  37. package/src/share/notebook.ts +416 -0
  38. package/src/share/oceanumOnly.ts +126 -0
  39. package/src/share/plugin.ts +1096 -0
  40. package/src/share/widgets.ts +217 -0
  41. package/style/index.css +111 -16
  42. package/lib/specStore.d.ts +0 -37
  43. package/lib/specStore.js +0 -57
  44. package/src/__tests__/specStore.spec.ts +0 -126
  45. package/src/specStore.ts +0 -82
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Notebook records in the Oceanum spec store: the link a local notebook keeps to its
3
+ * record, the body that is stored, and the names, paths and addresses around them.
4
+ *
5
+ * Ported from oceanum-notebook's share-oceanum package, which notebook.oceanum.io will
6
+ * drop in favour of this one.
7
+ */
8
+ import type { INotebookContent } from '@jupyterlab/nbformat';
9
+ /** Notebook metadata key linking a local notebook to its spec store record. */
10
+ export declare const METADATA_KEY = "oceanum";
11
+ /** Folder that notebooks opened from Oceanum.io are written to. */
12
+ export declare const OCEANUM_DIR = "Oceanum";
13
+ /** Query parameter carrying a shared notebook id. */
14
+ export declare const QUERY_PARAM = "oceanum-notebook";
15
+ /**
16
+ * Largest request body we send. Cloud Run rejects requests over 32 MiB; keep a margin
17
+ * for headers and the server's own encoding.
18
+ */
19
+ export declare const MAX_BODY_BYTES: number;
20
+ /** The `oceanum` notebook metadata value. */
21
+ export interface IOceanumLink {
22
+ spec_id: string;
23
+ /** Spec store the id belongs to, so a development id is never PUT to production. */
24
+ specs_url: string;
25
+ description?: string;
26
+ }
27
+ /** A spec store request body for `POST`/`PUT /specs/notebook`. */
28
+ export interface ISpecBody {
29
+ name: string;
30
+ description: string | null;
31
+ spec: INotebookContent;
32
+ }
33
+ /**
34
+ * The fields a `PATCH /specs/notebook/{id}` may change. A field left out keeps its
35
+ * stored value, so a rename need not send the notebook back.
36
+ *
37
+ * The store refuses anything else outright rather than ignoring it, and refuses a patch
38
+ * that carries no field at all, so never send an empty one. `spec` is replaced whole:
39
+ * there is no way to change one key of a stored notebook.
40
+ */
41
+ export interface ISpecPatch {
42
+ name?: string;
43
+ description?: string | null;
44
+ spec?: INotebookContent;
45
+ }
46
+ /** A spec store list entry (the list omits `spec`). */
47
+ export interface ISpecSummary {
48
+ id: string;
49
+ name: string;
50
+ description: string | null;
51
+ /**
52
+ * When the record was created, or last patched. A `PUT` does not touch it, so for a
53
+ * record nothing has patched this is still a creation time.
54
+ */
55
+ modified: string;
56
+ /** Null unless the caller created the record: the store hides other creators. */
57
+ creator: string | null;
58
+ }
59
+ /** A full spec store record. */
60
+ export interface ISpecRecord extends ISpecSummary {
61
+ spec: unknown;
62
+ }
63
+ export type BuildResult = {
64
+ ok: true;
65
+ body: ISpecBody;
66
+ bytes: number;
67
+ strippedOutputs: boolean;
68
+ } | {
69
+ ok: false;
70
+ bytes: number;
71
+ };
72
+ /** Whether `value` is a UUID, and so safe to put in a spec store URL. */
73
+ export declare function isSpecId(value: unknown): value is string;
74
+ /**
75
+ * Read the link from an `oceanum` metadata value, or `null` if it is missing, malformed,
76
+ * or belongs to a different spec store.
77
+ */
78
+ export declare function readLink(value: unknown, specsUrl: string): IOceanumLink | null;
79
+ /** The notebook name stored on Oceanum.io for a local path. */
80
+ export declare function nameFromPath(path: string): string;
81
+ /**
82
+ * The notebook with every cell's `trusted` metadata removed.
83
+ *
84
+ * JupyterLab trusts a cell whose metadata says `trusted: true` and then renders its HTML
85
+ * and Markdown unsanitised, running any script in them. A notebook from the spec store
86
+ * (anyone with write access can edit a record) must arrive untrusted; its outputs
87
+ * become trusted again when the reader re-runs the cells.
88
+ */
89
+ export declare function withoutTrust(content: INotebookContent): INotebookContent;
90
+ /**
91
+ * Build the spec store body for a notebook. The `oceanum` link is not stored in the
92
+ * record. If the body is over `maxBytes`, code-cell outputs and execution counts are
93
+ * dropped; if it is still too large, the result is not ok.
94
+ */
95
+ export declare function buildSpecBody(content: INotebookContent, path: string, specsUrl: string, maxBytes?: number): BuildResult;
96
+ /**
97
+ * The notebook stored in a record, with its `oceanum` link set. Throws if the record
98
+ * does not hold an nbformat 4 notebook.
99
+ */
100
+ export declare function notebookFromRecord(record: ISpecRecord, specsUrl: string): INotebookContent;
101
+ /** Make a record name safe to use as a single path segment. */
102
+ export declare function sanitizeName(name: string): string;
103
+ /**
104
+ * A path `Oceanum/<name>.ipynb` that does not collide with `existing` (file names in the
105
+ * `Oceanum` folder), appending ` (1)`, ` (2)`, ... as needed.
106
+ */
107
+ export declare function uniqueNotebookPath(name: string, existing: Iterable<string>): string;
108
+ /**
109
+ * The names in `existing` that `uniqueNotebookPath` could have given a record called
110
+ * `name`, in the order it would have tried them: `<base>.ipynb`, then `<base> (1).ipynb`,
111
+ * `<base> (2).ipynb` and so on.
112
+ *
113
+ * Opening a record looks through these for the copy a previous session left, so the same
114
+ * record keeps one local file. Without that the second copy is named `<base> (1)`, and
115
+ * since a record takes its name from the file on every save, opening a notebook twice
116
+ * would quietly rename it on Oceanum.
117
+ */
118
+ export declare function notebookNamesFor(name: string, existing: Iterable<string>): string[];
119
+ /**
120
+ * Split a listing into the caller's own notebooks and the rest, newest first.
121
+ *
122
+ * The store only sends `creator` on records the caller created, so anything else it
123
+ * returns is shared with them — directly, or because it is public. Addresses are
124
+ * compared case-insensitively: the store matches entities as case-sensitive globs, so
125
+ * the two sides can differ in case.
126
+ */
127
+ export declare function partitionSummaries(items: readonly ISpecSummary[], email: string | null): {
128
+ mine: ISpecSummary[];
129
+ shared: ISpecSummary[];
130
+ };
131
+ /** Parse a spec store timestamp, which is UTC but usually has no zone designator. */
132
+ export declare function parseTimestamp(value: string): Date | null;
133
+ /**
134
+ * Whether `email` can be granted access. Glob characters are refused: the server
135
+ * matches user entities as globs.
136
+ */
137
+ export declare function isShareableEmail(email: string): boolean;
138
+ /** Addresses typed into the share dialog, split into the ones we can grant and the rest. */
139
+ export interface IParsedShareEmails {
140
+ /** Valid, lower-cased and de-duplicated, in the order first written. */
141
+ emails: string[];
142
+ /** Everything that is not a shareable address, as written, for reporting back. */
143
+ invalid: string[];
144
+ }
145
+ /**
146
+ * Split what the user typed into individual addresses. Commas, semicolons and any
147
+ * whitespace all separate, so one-per-line and a comma-separated list both work.
148
+ *
149
+ * Bare addresses only: a mail client's "To:" line carries display names, and
150
+ * `"Alice B" <alice@example.com>` splits into tokens that are not addresses. Those
151
+ * come back in `invalid` so the user is told, rather than being silently dropped.
152
+ *
153
+ * Addresses are lower-cased because the spec store matches the user entity as a
154
+ * case-sensitive glob against the Auth0 email claim, which Auth0 stores lower case.
155
+ */
156
+ export declare function parseShareEmails(raw: string): IParsedShareEmails;
157
+ /**
158
+ * How to report share failures.
159
+ *
160
+ * `original` means hand the underlying error to the reporter untouched — it holds the
161
+ * only explanation the user gets, and the reporter reads its type to decide whether to
162
+ * offer sign-in. `partial` is only for the case the original error cannot describe on
163
+ * its own: some addresses worked and some did not, so the message has to name them.
164
+ */
165
+ export type ShareReport = {
166
+ kind: 'original';
167
+ } | {
168
+ kind: 'partial';
169
+ named: string[];
170
+ };
171
+ /**
172
+ * Decide how to report a set of failed grants.
173
+ *
174
+ * `entities` are the failed grants' entities, in order; the public grant's entity is
175
+ * the empty string, which names nobody.
176
+ */
177
+ export declare function reportForShareFailures(grantCount: number, entities: readonly string[], needsSignIn: boolean): ShareReport;
178
+ /**
179
+ * The link that opens a shared notebook in this deployment.
180
+ *
181
+ * JupyterLite is static files, so its page is `lab/index.html`; a JupyterLab server
182
+ * serves the application at `lab` and has no `index.html` to route.
183
+ */
184
+ export declare function shareLink(baseUrl: string, pageHref: string, id: string): string;
@@ -0,0 +1,277 @@
1
+ /** Notebook metadata key linking a local notebook to its spec store record. */
2
+ export const METADATA_KEY = 'oceanum';
3
+ /** Folder that notebooks opened from Oceanum.io are written to. */
4
+ export const OCEANUM_DIR = 'Oceanum';
5
+ /** Query parameter carrying a shared notebook id. */
6
+ export const QUERY_PARAM = 'oceanum-notebook';
7
+ /**
8
+ * Largest request body we send. Cloud Run rejects requests over 32 MiB; keep a margin
9
+ * for headers and the server's own encoding.
10
+ */
11
+ export const MAX_BODY_BYTES = 30 * 1024 * 1024;
12
+ const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
13
+ /** Whether `value` is a UUID, and so safe to put in a spec store URL. */
14
+ export function isSpecId(value) {
15
+ return typeof value === 'string' && UUID_RE.test(value);
16
+ }
17
+ function isObject(value) {
18
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
19
+ }
20
+ /**
21
+ * Read the link from an `oceanum` metadata value, or `null` if it is missing, malformed,
22
+ * or belongs to a different spec store.
23
+ */
24
+ export function readLink(value, specsUrl) {
25
+ if (!isObject(value) ||
26
+ !isSpecId(value.spec_id) ||
27
+ value.specs_url !== specsUrl) {
28
+ return null;
29
+ }
30
+ const link = { spec_id: value.spec_id, specs_url: specsUrl };
31
+ if (typeof value.description === 'string') {
32
+ link.description = value.description;
33
+ }
34
+ return link;
35
+ }
36
+ /** The notebook name stored on Oceanum.io for a local path. */
37
+ export function nameFromPath(path) {
38
+ var _a;
39
+ const base = (_a = path.split('/').pop()) !== null && _a !== void 0 ? _a : '';
40
+ return base.replace(/\.ipynb$/i, '') || 'Untitled';
41
+ }
42
+ function byteLength(text) {
43
+ return new TextEncoder().encode(text).length;
44
+ }
45
+ function withoutLink(content) {
46
+ const metadata = { ...content.metadata };
47
+ delete metadata[METADATA_KEY];
48
+ return { ...content, metadata };
49
+ }
50
+ /**
51
+ * The notebook with every cell's `trusted` metadata removed.
52
+ *
53
+ * JupyterLab trusts a cell whose metadata says `trusted: true` and then renders its HTML
54
+ * and Markdown unsanitised, running any script in them. A notebook from the spec store
55
+ * (anyone with write access can edit a record) must arrive untrusted; its outputs
56
+ * become trusted again when the reader re-runs the cells.
57
+ */
58
+ export function withoutTrust(content) {
59
+ const cells = content.cells.map(cell => {
60
+ if (!isObject(cell.metadata) || !('trusted' in cell.metadata)) {
61
+ return cell;
62
+ }
63
+ const metadata = { ...cell.metadata };
64
+ delete metadata.trusted;
65
+ return { ...cell, metadata };
66
+ });
67
+ return { ...content, cells };
68
+ }
69
+ function withoutOutputs(content) {
70
+ const cells = content.cells.map(cell => cell.cell_type === 'code'
71
+ ? { ...cell, outputs: [], execution_count: null }
72
+ : cell);
73
+ return { ...content, cells };
74
+ }
75
+ /**
76
+ * Build the spec store body for a notebook. The `oceanum` link is not stored in the
77
+ * record. If the body is over `maxBytes`, code-cell outputs and execution counts are
78
+ * dropped; if it is still too large, the result is not ok.
79
+ */
80
+ export function buildSpecBody(content, path, specsUrl, maxBytes = MAX_BODY_BYTES) {
81
+ var _a;
82
+ const link = readLink((_a = content.metadata) === null || _a === void 0 ? void 0 : _a[METADATA_KEY], specsUrl);
83
+ const make = (spec) => {
84
+ var _a;
85
+ return ({
86
+ name: nameFromPath(path),
87
+ description: (_a = link === null || link === void 0 ? void 0 : link.description) !== null && _a !== void 0 ? _a : null,
88
+ spec
89
+ });
90
+ };
91
+ const full = make(withoutTrust(withoutLink(content)));
92
+ const fullBytes = byteLength(JSON.stringify(full));
93
+ if (fullBytes <= maxBytes) {
94
+ return { ok: true, body: full, bytes: fullBytes, strippedOutputs: false };
95
+ }
96
+ const stripped = make(withoutOutputs(full.spec));
97
+ const strippedBytes = byteLength(JSON.stringify(stripped));
98
+ if (strippedBytes <= maxBytes) {
99
+ return {
100
+ ok: true,
101
+ body: stripped,
102
+ bytes: strippedBytes,
103
+ strippedOutputs: true
104
+ };
105
+ }
106
+ return { ok: false, bytes: strippedBytes };
107
+ }
108
+ /**
109
+ * The notebook stored in a record, with its `oceanum` link set. Throws if the record
110
+ * does not hold an nbformat 4 notebook.
111
+ */
112
+ export function notebookFromRecord(record, specsUrl) {
113
+ const spec = record.spec;
114
+ if (!isObject(spec) ||
115
+ spec.nbformat !== 4 ||
116
+ !Array.isArray(spec.cells) ||
117
+ !isObject(spec.metadata)) {
118
+ throw new Error('This Oceanum.io record is not a valid notebook.');
119
+ }
120
+ const link = { spec_id: record.id, specs_url: specsUrl };
121
+ if (record.description) {
122
+ link.description = record.description;
123
+ }
124
+ const content = withoutTrust(spec);
125
+ return {
126
+ ...content,
127
+ metadata: { ...content.metadata, [METADATA_KEY]: { ...link } }
128
+ };
129
+ }
130
+ /** Make a record name safe to use as a single path segment. */
131
+ export function sanitizeName(name) {
132
+ const clean = name
133
+ .trim()
134
+ .replace(/\.ipynb$/i, '')
135
+ .replace(/[/\\]/g, '-')
136
+ .trim();
137
+ return clean === '' || clean === '.' || clean === '..' ? 'Untitled' : clean;
138
+ }
139
+ /**
140
+ * A path `Oceanum/<name>.ipynb` that does not collide with `existing` (file names in the
141
+ * `Oceanum` folder), appending ` (1)`, ` (2)`, ... as needed.
142
+ */
143
+ export function uniqueNotebookPath(name, existing) {
144
+ const taken = new Set(existing);
145
+ const base = sanitizeName(name);
146
+ let candidate = `${base}.ipynb`;
147
+ for (let i = 1; taken.has(candidate); i++) {
148
+ candidate = `${base} (${i}).ipynb`;
149
+ }
150
+ return `${OCEANUM_DIR}/${candidate}`;
151
+ }
152
+ /**
153
+ * The names in `existing` that `uniqueNotebookPath` could have given a record called
154
+ * `name`, in the order it would have tried them: `<base>.ipynb`, then `<base> (1).ipynb`,
155
+ * `<base> (2).ipynb` and so on.
156
+ *
157
+ * Opening a record looks through these for the copy a previous session left, so the same
158
+ * record keeps one local file. Without that the second copy is named `<base> (1)`, and
159
+ * since a record takes its name from the file on every save, opening a notebook twice
160
+ * would quietly rename it on Oceanum.
161
+ */
162
+ export function notebookNamesFor(name, existing) {
163
+ const base = sanitizeName(name);
164
+ const prefix = `${base} (`;
165
+ const suffix = ').ipynb';
166
+ const plain = [];
167
+ const numbered = [];
168
+ for (const candidate of existing) {
169
+ if (candidate === `${base}.ipynb`) {
170
+ plain.push(candidate);
171
+ continue;
172
+ }
173
+ if (!candidate.startsWith(prefix) || !candidate.endsWith(suffix)) {
174
+ continue;
175
+ }
176
+ const index = candidate.slice(prefix.length, candidate.length - suffix.length);
177
+ if (/^\d+$/.test(index)) {
178
+ numbered.push({ name: candidate, index: Number(index) });
179
+ }
180
+ }
181
+ numbered.sort((a, b) => a.index - b.index);
182
+ return [...plain, ...numbered.map(entry => entry.name)];
183
+ }
184
+ /**
185
+ * Split a listing into the caller's own notebooks and the rest, newest first.
186
+ *
187
+ * The store only sends `creator` on records the caller created, so anything else it
188
+ * returns is shared with them — directly, or because it is public. Addresses are
189
+ * compared case-insensitively: the store matches entities as case-sensitive globs, so
190
+ * the two sides can differ in case.
191
+ */
192
+ export function partitionSummaries(items, email) {
193
+ var _a;
194
+ const me = (_a = email === null || email === void 0 ? void 0 : email.toLowerCase()) !== null && _a !== void 0 ? _a : null;
195
+ const sorted = [...items].sort((a, b) => b.modified.localeCompare(a.modified));
196
+ const isMine = (item) => { var _a; return me !== null && ((_a = item.creator) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === me; };
197
+ return {
198
+ mine: sorted.filter(isMine),
199
+ shared: sorted.filter(item => !isMine(item))
200
+ };
201
+ }
202
+ /** Parse a spec store timestamp, which is UTC but usually has no zone designator. */
203
+ export function parseTimestamp(value) {
204
+ const zoned = /(Z|[+-]\d{2}:?\d{2})$/i.test(value) ? value : `${value}Z`;
205
+ const date = new Date(zoned);
206
+ return Number.isNaN(date.getTime()) ? null : date;
207
+ }
208
+ /**
209
+ * Whether `email` can be granted access. Glob characters are refused: the server
210
+ * matches user entities as globs.
211
+ */
212
+ export function isShareableEmail(email) {
213
+ return /^[^\s@*?[\]]+@[^\s@*?[\]]+\.[^\s@*?[\]]+$/.test(email);
214
+ }
215
+ /**
216
+ * Split what the user typed into individual addresses. Commas, semicolons and any
217
+ * whitespace all separate, so one-per-line and a comma-separated list both work.
218
+ *
219
+ * Bare addresses only: a mail client's "To:" line carries display names, and
220
+ * `"Alice B" <alice@example.com>` splits into tokens that are not addresses. Those
221
+ * come back in `invalid` so the user is told, rather than being silently dropped.
222
+ *
223
+ * Addresses are lower-cased because the spec store matches the user entity as a
224
+ * case-sensitive glob against the Auth0 email claim, which Auth0 stores lower case.
225
+ */
226
+ export function parseShareEmails(raw) {
227
+ const emails = [];
228
+ const invalid = [];
229
+ const seen = new Set();
230
+ for (const part of raw.split(/[\s,;]+/)) {
231
+ const entry = part.trim();
232
+ if (entry === '') {
233
+ continue;
234
+ }
235
+ const email = entry.toLowerCase();
236
+ if (!isShareableEmail(email)) {
237
+ // Report it as written, so the user recognises what they typed.
238
+ invalid.push(entry);
239
+ continue;
240
+ }
241
+ if (!seen.has(email)) {
242
+ seen.add(email);
243
+ emails.push(email);
244
+ }
245
+ }
246
+ return { emails, invalid };
247
+ }
248
+ /**
249
+ * Decide how to report a set of failed grants.
250
+ *
251
+ * `entities` are the failed grants' entities, in order; the public grant's entity is
252
+ * the empty string, which names nobody.
253
+ */
254
+ export function reportForShareFailures(grantCount, entities, needsSignIn) {
255
+ const named = entities.filter(entity => entity !== '');
256
+ // A sign-in problem must reach the reporter as the original error or the user is
257
+ // never offered a way back in; an unnamed or total failure has nothing extra worth
258
+ // saying.
259
+ if (needsSignIn || named.length === 0 || entities.length >= grantCount) {
260
+ return { kind: 'original' };
261
+ }
262
+ return { kind: 'partial', named };
263
+ }
264
+ /**
265
+ * The link that opens a shared notebook in this deployment.
266
+ *
267
+ * JupyterLite is static files, so its page is `lab/index.html`; a JupyterLab server
268
+ * serves the application at `lab` and has no `index.html` to route.
269
+ */
270
+ export function shareLink(baseUrl, pageHref, id) {
271
+ const app = new URL(pageHref).pathname.endsWith('/index.html')
272
+ ? 'lab/index.html'
273
+ : 'lab';
274
+ const url = new URL(app, new URL(baseUrl, pageHref));
275
+ url.searchParams.set(QUERY_PARAM, id);
276
+ return url.toString();
277
+ }
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Oceanum-only file management: what a deployment gets when its environment says
3
+ * `fileManagement: 'oceanum'`, as notebook.oceanum.io does.
4
+ *
5
+ * The local drive stays as the working copy underneath, out of sight: the file browser
6
+ * is taken out of the sidebar, the File menu loses the entries that manage local files,
7
+ * and every completed local save is pushed to the spec store, so Ctrl+S, the toolbar
8
+ * button, Save All and autosave all reach Oceanum.
9
+ */
10
+ import type { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
11
+ import type { Menu, Widget } from '@lumino/widgets';
12
+ /** The widget id `@jupyterlab/filebrowser-extension:browser` gives the file browser. */
13
+ export declare const FILE_BROWSER_ID = "filebrowser";
14
+ /**
15
+ * File menu entries that manage local files. Save, Save All and Rename stay: saving is
16
+ * routed to Oceanum, and renaming the local file is how a notebook gets its name.
17
+ */
18
+ export declare const LOCAL_FILE_COMMANDS: readonly string[];
19
+ /**
20
+ * Take the file browser out of the sidebar; true if it was there. It is closed, not
21
+ * disposed: other extensions hold it through IDefaultFileBrowser and still use it to
22
+ * open paths.
23
+ */
24
+ export declare function hideFileBrowser(shell: {
25
+ widgets(area: 'left'): Iterable<Widget>;
26
+ }): boolean;
27
+ /**
28
+ * Submenus of local files. "Open Recent" is built by the document manager rather than
29
+ * declared in a schema, and its menu has no id, so it is known by the commands it holds.
30
+ */
31
+ export declare const LOCAL_FILE_SUBMENU_COMMANDS: readonly string[];
32
+ /**
33
+ * Remove the local-file entries from a menu; returns how many went. Idempotent, so it
34
+ * can run every time the menu is about to show: entries arrive from settings schemas as
35
+ * plugins load, not all at once.
36
+ */
37
+ export declare function pruneFileMenu(menu: Menu, commands?: readonly string[]): number;
38
+ /**
39
+ * Prune the menu again every time it opens. Entries arrive from settings schemas as
40
+ * plugins load rather than all at once, and a menu has no "about to show" signal; it
41
+ * is attached to the document each time it opens, which a message hook can watch.
42
+ */
43
+ export declare function pruneBeforeEachOpen(menu: Menu, commands?: readonly string[]): void;
44
+ /**
45
+ * Call `push` after every completed local save of every notebook, open now or later.
46
+ * `push` must guard against its own saves: writing the record link back to the notebook
47
+ * saves it again.
48
+ */
49
+ export declare function routeSaves(tracker: INotebookTracker, push: (panel: NotebookPanel) => Promise<void>): void;
@@ -0,0 +1,90 @@
1
+ import { MessageLoop } from '@lumino/messaging';
2
+ /** The widget id `@jupyterlab/filebrowser-extension:browser` gives the file browser. */
3
+ export const FILE_BROWSER_ID = 'filebrowser';
4
+ /**
5
+ * File menu entries that manage local files. Save, Save All and Rename stay: saving is
6
+ * routed to Oceanum, and renaming the local file is how a notebook gets its name.
7
+ */
8
+ export const LOCAL_FILE_COMMANDS = [
9
+ 'filebrowser:open-path',
10
+ 'filebrowser:open-url',
11
+ 'docmanager:save-as',
12
+ 'docmanager:reload',
13
+ 'docmanager:restore-checkpoint',
14
+ 'docmanager:duplicate',
15
+ 'docmanager:download'
16
+ ];
17
+ /**
18
+ * Take the file browser out of the sidebar; true if it was there. It is closed, not
19
+ * disposed: other extensions hold it through IDefaultFileBrowser and still use it to
20
+ * open paths.
21
+ */
22
+ export function hideFileBrowser(shell) {
23
+ for (const widget of shell.widgets('left')) {
24
+ if (widget.id === FILE_BROWSER_ID) {
25
+ widget.close();
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ }
31
+ /**
32
+ * Submenus of local files. "Open Recent" is built by the document manager rather than
33
+ * declared in a schema, and its menu has no id, so it is known by the commands it holds.
34
+ */
35
+ export const LOCAL_FILE_SUBMENU_COMMANDS = [
36
+ 'docmanager:open-recent',
37
+ 'docmanager:clear-recents'
38
+ ];
39
+ function isLocalSubmenu(item) {
40
+ return (item.type === 'submenu' &&
41
+ !!item.submenu &&
42
+ (item.submenu.title.label === 'Open Recent' ||
43
+ item.submenu.items.some(child => LOCAL_FILE_SUBMENU_COMMANDS.includes(child.command))));
44
+ }
45
+ /**
46
+ * Remove the local-file entries from a menu; returns how many went. Idempotent, so it
47
+ * can run every time the menu is about to show: entries arrive from settings schemas as
48
+ * plugins load, not all at once.
49
+ */
50
+ export function pruneFileMenu(menu, commands = LOCAL_FILE_COMMANDS) {
51
+ let removed = 0;
52
+ for (let i = menu.items.length - 1; i >= 0; i--) {
53
+ const item = menu.items[i];
54
+ if ((item.type === 'command' && commands.includes(item.command)) ||
55
+ isLocalSubmenu(item)) {
56
+ menu.removeItemAt(i);
57
+ removed++;
58
+ }
59
+ }
60
+ return removed;
61
+ }
62
+ /**
63
+ * Prune the menu again every time it opens. Entries arrive from settings schemas as
64
+ * plugins load rather than all at once, and a menu has no "about to show" signal; it
65
+ * is attached to the document each time it opens, which a message hook can watch.
66
+ */
67
+ export function pruneBeforeEachOpen(menu, commands = LOCAL_FILE_COMMANDS) {
68
+ MessageLoop.installMessageHook(menu, (_, message) => {
69
+ if (message.type === 'before-attach') {
70
+ pruneFileMenu(menu, commands);
71
+ }
72
+ return true;
73
+ });
74
+ }
75
+ /**
76
+ * Call `push` after every completed local save of every notebook, open now or later.
77
+ * `push` must guard against its own saves: writing the record link back to the notebook
78
+ * saves it again.
79
+ */
80
+ export function routeSaves(tracker, push) {
81
+ const watch = (panel) => {
82
+ panel.context.saveState.connect((_, state) => {
83
+ if (state === 'completed') {
84
+ void push(panel);
85
+ }
86
+ });
87
+ };
88
+ tracker.forEach(watch);
89
+ tracker.widgetAdded.connect((_, panel) => watch(panel));
90
+ }
@@ -0,0 +1,21 @@
1
+ import { JupyterFrontEndPlugin } from '@jupyterlab/application';
2
+ export declare const CommandIDs: {
3
+ readonly open: "oceanum-share:open";
4
+ readonly save: "oceanum-share:save";
5
+ readonly share: "oceanum-share:share";
6
+ readonly rename: "oceanum-share:rename";
7
+ readonly delete: "oceanum-share:delete";
8
+ };
9
+ export declare const SHARE_PLUGIN_ID = "@oceanum/oceanumlab:share";
10
+ /** The Notebooks tab marks each listed record with this, for its context menu. */
11
+ export declare const SPEC_ID_ATTRIBUTE = "data-spec-id";
12
+ export declare const NOTEBOOK_ITEM_SELECTOR = ".oceanum-notebooks-item[data-spec-id]";
13
+ /**
14
+ * Open, save, share, rename and delete notebooks through the Oceanum spec store.
15
+ *
16
+ * The commands sit in the File menu, the command palette, a notebook's context menu
17
+ * (and its tab's), and the Notebooks tab's rows. They are enabled only while signed in.
18
+ * With `fileManagement: 'oceanum'` in the environment the local drive goes out of
19
+ * sight and every local save is pushed to the store: see `oceanumOnly.ts`.
20
+ */
21
+ export declare const sharePlugin: JupyterFrontEndPlugin<void>;