Skip to content

Commit 7b8c7ce

Browse files
committed
Dash scaffolding
1 parent 56551bb commit 7b8c7ce

File tree

8 files changed

+32
-1
lines changed

8 files changed

+32
-1
lines changed

src/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const EXTRA_FILES = new Map([["node_modules/@observablehq/runtime/dist/runtime.j
2020
const CLIENT_BUNDLES: [entry: string, name: string][] = [
2121
["./src/client/index.js", "client.js"],
2222
["./src/client/stdlib.js", "stdlib.js"],
23+
["./src/client/stdlib/dash.js", "stdlib/dash.js"],
2324
["./src/client/stdlib/dot.js", "stdlib/dot.js"],
2425
["./src/client/stdlib/duckdb.js", "stdlib/duckdb.js"],
2526
["./src/client/stdlib/mermaid.js", "stdlib/mermaid.js"],

src/client/stdlib/dash.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./dash/resize.js";

src/client/stdlib/dash/resize.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// TODO Automatically disconnect the observer when the returned DIV is detached.
2+
export function resize(run: (width: number, height: number) => Node, invalidation?: Promise<void>): Node {
3+
const div = document.createElement("div");
4+
div.style.cssText = "position: relative; height: 100%;";
5+
const observer = new ResizeObserver(([entry]) => {
6+
const {width, height} = entry.contentRect;
7+
while (div.lastChild) div.lastChild.remove();
8+
if (width > 0) {
9+
const child = run(width, height);
10+
// prevent feedback loop if height is used
11+
if (run.length !== 1 && isElement(child)) child.style.position = "absolute";
12+
div.append(child);
13+
}
14+
});
15+
observer.observe(div);
16+
invalidation?.then(() => observer.disconnect());
17+
return div;
18+
}
19+
20+
function isElement(node: Node): node is HTMLElement {
21+
return node.nodeType === 1;
22+
}

src/client/stdlib/recommendedLibraries.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const _ = () => import("npm:lodash").then((lodash) => lodash.default);
22
export const aq = () => import("npm:arquero");
33
export const Arrow = () => import("npm:apache-arrow");
44
export const d3 = () => import("npm:d3");
5+
export const Dash = () => import("observablehq:stdlib/dash");
56
export const dot = () => import("observablehq:stdlib/dot").then((dot) => dot.default);
67
export const duckdb = () => import("npm:@duckdb/duckdb-wasm");
78
export const DuckDBClient = () => import("observablehq:stdlib/duckdb").then((duckdb) => duckdb.DuckDBClient);

src/javascript/imports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ export function createImportResolver(root: string, base: "." | "_import" = "."):
311311
? resolveBuiltin(base, path, "runtime.js")
312312
: specifier === "npm:@observablehq/stdlib"
313313
? resolveBuiltin(base, path, "stdlib.js")
314+
: specifier === "npm:@observablehq/dash"
315+
? resolveBuiltin(base, path, "stdlib/dash.js") // TODO publish to npm
314316
: specifier === "npm:@observablehq/dot"
315317
? resolveBuiltin(base, path, "stdlib/dot.js") // TODO publish to npm
316318
: specifier === "npm:@observablehq/duckdb"

src/rollup.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {existsSync} from "node:fs";
12
import {dirname, join, relative} from "node:path";
23
import {cwd} from "node:process";
34
import {fileURLToPath} from "node:url";
@@ -86,5 +87,7 @@ function importMetaResolve(): Plugin {
8687
}
8788

8889
export function getClientPath(entry: string): string {
89-
return relative(cwd(), join(dirname(fileURLToPath(import.meta.url)), "..", entry));
90+
const path = relative(cwd(), join(dirname(fileURLToPath(import.meta.url)), "..", entry));
91+
if (path.endsWith(".js") && !existsSync(path) && existsSync(path.slice(0, -2) + "ts")) return path.slice(0, -2) + "ts";
92+
return path;
9093
}

test/deploy-test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const EXTRA_FILES: string[] = [
1818
"_observablehq/client.js",
1919
"_observablehq/runtime.js",
2020
"_observablehq/stdlib.js",
21+
"_observablehq/stdlib/dash.js",
2122
"_observablehq/stdlib/dot.js",
2223
"_observablehq/stdlib/duckdb.js",
2324
"_observablehq/stdlib/mermaid.js",

test/output/build/simple-public/_observablehq/stdlib/dash.js

Whitespace-only changes.

0 commit comments

Comments
 (0)