Skip to content

feat(core,docs): add AccessibleImageBlock (alt=) and wire into homepa… #1942

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

Open
wants to merge 1 commit into
base: main
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
11 changes: 10 additions & 1 deletion docs/app/(home)/hero/DemoEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
BlockNoteSchema,
combineByGroup,
defaultBlockSpecs,
filterSuggestionItems,
uploadToTmpFilesDotOrg_DEV_ONLY,
AccessibleImageBlock,
} from "@blocknote/core";
import * as locales from "@blocknote/core/locales";
import "@blocknote/core/fonts/inter.css";
Expand Down Expand Up @@ -90,7 +92,14 @@ export default function DemoEditor() {
...locales.en,
multi_column: multiColumnLocales.en,
},
schema: withMultiColumn(BlockNoteSchema.create()),
schema: withMultiColumn(
BlockNoteSchema.create({
blockSpecs: {
...defaultBlockSpecs,
image: AccessibleImageBlock,
},
}),
),
dropCursor: multiColumnDropCursor,
collaboration: {
provider,
Expand Down
45 changes: 45 additions & 0 deletions packages/core/src/blocks/ImageBlockContent/AccessibleImageBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
import type {
BlockFromConfig,
BlockSchemaWithBlock,
} from "../../schema/blocks/types.js";
import type { InlineContentSchema } from "../../schema/inlineContent/types.js";
import type { StyleSchema } from "../../schema/styles/types.js";
import { createBlockSpec } from "../../schema/blocks/createSpec.js";
import {
imageBlockConfig,
imageParse,
imageRender,
imageToExternalHTML,
} from "./ImageBlockContent.js";

type ImageBlockConfig = typeof imageBlockConfig;

export const accessibleImageRender = (
block: BlockFromConfig<ImageBlockConfig, InlineContentSchema, StyleSchema>,
editor: BlockNoteEditor<
BlockSchemaWithBlock<ImageBlockConfig["type"], ImageBlockConfig>,
InlineContentSchema,
StyleSchema
>,
) => {
const imageRenderComputed = imageRender(block, editor);
const dom = imageRenderComputed.dom;
const imgSelector = dom.querySelector("img");

imgSelector?.setAttribute("alt", "");
imgSelector?.setAttribute("role", "presentation");
imgSelector?.setAttribute("aria-hidden", "true");
imgSelector?.setAttribute("tabindex", "-1");

return {
...imageRenderComputed,
dom,
};
};

export const AccessibleImageBlock = createBlockSpec(imageBlockConfig, {
render: accessibleImageRender,
parse: imageParse,
toExternalHTML: imageToExternalHTML,
});
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from "./blocks/FileBlockContent/helpers/toExternalHTML/createFigureWith
export * from "./blocks/FileBlockContent/helpers/toExternalHTML/createLinkWithCaption.js";
export * from "./blocks/FileBlockContent/uploadToTmpFilesDotOrg_DEV_ONLY.js";
export * from "./blocks/ImageBlockContent/ImageBlockContent.js";
export * from "./blocks/ImageBlockContent/AccessibleImageBlock.js";
export * from "./blocks/PageBreakBlockContent/getPageBreakSlashMenuItems.js";
export * from "./blocks/PageBreakBlockContent/PageBreakBlockContent.js";
export * from "./blocks/PageBreakBlockContent/schema.js";
Expand Down