@jvs-milkdown/utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/lib/composable/$command.d.ts +9 -0
- package/lib/composable/$command.d.ts.map +1 -0
- package/lib/composable/$ctx.d.ts +6 -0
- package/lib/composable/$ctx.d.ts.map +1 -0
- package/lib/composable/$input-rule.d.ts +8 -0
- package/lib/composable/$input-rule.d.ts.map +1 -0
- package/lib/composable/$mark.d.ts +11 -0
- package/lib/composable/$mark.d.ts.map +1 -0
- package/lib/composable/$node.d.ts +11 -0
- package/lib/composable/$node.d.ts.map +1 -0
- package/lib/composable/$paste-rule.d.ts +8 -0
- package/lib/composable/$paste-rule.d.ts.map +1 -0
- package/lib/composable/$prose.d.ts +9 -0
- package/lib/composable/$prose.d.ts.map +1 -0
- package/lib/composable/$shortcut.d.ts +10 -0
- package/lib/composable/$shortcut.d.ts.map +1 -0
- package/lib/composable/$view.d.ts +11 -0
- package/lib/composable/$view.d.ts.map +1 -0
- package/lib/composable/composed/$attr.d.ts +7 -0
- package/lib/composable/composed/$attr.d.ts.map +1 -0
- package/lib/composable/composed/$mark-schema.d.ts +18 -0
- package/lib/composable/composed/$mark-schema.d.ts.map +1 -0
- package/lib/composable/composed/$node-schema.d.ts +18 -0
- package/lib/composable/composed/$node-schema.d.ts.map +1 -0
- package/lib/composable/composed/$node-schema.spec.d.ts +2 -0
- package/lib/composable/composed/$node-schema.spec.d.ts.map +1 -0
- package/lib/composable/composed/$remark.d.ts +13 -0
- package/lib/composable/composed/$remark.d.ts.map +1 -0
- package/lib/composable/composed/$use-keymap.d.ts +25 -0
- package/lib/composable/composed/$use-keymap.d.ts.map +1 -0
- package/lib/composable/composed/index.d.ts +6 -0
- package/lib/composable/composed/index.d.ts.map +1 -0
- package/lib/composable/index.d.ts +12 -0
- package/lib/composable/index.d.ts.map +1 -0
- package/lib/composable/utils.d.ts +7 -0
- package/lib/composable/utils.d.ts.map +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +549 -0
- package/lib/index.js.map +1 -0
- package/lib/macro/call-command.d.ts +8 -0
- package/lib/macro/call-command.d.ts.map +1 -0
- package/lib/macro/force-update.d.ts +3 -0
- package/lib/macro/force-update.d.ts.map +1 -0
- package/lib/macro/get-html.d.ts +3 -0
- package/lib/macro/get-html.d.ts.map +1 -0
- package/lib/macro/get-markdown.d.ts +6 -0
- package/lib/macro/get-markdown.d.ts.map +1 -0
- package/lib/macro/index.d.ts +12 -0
- package/lib/macro/index.d.ts.map +1 -0
- package/lib/macro/insert-pos.d.ts +3 -0
- package/lib/macro/insert-pos.d.ts.map +1 -0
- package/lib/macro/insert.d.ts +3 -0
- package/lib/macro/insert.d.ts.map +1 -0
- package/lib/macro/markdown-to-slice.d.ts +3 -0
- package/lib/macro/markdown-to-slice.d.ts.map +1 -0
- package/lib/macro/outline.d.ts +7 -0
- package/lib/macro/outline.d.ts.map +1 -0
- package/lib/macro/replace-all.d.ts +3 -0
- package/lib/macro/replace-all.d.ts.map +1 -0
- package/lib/macro/replace-range.d.ts +6 -0
- package/lib/macro/replace-range.d.ts.map +1 -0
- package/lib/macro/set-attr.d.ts +4 -0
- package/lib/macro/set-attr.d.ts.map +1 -0
- package/lib/pipe.d.ts +7 -0
- package/lib/pipe.d.ts.map +1 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +35 -0
- package/src/composable/$command.ts +102 -0
- package/src/composable/$ctx.ts +31 -0
- package/src/composable/$input-rule.ts +55 -0
- package/src/composable/$mark.ts +87 -0
- package/src/composable/$node.ts +90 -0
- package/src/composable/$paste-rule.ts +59 -0
- package/src/composable/$prose.ts +70 -0
- package/src/composable/$shortcut.ts +66 -0
- package/src/composable/$view.ts +110 -0
- package/src/composable/composed/$attr.ts +29 -0
- package/src/composable/composed/$mark-schema.ts +65 -0
- package/src/composable/composed/$node-schema.spec.ts +139 -0
- package/src/composable/composed/$node-schema.ts +65 -0
- package/src/composable/composed/$remark.ts +53 -0
- package/src/composable/composed/$use-keymap.ts +91 -0
- package/src/composable/composed/index.ts +5 -0
- package/src/composable/index.ts +12 -0
- package/src/composable/utils.ts +63 -0
- package/src/index.ts +3 -0
- package/src/macro/call-command.ts +28 -0
- package/src/macro/force-update.ts +14 -0
- package/src/macro/get-html.ts +20 -0
- package/src/macro/get-markdown.ts +27 -0
- package/src/macro/index.ts +11 -0
- package/src/macro/insert-pos.ts +27 -0
- package/src/macro/insert.ts +38 -0
- package/src/macro/markdown-to-slice.ts +18 -0
- package/src/macro/outline.ts +21 -0
- package/src/macro/replace-all.ts +46 -0
- package/src/macro/replace-range.ts +18 -0
- package/src/macro/set-attr.ts +16 -0
- package/src/pipe.ts +72 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { Ctx } from '@jvs-milkdown/ctx'
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
editorStateOptionsCtx,
|
|
5
|
+
editorViewCtx,
|
|
6
|
+
parserCtx,
|
|
7
|
+
prosePluginsCtx,
|
|
8
|
+
schemaCtx,
|
|
9
|
+
} from '@jvs-milkdown/core'
|
|
10
|
+
import { Slice } from '@jvs-milkdown/prose/model'
|
|
11
|
+
import { EditorState } from '@jvs-milkdown/prose/state'
|
|
12
|
+
|
|
13
|
+
/// Replace all content of the editor with markdown string.
|
|
14
|
+
/// If flush is true, the editor state will be re-created.
|
|
15
|
+
export function replaceAll(markdown: string, flush = false) {
|
|
16
|
+
return (ctx: Ctx): void => {
|
|
17
|
+
const view = ctx.get(editorViewCtx)
|
|
18
|
+
const parser = ctx.get(parserCtx)
|
|
19
|
+
const doc = parser(markdown)
|
|
20
|
+
if (!doc) return
|
|
21
|
+
|
|
22
|
+
if (!flush) {
|
|
23
|
+
const { state } = view
|
|
24
|
+
return view.dispatch(
|
|
25
|
+
state.tr.replace(
|
|
26
|
+
0,
|
|
27
|
+
state.doc.content.size,
|
|
28
|
+
new Slice(doc.content, 0, 0)
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const schema = ctx.get(schemaCtx)
|
|
34
|
+
const overrideOptions = ctx.get(editorStateOptionsCtx)
|
|
35
|
+
const plugins = ctx.get(prosePluginsCtx)
|
|
36
|
+
const newOptions = overrideOptions({
|
|
37
|
+
schema,
|
|
38
|
+
doc,
|
|
39
|
+
plugins,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
const state = EditorState.create(newOptions)
|
|
43
|
+
|
|
44
|
+
view.updateState(state)
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Ctx } from '@jvs-milkdown/ctx'
|
|
2
|
+
|
|
3
|
+
import { editorViewCtx } from '@jvs-milkdown/core'
|
|
4
|
+
|
|
5
|
+
import { markdownToSlice } from './markdown-to-slice'
|
|
6
|
+
|
|
7
|
+
/// Replace the content of the given range with the markdown string.
|
|
8
|
+
export function replaceRange(
|
|
9
|
+
markdown: string,
|
|
10
|
+
range: { from: number; to: number }
|
|
11
|
+
) {
|
|
12
|
+
return (ctx: Ctx) => {
|
|
13
|
+
const view = ctx.get(editorViewCtx)
|
|
14
|
+
const slice = markdownToSlice(markdown)(ctx)
|
|
15
|
+
|
|
16
|
+
view.dispatch(view.state.tr.replace(range.from, range.to, slice))
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Ctx } from '@jvs-milkdown/ctx'
|
|
2
|
+
import type { Attrs } from '@jvs-milkdown/prose/model'
|
|
3
|
+
|
|
4
|
+
import { editorViewCtx } from '@jvs-milkdown/core'
|
|
5
|
+
|
|
6
|
+
/// Set the attributes of the node at the given position.
|
|
7
|
+
export function setAttr(pos: number, update: (prevAttrs: Attrs) => Attrs) {
|
|
8
|
+
return (ctx: Ctx) => {
|
|
9
|
+
const view = ctx.get(editorViewCtx)
|
|
10
|
+
const { tr } = view.state
|
|
11
|
+
const node = tr.doc.nodeAt(pos)
|
|
12
|
+
if (!node) return
|
|
13
|
+
const nextAttr = update(node.attrs)
|
|
14
|
+
return view.dispatch(tr.setNodeMarkup(pos, undefined, nextAttr))
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/pipe.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/// @internal
|
|
2
|
+
export type Many<T> = T | ReadonlyArray<T>
|
|
3
|
+
|
|
4
|
+
interface Pipe {
|
|
5
|
+
pipe: (<A extends any[], R1, R2, R3, R4, R5, R6, R7>(
|
|
6
|
+
f1: (...args: A) => R1,
|
|
7
|
+
f2: (a: R1) => R2,
|
|
8
|
+
f3: (a: R2) => R3,
|
|
9
|
+
f4: (a: R3) => R4,
|
|
10
|
+
f5: (a: R4) => R5,
|
|
11
|
+
f6: (a: R5) => R6,
|
|
12
|
+
f7: (a: R6) => R7
|
|
13
|
+
) => (...args: A) => R7) &
|
|
14
|
+
(<A extends any[], R1, R2, R3, R4, R5, R6, R7>(
|
|
15
|
+
f1: (...args: A) => R1,
|
|
16
|
+
f2: (a: R1) => R2,
|
|
17
|
+
f3: (a: R2) => R3,
|
|
18
|
+
f4: (a: R3) => R4,
|
|
19
|
+
f5: (a: R4) => R5,
|
|
20
|
+
f6: (a: R5) => R6,
|
|
21
|
+
f7: (a: R6) => R7,
|
|
22
|
+
...func: Array<Many<(a: any) => any>>
|
|
23
|
+
) => (...args: A) => any) &
|
|
24
|
+
(<A extends any[], R1, R2, R3, R4, R5, R6>(
|
|
25
|
+
f1: (...args: A) => R1,
|
|
26
|
+
f2: (a: R1) => R2,
|
|
27
|
+
f3: (a: R2) => R3,
|
|
28
|
+
f4: (a: R3) => R4,
|
|
29
|
+
f5: (a: R4) => R5,
|
|
30
|
+
f6: (a: R5) => R6
|
|
31
|
+
) => (...args: A) => R6) &
|
|
32
|
+
(<A extends any[], R1, R2, R3, R4, R5>(
|
|
33
|
+
f1: (...args: A) => R1,
|
|
34
|
+
f2: (a: R1) => R2,
|
|
35
|
+
f3: (a: R2) => R3,
|
|
36
|
+
f4: (a: R3) => R4,
|
|
37
|
+
f5: (a: R4) => R5
|
|
38
|
+
) => (...args: A) => R5) &
|
|
39
|
+
(<A extends any[], R1, R2, R3, R4>(
|
|
40
|
+
f1: (...args: A) => R1,
|
|
41
|
+
f2: (a: R1) => R2,
|
|
42
|
+
f3: (a: R2) => R3,
|
|
43
|
+
f4: (a: R3) => R4
|
|
44
|
+
) => (...args: A) => R4) &
|
|
45
|
+
(<A extends any[], R1, R2, R3>(
|
|
46
|
+
f1: (...args: A) => R1,
|
|
47
|
+
f2: (a: R1) => R2,
|
|
48
|
+
f3: (a: R2) => R3
|
|
49
|
+
) => (...args: A) => R3) &
|
|
50
|
+
(<A extends any[], R1, R2>(
|
|
51
|
+
f1: (...args: A) => R1,
|
|
52
|
+
f2: (a: R1) => R2
|
|
53
|
+
) => (...args: A) => R2) &
|
|
54
|
+
((...func: Array<Many<(...args: any[]) => any>>) => (...args: any[]) => any)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/// @internal
|
|
58
|
+
export const pipe: Pipe['pipe'] = (...funcs: any[]) => {
|
|
59
|
+
const length = funcs.length
|
|
60
|
+
let index = length
|
|
61
|
+
while (index--) {
|
|
62
|
+
if (typeof funcs[index] !== 'function')
|
|
63
|
+
throw new TypeError('Expected a function')
|
|
64
|
+
}
|
|
65
|
+
return (...args: any[]) => {
|
|
66
|
+
let index = 0
|
|
67
|
+
let result = length ? funcs[index](...args) : args[0]
|
|
68
|
+
while (++index < length) result = funcs[index](result)
|
|
69
|
+
|
|
70
|
+
return result
|
|
71
|
+
}
|
|
72
|
+
}
|