Public
Edited
Feb 22, 2024
2 forks
30 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
editor = (doc) => {
const { EditorState, EditorView, EditorSelection, basicSetup } = cmBasic;
const { javascript } = cmJS;
const { keymap } = cmView;

const updateViewOf = EditorView.updateListener.of((update) => {
const { dom } = update.view;
dom.value = update;
dom.dispatchEvent(new CustomEvent("input"));
});

const view = new EditorView({
state: EditorState.create({
doc,
extensions: [
basicSetup,
javascript(),
updateViewOf,
cmView.keymap.of([
{
key: "F9",
run: expand
}
]),
cmView.keymap.of([
{
key: "ctrl-r",
run: expand
}
])
]
})
});

return view.dom;
}
Insert cell
expand = function (view) {
view.dispatch(
view.state.changeByRange((range) => {
const result = evaluate(view.state.sliceDoc(range.from, range.to));

// Evaluation errored, don’t do anything
if (result === false) return { range };

// How much longer is the new string?
const rangeLengthDif = result.length - (range.to - range.from);

return {
changes: {
from: range.from,
to: range.to,
insert: result
},
// The updated selection range
range: cmState.EditorSelection.range(
range.from,
range.to + rangeLengthDif
)
};
})
);

return true;
}
Insert cell
// Currently just manually passes in d3 and lodash (_) as fun things to use.
// I’d love to figure out how to pass in everything in scope at the cursor!!
evaluate = (expr) => {
try {
const result = new Function(...Object.keys(libs), `return ${expr}`)(
...Object.values(libs)
);
return typeof result === "object" ? JSON.stringify(result) : String(result);
} catch {
return false;
}
}
Insert cell
// Add anything you want F9 to be able to reference during evaluation;
// these are passed to the new Function constructed above.
libs = ({
d3,
_
})
Insert cell
cmView = skypack("@codemirror/next/view")
Insert cell
cmState = skypack("@codemirror/next/state")
Insert cell
cmBasic = skypack("@codemirror/next/basic-setup")
Insert cell
cmJS = skypack("@codemirror/next/lang-javascript")
Insert cell
skypack = (library) => import(`https://cdn.skypack.dev/${library}?min`)
Insert cell
// Nice formatting to display keyboard shortcuts
key = (keys) =>
html`${keys
.split(" ")
.map(
(key) => `<span style="
font-family: -apple-system, BlinkMacSystemFont, 'avenir next', avenir,
helvetica, 'helvetica neue', ubuntu, roboto, noto, 'segoe ui', arial,
sans-serif;
font-size: 14px;
font-weight: 500;
box-shadow: 0 0 0 1px #dedede, 1px 1px 0 1px #e8e8e8;
margin: 0 4px;
min-width: 6px;
padding-left: 4px;
padding-right: 4px;
text-align: center;
white-space: nowrap;
border-radius: .25rem;">${key}</span>`
)
.join("")}`
Insert cell
import { tweet } from "@mbostock/tweet"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more