-
Notifications
You must be signed in to change notification settings - Fork 9
cms-admin: Add in-toolbar translate button to TipTap rich text block #6235
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
ab0c634
cms-admin: Add in-toolbar translate button to TipTap rich text block
VPS-Andreas ff0d5cf
cms-admin: Fix ambiguous lookups in the translation dialog story
VPS-Andreas 32fbcc6
cms-admin: Fix ambiguous heading lookup in the translation dialog story
VPS-Andreas 1d5fed1
cms-admin: Translate TipTap fields as a single HTML document
VPS-Andreas 18d7858
cms-admin: Handle link/child block translation and errors in TipTap t…
VPS-Andreas c3a2321
cms-admin: Externalize link mark data unconditionally before HTML tra…
VPS-Andreas b46f81f
cms-admin: Enforce headingLevels in the translation review dialog
VPS-Andreas 3fa8c5a
cms-admin: Add a story proving placeholders survive HTML translation
VPS-Andreas c5a987a
cms-admin: Reject a translate result missing an externalized placehol…
VPS-Andreas 3b127c6
cms-admin: Extract TipTap content translation logic into its own file
VPS-Andreas e13764b
cms-admin: Split TipTap translation stories into their own file
VPS-Andreas 0fe990b
cms-admin: Add unit tests for TipTap content translation, drop redund…
VPS-Andreas c7b443c
cms-admin: Drop TipTap translation stories now covered by unit tests
VPS-Andreas 93af6ab
cms-admin: Move TipTapContentTranslationDialog into its own file
VPS-Andreas 2c3521c
cms-admin: Fold link/child-block translation into translateTipTapCont…
VPS-Andreas 80670be
cms-admin: Drop the Async suffix from translateTipTapContent
VPS-Andreas a737aed
Merge remote-tracking branch 'origin/main' into tiptap-translate-button
VPS-Andreas eb339e0
Revert local-only regenerated schema.gql and brevo-api block-meta.json
VPS-Andreas 06bea36
Merge remote-tracking branch 'origin/main' into tiptap-translate-button
VPS-Andreas 4c4face
cms-admin: Rename disableContentTranslation to contentTranslation
VPS-Andreas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@dextinity/cms-admin": minor | ||
| --- | ||
|
|
||
| Add an in-toolbar translate button to the TipTap rich text block | ||
|
|
||
| The Draft.js-based rich text block already has a toolbar button to translate a single field, with an optional dialog to review the translation before applying it. The TipTap rich text block had no equivalent, leaving document-wide translation as the only option for TipTap fields. | ||
|
|
||
| The button now appears in the TipTap toolbar whenever a `ContentTranslationServiceProvider` is enabled, and can be hidden per block with the new `contentTranslation` option: | ||
|
|
||
| ```tsx | ||
| createTipTapRichTextBlock({ contentTranslation: false }); | ||
| ``` |
45 changes: 45 additions & 0 deletions
45
packages/admin/cms-admin/src/blocks/tipTap/TipTapContentTranslationDialog.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { BaseTranslationDialog } from "@dextinity/admin"; | ||
| import type { JSONContent } from "@tiptap/react"; | ||
|
|
||
| import { TipTapEditor, type TipTapEditorProps } from "./createTipTapRichTextBlock"; | ||
|
|
||
| interface TipTapContentTranslationDialogProps { | ||
| open: boolean; | ||
| onClose: () => void; | ||
| originalContent: JSONContent; | ||
| translatedContent: JSONContent; | ||
| onApplyTranslation: (content: JSONContent) => void; | ||
| editorProps: Pick< | ||
| TipTapEditorProps, | ||
| "resolvedOptions" | "textBlockStyles" | "inlineStyles" | "placeholders" | "linkBlock" | "childBlocks" | "maxTextBlocks" | "listLevelMax" | ||
| >; | ||
| } | ||
|
|
||
| export const TipTapContentTranslationDialog = ({ | ||
| open, | ||
| onClose, | ||
| originalContent, | ||
| translatedContent, | ||
| onApplyTranslation, | ||
| editorProps, | ||
| }: TipTapContentTranslationDialogProps) => ( | ||
| <BaseTranslationDialog | ||
| open={open} | ||
| onClose={onClose} | ||
| originalText={originalContent} | ||
| translatedText={translatedContent} | ||
| onApplyTranslation={onApplyTranslation} | ||
| renderOriginalText={(content) => <TipTapEditor state={{ tipTapContent: content }} updateState={() => {}} {...editorProps} readOnly />} | ||
| renderTranslatedText={(content, onChange) => ( | ||
| <TipTapEditor | ||
| state={{ tipTapContent: content }} | ||
| updateState={(next) => { | ||
| const nextState = typeof next === "function" ? next({ tipTapContent: content }) : next; | ||
| onChange(nextState.tipTapContent); | ||
| }} | ||
| {...editorProps} | ||
| resolvedOptions={{ ...editorProps.resolvedOptions, contentTranslation: false }} | ||
| /> | ||
| )} | ||
| /> | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
205 changes: 205 additions & 0 deletions
205
.../admin/cms-admin/src/blocks/tipTap/__stories__/TipTapRichTextBlockTranslation.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,205 @@ | ||
| import { ContentTranslationServiceProvider } from "@dextinity/admin"; | ||
| import { Box } from "@mui/material"; | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { type PropsWithChildren, type ReactNode, useState } from "react"; | ||
| import { expect, waitFor, within } from "storybook/test"; | ||
|
|
||
| import { createTipTapRichTextBlock, type TipTapRichTextBlockState } from "../createTipTapRichTextBlock"; | ||
|
|
||
| function StatePreview({ state }: { state: TipTapRichTextBlockState }) { | ||
| return ( | ||
| <Box component="pre" sx={{ mt: 2, p: 2, backgroundColor: "#f5f5f5", fontSize: 12, overflow: "auto", borderRadius: 1 }}> | ||
| {JSON.stringify(state, null, 2)} | ||
| </Box> | ||
| ); | ||
| } | ||
|
|
||
| function StoryWrapper({ children, state }: { children: ReactNode; state: TipTapRichTextBlockState }) { | ||
| return ( | ||
| <> | ||
| {children} | ||
| <StatePreview state={state} /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| const config: Meta = { | ||
| title: "blocks/TipTapRichTextBlock/Translation", | ||
| }; | ||
|
|
||
| export default config; | ||
|
|
||
| const uppercaseTranslate = async (text: string): Promise<string> => text.toUpperCase(); | ||
|
|
||
| // Disabling every other feature makes the translate button the toolbar's sole (and thus unambiguous) button. | ||
| const TranslationBlock = createTipTapRichTextBlock({ | ||
| undoRedoButtons: false, | ||
| bold: false, | ||
| italic: false, | ||
| strike: false, | ||
| sub: false, | ||
| sup: false, | ||
| heading: false, | ||
| orderedList: false, | ||
| unorderedList: false, | ||
| nonBreakingSpace: false, | ||
| softHyphen: false, | ||
| }); | ||
|
|
||
| const translationInitialState: TipTapRichTextBlockState = { | ||
| tipTapContent: { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "Hello world" }] }] }, | ||
| }; | ||
|
|
||
| function TranslationProvider({ children, showApplyTranslationDialog }: PropsWithChildren<{ showApplyTranslationDialog?: boolean }>) { | ||
| return ( | ||
| <ContentTranslationServiceProvider enabled translate={uppercaseTranslate} showApplyTranslationDialog={showApplyTranslationDialog}> | ||
| {children} | ||
| </ContentTranslationServiceProvider> | ||
| ); | ||
| } | ||
|
|
||
| function TranslationStory({ showApplyTranslationDialog }: { showApplyTranslationDialog?: boolean }) { | ||
| const [state, setState] = useState<TipTapRichTextBlockState>(translationInitialState); | ||
|
|
||
| return ( | ||
| <TranslationProvider showApplyTranslationDialog={showApplyTranslationDialog}> | ||
| <StoryWrapper state={state}> | ||
| <TranslationBlock.AdminComponent state={state} updateState={setState} /> | ||
| </StoryWrapper> | ||
| </TranslationProvider> | ||
| ); | ||
| } | ||
|
|
||
| export const Translation: StoryObj<typeof TranslationStory> = { | ||
| render: () => <TranslationStory />, | ||
| play: async ({ canvas, userEvent, step }) => { | ||
| await step("Editor is ready with a translate button", async () => { | ||
| await waitFor( | ||
| () => { | ||
| expect(canvas.getByRole("textbox")).toBeInTheDocument(); | ||
| expect(canvas.getByRole("button")).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 5000 }, | ||
| ); | ||
| }); | ||
|
|
||
| await step("Clicking translate replaces the content immediately (no review dialog)", async () => { | ||
| await userEvent.click(canvas.getByRole("button")); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| expect(canvas.getByRole("textbox")).toHaveTextContent("HELLO WORLD"); | ||
| }, | ||
| { timeout: 3000 }, | ||
| ); | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| export const TranslationWithApplyDialog: StoryObj<typeof TranslationStory> = { | ||
| render: () => <TranslationStory showApplyTranslationDialog />, | ||
| play: async ({ canvas, userEvent, step }) => { | ||
| await step("Editor is ready with a translate button", async () => { | ||
| await waitFor( | ||
| () => { | ||
| expect(canvas.getByRole("button")).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 5000 }, | ||
| ); | ||
| }); | ||
|
|
||
| await step("Clicking translate opens a review dialog with the original and translated content", async () => { | ||
| await userEvent.click(canvas.getByRole("button")); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| expect(within(document.body).getByRole("heading", { name: "Translation", level: 2 })).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 3000 }, | ||
| ); | ||
|
|
||
| const dialog = within(document.body).getByRole("dialog"); | ||
| expect(within(dialog).getByText("Hello world")).toBeInTheDocument(); | ||
| expect(within(dialog).getByText("HELLO WORLD")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| await step("Applying the translation updates the editor and closes the dialog", async () => { | ||
| await userEvent.click(within(document.body).getByRole("button", { name: "Apply" })); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| expect(canvas.getByRole("textbox")).toHaveTextContent("HELLO WORLD"); | ||
| expect(within(document.body).queryByRole("dialog")).not.toBeInTheDocument(); | ||
| }, | ||
| { timeout: 3000 }, | ||
| ); | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
| const TranslationHeadingLevelsBlock = createTipTapRichTextBlock({ | ||
| undoRedoButtons: false, | ||
| bold: false, | ||
| italic: false, | ||
| strike: false, | ||
| sub: false, | ||
| sup: false, | ||
| heading: { levels: [2, 3] }, | ||
| orderedList: false, | ||
| unorderedList: false, | ||
| nonBreakingSpace: false, | ||
| softHyphen: false, | ||
| }); | ||
|
|
||
| function TranslationHeadingLevelsStory() { | ||
| const [state, setState] = useState<TipTapRichTextBlockState>(translationInitialState); | ||
|
|
||
| return ( | ||
| <TranslationProvider showApplyTranslationDialog> | ||
| <StoryWrapper state={state}> | ||
| <TranslationHeadingLevelsBlock.AdminComponent state={state} updateState={setState} /> | ||
| </StoryWrapper> | ||
| </TranslationProvider> | ||
| ); | ||
| } | ||
|
|
||
| export const TranslationRespectsHeadingLevels: StoryObj<typeof TranslationHeadingLevelsStory> = { | ||
| render: () => <TranslationHeadingLevelsStory />, | ||
| play: async ({ canvas, userEvent, step }) => { | ||
| await step("Editor is ready with a translate button", async () => { | ||
| await waitFor( | ||
| () => { | ||
| expect(canvas.getByRole("button")).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 5000 }, | ||
| ); | ||
| }); | ||
|
|
||
| await step("Open the translation review dialog", async () => { | ||
| await userEvent.click(canvas.getByRole("button")); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| expect(within(document.body).getByRole("dialog")).toBeInTheDocument(); | ||
| }, | ||
| { timeout: 3000 }, | ||
| ); | ||
| }); | ||
|
|
||
| await step("The translated-side heading dropdown only offers Heading 2-3, matching the block's headingLevels", async () => { | ||
| const dialog = within(document.body).getByRole("dialog"); | ||
| await userEvent.click(within(dialog).getByRole("combobox")); | ||
|
|
||
| await waitFor( | ||
| () => { | ||
| const body = within(document.body); | ||
| expect(body.getByText("Heading 2")).toBeInTheDocument(); | ||
| expect(body.getByText("Heading 3")).toBeInTheDocument(); | ||
| expect(body.queryByText("Heading 1")).not.toBeInTheDocument(); | ||
| expect(body.queryByText("Heading 4")).not.toBeInTheDocument(); | ||
| }, | ||
| { timeout: 3000 }, | ||
| ); | ||
| }); | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.