From 54a9354b8bd53c098d7aa8e98d086029f2523453 Mon Sep 17 00:00:00 2001 From: Taras Novak Date: Sun, 17 May 2020 23:41:40 -0500 Subject: [PATCH] #30 refactored observable js notebook content loading and module parsing --- .../observable.notebook.provider.ts | 6 ++---- src/notebook.ts | 18 +++++++++++++++--- src/views/notebook.view.ts | 6 ++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/notebook.providers/observable.notebook.provider.ts b/src/notebook.providers/observable.notebook.provider.ts index 10a268b..8e03c1f 100644 --- a/src/notebook.providers/observable.notebook.provider.ts +++ b/src/notebook.providers/observable.notebook.provider.ts @@ -37,10 +37,8 @@ export class ObservableNotebookProvider implements INotebookProvider { fetch(notebookDocumentUrl) .then((response: any) => response.text()) .then((notebookJS: string) => { - // parse notebook JS and create a notebook prototype for introspection - const notebookInfo = new Function(`${notebookJS.slice(0, -26)} return notebook;`)(); - // this.logger.debug('notebook:', JSON.stringify(notebookInfo)); - loadNotebook(notebookInfo); + this.logger.debug('notebookJS:', notebookJS); + loadNotebook(notebookJS); }); } catch (error) { this.logger.logMessage( diff --git a/src/notebook.ts b/src/notebook.ts index ad77be0..db2046b 100644 --- a/src/notebook.ts +++ b/src/notebook.ts @@ -9,9 +9,10 @@ export class Notebook { * @param fileName Notebook file name. * @param authorName Notebook author name. */ - constructor(public url: string = '', - public fileName: string = '', - public authorName: string = '') { + constructor(public url: string, + public fileName: string, + public authorName: string = '', + public source: string = '') { } @@ -22,4 +23,15 @@ export class Notebook { return `${this.authorName}/${this.fileName}.js`; } + /** + * Gets notebook js module. + */ + public get module(): Function { + let jsModule: Function = new Function(`return undefined`); + if (this.source.length > 0) { + // parse notebook JS and create a notebook prototype for introspection + jsModule = new Function(`${this.source.slice(0, -26)} return notebook;`)(); + } + return jsModule; + } } diff --git a/src/views/notebook.view.ts b/src/views/notebook.view.ts index 5bbe334..3196237 100644 --- a/src/views/notebook.view.ts +++ b/src/views/notebook.view.ts @@ -331,8 +331,10 @@ export class NotebookView { if (this._url.startsWith('https://') && dataEncoding === 'utf8') { // load remote notebook document notebookManager.getNotebook(this._url, {}, // parse options - (notebookDocument: any) => { - this.refreshView(notebookDocument); + (notebookJS: string) => { + this._content = notebookJS; + this._notebook.source = notebookJS; + this.refreshView(this._notebook.module); }); } else if (dataEncoding === 'utf8') { // open local text document