@observablehq/notebook-kit 2.5.9 → 2.6.0

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.
package/dist/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "2.5.9",
8
+ "version": "2.6.0",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",
@@ -130,10 +130,7 @@ function getDatabase(cell) {
130
130
  return `DatabaseClient(${JSON.stringify(database)}, {id: ${id}${since === undefined ? "" : `, since: ${JSON.stringify(since)}`}})`;
131
131
  }
132
132
  function getSqlPrefix(cell) {
133
- return `${cell.hidden ? "" : cell.output ? "((_) => (display(Inputs.table(_)), _))(" : "Inputs.table("}await ${getDatabase(cell)}.sql`;
134
- }
135
- function getSqlSuffix(cell) {
136
- return cell.hidden ? "" : ")";
133
+ return `${getDatabase(cell)}.sql`;
137
134
  }
138
135
  function getSqlViewSuffix(cell) {
139
136
  return `.bind(${getDatabase(cell)})`;
@@ -143,13 +140,11 @@ function getInterpreterPrefix(cell) {
143
140
  return `Interpreter(${JSON.stringify(mode)}, {id: ${id}${format === undefined ? "" : `, format: ${JSON.stringify(format)}`}${since === undefined ? "" : `, since: ${JSON.stringify(since)}`}}).run(`;
144
141
  }
145
142
  function getSuffix(cell) {
146
- return cell.mode === "sql"
147
- ? getSqlSuffix(cell)
148
- : cell.mode === "sql.view"
149
- ? getSqlViewSuffix(cell)
150
- : isInterpreter(cell.mode)
151
- ? getInterpreterSuffix(cell)
152
- : "";
143
+ return cell.mode === "sql.view"
144
+ ? getSqlViewSuffix(cell)
145
+ : isInterpreter(cell.mode)
146
+ ? getInterpreterSuffix(cell)
147
+ : "";
153
148
  }
154
149
  function getInterpreterSuffix(cell) {
155
150
  const method = getInterpreterMethod(cell.format);
@@ -26,12 +26,8 @@ export function transpile(input, mode, options) {
26
26
  : mode !== "js"
27
27
  ? transpileJavaScript(transpileTemplate(cell), options)
28
28
  : transpileJavaScript(input, options);
29
- if (mode !== "ts" && mode !== "js" && mode !== "ojs") {
30
- if (cell.output)
31
- transpiled.output = cell.output;
32
- if (cell.value)
33
- transpiled.autodisplay = true;
34
- }
29
+ if (mode !== "ts" && mode !== "js" && mode !== "ojs")
30
+ transpiled.output = cell.output;
35
31
  if (cell.hidden)
36
32
  transpiled.autodisplay = false;
37
33
  return transpiled;
@@ -1,5 +1,5 @@
1
1
  import type { Module, Variable, VariableDefinition } from "@observablehq/runtime";
2
- import type { DisplayState } from "./display.js";
2
+ import type { DisplayState, DisplayMode } from "./display.js";
3
3
  import { observe } from "./display.js";
4
4
  export type DefineState = DisplayState & {
5
5
  /** the runtime variables associated with this cell */
@@ -24,6 +24,8 @@ export type Definition = {
24
24
  automutable?: boolean;
25
25
  /** whether to define the display and view builtins; defaults to true */
26
26
  display?: boolean;
27
+ /** whether to use the default inspector, or the special table display (for SQL cells) */
28
+ displayMode?: DisplayMode;
27
29
  /** an asset mapping to apply to any autodisplayed assets (e.g., images and videos) */
28
30
  assets?: Map<string, string>;
29
31
  };
@@ -2,7 +2,7 @@ import { clear, display, observe } from "./display.js";
2
2
  import { input } from "./stdlib/generators/index.js";
3
3
  import { Mutator } from "./stdlib/mutable.js";
4
4
  export function define(main, state, definition, observer = observe) {
5
- const { id, body, inputs = [], outputs = [], output } = definition;
5
+ const { id, body, inputs = [], outputs = [], output, displayMode } = definition;
6
6
  const { autodisplay, autoview, automutable, display: defineDisplay = true } = definition;
7
7
  const variables = state.variables;
8
8
  const v = main.variable(observer(state, definition), { shadow: {} });
@@ -21,7 +21,7 @@ export function define(main, state, definition, observer = observe) {
21
21
  else if (version > displayVersion)
22
22
  clear(state);
23
23
  displayVersion = version;
24
- display(state, value, output);
24
+ display(state, value, output, displayMode);
25
25
  return value;
26
26
  };
27
27
  });
@@ -1,4 +1,5 @@
1
1
  import type { Definition } from "./define.js";
2
+ export type DisplayMode = "table" | "default";
2
3
  export type DisplayState = {
3
4
  /** the HTML element in which to render this cell’s display */
4
5
  root: HTMLDivElement;
@@ -7,9 +8,9 @@ export type DisplayState = {
7
8
  /** for inspected values, any expanded paths; see getExpanded */
8
9
  expanded: (number[][] | undefined)[];
9
10
  };
10
- export declare function display(state: DisplayState, value: unknown, name?: string): void;
11
+ export declare function display(state: DisplayState, value: unknown, name?: string, mode?: DisplayMode): void;
11
12
  export declare function clear(state: DisplayState): void;
12
- export declare function observe(state: DisplayState, { autodisplay, assets, output }: Definition): {
13
+ export declare function observe(state: DisplayState, { autodisplay, assets, output: name, displayMode: mode }: Definition): {
13
14
  _error: boolean;
14
15
  _node: HTMLDivElement;
15
16
  pending(): void;
@@ -1,10 +1,22 @@
1
1
  import { inspect, inspectError, getExpanded } from "./inspect.js";
2
2
  import { mapAssets } from "./stdlib/assets.js";
3
- export function display(state, value, name) {
3
+ export function display(state, value, name, mode) {
4
+ return mode === "table" ? displayTable(state, value) : displayDefault(state, value, name);
5
+ }
6
+ function displayDefault(state, value, name) {
4
7
  const { root, expanded } = state;
5
8
  const node = isDisplayable(value, root) ? value : inspect(value, expanded[root.childNodes.length], name); // prettier-ignore
6
9
  displayNode(state, node);
7
10
  }
11
+ function displayTable(state, value) {
12
+ const placeholder = document.createComment("table");
13
+ displayNode(state, placeholder);
14
+ import("./stdlib/inputs.js").then(({ table }) => {
15
+ if (!placeholder.parentNode)
16
+ return; // don’t bother rendering if detached
17
+ placeholder.replaceWith(table(value));
18
+ });
19
+ }
8
20
  function displayNode(state, node) {
9
21
  if (node.nodeType === 11) {
10
22
  let child;
@@ -33,7 +45,7 @@ export function clear(state) {
33
45
  while (state.root.lastChild)
34
46
  state.root.lastChild.remove();
35
47
  }
36
- export function observe(state, { autodisplay, assets, output }) {
48
+ export function observe(state, { autodisplay, assets, output: name, displayMode: mode }) {
37
49
  return {
38
50
  _error: false,
39
51
  _node: state.root, // _node for visibility promise
@@ -48,7 +60,7 @@ export function observe(state, { autodisplay, assets, output }) {
48
60
  if (assets && value instanceof Element)
49
61
  mapAssets(value, assets);
50
62
  clear(state);
51
- display(state, value, output);
63
+ display(state, value, name, mode);
52
64
  }
53
65
  else if (state.autoclear) {
54
66
  clear(state);
@@ -58,7 +70,7 @@ export function observe(state, { autodisplay, assets, output }) {
58
70
  console.error(error);
59
71
  this._error = true;
60
72
  clear(state);
61
- displayError(state, error, output);
73
+ displayError(state, error, name);
62
74
  }
63
75
  };
64
76
  }
@@ -8,7 +8,6 @@ import { JSDOM } from "jsdom";
8
8
  import { getQueryCachePath } from "../databases/index.js";
9
9
  import { getInterpreterCachePath, getInterpreterCommand } from "../interpreters/index.js";
10
10
  import { getInterpreterMethod, isInterpreter } from "../lib/interpreters.js";
11
- import { isDatabase } from "../lib/notebook.js";
12
11
  import { deserialize } from "../lib/serialize.js";
13
12
  import { Sourcemap } from "../javascript/sourcemap.js";
14
13
  import { transpile } from "../javascript/transpile.js";
@@ -69,7 +68,7 @@ export function observable({ window = new JSDOM().window, parser = new window.DO
69
68
  statics.add(cell);
70
69
  div.innerHTML = stripExpressions(template, value);
71
70
  }
72
- else if (isDatabase(mode) && cell.database && !cell.database.startsWith("var:")) {
71
+ else if (mode === "sql" && cell.database && !cell.database.startsWith("var:")) {
73
72
  const template = parseTemplate(value);
74
73
  if (!template.expressions.length) {
75
74
  const dir = dirname(context.filename);
@@ -182,7 +181,8 @@ define(
182
181
  inputs: ${JSON.stringify(transpiled.inputs)},
183
182
  outputs: ${JSON.stringify(transpiled.outputs)},
184
183
  output: ${JSON.stringify(transpiled.output)},
185
- display: ${cell.mode === "js" || cell.mode === "ts" || cell.mode === "sql"},
184
+ display: ${cell.mode === "js" || cell.mode === "ts"},
185
+ displayMode: ${JSON.stringify(cell.mode === "sql" ? "table" : "default")},
186
186
  assets: ${assets.size > 0 ? "assets" : "undefined"},
187
187
  autodisplay: ${transpiled.autodisplay},
188
188
  autoview: ${transpiled.autoview},
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/observablehq/notebook-kit.git"
7
7
  },
8
- "version": "2.5.9",
8
+ "version": "2.6.0",
9
9
  "type": "module",
10
10
  "scripts": {
11
11
  "test": "TZ=America/Los_Angeles vitest",