|
1 | | -import type { BytemdToolbarItem } from './types'; |
| 1 | +import type { BytemdToolbarItem, EditorProps } from './types'; |
2 | 2 | import * as iconpark from '@icon-park/svg'; |
3 | 3 |
|
4 | | -export const builtinMap: Record<string, BytemdToolbarItem> = { |
| 4 | +function handleText(editor: CodeMirror.Editor, before: string, after: string) { |
| 5 | + if (editor.somethingSelected()) { |
| 6 | + editor.replaceSelection(before + editor.getSelection() + after); |
| 7 | + } else { |
| 8 | + const { anchor, head } = editor.findWordAt(editor.getCursor()); |
| 9 | + const word = editor.getRange(anchor, head); |
| 10 | + editor.replaceRange(before + word + after, anchor, head); |
| 11 | + } |
| 12 | + editor.focus(); |
| 13 | +} |
| 14 | + |
| 15 | +function handlePrepend( |
| 16 | + editor: CodeMirror.Editor, |
| 17 | + replace: (lines: string[]) => string[] |
| 18 | +) { |
| 19 | + const [selection] = editor.listSelections(); |
| 20 | + const fromLine = selection.from().line; |
| 21 | + const toLine = selection.to().line; |
| 22 | + const lines = editor |
| 23 | + // @ts-ignore |
| 24 | + .getRange({ line: fromLine, ch: 0 }, { line: toLine }) |
| 25 | + .split('\n'); |
| 26 | + editor.replaceRange( |
| 27 | + replace(lines).join('\n'), |
| 28 | + { line: fromLine, ch: 0 }, |
| 29 | + // @ts-ignore |
| 30 | + { line: toLine } |
| 31 | + ); |
| 32 | + |
| 33 | + editor.focus(); |
| 34 | +} |
| 35 | + |
| 36 | +const builtinMap: Record<string, BytemdToolbarItem> = { |
5 | 37 | h1: { |
6 | 38 | tooltip: 'H1', |
7 | 39 | icon: iconpark.H1({}), |
@@ -105,34 +137,11 @@ export const builtinMap: Record<string, BytemdToolbarItem> = { |
105 | 137 | }, |
106 | 138 | }; |
107 | 139 |
|
108 | | -function handleText(editor: CodeMirror.Editor, before: string, after: string) { |
109 | | - if (editor.somethingSelected()) { |
110 | | - editor.replaceSelection(before + editor.getSelection() + after); |
111 | | - } else { |
112 | | - const { anchor, head } = editor.findWordAt(editor.getCursor()); |
113 | | - const word = editor.getRange(anchor, head); |
114 | | - editor.replaceRange(before + word + after, anchor, head); |
115 | | - } |
116 | | - editor.focus(); |
117 | | -} |
118 | | - |
119 | | -function handlePrepend( |
120 | | - editor: CodeMirror.Editor, |
121 | | - replace: (lines: string[]) => string[] |
122 | | -) { |
123 | | - const [selection] = editor.listSelections(); |
124 | | - const fromLine = selection.from().line; |
125 | | - const toLine = selection.to().line; |
126 | | - const lines = editor |
127 | | - // @ts-ignore |
128 | | - .getRange({ line: fromLine, ch: 0 }, { line: toLine }) |
129 | | - .split('\n'); |
130 | | - editor.replaceRange( |
131 | | - replace(lines).join('\n'), |
132 | | - { line: fromLine, ch: 0 }, |
133 | | - // @ts-ignore |
134 | | - { line: toLine } |
135 | | - ); |
136 | | - |
137 | | - editor.focus(); |
| 140 | +// TODO: |
| 141 | +export function getItemMap(plugins: EditorProps['plugins']) { |
| 142 | + const map = { ...builtinMap }; |
| 143 | + plugins?.forEach((p) => { |
| 144 | + Object.assign(map, p.toolbar); |
| 145 | + }); |
| 146 | + return map; |
138 | 147 | } |
0 commit comments