Skip to content

Commit 7f8cee9

Browse files
committed
feat(md): MarkdownMD 编辑器支持自定义配置工具栏 DevCloudFE#1862
1 parent 18e63c4 commit 7f8cee9

File tree

7 files changed

+92
-8
lines changed

7 files changed

+92
-8
lines changed

packages/devui-vue/devui/editor-md/src/components/toolbar.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ import './toolbar.scss';
66
export default defineComponent({
77
name: 'DMdToolbar',
88
setup() {
9-
const { toolbars, toolbarConfig } = useToolbar();
10-
9+
const { toolbars, toolbarConfig, customToolbars } = useToolbar();
10+
const tempToolbars = { ...toolbars, ...customToolbars?.value };
1111
return () => (
1212
<div class="md-toolbar-container">
1313
{toolbarConfig.value.map((item, index) =>
1414
Array.isArray(item) ? (
1515
<>
1616
{item.map((key, idx) => (
17-
<ToolbarItem config={toolbars[key]} key={`${index}-${idx}`} />
17+
<ToolbarItem config={tempToolbars[key]} key={`${index}-${idx}`} />
1818
))}
1919
<span class="md-toolbar-span"></span>
2020
</>
2121
) : (
22-
<ToolbarItem config={toolbars[item]} key={index} />
22+
<ToolbarItem config={tempToolbars[item]} key={index} />
2323
)
2424
)}
2525
</div>

packages/devui-vue/devui/editor-md/src/composables/use-editor-md-toolbar.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { inject } from 'vue';
22
import { EditorMdInjectionKey, IEditorMdInjection } from '../editor-md-types';
33

44
export function useToolbar() {
5-
const { toolbars, toolbarConfig } = inject(EditorMdInjectionKey) as IEditorMdInjection;
5+
const { toolbars, toolbarConfig, customToolbars } = inject(EditorMdInjectionKey) as IEditorMdInjection;
66

7-
return { toolbars, toolbarConfig };
7+
return { toolbars, toolbarConfig, customToolbars };
88
}

packages/devui-vue/devui/editor-md/src/composables/use-editor-md.ts

+2
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,10 @@ export function useEditorMd(props: EditorMdProps, ctx: SetupContext) {
414414
renderRef,
415415
containerRef,
416416
toolbars,
417+
toolbarConfig,
417418
previewHtmlList,
418419
isHintShow,
420+
customToolbars,
419421
getEditorIns,
420422
onPaste,
421423
previewContentChange,

packages/devui-vue/devui/editor-md/src/editor-md-types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export interface IEditorMdInjection {
136136
showFullscreen: Ref<boolean>;
137137
toolbars: Record<string, IToolbarItemConfig>;
138138
toolbarConfig: Ref<ToolbarConfigProp>;
139+
customToolbars: Ref<Record<string, IToolbarItemConfig> | undefined> | undefined;
139140
getEditorIns: () => any;
140141
t: (name: string) => string;
141142
}

packages/devui-vue/devui/editor-md/src/editor-md.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default defineComponent({
1717
const {
1818
mode,
1919
toolbarConfig,
20+
customToolbars,
2021
editorContainerHeight,
2122
hidePreviewView,
2223
placeholder,
@@ -66,6 +67,7 @@ export default defineComponent({
6667
showFullscreen,
6768
toolbars,
6869
toolbarConfig,
70+
customToolbars,
6971
getEditorIns,
7072
t: locale,
7173
});

packages/devui-vue/devui/editor-md/src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,5 @@ export function locale(key: string): string {
8484
loading: '正在加载中...',
8585
pasting: '您粘贴内容较多, 正在努力加载中,请耐心等待...',
8686
};
87-
return localeMap[key];
87+
return localeMap[key] || key;
8888
}

packages/devui-vue/docs/components/editor-md/index.md

+80-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,64 @@ export default defineComponent({
8888
});
8989
</script>
9090
```
91+
:::
92+
93+
### 自定义工具栏
94+
95+
:::demo 自定义编辑器的工具栏
96+
97+
```vue
98+
<template>
99+
<d-editor-md v-model="content" :toolbar-config="toolbarConfig" :custom-toolbars="customToolbars"></d-editor-md>
100+
</template>
101+
102+
<script>
103+
import { defineComponent, ref } from 'vue';
104+
105+
export default defineComponent({
106+
setup() {
107+
const content = ref('');
108+
const toolbarConfig = ['add',
109+
['undo', 'redo'],
110+
['h1', 'h2', 'bold', 'italic', 'strike', 'underline', 'color', 'font'],
111+
['ul', 'ol', 'checklist', 'code', 'link', 'image', 'table'],
112+
'fullscreen',
113+
];
114+
const customToolbars = {
115+
add: {
116+
id: 'add',
117+
name: '新增',
118+
exitName: '新增',
119+
type: 'button',
120+
icon: `<span>+</span>`,
121+
shortKey: 'ALT+K',
122+
handler: () => {
123+
console.log('自定义工具点击事件');
124+
},
125+
},
126+
undo: {
127+
id: 'undo',
128+
name: '撤销',
129+
exitName: '撤销',
130+
type: 'button',
131+
icon: `<svg width="16px" height="14px" viewBox="0 0 16 14">
132+
<g stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
133+
<g fill="#293040">
134+
<path d="M11,5 C13.7614237,5 16,7.23857625 16,10 C16,12.7614237 13.7614237,15 11,15 L7,15 L7,14 L11,14 C13.209139,14 15,12.209139 15,10 C15,7.790861 13.209139,6 11,6 L5,6 L5,10 L0,5.5 L5,1 L5,5 L11,5 Z" id="路径"></path>
135+
</g>
136+
</g>
137+
</svg>`,
138+
shortKey: 'Ctrl+M',
139+
handler: () => {
140+
console.log('覆盖原有工具栏功能事件');
141+
},
142+
}
143+
};
144+
return { content, toolbarConfig, customToolbars };
145+
},
146+
});
147+
</script>
148+
```
91149

92150
:::
93151

@@ -629,7 +687,9 @@ Bob-->>John: Jolly good!
629687
| placeholder | `string` | '' | 编辑器无内容是的提示信息 |
630688
| fullscreen-z-index | `number` | 10 | 编辑器全屏状态的 z-index |
631689
| image-upload-to-server | `boolean` | false | 是否打开图片自定义上传开关(打开后将将监听图片的复制,toolbar 图片功能上传,传出事件回调) |
632-
|editor-container-height|`number`|--|可选,编辑器内容区高度||
690+
| editor-container-height| `number`|--|可选,编辑器内容区高度 ||
691+
| toolbar-config | `Array(string)` |`[['undo', 'redo'],['h1', 'h2', 'bold', 'italic', 'strike', 'underline', 'color', 'font'],['ul', 'ol', 'checklist', 'code', 'link', 'image', 'table'],'fullscreen']`|展示在toolbar工具栏处的按钮,用[]包起来的表示是同一组,不同组的会有线隔开。也可以自定义,自定义时需要配置参数custom-toolbars ||
692+
| custom-toolbars | {[IToolbarItemConfig](#itoolbaritemconfig)} |--|配置toolbar-config中对应按钮的具体设置 [自定义工具栏](#自定义工具栏) | |
633693
634694
### EditorMd 事件
635695
@@ -696,3 +756,22 @@ export interface HintConfig {
696756
[key: string]: HintConfigItem; // key为触发提示前缀配置
697757
}
698758
```
759+
760+
761+
### IToolbarItemConfig
762+
```ts
763+
export interface IToolbarItemConfig {
764+
id: string;
765+
name?: string;
766+
exitName?: string;
767+
type?: 'button' | 'dropDown';
768+
icon?: string;
769+
exitIcon?: string;
770+
template?: any;
771+
component?: any;
772+
shortKey?: string;
773+
params?: { [key: string]: any };
774+
handler?(editor?: any, params?: any): void;
775+
}
776+
const toolbars = Record<string, IToolbarItemConfig>
777+
```

0 commit comments

Comments
 (0)