-
-
Notifications
You must be signed in to change notification settings - Fork 377
/
main.ts
56 lines (45 loc) 路 1.51 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Copyright 2021, Milkdown by Mirone. */
import './style.css';
import { Ctx, defaultValueCtx, Editor, editorViewCtx, editorViewOptionsCtx, themeManagerCtx } from '@milkdown/core';
import { math } from '@milkdown/plugin-math';
import { menu } from '@milkdown/plugin-menu';
import { slash } from '@milkdown/plugin-slash';
import { commonmark } from '@milkdown/preset-commonmark';
import { nord } from '@milkdown/theme-nord';
import { tokyo } from '@milkdown/theme-tokyo';
const sleep = (ms: number) =>
new Promise((resolve) => {
setTimeout(resolve, ms);
});
let editable = true;
async function main() {
const editor = await Editor.make()
.config((ctx) => {
ctx.set(defaultValueCtx, '# Here is [mylink](https://milkdown.dev)');
ctx.set(editorViewOptionsCtx, {
editable: () => editable,
});
})
.use(tokyo)
.use(commonmark)
.use(math)
.use(menu)
.use(slash)
.create();
await sleep(2000);
const toggleEditable = (ctx: Ctx) => {
editable = !editable;
const view = ctx.get(editorViewCtx);
const { tr } = view.state;
const nextTr = Object.assign(Object.create(tr), tr).setTime(Date.now());
view.dispatch(nextTr);
};
editor.action(toggleEditable);
editor.action(async (ctx) => {
const themeManager = ctx.get(themeManagerCtx);
themeManager.switch(ctx, nord);
});
await sleep(2000);
editor.action(toggleEditable);
}
main();