Skip to content
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
8 changes: 8 additions & 0 deletions examples/03-ui-components/21-block-language/.bnexample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"playground": true,
"docs": true,
"author": "",
"tags": ["Intermediate", "Blocks", "UI Components", "Block Side Menu"],
"dependencies": {},
"pro": false
}
9 changes: 9 additions & 0 deletions examples/03-ui-components/21-block-language/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Block languages

This example adds an optional language submenu to the drag handle menu, with English, French, and German. Each block can declare its language for assistive technology or inherit the surrounding language.

**Try it out:** Open a block's drag handle menu and choose a language.

## Relevant Docs

- [Changing the Drag Handle Menu](https://www.blocknotejs.org/docs/react/components/side-menu)
17 changes: 17 additions & 0 deletions examples/03-ui-components/21-block-language/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, interactive-widget=resizes-content"
/>
<title>Block languages</title>
<script>
<!-- AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY -->
</script>
</head>
<body>
<div id="root"></div>
<script type="module" src="./main.tsx"></script>
</body>
</html>
11 changes: 11 additions & 0 deletions examples/03-ui-components/21-block-language/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
import React from "react";
import { createRoot } from "react-dom/client";
import App from "./src/App.jsx";

const root = createRoot(document.getElementById("root")!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
);
30 changes: 30 additions & 0 deletions examples/03-ui-components/21-block-language/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@blocknote/example-ui-components-block-language",
"description": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"type": "module",
"private": true,
"version": "0.12.4",
"scripts": {
"start": "vite",
"dev": "vite",
"build:prod": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@blocknote/ariakit": "latest",
"@blocknote/core": "latest",
"@blocknote/mantine": "latest",
"@blocknote/react": "latest",
"@blocknote/shadcn": "latest",
"@mantine/core": "^9.0.2",
"@mantine/hooks": "^9.0.2",
"react": "^19.2.3",
"react-dom": "^19.2.3"
},
"devDependencies": {
"@types/react": "^19.2.3",
"@types/react-dom": "^19.2.3",
"@vitejs/plugin-react": "^6.0.1",
"vite": "^8.0.0"
}
}
70 changes: 70 additions & 0 deletions examples/03-ui-components/21-block-language/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import "@blocknote/core/fonts/inter.css";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import {
BlockColorsItem,
BlockLanguageItem,
DragHandleMenu,
RemoveBlockItem,
SideMenu,
SideMenuController,
type SideMenuProps,
useCreateBlockNote,
} from "@blocknote/react";

function LanguageDragHandleMenu() {
return (
<DragHandleMenu>
<RemoveBlockItem>Delete</RemoveBlockItem>
<BlockColorsItem>Colors</BlockColorsItem>
<BlockLanguageItem
languages={[
{ label: "English", language: "en" },
{ label: "Français", language: "fr" },
{ label: "Deutsch", language: "de" },
]}
>
Language
</BlockLanguageItem>
</DragHandleMenu>
);
}

function LanguageSideMenu(props: SideMenuProps) {
return <SideMenu {...props} dragHandleMenu={LanguageDragHandleMenu} />;
}

