Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 20 additions & 39 deletions packages/react/src/editor/BlockNoteView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import React, {
ReactNode,
Ref,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
Expand Down Expand Up @@ -141,10 +140,6 @@ function BlockNoteViewComponent<
useEditorChange(onChange || emptyFn, editor);
useEditorSelectionChange(onSelectionChange || emptyFn, editor);

useEffect(() => {
editor.isEditable = editable !== false;
}, [editable, editor]);

const setElementRenderer = useCallback(
(ref: (typeof editor)["elementRenderer"]) => {
editor.elementRenderer = ref;
Expand All @@ -170,6 +165,7 @@ function BlockNoteViewComponent<
editorProps: {
autoFocus,
contentEditableProps,
editable,
},
defaultUIProps: {
formattingToolbar,
Expand All @@ -185,6 +181,7 @@ function BlockNoteViewComponent<
}, [
autoFocus,
contentEditableProps,
editable,
formattingToolbar,
linkToolbar,
slashMenu,
Expand All @@ -202,7 +199,6 @@ function BlockNoteViewComponent<
<BlockNoteViewContainer
className={className}
renderEditor={renderEditor}
editable={editable}
editorColorScheme={editorColorScheme}
ref={ref}
{...rest}
Expand All @@ -222,34 +218,26 @@ const BlockNoteViewContainer = React.forwardRef<
HTMLDivElement,
{
renderEditor: boolean;
editable?: boolean;
editorColorScheme: "light" | "dark";
children: ReactNode;
} & Omit<
HTMLAttributes<HTMLDivElement>,
"onChange" | "onSelectionChange" | "children"
>
>(
(
{ className, renderEditor, editable, editorColorScheme, children, ...rest },
ref,
) => (
<div
className={mergeCSSClasses("bn-container", editorColorScheme, className)}
data-color-scheme={editorColorScheme}
{...rest}
ref={ref}
>
{renderEditor ? (
<BlockNoteViewEditor editable={editable}>
{children}
</BlockNoteViewEditor>
) : (
children
)}
</div>
),
);
>(({ className, renderEditor, editorColorScheme, children, ...rest }, ref) => (
<div
className={mergeCSSClasses("bn-container", editorColorScheme, className)}
data-color-scheme={editorColorScheme}
{...rest}
ref={ref}
>
{renderEditor ? (
<BlockNoteViewEditor>{children}</BlockNoteViewEditor>
) : (
children
)}
</div>
));

// https://fettblog.eu/typescript-react-generic-forward-refs/
export const BlockNoteViewRaw = React.forwardRef(BlockNoteViewComponent) as <
Expand All @@ -269,10 +257,7 @@ export const BlockNoteViewRaw = React.forwardRef(BlockNoteViewComponent) as <
* Renders the contentEditable editor itself (.bn-editor element) and the
* default UI elements.
*/
export const BlockNoteViewEditor = (props: {
editable?: boolean;
children?: ReactNode;
}) => {
export const BlockNoteViewEditor = (props: { children?: ReactNode }) => {
const ctx = useBlockNoteViewContext()!;
const editor = useBlockNoteEditor();

Expand All @@ -282,12 +267,8 @@ export const BlockNoteViewEditor = (props: {

const mount = useCallback(
(element: HTMLElement | null) => {
if (
props.editable !== undefined &&
props.editable !== editor.isEditable
) {
editor.isEditable = props.editable;
}
// Set editable state of the actual editor.
editor.isEditable = ctx.editorProps.editable !== false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that editable is not "reactive", i.e. you change that context value & it won't switch the editor between editable & not editable modes?

We should check this behavior in an example. Since I think people may have things like:

function Component(){
const [isEditable, setEditable] = useState(true);

return <><button onClick={()=>setEditable(a=>!a)}>Toggle</button><Editor editable={isEditable} /></>
}

And, expect it to react to the button.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified the existing example where this issue was coming up - all good, editor is re-mounted with the toggled editable state and everything works as expected. ctx.editorProps.editable is in the dependency array of mount, so I think this makes sense.

// Since we are not using TipTap's React Components, we need to set up the contentComponent it expects
// This is a simple replacement for the state management that Tiptap does internally
editor._tiptapEditor.contentComponent = portalManager;
Expand All @@ -297,7 +278,7 @@ export const BlockNoteViewEditor = (props: {
editor.unmount();
}
},
[editor, portalManager, props.editable],
[ctx.editorProps.editable, editor, portalManager],
);

return (
Expand Down
1 change: 1 addition & 0 deletions packages/react/src/editor/BlockNoteViewContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type BlockNoteViewContextValue = {
editorProps: {
autoFocus?: boolean;
contentEditableProps?: Record<string, any>;
editable?: boolean;
};
defaultUIProps: BlockNoteDefaultUIProps;
};
Expand Down
Loading