Skip to content

Commit

Permalink
#30 refactored observable js notebook content loading and module parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomFractals committed May 18, 2020
1 parent 491cfb7 commit 54a9354
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 2 additions & 4 deletions src/notebook.providers/observable.notebook.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 15 additions & 3 deletions src/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '') {

}

Expand All @@ -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;
}
}
6 changes: 4 additions & 2 deletions src/views/notebook.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 54a9354

Please sign in to comment.