Skip to content

Redefining a cell #26

Answered by mbostock
GordonSmith asked this question in Q&A
Aug 1, 2025 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

Something like this:

import type {DefineState, Definition} from "@observablehq/notebook-kit/runtime";
import {define} from "@observablehq/notebook-kit/runtime";

const main = document.querySelector("main")!;
const stateById = new Map<number, DefineState>();

export function add(definition: Definition): void {
  const {id} = definition;
  let state = stateById.get(id);
  if (state) {
    state.variables.forEach((v) => v.delete());
    state.variables = [];
  } else {
    const root = main.appendChild(document.createElement("div"));
    state = {root, expanded: [], variables: []};
    stateById.set(id, state);
  }
  define(state, definition);
}

export function remove(id: number): void {
  c…

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@GordonSmith
Comment options

@mbostock
Comment options

Answer selected by mbostock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants