Skip to content

Commit e3315db

Browse files
committed
Fixed issues when editor is not editable
1 parent c262cee commit e3315db

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

packages/core/src/blocks/File/helpers/render/createAddFileButton.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export const createAddFileButton = (
3838
};
3939
// Opens the file toolbar.
4040
const addFileButtonClickHandler = () => {
41+
if (!editor.isEditable) {
42+
return;
43+
}
44+
4145
editor.getExtension(FilePanelExtension)?.showMenu(block.id);
4246
};
4347
addFileButton.addEventListener(

packages/core/src/blocks/ToggleWrapper/createToggleWrapper.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export const createToggleWrapper = (
6262
toggledState.set(editor.getBlock(block)!, true);
6363

6464
if (
65+
editor.isEditable &&
6566
editor.getBlock(block)?.children.length === 0 &&
6667
!dom.contains(toggleAddBlockButton)
6768
) {
@@ -139,7 +140,7 @@ export const createToggleWrapper = (
139140
if (toggledState.get(block)) {
140141
toggleWrapper.setAttribute("data-show-children", "true");
141142

142-
if (block.children.length === 0) {
143+
if (editor.isEditable && block.children.length === 0) {
143144
// If the toggle is set to show children, but there are no children,
144145
// we add the "add block" button.
145146
dom.appendChild(toggleAddBlockButton);

packages/react/src/blocks/File/helpers/render/AddFileButton.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RiFile2Line } from "react-icons/ri";
66
import { useDictionary } from "../../../../i18n/dictionary.js";
77
import { ReactCustomBlockRenderProps } from "../../../../schema/ReactBlockSpec.js";
88
import { useExtension } from "../../../../hooks/useExtension.js";
9+
import { useBlockNoteEditor } from "../../../../hooks/useBlockNoteEditor.js";
910

1011
export const AddFileButton = (
1112
props: Omit<
@@ -19,6 +20,7 @@ export const AddFileButton = (
1920
buttonIcon?: ReactNode;
2021
},
2122
) => {
23+
const editor = useBlockNoteEditor<any, any, any>();
2224
const dict = useDictionary();
2325

2426
const filePanel = useExtension(FilePanelExtension);
@@ -32,8 +34,12 @@ export const AddFileButton = (
3234
);
3335
// Opens the file toolbar.
3436
const addFileButtonClickHandler = useCallback(() => {
37+
if (!editor.isEditable) {
38+
return;
39+
}
40+
3541
props.editor.transact(() => filePanel.showMenu(props.block.id));
36-
}, [filePanel, props.block.id, props.editor]);
42+
}, [editor.isEditable, filePanel, props.block.id, props.editor]);
3743

3844
return (
3945
<div

packages/react/src/blocks/ToggleWrapper/ToggleWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export const ToggleWrapper = (
133133
</button>
134134
{children}
135135
</div>
136-
{showChildren && childCount === 0 && (
136+
{editor.isEditable && showChildren && childCount === 0 && (
137137
<button
138138
className="bn-toggle-add-block-button"
139139
type="button"

packages/react/src/components/FormattingToolbar/DefaultButtons/FileDownloadButton.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ export const FileDownloadButton = () => {
2626
const state = useEditorState({
2727
editor,
2828
selector: ({ editor }) => {
29-
if (!editor.isEditable) {
30-
return undefined;
31-
}
32-
3329
const selectedBlocks = editor.getSelection()?.blocks || [
3430
editor.getTextCursorPosition().block,
3531
];

packages/react/src/components/LinkToolbar/LinkToolbarController.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ export const LinkToolbarController = (props: {
164164
mountedBoundingClientRect.current = link.element.getBoundingClientRect();
165165
}
166166

167+
if (!editor.isEditable) {
168+
return null;
169+
}
170+
167171
const Component = props.linkToolbar || LinkToolbar;
168172

169173
return (

0 commit comments

Comments
 (0)