Skip to content

Commit

Permalink
Dash scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Dec 12, 2023
1 parent 56551bb commit 7b8c7ce
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const EXTRA_FILES = new Map([["node_modules/@observablehq/runtime/dist/runtime.j
const CLIENT_BUNDLES: [entry: string, name: string][] = [
["./src/client/index.js", "client.js"],
["./src/client/stdlib.js", "stdlib.js"],
["./src/client/stdlib/dash.js", "stdlib/dash.js"],
["./src/client/stdlib/dot.js", "stdlib/dot.js"],
["./src/client/stdlib/duckdb.js", "stdlib/duckdb.js"],
["./src/client/stdlib/mermaid.js", "stdlib/mermaid.js"],
Expand Down
1 change: 1 addition & 0 deletions src/client/stdlib/dash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dash/resize.js";
22 changes: 22 additions & 0 deletions src/client/stdlib/dash/resize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// TODO Automatically disconnect the observer when the returned DIV is detached.
export function resize(run: (width: number, height: number) => Node, invalidation?: Promise<void>): Node {
const div = document.createElement("div");
div.style.cssText = "position: relative; height: 100%;";
const observer = new ResizeObserver(([entry]) => {
const {width, height} = entry.contentRect;
while (div.lastChild) div.lastChild.remove();
if (width > 0) {
const child = run(width, height);
// prevent feedback loop if height is used
if (run.length !== 1 && isElement(child)) child.style.position = "absolute";
div.append(child);
}
});
observer.observe(div);
invalidation?.then(() => observer.disconnect());
return div;
}

function isElement(node: Node): node is HTMLElement {
return node.nodeType === 1;
}
1 change: 1 addition & 0 deletions src/client/stdlib/recommendedLibraries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const _ = () => import("npm:lodash").then((lodash) => lodash.default);
export const aq = () => import("npm:arquero");
export const Arrow = () => import("npm:apache-arrow");
export const d3 = () => import("npm:d3");
export const Dash = () => import("observablehq:stdlib/dash");
export const dot = () => import("observablehq:stdlib/dot").then((dot) => dot.default);
export const duckdb = () => import("npm:@duckdb/duckdb-wasm");
export const DuckDBClient = () => import("observablehq:stdlib/duckdb").then((duckdb) => duckdb.DuckDBClient);
Expand Down
2 changes: 2 additions & 0 deletions src/javascript/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ export function createImportResolver(root: string, base: "." | "_import" = "."):
? resolveBuiltin(base, path, "runtime.js")
: specifier === "npm:@observablehq/stdlib"
? resolveBuiltin(base, path, "stdlib.js")
: specifier === "npm:@observablehq/dash"
? resolveBuiltin(base, path, "stdlib/dash.js") // TODO publish to npm
: specifier === "npm:@observablehq/dot"
? resolveBuiltin(base, path, "stdlib/dot.js") // TODO publish to npm
: specifier === "npm:@observablehq/duckdb"
Expand Down
5 changes: 4 additions & 1 deletion src/rollup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {existsSync} from "node:fs";
import {dirname, join, relative} from "node:path";
import {cwd} from "node:process";
import {fileURLToPath} from "node:url";
Expand Down Expand Up @@ -86,5 +87,7 @@ function importMetaResolve(): Plugin {
}

export function getClientPath(entry: string): string {
return relative(cwd(), join(dirname(fileURLToPath(import.meta.url)), "..", entry));
const path = relative(cwd(), join(dirname(fileURLToPath(import.meta.url)), "..", entry));
if (path.endsWith(".js") && !existsSync(path) && existsSync(path.slice(0, -2) + "ts")) return path.slice(0, -2) + "ts";
return path;
}
1 change: 1 addition & 0 deletions test/deploy-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const EXTRA_FILES: string[] = [
"_observablehq/client.js",
"_observablehq/runtime.js",
"_observablehq/stdlib.js",
"_observablehq/stdlib/dash.js",
"_observablehq/stdlib/dot.js",
"_observablehq/stdlib/duckdb.js",
"_observablehq/stdlib/mermaid.js",
Expand Down
Empty file.

0 comments on commit 7b8c7ce

Please sign in to comment.