Skip to content

Commit 4a863c7

Browse files
committed
feat: expose toolbar items
1 parent 782b0e3 commit 4a863c7

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

packages/bytemd/src/toolbar.svelte

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script lang="ts">
22
import { createEventDispatcher } from 'svelte';
33
import ToolbarButton from './toolbar-button.svelte';
4-
import { builtinMap } from './toolbar';
4+
import { getItemMap } from './toolbar';
55
import type { EditorProps, EditorContext, BytemdPlugin } from './types';
66
import { Info } from '@icon-park/svg';
77
@@ -13,14 +13,6 @@
1313
export let plugins: EditorProps['plugins'];
1414
export let toolbar: EditorProps['toolbar'];
1515
16-
export function getItemMap(plugins: EditorProps['plugins']) {
17-
const map = { ...builtinMap };
18-
plugins?.forEach((p) => {
19-
Object.assign(map, p.toolbar);
20-
});
21-
return map;
22-
}
23-
2416
function normalize(itemMap: NonNullable<BytemdPlugin['toolbar']>) {
2517
if (toolbar == null) {
2618
return Object.keys(itemMap);

packages/bytemd/src/toolbar.ts

Lines changed: 41 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,39 @@
1-
import type { BytemdToolbarItem } from './types';
1+
import type { BytemdToolbarItem, EditorProps } from './types';
22
import * as iconpark from '@icon-park/svg';
33

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> = {
537
h1: {
638
tooltip: 'H1',
739
icon: iconpark.H1({}),
@@ -105,34 +137,11 @@ export const builtinMap: Record<string, BytemdToolbarItem> = {
105137
},
106138
};
107139

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;
138147
}

0 commit comments

Comments
 (0)