|
1 |
| -// import { Runtime, Inspector, Library } from 'https://cdn.jsdelivr.net/npm/@observablehq/runtime@4.7.2/src/index.js' //@observablehq/runtime'; |
2 |
| - |
3 |
| -import { |
4 |
| - Runtime, |
5 |
| - Inspector, |
6 |
| - Library, |
7 |
| -} from 'https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js'; |
| 1 | +import { Runtime, Inspector, Library } from '@observablehq/runtime'; |
8 | 2 |
|
9 | 3 | const staticAttrs = ['notebook', 'cell', 'class', 'style'];
|
10 | 4 |
|
@@ -43,14 +37,34 @@ class ObservablehqCell extends HTMLElement {
|
43 | 37 | this.init();
|
44 | 38 | }
|
45 | 39 | async init() {
|
46 |
| - this._notebook = await this.importNotebook(this.notebook, this.injections); |
47 |
| - const generator = await this._notebook.cell(this.cell); |
48 |
| - if (!generator) return; |
| 40 | + // inspired by |
| 41 | + // https://observablehq.com/@mbostock/dataflow |
| 42 | + // https://observablehq.com/d/d0bb058f650143a9 |
| 43 | + // load selected notebook definitions |
| 44 | + const { default: define } = await import( |
| 45 | + /^https:/.test(this.notebook) |
| 46 | + ? this.notebook |
| 47 | + : `https://api.observablehq.com/${this.notebook}.js?v=3` |
| 48 | + ); |
| 49 | + // create runtime with or not custom library |
| 50 | + const runtime = new Runtime(this.library); |
| 51 | + |
| 52 | + // Create the main module, including any injected values. |
| 53 | + const main = runtime.module(); |
| 54 | + for (const name in this.injections) { |
| 55 | + main.define(name, [], () => this.injections[name]); |
| 56 | + } |
| 57 | + const imported = runtime.module(define); |
| 58 | + this._notebook = imported.derive([...Object.keys(this.injections)], main); |
49 | 59 |
|
| 60 | + if (!this.cell) { |
| 61 | + new Runtime(this.library).module(define, Inspector.into(this.wrapper)); |
| 62 | + return; |
| 63 | + } |
50 | 64 | try {
|
51 |
| - const result = generator.next(); // yield generator |
52 |
| - const value = await result.value; // await cell value |
53 |
| - this.wrapper.appendChild(value); // append it to shadowelement root |
| 65 | + main |
| 66 | + .variable(new Inspector(this.wrapper)) |
| 67 | + .import(this.cell, this._notebook); |
54 | 68 | } catch (e) {
|
55 | 69 | console.error('cell name error', e);
|
56 | 70 | }
|
@@ -88,94 +102,6 @@ class ObservablehqCell extends HTMLElement {
|
88 | 102 | // setter converts it back to string
|
89 | 103 | this.setAttribute('injections', JSON.stringify(newValue));
|
90 | 104 | }
|
91 |
| - // from https://observablehq.com/@mbostock/dataflow |
92 |
| - // https://observablehq.com/d/d0bb058f650143a9 |
93 |
| - importNotebook( |
94 |
| - notebookSpecifier, // e.g., "@d3/bar-chart" |
95 |
| - injections = {} // e.g., {data: [{name, value}, …]} |
96 |
| - ) { |
97 |
| - const promise = (async () => { |
98 |
| - // Create the main module, including any injected values. |
99 |
| - const runtime = new Runtime(this.library); |
100 |
| - const main = runtime.module(); |
101 |
| - for (const name in injections) { |
102 |
| - main.define(name, [], () => injections[name]); |
103 |
| - } |
104 |
| - |
105 |
| - // Load the requested notebook’s definition as an ES module. |
106 |
| - const { default: define } = await import( |
107 |
| - /^https:/.test(notebookSpecifier) |
108 |
| - ? notebookSpecifier |
109 |
| - : `https://api.observablehq.com/${notebookSpecifier}.js?v=3` |
110 |
| - ); |
111 |
| - |
112 |
| - // Create the imported notebook’s module, and then derive a module |
113 |
| - // from it to inject the desired values. (If there are none, then |
114 |
| - // this is basically a noop.) |
115 |
| - const imported = runtime.module(define); |
116 |
| - const derived = imported.derive([...Object.keys(injections)], main); |
117 |
| - // return full notebook if no cells are presented |
118 |
| - |
119 |
| - // In many cases the imported cell will only have a single value, but |
120 |
| - // we must use the most generic representation (an async generator) as |
121 |
| - // the imported cell may be an async generator, or may reference one. |
122 |
| - |
123 |
| - derived.cell = (cellName) => { |
124 |
| - if (!cellName) { |
125 |
| - const runtime = new Runtime(this.library).module( |
126 |
| - define, |
127 |
| - Inspector.into(this.wrapper) |
128 |
| - ); |
129 |
| - return; |
130 |
| - } |
131 |
| - return this.library.Generators.observe((notify) => { |
132 |
| - // Create the primary variable with an observer that will report the |
133 |
| - // desired cell’s fulfilled or rejected values. |
134 |
| - console.log("testt") |
135 |
| - main |
136 |
| - .variable({ |
137 |
| - fulfilled(value) { |
138 |
| - notify(value); |
139 |
| - }, |
140 |
| - rejected(value) { |
141 |
| - notify(Promise.reject(value)); |
142 |
| - }, |
143 |
| - }) |
144 |
| - .import(cellName, derived); |
145 |
| - |
146 |
| - // Lastly, when this generator is disposed, dispose the runtime to |
147 |
| - // ensure that any imported generators terminate. |
148 |
| - return () => runtime.dispose(); |
149 |
| - }); |
150 |
| - }; |
151 |
| - |
152 |
| - return derived; |
153 |
| - })(); |
154 |
| - promise.cell = (cellName) => |
155 |
| - promise.then((notebook) => notebook.cell(cellName)); |
156 |
| - return promise; |
157 |
| - } |
158 |
| - |
159 |
| - importCell( |
160 |
| - cellName, // e.g., "chart" |
161 |
| - notebookSpecifier, // e.g., "@d3/bar-chart" |
162 |
| - injections = {} // e.g., {data: [{name, value}, …]} |
163 |
| - ) { |
164 |
| - return this.importNotebook(notebookSpecifier, injections).cell(cellName); |
165 |
| - } |
166 |
| - // observeAttrChange(el, callback) { |
167 |
| - // var observer = new MutationObserver((mutations) => { |
168 |
| - // mutations.forEach((mutation) => { |
169 |
| - // if (mutation.type === 'attributes') { |
170 |
| - // let newVal = mutation.target.getAttribute(mutation.attributeName); |
171 |
| - // callback(mutation.attributeName, newVal); |
172 |
| - // } |
173 |
| - // }); |
174 |
| - // }); |
175 |
| - // observer.observe(el, { attributes: true }); |
176 |
| - // return observer; |
177 |
| - // } |
178 |
| - // Fires when an instance was removed from the document |
179 | 105 | disconnectedCallback() {}
|
180 | 106 | static get observedAttributes() {
|
181 | 107 | return ['injections'];
|
|
0 commit comments