main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // tldraw offline — document script (script/main.js)
  2. //
  3. // A document script's default export receives a ctx object and runs whenever the
  4. // document loads (and reruns when you save the script). Contract, verified against
  5. // the app's bundled script-context.d.ts:
  6. //
  7. // export default function ({ editor, helpers, signal }) { ... }
  8. //
  9. // editor — the live tldraw Editor for this document
  10. // helpers — editor-bound conveniences: createShapeIfMissing, createShapesIfMissing,
  11. // createArrowBetweenShapes, translateShapes, onShapeTranslate,
  12. // richTextToPlainText, boxShapes, getLints
  13. // signal — an AbortSignal fired before the script reruns and when the board
  14. // closes. Register ALL cleanup on it (this is how you avoid leaks).
  15. //
  16. // Pure tldraw primitives (createShapeId, toRichText, Vec, ...) are imported from
  17. // the `tldraw` app module — NOT globals. react / react-dom are also importable.
  18. // It is not a Node project; only those modules are available.
  19. import { createShapeId, toRichText } from 'tldraw'
  20. export default function ({ editor, helpers, signal }) {
  21. const { createShapeIfMissing, createArrowBetweenShapes } = helpers
  22. // --- 1. Build durable "furniture" idempotently (stable ids, create-if-missing).
  23. // Re-running the script must NOT duplicate or clobber user edits.
  24. const nodes = [
  25. { id: createShapeId('node-ui'), x: 0, y: 0, color: 'blue', label: 'CLI / Gateway' },
  26. { id: createShapeId('node-core'), x: 280, y: 0, color: 'violet', label: 'Agent Core' },
  27. { id: createShapeId('node-tools'), x: 560, y: 0, color: 'green', label: 'Tools' },
  28. ]
  29. editor.run(() => {
  30. for (const n of nodes) {
  31. createShapeIfMissing({
  32. id: n.id,
  33. type: 'geo',
  34. x: n.x,
  35. y: n.y,
  36. props: {
  37. geo: 'rectangle',
  38. w: 220,
  39. h: 110,
  40. color: n.color,
  41. fill: 'solid',
  42. richText: toRichText(n.label),
  43. },
  44. })
  45. }
  46. })
  47. // Connect them (arrows bind to the shapes, so they follow when moved).
  48. createArrowBetweenShapes(nodes[0].id, nodes[1].id, { arrowheadEnd: 'arrow' })
  49. createArrowBetweenShapes(nodes[1].id, nodes[2].id, { arrowheadEnd: 'arrow' })
  50. // --- 2. Add reactive behavior: recolor the last node based on arrow count.
  51. // store.listen fires on the tick AFTER a commit — never read state you just
  52. // wrote synchronously and expect the listener to have run yet.
  53. const targetId = nodes[2].id
  54. function update() {
  55. const hasArrows = editor.getCurrentPageShapes().some((s) => s.type === 'arrow')
  56. editor.run(
  57. () =>
  58. editor.updateShape({
  59. id: targetId,
  60. type: 'geo',
  61. props: { fill: hasArrows ? 'solid' : 'none' },
  62. }),
  63. { history: 'ignore' } // keep script-owned writes out of the user's undo stack
  64. )
  65. }
  66. const stop = editor.store.listen(update)
  67. signal.addEventListener('abort', () => stop()) // <-- the one required cleanup
  68. update() // run once on load
  69. editor.zoomToFit({ animation: { duration: 200 } })
  70. }