Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add file button accessibility #758

Open
wants to merge 1 commit into
base: file-blocks
Choose a base branch
from
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
16 changes: 15 additions & 1 deletion packages/core/src/blocks/FileBlockContent/fileBlockHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createAddFileButton = (
buttonText?: string,
buttonIcon?: HTMLElement
) => {
const addFileButton = document.createElement("div");
const addFileButton = document.createElement("button");
addFileButton.className = "bn-add-file-button";

const addFileButtonIcon = document.createElement("div");
Expand Down Expand Up @@ -81,6 +81,18 @@ export const createAddFileButton = (
})
);
};
const windowKeyDownHandler = (event: KeyboardEvent) => {
if (
event.key === "Enter" &&
editor.getTextCursorPosition().block.id === block.id
) {
editor._tiptapEditor.view.dispatch(
editor._tiptapEditor.state.tr.setMeta(editor.filePanel!.plugin, {
block: block,
})
);
}
};

addFileButton.appendChild(addFileButtonIcon);
addFileButton.appendChild(addFileButtonText);
Expand All @@ -91,6 +103,7 @@ export const createAddFileButton = (
true
);
addFileButton.addEventListener("click", addFileButtonClickHandler, true);
window.addEventListener("keydown", windowKeyDownHandler, true);

return {
dom: addFileButton,
Expand All @@ -105,6 +118,7 @@ export const createAddFileButton = (
addFileButtonClickHandler,
true
);
window.removeEventListener("keydown", windowKeyDownHandler, true);
},
};
};
Expand Down
25 changes: 23 additions & 2 deletions packages/core/src/editor/Block.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ BASIC STYLES
/*margin: 0px;*/
}

.bn-block-content.ProseMirror-selectednode > *,
.ProseMirror-focused .bn-block-content.ProseMirror-selectednode > *,
/* Case for node view renderers */
.ProseMirror-selectednode > .bn-block-content > * {
.ProseMirror-focused .ProseMirror-selectednode > .bn-block-content > * {
border-radius: 4px;
outline: 4px solid rgb(100, 160, 255);
}
Expand Down Expand Up @@ -256,6 +256,7 @@ NESTED BLOCKS
[data-file-block] .bn-add-file-button {
align-items: center;
background-color: rgb(242, 241, 238);
border: none;
border-radius: 4px;
color: rgb(125, 121, 122);
cursor: pointer;
Expand Down Expand Up @@ -295,6 +296,26 @@ NESTED BLOCKS
width: 100%;
}

[data-text-alignment="left"] .bn-file-default-preview, [data-text-alignment="left"] .bn-file-and-caption-wrapper {
align-items: flex-start;
justify-content: flex-start;
}

[data-text-alignment="center"] .bn-file-default-preview, [data-text-alignment="center"] .bn-file-and-caption-wrapper {
align-items: center;
justify-content: center;
}

[data-text-alignment="right"] .bn-file-default-preview, [data-text-alignment="right"] .bn-file-and-caption-wrapper {
align-items: flex-end;
justify-content: flex-end;
}

[data-text-alignment="justify"] .bn-file-default-preview, [data-text-alignment="justify"] .bn-file-and-caption-wrapper {
align-items: flex-start;
justify-content: flex-start;
}

[data-file-block] .bn-file-default-preview:hover,
.ProseMirror-selectednode .bn-file-default-preview {
background-color: rgb(225, 225, 225);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,30 @@ export const NonEditableBlockPlugin = () => {
}
// Checks if key press is Enter
if (event.key === "Enter") {
// Kind of a hacky way to ensure that pressing Enter when a file
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, this seems a bit hacky. I'm not a proponent of merging this as it makes the codebase worse for a minor win. We should invest more time in finding a cleaner solution (if we want to prioritize this(

// block is showing the add file button will open the file panel.
// Here, we just make the Enter handler skip this case and the file
// block's implementation will have to handle it itself. It would be
// cleaner if both handlers were in the same place, however:
// - This plugin takes precedence over handlers in the file block's
// implementation, so we can't override the behaviour there.
// - This plugin has no access to the BN schema, so it can't convert
// the node to a block for the file panel plugin, and therefore
// can't open the file plugin here.
let blockContentDOM = view.domAtPos(view.state.selection.from)
.node as HTMLElement;
while (!blockContentDOM.className.includes("bn-block-content")) {
blockContentDOM = blockContentDOM.firstChild as HTMLElement;
}

const isFileBlock =
blockContentDOM.getAttribute("data-file-block") !== null;
const hasURL = blockContentDOM.getAttribute("data-url") !== null;

if (isFileBlock && !hasURL) {
return false;
}

const tr = view.state.tr;
view.dispatch(
tr
Expand Down
30 changes: 28 additions & 2 deletions packages/react/src/blocks/FileBlockContent/fileBlockHelpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,43 @@ export const AddFileButton = (
props.editor._tiptapEditor.view,
props.editor.filePanel,
]);
const windowKeyDownHandler = useCallback(
(event: KeyboardEvent) => {
if (
event.key === "Enter" &&
props.editor.getTextCursorPosition().block.id === props.block.id
) {
props.editor._tiptapEditor.view.dispatch(
props.editor._tiptapEditor.state.tr.setMeta(
props.editor.filePanel!.plugin,
{
block: props.block,
}
)
);
}
},
[props.block, props.editor]
);

useEffect(() => {
window.addEventListener("keydown", windowKeyDownHandler);

return () => {
window.removeEventListener("keydown", windowKeyDownHandler);
};
}, [windowKeyDownHandler]);

return (
<div
<button
className={"bn-add-file-button"}
onMouseDown={addFileButtonMouseDownHandler}
onClick={addFileButtonClickHandler}>
<div className={"bn-add-file-button-icon"}>
{props.buttonIcon || <RiFile2Line size={24} />}
</div>
<div className={"bn-add-file-button-text"}>{props.buttonText}</div>
</div>
</button>
);
};

Expand Down
Loading