1- import { type DefineState , NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime" ;
2- import { type Definition } from "./index.ts" ;
1+ import { type Definition , type DefineState , NotebookRuntime as NotebookRuntimeBase } from "@observablehq/notebook-kit/runtime" ;
32
43import "@observablehq/notebook-kit/index.css" ;
54import "@observablehq/notebook-kit/theme-air.css" ;
65
6+ export { Definition , DefineState } ;
7+
78export class NotebookRuntime extends NotebookRuntimeBase {
89
910 stateById = new Map < number , DefineState > ( ) ;
@@ -16,23 +17,50 @@ export class NotebookRuntime extends NotebookRuntimeBase {
1617 return this . stateById . has ( cellId ) ;
1718 }
1819
19- async add ( cellId : number , definition : Definition , placeholderDiv : HTMLDivElement ) : Promise < void > {
20- let state : DefineState | undefined = this . stateById . get ( cellId ) ;
21- if ( state ) {
22- this . remove ( cellId ) ;
20+ async add ( definition : Definition ) : Promise < HTMLDivElement > {
21+ if ( this . stateById . has ( definition . id ) ) {
22+ throw new Error ( `Cell with id ${ definition . id } already exists` ) ;
2323 }
24- state = { root : placeholderDiv , expanded : [ ] , variables : [ ] } ;
25- this . stateById . set ( cellId , state ) ;
24+ const placeholderDiv = document . createElement ( "div" ) ;
25+ placeholderDiv . className = "observablehq observablehq--cell" ;
26+ const state = { root : placeholderDiv , expanded : [ ] , variables : [ ] } ;
27+ this . stateById . set ( definition . id , state ) ;
2628 this . define ( state , definition ) ;
2729 await this . runtime . _computeNow ( ) ;
30+ return state . root ;
2831 }
2932
30- async remove ( cellId : number ) : Promise < void > {
33+ async update ( definition : Definition ) : Promise < HTMLDivElement > {
34+ const state = this . stateById . get ( definition . id ) ;
35+ if ( ! state ) {
36+ throw new Error ( `Cell with id ${ definition . id } does not exist` ) ;
37+ }
38+ await this . clear ( definition . id ) ;
39+ this . define ( state , definition ) ;
40+ await this . runtime . _computeNow ( ) ;
41+ return state . root ;
42+ }
43+
44+ async clear ( cellId : number ) : Promise < void > {
3145 const state = this . stateById . get ( cellId ) ;
32- if ( ! state ) return ;
46+ if ( ! state ) {
47+ throw new Error ( `Cell with id ${ cellId } does not exist` ) ;
48+ }
3349 [ ...state . variables ] . forEach ( v => v . delete ( ) ) ;
50+ await this . runtime . _computeNow ( ) ;
51+ state . variables = [ ] ;
52+ state . expanded = [ ] ;
53+ state . root . innerHTML = "" ;
54+ }
55+
56+ async remove ( cellId : number ) : Promise < void > {
57+ const state = this . stateById . get ( cellId ) ;
58+ if ( ! state ) {
59+ throw new Error ( `Cell with id ${ cellId } does not exist` ) ;
60+ }
61+ void this . clear ( cellId ) ;
3462 this . stateById . delete ( cellId ) ;
35- state . root ? .remove ( ) ;
63+ state . root . remove ( ) ;
3664 await this . runtime . _computeNow ( ) ;
3765 }
3866
@@ -44,14 +72,15 @@ export class NotebookRuntime extends NotebookRuntimeBase {
4472 await this . runtime . _computeNow ( ) ;
4573 }
4674
47- render ( definitions : Definition [ ] , target : HTMLElement ) {
48- definitions . forEach ( definition => {
49- const placeholderDiv = document . createElement ( "div" ) ;
50- placeholderDiv . id = `cell-${ definition . id } ` ;
51- placeholderDiv . className = "observablehq observablehq--cell" ;
52- this . stateById . set ( definition . id , { root : placeholderDiv , expanded : [ ] , variables : [ ] } ) ;
53- this . define ( this . stateById . get ( definition . id ) ! , definition ) ;
54- target . appendChild ( placeholderDiv ) ;
55- } ) ;
75+ async render ( definitions : Definition [ ] , target : HTMLElement ) {
76+ for ( const definition of definitions ) {
77+ let observableDiv : HTMLDivElement ;
78+ if ( this . stateById . has ( definition . id ) ) {
79+ observableDiv = await this . update ( definition ) ;
80+ } else {
81+ observableDiv = await this . add ( definition ) ;
82+ }
83+ target . appendChild ( observableDiv ) ;
84+ }
5685 }
5786}
0 commit comments