Skip to content

Commit

Permalink
feat(toolbars): fixed types, i18n, memo
Browse files Browse the repository at this point in the history
  • Loading branch information
makhnatkin committed Dec 19, 2024
1 parent e29ff33 commit f0524e0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
30 changes: 25 additions & 5 deletions src/bundle/MarkdownEditorView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
toolbarsPreset,
toaster,
stickyToolbar,
wysiwygToolbarConfig: initialWysiwygToolbarConfig,
markupToolbarConfig: initialMarkupToolbarConfig,
wysiwygHiddenActionsConfig: initialWysiwygHiddenActionsConfig,
markupHiddenActionsConfig: initialMarkupHiddenActionsConfig,
enableSubmitInPreview = true,
hidePreviewAfterSubmit = false,
} = props;
Expand All @@ -91,11 +95,27 @@ export const MarkdownEditorView = React.forwardRef<HTMLDivElement, MarkdownEdito
markupToolbarConfig,
wysiwygHiddenActionsConfig,
markupHiddenActionsConfig,
} = getToolbarsConfigs({
toolbarsPreset,
props,
preset: editor.preset,
});
} = useMemo(
() =>
getToolbarsConfigs({
toolbarsPreset,
props: {
wysiwygToolbarConfig: initialWysiwygToolbarConfig,
markupToolbarConfig: initialMarkupToolbarConfig,
wysiwygHiddenActionsConfig: initialWysiwygHiddenActionsConfig,
markupHiddenActionsConfig: initialMarkupHiddenActionsConfig,
},
preset: editor.preset,
}),
[
toolbarsPreset,
initialWysiwygToolbarConfig,
initialMarkupToolbarConfig,
initialWysiwygHiddenActionsConfig,
initialMarkupHiddenActionsConfig,
editor.preset,
],
);

const rerender = useUpdate();
React.useLayoutEffect(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/bundle/toolbar/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {ToolbarName} from '../../modules/toolbars/constants';
import {commonmark, defaultPreset, full, yfm, zero} from '../../modules/toolbars/presets';
import type {
EditorPreset,
ToolbarItem,
ToolbarItemMarkup,
ToolbarItemWysiwyg,
Expand All @@ -10,8 +9,9 @@ import type {
import type {MToolbarData, MToolbarItemData, WToolbarData, WToolbarItemData} from '../../toolbar';
import {ToolbarDataType, ToolbarIconData} from '../../toolbar';
import type {MarkdownEditorViewProps} from '../MarkdownEditorView';
import {MarkdownEditorPreset} from '../types';

const defaultPresets: Record<EditorPreset, ToolbarsPreset> = {
const defaultPresets: Record<MarkdownEditorPreset, ToolbarsPreset> = {
zero,
commonmark,
default: defaultPreset,
Expand Down Expand Up @@ -60,7 +60,7 @@ const transformItem = (

export const createConfig = <T extends WToolbarData | MToolbarData>(
editorType: 'wysiwyg' | 'markup',
toolbarPreset: ToolbarsPreset | EditorPreset,
toolbarPreset: ToolbarsPreset | MarkdownEditorPreset,
toolbarName: string,
): T => {
const preset =
Expand Down Expand Up @@ -100,7 +100,7 @@ interface GetToolbarsConfigsArgs {
| 'wysiwygHiddenActionsConfig'
| 'markupHiddenActionsConfig'
>;
preset: EditorPreset;
preset: MarkdownEditorPreset;
}
export const getToolbarsConfigs = ({toolbarsPreset, props, preset}: GetToolbarsConfigsArgs) => {
const wysiwygToolbarConfig = toolbarsPreset
Expand Down
1 change: 1 addition & 0 deletions src/i18n/menubar/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"mermaid": "Mermaid",
"mono": "Monospace",
"more_action": "More action",
"move_list": "Move list item",
"note": "Note",
"olist": "Ordered list",
"quote": "Quote",
Expand Down
1 change: 1 addition & 0 deletions src/i18n/menubar/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"mermaid": "Mermaid",
"mono": "Моноширинный",
"more_action": "Другие действия",
"move_list": "Переместить элемент списка",
"note": "Примечание",
"olist": "Нумерованный список",
"quote": "Цитата",
Expand Down
2 changes: 1 addition & 1 deletion src/modules/toolbars/items.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ export const moveListItemView: ToolbarItemView<ToolbarDataType.ListButton> = {
type: ToolbarDataType.ListButton,
icon: icons.lift,
withArrow: true,
title: 'Move list item', // TODO: @makhnatkin add i18n
title: i18n.bind(null, 'move_list'),
};

// ---- Code list ----
Expand Down
5 changes: 3 additions & 2 deletions src/modules/toolbars/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export interface ToolbarList {
items: ToolbarItemId[];
}

/**
* The default value for the `type` property is `ToolbarDataType.SingleButton`.
*/
export type ToolbarItemView<T extends ToolbarDataType = ToolbarDataType.SingleButton> = {
className?: string;
hint?: string | (() => string);
Expand Down Expand Up @@ -81,5 +84,3 @@ export interface ToolbarsPreset {
items: ToolbarsItems;
orders: ToolbarsOrders;
}

export type EditorPreset = 'zero' | 'commonmark' | 'default' | 'yfm' | 'full';

0 comments on commit f0524e0

Please sign in to comment.