🌐(frontend) add localization to editor
Currently, when you change language the editor does not change. So we add this functionality
This commit is contained in:
committed by
Nathan Panchout
parent
60120852f5
commit
7fc59ed497
@@ -12,6 +12,7 @@ and this project adheres to
|
|||||||
## Added
|
## Added
|
||||||
|
|
||||||
- 📝Contributing.md #352
|
- 📝Contributing.md #352
|
||||||
|
- 🌐(frontend) add localization to editor #268
|
||||||
|
|
||||||
## Fixed
|
## Fixed
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,41 @@ test.beforeEach(async ({ page }) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test.describe('Doc Editor', () => {
|
test.describe('Doc Editor', () => {
|
||||||
|
test('it check translations of the slash menu when changing language', async ({
|
||||||
|
page,
|
||||||
|
browserName,
|
||||||
|
}) => {
|
||||||
|
await createDoc(page, 'doc-toolbar', browserName, 1);
|
||||||
|
|
||||||
|
const header = page.locator('header').first();
|
||||||
|
const editor = page.locator('.ProseMirror');
|
||||||
|
// Trigger slash menu to show english menu
|
||||||
|
await editor.click();
|
||||||
|
await editor.fill('/');
|
||||||
|
await expect(page.getByText('Headings', { exact: true })).toBeVisible();
|
||||||
|
await header.click();
|
||||||
|
await expect(page.getByText('Headings', { exact: true })).toBeHidden();
|
||||||
|
|
||||||
|
// Reset menu
|
||||||
|
await editor.click();
|
||||||
|
await editor.fill('');
|
||||||
|
|
||||||
|
// Change language to French
|
||||||
|
await header.click();
|
||||||
|
await header.getByRole('combobox').getByText('English').click();
|
||||||
|
await header.getByRole('option', { name: 'Français' }).click();
|
||||||
|
await expect(
|
||||||
|
header.getByRole('combobox').getByText('Français'),
|
||||||
|
).toBeVisible();
|
||||||
|
|
||||||
|
// Trigger slash menu to show french menu
|
||||||
|
await editor.click();
|
||||||
|
await editor.fill('/');
|
||||||
|
await expect(page.getByText('Titres', { exact: true })).toBeVisible();
|
||||||
|
await header.click();
|
||||||
|
await expect(page.getByText('Titres', { exact: true })).toBeHidden();
|
||||||
|
});
|
||||||
|
|
||||||
test('it checks default toolbar buttons are displayed', async ({
|
test('it checks default toolbar buttons are displayed', async ({
|
||||||
page,
|
page,
|
||||||
browserName,
|
browserName,
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { BlockNoteEditor as BlockNoteEditorCore } from '@blocknote/core';
|
import { locales } from '@blocknote/core';
|
||||||
import '@blocknote/core/fonts/inter.css';
|
import '@blocknote/core/fonts/inter.css';
|
||||||
import { BlockNoteView } from '@blocknote/mantine';
|
import { BlockNoteView } from '@blocknote/mantine';
|
||||||
import '@blocknote/mantine/style.css';
|
import '@blocknote/mantine/style.css';
|
||||||
|
import { useCreateBlockNote } from '@blocknote/react';
|
||||||
import { HocuspocusProvider } from '@hocuspocus/provider';
|
import { HocuspocusProvider } from '@hocuspocus/provider';
|
||||||
import React, { useCallback, useEffect, useMemo } from 'react';
|
import React, { useCallback, useEffect } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { Box, TextErrors } from '@/components';
|
import { Box, TextErrors } from '@/components';
|
||||||
import { mediaUrl } from '@/core';
|
import { mediaUrl } from '@/core';
|
||||||
@@ -113,6 +115,8 @@ export const BlockNoteContent = ({
|
|||||||
error: errorAttachment,
|
error: errorAttachment,
|
||||||
} = useCreateDocAttachment();
|
} = useCreateDocAttachment();
|
||||||
const { setHeadings, resetHeadings } = useHeadingStore();
|
const { setHeadings, resetHeadings } = useHeadingStore();
|
||||||
|
const { i18n } = useTranslation();
|
||||||
|
const lang = i18n.language;
|
||||||
|
|
||||||
const uploadFile = useCallback(
|
const uploadFile = useCallback(
|
||||||
async (file: File) => {
|
async (file: File) => {
|
||||||
@@ -129,12 +133,8 @@ export const BlockNoteContent = ({
|
|||||||
[createDocAttachment, doc.id],
|
[createDocAttachment, doc.id],
|
||||||
);
|
);
|
||||||
|
|
||||||
const editor = useMemo(() => {
|
const editor = useCreateBlockNote(
|
||||||
if (storedEditor) {
|
{
|
||||||
return storedEditor;
|
|
||||||
}
|
|
||||||
|
|
||||||
return BlockNoteEditorCore.create({
|
|
||||||
collaboration: {
|
collaboration: {
|
||||||
provider,
|
provider,
|
||||||
fragment: provider.document.getXmlFragment('document-store'),
|
fragment: provider.document.getXmlFragment('document-store'),
|
||||||
@@ -143,9 +143,11 @@ export const BlockNoteContent = ({
|
|||||||
color: randomColor(),
|
color: randomColor(),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
dictionary: locales[lang as keyof typeof locales],
|
||||||
uploadFile,
|
uploadFile,
|
||||||
});
|
},
|
||||||
}, [provider, storedEditor, uploadFile, userData?.email]);
|
[provider, uploadFile, userData?.email, lang],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setStore(storeId, { editor });
|
setStore(storeId, { editor });
|
||||||
@@ -176,7 +178,7 @@ export const BlockNoteContent = ({
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<BlockNoteView
|
<BlockNoteView
|
||||||
editor={editor}
|
editor={storedEditor ?? editor}
|
||||||
formattingToolbar={false}
|
formattingToolbar={false}
|
||||||
editable={!readOnly}
|
editable={!readOnly}
|
||||||
theme="light"
|
theme="light"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
} from '@blocknote/react';
|
} from '@blocknote/react';
|
||||||
import { forEach, isArray } from 'lodash';
|
import { forEach, isArray } from 'lodash';
|
||||||
import React, { useMemo } from 'react';
|
import React, { useMemo } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
type Block = {
|
type Block = {
|
||||||
type: string;
|
type: string;
|
||||||
@@ -42,6 +43,7 @@ export function MarkdownButton() {
|
|||||||
const editor = useBlockNoteEditor();
|
const editor = useBlockNoteEditor();
|
||||||
const Components = useComponentsContext();
|
const Components = useComponentsContext();
|
||||||
const selectedBlocks = useSelectedBlocks(editor);
|
const selectedBlocks = useSelectedBlocks(editor);
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const handleConvertMarkdown = () => {
|
const handleConvertMarkdown = () => {
|
||||||
const blocks = editor.getSelection()?.blocks;
|
const blocks = editor.getSelection()?.blocks;
|
||||||
@@ -75,7 +77,7 @@ export function MarkdownButton() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Components.FormattingToolbar.Button
|
<Components.FormattingToolbar.Button
|
||||||
mainTooltip="Convert Markdown"
|
mainTooltip={t('Convert Markdown')}
|
||||||
onClick={handleConvertMarkdown}
|
onClick={handleConvertMarkdown}
|
||||||
>
|
>
|
||||||
M
|
M
|
||||||
|
|||||||
Reference in New Issue
Block a user