From bcba35550abaed68b43d53fea7fbad6bb5debb4a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 16 Jan 2025 17:28:50 -0500 Subject: [PATCH] fix: NodeView does not re-render when editable prop changes (#8237) --- app/editor/components/NodeViewRenderer.tsx | 5 +++++ app/editor/index.tsx | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/editor/components/NodeViewRenderer.tsx b/app/editor/components/NodeViewRenderer.tsx index 701645779c..ca09ac2b33 100644 --- a/app/editor/components/NodeViewRenderer.tsx +++ b/app/editor/components/NodeViewRenderer.tsx @@ -25,4 +25,9 @@ export class NodeViewRenderer { this.props = props; } } + + @action + public setProp(key: K, value: T[K]) { + this.props[key] = value; + } } diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 5f63a08379..eff1e015fe 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -1,5 +1,6 @@ /* global File Promise */ import { PluginSimple } from "markdown-it"; +import { Observer } from "mobx-react"; import { darken, transparentize } from "polished"; import { baseKeymap } from "prosemirror-commands"; import { dropCursor } from "prosemirror-dropcursor"; @@ -247,6 +248,12 @@ export class Editor extends React.PureComponent< ...this.view.props, editable: () => !this.props.readOnly, }); + + // NodeView will not automatically render when editable changes so we must trigger an update + // manually, see: https://discuss.prosemirror.net/t/re-render-custom-nodeview-when-view-editable-changes/6441 + Array.from(this.renderers).forEach((view) => + view.setProp("isEditable", !this.props.readOnly) + ); } if (this.props.scrollTo && this.props.scrollTo !== prevProps.scrollTo) { @@ -848,7 +855,11 @@ export class Editor extends React.PureComponent< Object.values(this.widgets).map((Widget, index) => ( ))} - {Array.from(this.renderers).map((view) => view.content)} + + {() => ( + <>{Array.from(this.renderers).map((view) => view.content)} + )} +