-
-
Notifications
You must be signed in to change notification settings - Fork 734
feat(react): Added select component for text align feature #2728
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
GuiLeme
wants to merge
7
commits into
TypeCellOS:main
Choose a base branch
from
GuiLeme:text-align-select
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
36480f4
[text-align-select] created select component for text align feature
GuiLeme 3e9202b
[text-align-select] add i18n for all supported languages
GuiLeme d39118a
[text-align-select] added unit test for the TextAlignSelect
GuiLeme b5a99ab
[text-align-select] added documentation for TextAlignSelect
GuiLeme b1052b0
[text-align-select] fix code-rabbit suggestions
GuiLeme b0a9bf5
[text-align-select] apply code-rabbit suggestions
GuiLeme 5443eb0
[text-align-select] returned focus to table when text align is changed
GuiLeme 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
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
179 changes: 179 additions & 0 deletions
179
packages/react/src/components/FormattingToolbar/DefaultSelects/TextAlignSelect.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,179 @@ | ||
| import { | ||
| blockHasType, | ||
| BlockSchema, | ||
| defaultProps, | ||
| DefaultProps, | ||
| editorHasBlockWithType, | ||
| InlineContentSchema, | ||
| mapTableCell, | ||
| StyleSchema, | ||
| TableContent, | ||
| } from "@blocknote/core"; | ||
| import { TableHandlesExtension } from "@blocknote/core/extensions"; | ||
| import { useCallback, useMemo } from "react"; | ||
| import { IconType } from "react-icons"; | ||
| import { | ||
| RiAlignCenter, | ||
| RiAlignJustify, | ||
| RiAlignLeft, | ||
| RiAlignRight, | ||
| } from "react-icons/ri"; | ||
|
|
||
| import { | ||
| ComponentProps, | ||
| useComponentsContext, | ||
| } from "../../../editor/ComponentsContext.js"; | ||
| import { useBlockNoteEditor } from "../../../hooks/useBlockNoteEditor.js"; | ||
| import { useEditorState } from "../../../hooks/useEditorState.js"; | ||
| import { useDictionary } from "../../../i18n/dictionary.js"; | ||
|
|
||
| type TextAlignment = DefaultProps["textAlignment"]; | ||
|
|
||
| const icons: Record<TextAlignment, IconType> = { | ||
| left: RiAlignLeft, | ||
| center: RiAlignCenter, | ||
| right: RiAlignRight, | ||
| justify: RiAlignJustify, | ||
| }; | ||
|
|
||
| const alignments: TextAlignment[] = ["left", "center", "right", "justify"]; | ||
|
|
||
| export const TextAlignSelect = () => { | ||
| const Components = useComponentsContext()!; | ||
| const dict = useDictionary(); | ||
|
|
||
| const editor = useBlockNoteEditor< | ||
| BlockSchema, | ||
| InlineContentSchema, | ||
| StyleSchema | ||
| >(); | ||
|
|
||
| const state = useEditorState({ | ||
| editor, | ||
| selector: ({ editor }) => { | ||
| if (!editor.isEditable) { | ||
| return undefined; | ||
| } | ||
|
|
||
| const selectedBlocks = editor.getSelection()?.blocks || [ | ||
| editor.getTextCursorPosition().block, | ||
| ]; | ||
|
|
||
| const firstBlock = selectedBlocks[0]; | ||
|
|
||
| if ( | ||
| blockHasType(firstBlock, editor, firstBlock.type, { | ||
| textAlignment: defaultProps.textAlignment, | ||
| }) | ||
| ) { | ||
| return { | ||
| textAlignment: firstBlock.props.textAlignment, | ||
| blocks: selectedBlocks, | ||
| }; | ||
| } | ||
|
|
||
| if ( | ||
| selectedBlocks.length === 1 && | ||
| blockHasType(firstBlock, editor, "table") | ||
| ) { | ||
| const cellSelection = editor | ||
| .getExtension(TableHandlesExtension) | ||
| ?.getCellSelection(); | ||
|
|
||
| if (!cellSelection) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| textAlignment: mapTableCell( | ||
| (firstBlock.content as TableContent<any, any>).rows[0].cells[0], | ||
| ).props.textAlignment, | ||
| blocks: [firstBlock], | ||
| }; | ||
| } | ||
|
GuiLeme marked this conversation as resolved.
|
||
|
|
||
| return undefined; | ||
| }, | ||
| }); | ||
|
|
||
| const setTextAlignment = useCallback( | ||
| (textAlignment: TextAlignment) => { | ||
| if (state === undefined) { | ||
| return; | ||
| } | ||
|
|
||
| editor.focus(); | ||
|
|
||
| for (const block of state.blocks) { | ||
| if ( | ||
| blockHasType(block, editor, block.type, { | ||
| textAlignment: defaultProps.textAlignment, | ||
| }) && | ||
| editorHasBlockWithType(editor, block.type, { | ||
| textAlignment: defaultProps.textAlignment, | ||
| }) | ||
| ) { | ||
| editor.updateBlock(block, { | ||
| props: { textAlignment }, | ||
| }); | ||
| } else if (block.type === "table") { | ||
| const cellSelection = editor | ||
| .getExtension(TableHandlesExtension) | ||
| ?.getCellSelection(); | ||
| if (!cellSelection) { | ||
| continue; | ||
| } | ||
|
|
||
| const newTable = (block.content as TableContent<any, any>).rows.map( | ||
| (row) => ({ | ||
| ...row, | ||
| cells: row.cells.map((cell) => mapTableCell(cell)), | ||
| }), | ||
| ); | ||
|
|
||
| cellSelection.cells.forEach(({ row, col }) => { | ||
| newTable[row].cells[col].props.textAlignment = textAlignment; | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| editor.updateBlock(block, { | ||
| type: "table", | ||
| content: { | ||
| ...(block.content as TableContent<any, any>), | ||
| type: "tableContent", | ||
| rows: newTable, | ||
| } as any, | ||
| }); | ||
|
|
||
| editor.setTextCursorPosition(block); | ||
| } | ||
| } | ||
| }, | ||
| [editor, state], | ||
| ); | ||
|
|
||
| const selectItems: ComponentProps["FormattingToolbar"]["Select"]["items"] = | ||
| useMemo( | ||
| () => | ||
| alignments.map((alignment) => { | ||
| const Icon = icons[alignment]; | ||
| return { | ||
| text: dict.formatting_toolbar[`align_${alignment}`].label, | ||
| icon: <Icon size={16} />, | ||
| isSelected: state?.textAlignment === alignment, | ||
| onClick: () => setTextAlignment(alignment), | ||
| }; | ||
| }), | ||
| [dict, setTextAlignment, state?.textAlignment], | ||
| ); | ||
|
|
||
| if (state === undefined) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <Components.FormattingToolbar.Select | ||
| className={"bn-select"} | ||
| items={selectItems} | ||
| /> | ||
| ); | ||
| }; | ||
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
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.