Skip to content

Commit 84ae9eb

Browse files
committed
feat: add editor config
1 parent 31042fc commit 84ae9eb

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

packages/bytemd/src/editor.svelte

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script lang="ts">
2+
import type { Editor } from 'codemirror';
3+
import type {} from 'codemirror/addon/display/placeholder';
24
import type { BytemdPlugin, EditorProps, ViewerProps } from './types';
35
import { onMount, createEventDispatcher, onDestroy, tick } from 'svelte';
46
import debounce from 'lodash.debounce';
@@ -11,6 +13,7 @@
1113
export let mode: EditorProps['mode'] = 'split';
1214
export let previewDebounce: EditorProps['previewDebounce'] = 300;
1315
export let toolbar: EditorProps['toolbar'];
16+
export let editorConfig: EditorProps['editorConfig'];
1417
1518
let el: HTMLElement;
1619
let previewEl: HTMLElement;
@@ -20,7 +23,7 @@
2023
sanitize,
2124
};
2225
let textarea: HTMLTextAreaElement;
23-
let editor: CodeMirror.Editor;
26+
let editor: Editor;
2427
let activeTab = 0;
2528
2629
$: context = { editor, $el: el };
@@ -76,7 +79,7 @@
7679
editor = codemirror.fromTextArea(textarea, {
7780
mode: 'yaml-frontmatter',
7881
lineWrapping: true,
79-
placeholder: 'Start writing...',
82+
...editorConfig,
8083
});
8184
8285
// https://github.com/codemirror/CodeMirror/issues/2428#issuecomment-39315423

packages/bytemd/src/toolbar.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { BytemdToolbarItem, EditorProps } from './types';
2+
import type { Editor } from 'codemirror';
23
import { icons } from './icons';
34

4-
function handleText(editor: CodeMirror.Editor, before: string, after: string) {
5+
function handleText(editor: Editor, before: string, after: string) {
56
if (editor.somethingSelected()) {
67
editor.replaceSelection(before + editor.getSelection() + after);
78
} else {
@@ -12,10 +13,7 @@ function handleText(editor: CodeMirror.Editor, before: string, after: string) {
1213
editor.focus();
1314
}
1415

15-
function handlePrepend(
16-
editor: CodeMirror.Editor,
17-
replace: (lines: string[]) => string[]
18-
) {
16+
function handlePrepend(editor: Editor, replace: (lines: string[]) => string[]) {
1917
const [selection] = editor.listSelections();
2018
const fromLine = selection.from().line;
2119
const toLine = selection.to().line;

packages/bytemd/src/types.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
11
import type { Processor } from 'unified';
22
import type { Schema } from 'hast-util-sanitize';
33
import type { VFile } from 'vfile';
4+
import type { Editor, EditorConfiguration } from 'codemirror';
45

56
export interface EditorContext {
67
/**
78
* CodeMirror editor instance
89
*/
9-
editor: CodeMirror.Editor;
10+
editor: Editor;
1011
/**
1112
* Root element, `$('.bytemd')`
1213
*/
1314
$el: HTMLElement;
1415
}
1516

17+
export interface ViewerContext {
18+
/**
19+
* Root element of the Viewer, `$('.markdown-body')`
20+
*/
21+
$el: HTMLElement;
22+
/**
23+
* Markdown process result
24+
*/
25+
result: VFile;
26+
}
27+
1628
export interface BytemdToolbarItem {
1729
/**
1830
* Tooltip of toolbar item
@@ -52,16 +64,7 @@ export interface BytemdPlugin {
5264
/**
5365
* Side effect for viewer, triggers when HTML or plugin list changes
5466
*/
55-
viewerEffect?(context: {
56-
/**
57-
* Root element of the Viewer, `$('.markdown-body')`
58-
*/
59-
$el: HTMLElement;
60-
/**
61-
* Markdown process result
62-
*/
63-
result: VFile;
64-
}): void | (() => void);
67+
viewerEffect?(context: ViewerContext): void | (() => void);
6568
}
6669

6770
export interface EditorProps extends ViewerProps {
@@ -84,6 +87,10 @@ export interface EditorProps extends ViewerProps {
8487
toolbar?:
8588
| string[]
8689
| ((itemMap: Record<string, BytemdToolbarItem>) => string[]);
90+
/**
91+
* CodeMirror editor config
92+
*/
93+
editorConfig: Omit<EditorConfiguration, 'value' | 'mode'>;
8794
}
8895

8996
export interface ViewerProps {

0 commit comments

Comments
 (0)