Embedding Monaco in React Flow: does a read-only JSON preview still need to guard onChange?

January 6, 2025

I used Monaco to edit JSON inside PuppyAgent workflow nodes. A node has two deceptively similar views. The input view shows the JSON the user wrote, while the embedding view shows chunks extracted from it. Both use indentation and syntax highlighting, but only one should modify the original text. The work lay in telling the editor which text to display and telling the node when it could accept a write.1

Why was I reading about Emitter?

On the afternoon of January 6, 2025, I worked my way from Monaco’s API index to JSON Schema and Emitter. That evening I committed the first version of the change that built an embedding view from JSON. It gives the reading some concrete project context, although my records no longer tell me whether a particular bug sent me to the last page.2

Monaco describes Emitter<T> in one line: “A helper that allows to emit and listen to typed events”. The creator emits an event with fire(value), and subscribers receive notifications through event(listener). Monaco already exposes focus events and others for the editor, so an embedding component can subscribe directly.3

That was how the project’s JSONForm worked. The React wrapper’s onMount supplied the editor instance. On focus, a callback set the focus styling and called preventParentDrag(). On blur, it called allowParentDrag(). This separated selecting text inside the editor from dragging a node on the canvas. Content changes traveled separately through the wrapper’s onChange, updating data.content by node ID. The implementation shows the event subscribers. I found no call that created a monaco.Emitter of my own.45

To remove an editor border, do I change Monaco or the node?

On January 16 I asked Cursor two short questions: “make monaco editor border less” and “set this monaco editor to not editable”. Both sound like editor options, but they belong to different layers.6

The div around JSONForm drew the border. It already had a one-pixel border and added an outline, shadow, and different border colors on focus. Changing only the editor theme would leave the outer frame in place. That day’s commit gave the container border-none and removed the focus-dependent styling. Whether the keyboard could modify the text, on the other hand, was controlled by options.readOnly passed to Monaco.7

Size had a third layer. The parent node passed in width and height, and the outer container used both. The editor itself received the width minus 8 and the height minus 12. automaticLayout can follow changes in the container, but the space subtracted by the parent and child still has to add up. I also ran into a JSON node whose height kept increasing when resized. I covered its measurement and write-back behavior in Workflow node resizing.4

When switching to a read-only preview, which data can be written back?

Suppose the input is {"city":"上海"}. The input tab needs to preserve that exact text. The embedding tab might show an array containing the extracted “上海” and metadata locating the source field. If the component always saves “whatever text is currently on screen” to content, the two views will compete for the same field. Three questions need separate answers.1

ViewSource of Monaco’s valueCan the user edit?Where should changes go?
InputThe node’s original contentYes during normal editing, optionally locked while generating a new nodeUpdate the original node’s content after editing
Embedding previewDisplay text generated from the node’s chunksRead-onlyGeneration logic updates chunks. The preview has no reason to write back to the original text.

The January 16 implementation let the parent pass inputvalue and readonly. One line captures why a later change was needed: value={inputvalue ? inputvalue : …}. If a caller deliberately supplied an empty string, the component fell back to the node’s original text. “Was an alternative provided?” and “Does the alternative contain any characters?” had become the same condition.7

On January 19, while fixing switching between input and embedding views, I added synced. The input tab passed synced={true}, while the preview passed readonly={true} and synced={false}. The shared callback updated the node only when synchronization was allowed. That encoded two decisions: readOnly controlled user input, and synced controlled whether the application allowed this callback to write data back. A display value updated by the caller no longer needed to count as an edit to the original text.1

By March 5 I was working on JSON node rendering again. This time the parent explicitly passed an input-view or embedding-view marker, and the editor read content or chunks accordingly. Looking back, if I wrote it today I’d make the view the only mode parameter, store only structured data in chunks, and serialize it consistently for display. The example below is adapted to follow that convention.8

const isPreview = view === 'embedding'
const text = isPreview
  ? JSON.stringify(chunks ?? [], null, 2)
  : (content ?? '')

return (
  <Editor
    defaultLanguage="json"
    value={text}
    options={{ readOnly: isPreview || isGeneratingNode }}
    onChange={(next)=> {
      if (!isPreview && !isGeneratingNode) saveContent(next ?? '')
    }}
  />
)

I’d test the editor with two actions. First clear the input and confirm it stays blank. Then enter some JSON, switch to the chunk view and back, and confirm the original text survives character for character. The first exposes an empty-string fallback. The second catches a preview value accidentally written back to the source. Together they say more about correct view switching than watching for a cursor on the preview tab.

References

Footnotes

  1. CaiBorui [S2thend]. (2025, January 19). Fix bug cannot switch between input view and embedding view [Git 提交]. GitHub. JsonNode 的两个调用位置与 JSONForm 的写回条件。 ↩ ↩2 ↩3

  2. CaiBorui. (2025, January 6). Monaco Editor API 浏览记录 [本地 Firefox 历史]。文章日期取此次访问日。北京时间 17:05 进入 editor、Thenable 页面,随后查看 JSONSchema 的 patternErrorMessage、JSONSchemaMap、JSONSchema,17:06:54 从 JSONSchema 进入 Emitter 原始地址。同日晚 20:53 提交 JSON 到 embedding 视图的转换。访问顺序与代码于 2026 年 11 月 18 日核对。 ↩

  3. Microsoft. (n.d.). Emitter. Monaco Editor API. 类说明首段及 event、fire、dispose 成员。 ↩

  4. PuppyAgent contributors. (2025, January 6). JSONForm.tsx [提交 343a5e95 时已有的代码]. GitHub. 文本变化与焦点处理;容器和编辑器配置。 ↩ ↩2

  5. Atoyan, S. (n.d.). Monaco Editor for React. GitHub. Editor props 中的 value、onChange 和 onMount。 ↩

  6. CaiBorui. (2025, January 16). Customizing Monaco Editor Border 与 Setting Monaco Editor to Read-Only [与 Cursor 的未公开开发对话,两段引文分别取自各会话首条用户消息]。 ↩

  7. CaiBorui [S2thend]. (2025, January 16). Finish json node style adjustment [Git 提交]. GitHub. JSONForm 的边框、显示值和只读选项。 ↩ ↩2

  8. CaiBorui [S2thend]. (2025, March 5). Fix json node render bug [Git 提交]. GitHub. 视图标记、chunks 写入和 value 分支。 ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-01-06). Embedding Monaco in React Flow: does a read-only JSON preview still need to guard onChange?. https://caiborui.com/blog/2025-01-06-zh-monaco-json-editor-readonly-and-change-events-entrans

@misc{borui2025-4c3c1ae1,
  author = {borui},
  title = {{Embedding Monaco in React Flow: does a read-only JSON preview still need to guard onChange?}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-01-06-zh-monaco-json-editor-readonly-and-change-events-entrans}
}