Skip to content

Commit 9c8029d

Browse files
fix: Issues when editor is non-editable (#783)
* Fixed some issues when editor is non-editable * Small typing fix * Added comment
1 parent cbabb81 commit 9c8029d

File tree

16 files changed

+61
-41
lines changed

16 files changed

+61
-41
lines changed

packages/core/src/api/parsers/pasteExtension.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ export const createPasteFromClipboardExtension = <
2727
handleDOMEvents: {
2828
paste(_view, event) {
2929
event.preventDefault();
30+
31+
if (!editor.isEditable) {
32+
return;
33+
}
34+
3035
let format: (typeof acceptedMIMETypes)[number] | null = null;
3136

3237
for (const mimeType of acceptedMIMETypes) {

packages/core/src/editor/Block.css

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,14 @@ NESTED BLOCKS
279279
background-color: rgb(242, 241, 238);
280280
border-radius: 4px;
281281
color: rgb(125, 121, 122);
282-
cursor: pointer;
283282
display: flex;
284283
flex-direction: row;
285284
gap: 10px;
286285
padding: 12px;
287286
width: 100%;
288287
}
289288

290-
[data-file-block] .bn-add-file-button:hover {
289+
.bn-editor[contenteditable="true"] [data-file-block] .bn-add-file-button:hover {
291290
background-color: rgb(225, 225, 225);
292291
}
293292

packages/core/src/editor/BlockNoteEditor.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ export class BlockNoteEditor<
194194
ISchema,
195195
SSchema
196196
>;
197-
public readonly filePanel?: FilePanelProsemirrorPlugin<
198-
BSchema,
199-
ISchema,
200-
SSchema
201-
>;
197+
public readonly filePanel?: FilePanelProsemirrorPlugin<ISchema, SSchema>;
202198
public readonly tableHandles?: TableHandlesProsemirrorPlugin<
203199
ISchema,
204200
SSchema

packages/core/src/extensions/FilePanel/FilePanelPlugin.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { BlockNoteEditor } from "../../editor/BlockNoteEditor";
55
import { UiElementPosition } from "../../extensions-shared/UiElementPosition";
66
import type {
77
BlockFromConfig,
8-
BlockSchema,
98
FileBlockConfig,
109
InlineContentSchema,
1110
StyleSchema,
@@ -26,9 +25,12 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
2625
public state?: FilePanelState<I, S>;
2726
public emitUpdate: () => void;
2827

29-
public prevWasEditable: boolean | null = null;
30-
3128
constructor(
29+
private readonly editor: BlockNoteEditor<
30+
Record<string, FileBlockConfig>,
31+
I,
32+
S
33+
>,
3234
private readonly pluginKey: PluginKey,
3335
private readonly pmView: EditorView,
3436
emitUpdate: (state: FilePanelState<I, S>) => void
@@ -81,7 +83,7 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
8183
block: BlockFromConfig<FileBlockConfig, I, S>;
8284
} = this.pluginKey.getState(view.state);
8385

84-
if (!this.state?.show && pluginState.block) {
86+
if (!this.state?.show && pluginState.block && this.editor.isEditable) {
8587
const blockElement = document.querySelector(
8688
`[data-node-type="blockContainer"][data-id="${pluginState.block.id}"]`
8789
)!;
@@ -99,7 +101,8 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
99101

100102
if (
101103
!view.state.selection.eq(prevState.selection) ||
102-
!view.state.doc.eq(prevState.doc)
104+
!view.state.doc.eq(prevState.doc) ||
105+
!this.editor.isEditable
103106
) {
104107
if (this.state?.show) {
105108
this.state.show = false;
@@ -128,22 +131,21 @@ export class FilePanelView<I extends InlineContentSchema, S extends StyleSchema>
128131
const filePanelPluginKey = new PluginKey("FilePanelPlugin");
129132

130133
export class FilePanelProsemirrorPlugin<
131-
B extends BlockSchema,
132134
I extends InlineContentSchema,
133135
S extends StyleSchema
134136
> extends EventEmitter<any> {
135137
private view: FilePanelView<I, S> | undefined;
136138
public readonly plugin: Plugin;
137139

138-
constructor(_editor: BlockNoteEditor<B, I, S>) {
140+
constructor(editor: BlockNoteEditor<Record<string, FileBlockConfig>, I, S>) {
139141
super();
140142
this.plugin = new Plugin<{
141143
block: BlockFromConfig<FileBlockConfig, I, S> | undefined;
142144
}>({
143145
key: filePanelPluginKey,
144146
view: (editorView) => {
145-
this.view = new FilePanelView(
146-
// editor,
147+
this.view = new FilePanelView<I, S>(
148+
editor,
147149
filePanelPluginKey,
148150
editorView,
149151
(state) => {

packages/core/src/extensions/FormattingToolbar/FormattingToolbarPlugin.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export class FormattingToolbarView implements PluginView {
1515

1616
public preventHide = false;
1717
public preventShow = false;
18-
public prevWasEditable: boolean | null = null;
1918

2019
public shouldShow: (props: {
2120
view: EditorView;
@@ -96,16 +95,10 @@ export class FormattingToolbarView implements PluginView {
9695
const isSame =
9796
oldState && oldState.doc.eq(doc) && oldState.selection.eq(selection);
9897

99-
if (
100-
(this.prevWasEditable === null ||
101-
this.prevWasEditable === this.editor.isEditable) &&
102-
(composing || isSame)
103-
) {
98+
if (composing || isSame) {
10499
return;
105100
}
106101

107-
this.prevWasEditable = this.editor.isEditable;
108-
109102
// support for CellSelections
110103
const { ranges } = selection;
111104
const from = Math.min(...ranges.map((range) => range.$from.pos));
@@ -119,11 +112,12 @@ export class FormattingToolbarView implements PluginView {
119112
});
120113

121114
// Checks if menu should be shown/updated.
122-
if (
123-
this.editor.isEditable &&
124-
!this.preventShow &&
125-
(shouldShow || this.preventHide)
126-
) {
115+
if (!this.preventShow && (shouldShow || this.preventHide)) {
116+
// Unlike other UI elements, we don't prevent the formatting toolbar from
117+
// showing when the editor is not editable. This is because some buttons,
118+
// e.g. the download file button, should still be accessible. Therefore,
119+
// logic for hiding when the editor is non-editable is handled
120+
// individually in each button.
127121
this.state = {
128122
show: true,
129123
referencePos: this.getSelectionBoundingBox(),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const BasicTextStyleButton = <Style extends BasicTextStyle>(props: {
101101
return !!selectedBlocks.find((block) => block.content !== undefined);
102102
}, [basicTextStyleInSchema, selectedBlocks]);
103103

104-
if (!show) {
104+
if (!show || !editor.isEditable) {
105105
return null;
106106
}
107107

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const ColorStyleButton = () => {
131131
return false;
132132
}, [backgroundColorInSchema, selectedBlocks, textColorInSchema]);
133133

134-
if (!show) {
134+
if (!show || !editor.isEditable) {
135135
return null;
136136
}
137137

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ export const CreateLinkButton = () => {
7878
return true;
7979
}, [linkInSchema, selectedBlocks]);
8080

81-
if (!show || !("link" in editor.schema.inlineContentSchema)) {
81+
if (
82+
!show ||
83+
!("link" in editor.schema.inlineContentSchema) ||
84+
!editor.isEditable
85+
) {
8286
return null;
8387
}
8488

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ export const FileCaptionButton = () => {
6969
[]
7070
);
7171

72-
if (!fileBlock || checkBlockIsFileBlockWithPlaceholder(fileBlock, editor)) {
72+
if (
73+
!fileBlock ||
74+
checkBlockIsFileBlockWithPlaceholder(fileBlock, editor) ||
75+
!editor.isEditable
76+
) {
7377
return null;
7478
}
7579

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export const FileDeleteButton = () => {
4545
editor.removeBlocks([fileBlock!]);
4646
}, [editor, fileBlock]);
4747

48-
if (!fileBlock || checkBlockIsFileBlockWithPlaceholder(fileBlock, editor)) {
48+
if (
49+
!fileBlock ||
50+
checkBlockIsFileBlockWithPlaceholder(fileBlock, editor) ||
51+
!editor.isEditable
52+
) {
4953
return null;
5054
}
5155

0 commit comments

Comments
 (0)