Published
Edited
Sep 26, 2022
1 fork
Importers
3 stars
Insert cell
Insert cell
copyCellButton(["x = Math.sin(now / 1000)", "y = Math.round(x * 10)"])
Insert cell
copyCellButton(d3.range(3))
Insert cell
copyCellButton([{type: "md", value: "# Hello"}, "now"])
Insert cell
Insert cell
Insert cell
copyCellButton = cells => Inputs.button("Copy cells", {reduce: () => {
function listener(e) {
e.clipboardData.setData("text/plain", cells.join("\n\n"));
e.clipboardData.setData("application/vnd.observablehq+json", JSON.stringify(cells.map(makeCell)));
e.preventDefault();
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
}})
Insert cell
makeCell = (value) => {
const id = mutable id++;
if (typeof value === "string") {
return { ...defaultJS, id, value };
} else if (typeof value === "object") {
if (value.type === "js") return { ...defaultJS, id, ...value };
return { ...defaultOther, id, ...value };
} else {
throw new Error("Value must be string or object");
}
}
Insert cell
defaultJS = ({
id: 0,
value: "",
pinned: true,
mode: "js",
data: null,
name: null
});
Insert cell
defaultOther = ({
id: 0,
value: "",
pinned: false,
mode: "md",
data: null,
name: ""
})
Insert cell
// I don't think pasting cells even looks at their IDs but this lets them autoincrement
mutable id = 0
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more