Skip to content

Commit e4ff4bb

Browse files
committed
Changed add block button to be a button element and added Enter handler
1 parent 2578871 commit e4ff4bb

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

packages/core/src/blocks/FileBlockContent/fileBlockHelpers.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const createAddFileButton = (
5252
buttonText?: string,
5353
buttonIcon?: HTMLElement
5454
) => {
55-
const addFileButton = document.createElement("div");
55+
const addFileButton = document.createElement("button");
5656
addFileButton.className = "bn-add-file-button";
5757

5858
const addFileButtonIcon = document.createElement("div");
@@ -81,6 +81,18 @@ export const createAddFileButton = (
8181
})
8282
);
8383
};
84+
const windowKeyDownHandler = (event: KeyboardEvent) => {
85+
if (
86+
event.key === "Enter" &&
87+
editor.getTextCursorPosition().block.id === block.id
88+
) {
89+
editor._tiptapEditor.view.dispatch(
90+
editor._tiptapEditor.state.tr.setMeta(editor.filePanel!.plugin, {
91+
block: block,
92+
})
93+
);
94+
}
95+
};
8496

8597
addFileButton.appendChild(addFileButtonIcon);
8698
addFileButton.appendChild(addFileButtonText);
@@ -91,6 +103,7 @@ export const createAddFileButton = (
91103
true
92104
);
93105
addFileButton.addEventListener("click", addFileButtonClickHandler, true);
106+
window.addEventListener("keydown", windowKeyDownHandler, true);
94107

95108
return {
96109
dom: addFileButton,
@@ -105,6 +118,7 @@ export const createAddFileButton = (
105118
addFileButtonClickHandler,
106119
true
107120
);
121+
window.removeEventListener("keydown", windowKeyDownHandler, true);
108122
},
109123
};
110124
};

packages/core/src/editor/Block.css

+23-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ BASIC STYLES
3030
/*margin: 0px;*/
3131
}
3232

33-
.bn-block-content.ProseMirror-selectednode > *,
33+
.ProseMirror-focused .bn-block-content.ProseMirror-selectednode > *,
3434
/* Case for node view renderers */
35-
.ProseMirror-selectednode > .bn-block-content > * {
35+
.ProseMirror-focused .ProseMirror-selectednode > .bn-block-content > * {
3636
border-radius: 4px;
3737
outline: 4px solid rgb(100, 160, 255);
3838
}
@@ -256,6 +256,7 @@ NESTED BLOCKS
256256
[data-file-block] .bn-add-file-button {
257257
align-items: center;
258258
background-color: rgb(242, 241, 238);
259+
border: none;
259260
border-radius: 4px;
260261
color: rgb(125, 121, 122);
261262
cursor: pointer;
@@ -295,6 +296,26 @@ NESTED BLOCKS
295296
width: 100%;
296297
}
297298

299+
[data-text-alignment="left"] .bn-file-default-preview, [data-text-alignment="left"] .bn-file-and-caption-wrapper {
300+
align-items: flex-start;
301+
justify-content: flex-start;
302+
}
303+
304+
[data-text-alignment="center"] .bn-file-default-preview, [data-text-alignment="center"] .bn-file-and-caption-wrapper {
305+
align-items: center;
306+
justify-content: center;
307+
}
308+
309+
[data-text-alignment="right"] .bn-file-default-preview, [data-text-alignment="right"] .bn-file-and-caption-wrapper {
310+
align-items: flex-end;
311+
justify-content: flex-end;
312+
}
313+
314+
[data-text-alignment="justify"] .bn-file-default-preview, [data-text-alignment="justify"] .bn-file-and-caption-wrapper {
315+
align-items: flex-start;
316+
justify-content: flex-start;
317+
}
318+
298319
[data-file-block] .bn-file-default-preview:hover,
299320
.ProseMirror-selectednode .bn-file-default-preview {
300321
background-color: rgb(225, 225, 225);

packages/core/src/extensions/NonEditableBlocks/NonEditableBlockPlugin.ts

+24
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ export const NonEditableBlockPlugin = () => {
3434
}
3535
// Checks if key press is Enter
3636
if (event.key === "Enter") {
37+
// Kind of a hacky way to ensure that pressing Enter when a file
38+
// block is showing the add file button will open the file panel.
39+
// Here, we just make the Enter handler skip this case and the file
40+
// block's implementation will have to handle it itself. It would be
41+
// cleaner if both handlers were in the same place, however:
42+
// - This plugin takes precedence over handlers in the file block's
43+
// implementation, so we can't override the behaviour there.
44+
// - This plugin has no access to the BN schema, so it can't convert
45+
// the node to a block for the file panel plugin, and therefore
46+
// can't open the file plugin here.
47+
let blockContentDOM = view.domAtPos(view.state.selection.from)
48+
.node as HTMLElement;
49+
while (!blockContentDOM.className.includes("bn-block-content")) {
50+
blockContentDOM = blockContentDOM.firstChild as HTMLElement;
51+
}
52+
53+
const isFileBlock =
54+
blockContentDOM.getAttribute("data-file-block") !== null;
55+
const hasURL = blockContentDOM.getAttribute("data-url") !== null;
56+
57+
if (isFileBlock && !hasURL) {
58+
return false;
59+
}
60+
3761
const tr = view.state.tr;
3862
view.dispatch(
3963
tr

packages/react/src/blocks/FileBlockContent/fileBlockHelpers.tsx

+28-2
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,43 @@ export const AddFileButton = (
6868
props.editor._tiptapEditor.view,
6969
props.editor.filePanel,
7070
]);
71+
const windowKeyDownHandler = useCallback(
72+
(event: KeyboardEvent) => {
73+
if (
74+
event.key === "Enter" &&
75+
props.editor.getTextCursorPosition().block.id === props.block.id
76+
) {
77+
props.editor._tiptapEditor.view.dispatch(
78+
props.editor._tiptapEditor.state.tr.setMeta(
79+
props.editor.filePanel!.plugin,
80+
{
81+
block: props.block,
82+
}
83+
)
84+
);
85+
}
86+
},
87+
[props.block, props.editor]
88+
);
89+
90+
useEffect(() => {
91+
window.addEventListener("keydown", windowKeyDownHandler);
92+
93+
return () => {
94+
window.removeEventListener("keydown", windowKeyDownHandler);
95+
};
96+
}, [windowKeyDownHandler]);
7197

7298
return (
73-
<div
99+
<button
74100
className={"bn-add-file-button"}
75101
onMouseDown={addFileButtonMouseDownHandler}
76102
onClick={addFileButtonClickHandler}>
77103
<div className={"bn-add-file-button-icon"}>
78104
{props.buttonIcon || <RiFile2Line size={24} />}
79105
</div>
80106
<div className={"bn-add-file-button-text"}>{props.buttonText}</div>
81-
</div>
107+
</button>
82108
);
83109
};
84110

0 commit comments

Comments
 (0)