Skip to content

Commit 76fd657

Browse files
committed
add editor config menu to markdown toolbar
1 parent 1a1387c commit 76fd657

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from "react";
2+
3+
import { ContextMenu, ContextMenuProps, MenuItem } from "../../../components";
4+
5+
export interface EditorAppearanceConfigMenuProps {
6+
/** Object containing a `true`/`false` value for each property */
7+
config: { [s: string]: boolean };
8+
/** Object containing `true` for each property that cannot be changed by user */
9+
configLocked?: { [s: string]: boolean | undefined };
10+
/** Handler that returns a translation for each config property key */
11+
configPropertyTranslate?: (key: string) => string | false;
12+
/** Handler to update config after user changes */
13+
setConfig: React.Dispatch<React.SetStateAction<{ [s: string]: boolean }>>;
14+
/** Additional properties used for the included `ContextMenu` */
15+
contextMenuProps?: ContextMenuProps;
16+
}
17+
18+
/**
19+
* Returns a simple context menu that provides switches to control the editor appearance.
20+
*/
21+
export function EditorAppearanceConfigMenu({
22+
config,
23+
configLocked = {},
24+
configPropertyTranslate = (s) => s,
25+
setConfig,
26+
contextMenuProps,
27+
}: EditorAppearanceConfigMenuProps) {
28+
return (
29+
<ContextMenu
30+
togglerElement={"item-settings"}
31+
{...contextMenuProps}
32+
disabled={
33+
contextMenuProps?.disabled ??
34+
Object.values(config).length ===
35+
Object.values(configLocked).filter((value) => {
36+
return typeof value !== "undefined";
37+
}).length
38+
}
39+
>
40+
{Object.entries(config).map(([key, value]) => {
41+
return (
42+
<MenuItem
43+
key={key}
44+
roleStructure={"listoption"}
45+
selected={value}
46+
text={configPropertyTranslate(key) || key}
47+
disabled={typeof configLocked[key] !== "undefined"}
48+
onClick={() => {
49+
setConfig({
50+
...config,
51+
[key]: !value,
52+
});
53+
}}
54+
/>
55+
);
56+
})}
57+
</ContextMenu>
58+
);
59+
}

src/extensions/codemirror/toolbars/markdown.toolbar.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Spacing } from "../../../components/Separation/Spacing";
99
import { Toolbar, ToolbarSection } from "../../../components/Toolbar";
1010

1111
import MarkdownCommand from "./commands/markdown.command";
12+
import { EditorAppearanceConfigMenu } from "./EditorAppearanceConfigMenu";
1213

1314
interface MarkdownToolbarProps {
1415
view?: EditorView;
@@ -17,6 +18,7 @@ interface MarkdownToolbarProps {
1718
translate: (key: string) => string | false;
1819
disabled?: boolean;
1920
readonly?: boolean;
21+
configMenu?: React.ReactElement<typeof EditorAppearanceConfigMenu>;
2022
}
2123

2224
export const MarkdownToolbar: React.FC<MarkdownToolbarProps> = ({
@@ -25,7 +27,8 @@ export const MarkdownToolbar: React.FC<MarkdownToolbarProps> = ({
2527
showPreview,
2628
disabled,
2729
readonly,
28-
translate
30+
translate,
31+
configMenu,
2932
}) => {
3033
const commandRef = React.useRef<MarkdownCommand | null>(null);
3134

@@ -35,10 +38,10 @@ export const MarkdownToolbar: React.FC<MarkdownToolbarProps> = ({
3538
}
3639
}, [view]);
3740

38-
const getTranslation = (fallback: string) : string => {
41+
const getTranslation = (fallback: string): string => {
3942
const key = fallback.toLowerCase().replace(" ", "-");
4043
return translate(key) || fallback;
41-
}
44+
};
4245

4346
const { basic, lists, attachments } = MarkdownCommand.commands;
4447
return (
@@ -112,6 +115,17 @@ export const MarkdownToolbar: React.FC<MarkdownToolbarProps> = ({
112115
disabled={disabled}
113116
/>
114117
</ToolbarSection>
118+
{configMenu && (
119+
<ToolbarSection>
120+
<Spacing vertical size="small" hasDivider />
121+
{React.cloneElement(configMenu, {
122+
...{
123+
...configMenu.props,
124+
contextMenuProps: { disabled: showPreview || disabled ? true : undefined },
125+
},
126+
})}
127+
</ToolbarSection>
128+
)}
115129
</Toolbar>
116130
);
117131
};

0 commit comments

Comments
 (0)