|
1 | | -import type {NodeSpec} from 'prosemirror-model'; |
2 | 1 | import type {Command} from 'prosemirror-state'; |
3 | 2 | import {setBlockType} from 'prosemirror-commands'; |
4 | 3 | import {hasParentNodeOfType} from 'prosemirror-utils'; |
5 | 4 | import type {Action, ExtensionAuto} from '../../../core'; |
6 | | -import {nodeTypeFactory} from '../../../utils/schema'; |
| 5 | +import {BaseSchemaSpecs, BaseSchemaSpecsOptions, pType} from './BaseSchemaSpecs'; |
7 | 6 |
|
8 | | -export enum BaseNode { |
9 | | - Doc = 'doc', |
10 | | - Text = 'text', |
11 | | - Paragraph = 'paragraph', |
12 | | -} |
| 7 | +export {BaseNode, pType} from './BaseSchemaSpecs'; |
13 | 8 |
|
14 | | -export const pType = nodeTypeFactory(BaseNode.Paragraph); |
15 | 9 | const pAction = 'toParagraph'; |
16 | 10 |
|
17 | 11 | export const toParagraph: Command = (state, dispatch) => |
18 | 12 | setBlockType(pType(state.schema))(state, dispatch); |
19 | 13 |
|
20 | | -export type BaseSchemaOptions = { |
| 14 | +export type BaseSchemaOptions = BaseSchemaSpecsOptions & { |
21 | 15 | paragraphKey?: string | null; |
22 | | - paragraphPlaceholder?: NonNullable<NodeSpec['placeholder']>['content']; |
23 | 16 | }; |
24 | 17 |
|
25 | 18 | export const BaseSchema: ExtensionAuto<BaseSchemaOptions> = (builder, opts) => { |
26 | | - const {paragraphKey, paragraphPlaceholder} = opts; |
27 | | - |
28 | | - builder |
29 | | - .addNode(BaseNode.Doc, () => ({ |
30 | | - spec: { |
31 | | - content: 'block+', |
32 | | - }, |
33 | | - fromYfm: {tokenSpec: {name: BaseNode.Doc, type: 'block', ignore: true}}, |
34 | | - toYfm: () => { |
35 | | - throw new Error('Unexpected toYfm() call on doc node'); |
36 | | - }, |
37 | | - })) |
38 | | - .addNode(BaseNode.Text, () => ({ |
39 | | - spec: { |
40 | | - group: 'inline', |
41 | | - }, |
42 | | - fromYfm: {tokenSpec: {name: BaseNode.Text, type: 'node', ignore: true}}, |
43 | | - toYfm: (state, node, parent) => { |
44 | | - const {escapeText} = parent.type.spec; |
45 | | - state.text(node.text, escapeText ?? !state.isAutolink); |
46 | | - }, |
47 | | - })) |
48 | | - .addNode(BaseNode.Paragraph, () => ({ |
49 | | - spec: { |
50 | | - content: 'inline*', |
51 | | - group: 'block', |
52 | | - parseDOM: [{tag: 'p'}], |
53 | | - toDOM() { |
54 | | - return ['p', 0]; |
55 | | - }, |
56 | | - placeholder: paragraphPlaceholder |
57 | | - ? { |
58 | | - content: paragraphPlaceholder, |
59 | | - alwaysVisible: false, |
60 | | - } |
61 | | - : undefined, |
62 | | - }, |
63 | | - fromYfm: {tokenSpec: {name: BaseNode.Paragraph, type: 'block'}}, |
64 | | - toYfm: (state, node) => { |
65 | | - state.renderInline(node); |
66 | | - state.closeBlock(node); |
67 | | - }, |
68 | | - })); |
| 19 | + builder.use(BaseSchemaSpecs, opts); |
69 | 20 |
|
| 21 | + const {paragraphKey} = opts; |
70 | 22 | if (paragraphKey) { |
71 | 23 | builder.addKeymap(({schema}) => ({[paragraphKey]: setBlockType(pType(schema))})); |
72 | 24 | } |
|
0 commit comments