export default function App() {
const editor = useCreateBlockNote({
initialContent: [
{
type: "paragraph",
content: "Open a block's drag handle menu to choose its language.",
},
{
type: "paragraph",
props: { lang: "en" },
content:
"The block looks the same, but screen readers will read content inside with the correct pronounciation for that language.",
},
{
type: "paragraph",
props: { lang: "fr" },
content: "Bonjour tout le monde !",
},
{
type: "paragraph",
props: { lang: "de" },
content: "Guten Tag zusammen!",
},
],
});

return (
<div lang="en">
<BlockNoteView editor={editor} sideMenu={false}>
<SideMenuController sideMenu={LanguageSideMenu} />
</BlockNoteView>
</div>
);
}
32 changes: 32 additions & 0 deletions examples/03-ui-components/21-block-language/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"__comment": "AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"composite": true,
"paths": {
"@shared/*": ["../../../shared/*"]
}
},
"include": ["."],
"__ADD_FOR_LOCAL_DEV_references": [
{
"path": "../../../packages/core/"
},
{
"path": "../../../packages/react/"
}
]
}
1 change: 1 addition & 0 deletions examples/03-ui-components/21-block-language/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
35 changes: 35 additions & 0 deletions examples/03-ui-components/21-block-language/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// AUTO-GENERATED FILE, DO NOT EDIT DIRECTLY
import react from "@vitejs/plugin-react";
import * as fs from "fs";
import * as path from "path";
import { defineConfig } from "vite";
// https://vitejs.dev/config/
export default defineConfig(((conf: { command: string }) => ({
plugins: [react()],
optimizeDeps: {},
build: {
sourcemap: true,
},
resolve: {
alias:
conf.command === "build" ||
!fs.existsSync(path.resolve(__dirname, "../../../packages/core/src"))
? {}
: ({
// The repo-wide alias for the shared test-utils directory (private,
// so it only resolves inside the monorepo). Harmless for examples
// that don't use it.
"@shared": path.resolve(__dirname, "../../../shared/"),
// Comment out the lines below to load a built version of blocknote
// or, keep as is to load live from sources with live reload working
"@blocknote/core": path.resolve(
__dirname,
"../../../packages/core/src/",
),
"@blocknote/react": path.resolve(
__dirname,
"../../../packages/react/src/",
),
} as any),
},
})) as Parameters<typeof defineConfig>[0]);
2 changes: 2 additions & 0 deletions packages/core/src/blocks/ListItem/CheckListItem/block.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ it("renders checkbox as enabled when editor is editable", () => {
id: "1",
type: "checkListItem",
props: {
lang: undefined,
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
Expand All @@ -46,6 +47,7 @@ it("renders checkbox as disabled when editor is not editable", () => {
id: "1",
type: "checkListItem",
props: {
lang: undefined,
backgroundColor: "default",
textAlignment: "left",
textColor: "default",
Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/blocks/Quote/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const createQuoteBlockConfig = createBlockConfig(
({
type: "quote" as const,
propSchema: {
lang: defaultProps.lang,
backgroundColor: defaultProps.backgroundColor,
textColor: defaultProps.textColor,
},
Expand All @@ -28,9 +29,7 @@ export const createQuoteBlockSpec = createBlockSpec(
},
parse(element) {
if (element.tagName === "BLOCKQUOTE") {
const { backgroundColor, textColor } = parseDefaultProps(element);

return { backgroundColor, textColor };
return parseDefaultProps(element);
}

return undefined;
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/blocks/defaultProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type { Props, PropSchema } from "../schema/index.js";
// The dependency from schema on this file doesn't make sense

export const defaultProps = {
lang: {
default: undefined,
type: "string" as const,
},
backgroundColor: {
default: "default" as const,
},
Expand All @@ -24,6 +28,12 @@ export type DefaultProps = Props<typeof defaultProps>;
export const parseDefaultProps = (element: HTMLElement) => {
const props: Partial<DefaultProps> = {};

if (element.hasAttribute("data-lang")) {
props.lang = element.getAttribute("data-lang")!;
} else if (element.hasAttribute("lang")) {
props.lang = element.lang;
}

// If the `data-` attribute is found, set the prop to the value, as this most
// likely means the parsed element was exported by BlockNote originally.
// Otherwise, just use whatever is found in the inline styles, if anything.
Expand Down Expand Up @@ -55,6 +65,10 @@ export const addDefaultPropsExternalHTML = (
props: Partial<DefaultProps>,
element: HTMLElement,
) => {
if (props.lang !== undefined) {
element.lang = props.lang;
}

if (
props.backgroundColor &&
props.backgroundColor !== defaultProps.backgroundColor.default
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/editor/BlockNoteEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ it("sets an initial block id when using Y.js", async () => {
{
"attrs": {
"backgroundColor": "default",
"lang": undefined,
"textAlignment": "left",
"textColor": "default",
},
Expand Down Expand Up @@ -207,7 +208,7 @@ it("sets an initial block id when using Y.js", async () => {
expect(transactionCount).toBe(2);
// Only after a real modification is made, will the fragment be updated
expect(fragment.toJSON()).toMatch(
/^<blockgroup><blockcontainer id="[^"]+"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello<\/paragraph><\/blockcontainer><\/blockgroup>$/,
/^<blockgroup><blockcontainer id="[^"]+"><paragraph backgroundColor="default" lang="undefined" textAlignment="left" textColor="default">Hello<\/paragraph><\/blockcontainer><\/blockgroup>$/,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
} from "../../../extensions/index.js";
import {
BackgroundColorExtension,
LanguageExtension,
HardBreak,
KeyboardShortcutsExtension,
LinkExtension,
Expand Down Expand Up @@ -83,6 +84,7 @@ export function getDefaultTiptapExtensions(
});
}) as any[]),

LanguageExtension,
TextColorExtension,

BackgroundColorExtension,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Extension } from "@tiptap/core";
import { Plugin } from "prosemirror-state";
import { Decoration, DecorationSet } from "prosemirror-view";

// The `lang` prop renders out to a `data-lang` HTML attributes like all other props. To be useful
// for assistive technologies though, a `lang` attribute has to be rendered. This extension applies
// the `lang` attribute wherever `data-lang` appears.
export const LanguageExtension = Extension.create({
name: "blockLanguage",
addProseMirrorPlugins() {
return [
new Plugin({
props: {
decorations: ({ doc }) => {
const decorations: Decoration[] = [];
doc.descendants((node, pos) => {
if (typeof node.attrs.lang === "string") {
decorations.push(
Decoration.node(pos, pos + node.nodeSize, {
lang: node.attrs.lang,
}),
);
}
});
return DecorationSet.create(doc, decorations);
},
},
}),
];
},
});
1 change: 1 addition & 0 deletions packages/core/src/extensions/tiptap-extensions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./TextAlignment/TextAlignmentExtension.js";
export * from "./TextColor/TextColorExtension.js";
export * from "./UniqueID/UniqueID.js";
export * from "./Link/link.js";
export * from "./Language/LanguageExtension.js";
3 changes: 3 additions & 0 deletions packages/core/src/i18n/locales/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ export const ar: Dictionary = {
suggestion_menu: {
no_items_title: "لم يتم العثور على عناصر",
},
language_picker: {
default: "تلقائي",
},
color_picker: {
text_title: "نص",
background_title: "خلفية",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/i18n/locales/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ export const de: Dictionary = {
suggestion_menu: {
no_items_title: "Keine Elemente gefunden",
},
language_picker: {
default: "Automatisch",
},
color_picker: {
text_title: "Text",
background_title: "Hintergrund",
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ export const en = {
suggestion_menu: {
no_items_title: "No items found",
},
language_picker: {
default: "Auto",
},
color_picker: {
text_title: "Text",
background_title: "Background",
Expand Down
Loading
Loading