@lexion-rte/extensions 0.1.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 +674 -0
- package/dist/ai.d.ts +21 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +40 -0
- package/dist/ai.js.map +1 -0
- package/dist/collaboration.d.ts +15 -0
- package/dist/collaboration.d.ts.map +1 -0
- package/dist/collaboration.js +28 -0
- package/dist/collaboration.js.map +1 -0
- package/dist/command-names.d.ts +15 -0
- package/dist/command-names.d.ts.map +1 -0
- package/dist/command-names.js +15 -0
- package/dist/command-names.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/starter-kit.d.ts +7 -0
- package/dist/starter-kit.d.ts.map +1 -0
- package/dist/starter-kit.js +119 -0
- package/dist/starter-kit.js.map +1 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +52 -0
- package/src/ai.ts +59 -0
- package/src/collaboration.ts +48 -0
- package/src/command-names.ts +14 -0
- package/src/index.ts +7 -0
- package/src/starter-kit.ts +153 -0
- package/src/types.ts +8 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { setBlockType, toggleMark, baseKeymap } from "prosemirror-commands";
|
|
2
|
+
import { history, redo, undo } from "prosemirror-history";
|
|
3
|
+
import { keymap } from "prosemirror-keymap";
|
|
4
|
+
import { Schema, type MarkType, type NodeType } from "prosemirror-model";
|
|
5
|
+
import { schema as basicSchema } from "prosemirror-schema-basic";
|
|
6
|
+
import {
|
|
7
|
+
addListNodes,
|
|
8
|
+
liftListItem as pmLiftListItem,
|
|
9
|
+
sinkListItem as pmSinkListItem,
|
|
10
|
+
wrapInList
|
|
11
|
+
} from "prosemirror-schema-list";
|
|
12
|
+
import type { Command } from "prosemirror-state";
|
|
13
|
+
import type { CommandHandler, CommandMap, LexionExtension } from "@lexion-rte/core";
|
|
14
|
+
|
|
15
|
+
import { starterKitCommandNames } from "./command-names.js";
|
|
16
|
+
import type { HeadingAttributes, LinkAttributes } from "./types.js";
|
|
17
|
+
|
|
18
|
+
const starterKitNodes = addListNodes(basicSchema.spec.nodes, "paragraph block*", "block");
|
|
19
|
+
|
|
20
|
+
export const createStarterKitSchema = (): Schema =>
|
|
21
|
+
new Schema({
|
|
22
|
+
nodes: starterKitNodes,
|
|
23
|
+
marks: basicSchema.spec.marks
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
export const starterKitSchema = createStarterKitSchema();
|
|
27
|
+
|
|
28
|
+
const asProseMirrorCommand = (command: Command): CommandHandler => (context) =>
|
|
29
|
+
command(context.state, context.dispatch);
|
|
30
|
+
|
|
31
|
+
const getNodeType = (name: string, schema: Schema): NodeType => {
|
|
32
|
+
const nodeType = schema.nodes[name];
|
|
33
|
+
if (!nodeType) {
|
|
34
|
+
throw new Error(`Missing required node type: ${name}`);
|
|
35
|
+
}
|
|
36
|
+
return nodeType;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const getMarkType = (name: string, schema: Schema): MarkType => {
|
|
40
|
+
const markType = schema.marks[name];
|
|
41
|
+
if (!markType) {
|
|
42
|
+
throw new Error(`Missing required mark type: ${name}`);
|
|
43
|
+
}
|
|
44
|
+
return markType;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const asHeadingAttributes = (value: unknown): HeadingAttributes | null => {
|
|
48
|
+
if (typeof value !== "number") {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
if (value < 1 || value > 6) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
return { level: value as HeadingAttributes["level"] };
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const asLinkAttributes = (value: unknown): LinkAttributes | null => {
|
|
58
|
+
if (typeof value !== "object" || value === null) {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
const candidate = value as Record<string, unknown>;
|
|
62
|
+
if (typeof candidate["href"] !== "string" || candidate["href"].length === 0) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const titleValue = candidate["title"];
|
|
67
|
+
if (titleValue !== undefined && titleValue !== null && typeof titleValue !== "string") {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
href: candidate["href"],
|
|
73
|
+
title: (titleValue as string | null | undefined) ?? null
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export const createStarterKitCommands = (): CommandMap => ({
|
|
78
|
+
[starterKitCommandNames.setParagraph]: (context) => {
|
|
79
|
+
const paragraph = getNodeType("paragraph", context.schema);
|
|
80
|
+
return setBlockType(paragraph)(context.state, context.dispatch);
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
[starterKitCommandNames.toggleHeading]: (context, levelValue) => {
|
|
84
|
+
const heading = getNodeType("heading", context.schema);
|
|
85
|
+
const headingAttributes = asHeadingAttributes(levelValue);
|
|
86
|
+
if (!headingAttributes) {
|
|
87
|
+
return false;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return setBlockType(heading, { level: headingAttributes.level })(context.state, context.dispatch);
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
[starterKitCommandNames.toggleBold]: (context) => {
|
|
94
|
+
const strong = getMarkType("strong", context.schema);
|
|
95
|
+
return toggleMark(strong)(context.state, context.dispatch);
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
[starterKitCommandNames.toggleItalic]: (context) => {
|
|
99
|
+
const em = getMarkType("em", context.schema);
|
|
100
|
+
return toggleMark(em)(context.state, context.dispatch);
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
[starterKitCommandNames.wrapBulletList]: (context) => {
|
|
104
|
+
const bulletList = getNodeType("bullet_list", context.schema);
|
|
105
|
+
return wrapInList(bulletList)(context.state, context.dispatch);
|
|
106
|
+
},
|
|
107
|
+
|
|
108
|
+
[starterKitCommandNames.wrapOrderedList]: (context) => {
|
|
109
|
+
const orderedList = getNodeType("ordered_list", context.schema);
|
|
110
|
+
return wrapInList(orderedList)(context.state, context.dispatch);
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
[starterKitCommandNames.liftListItem]: (context) => {
|
|
114
|
+
const listItem = getNodeType("list_item", context.schema);
|
|
115
|
+
return pmLiftListItem(listItem)(context.state, context.dispatch);
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
[starterKitCommandNames.sinkListItem]: (context) => {
|
|
119
|
+
const listItem = getNodeType("list_item", context.schema);
|
|
120
|
+
return pmSinkListItem(listItem)(context.state, context.dispatch);
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
[starterKitCommandNames.setLink]: (context, linkValue) => {
|
|
124
|
+
const link = getMarkType("link", context.schema);
|
|
125
|
+
const attributes = asLinkAttributes(linkValue);
|
|
126
|
+
if (!attributes || context.state.selection.empty) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const { from, to } = context.state.selection;
|
|
131
|
+
const transaction = context.state.tr.removeMark(from, to, link).addMark(from, to, link.create(attributes));
|
|
132
|
+
context.dispatch(transaction.scrollIntoView());
|
|
133
|
+
return true;
|
|
134
|
+
},
|
|
135
|
+
|
|
136
|
+
[starterKitCommandNames.unsetLink]: (context) => {
|
|
137
|
+
const link = getMarkType("link", context.schema);
|
|
138
|
+
const { from, to } = context.state.selection;
|
|
139
|
+
const transaction = context.state.tr.removeMark(from, to, link);
|
|
140
|
+
context.dispatch(transaction.scrollIntoView());
|
|
141
|
+
return true;
|
|
142
|
+
},
|
|
143
|
+
|
|
144
|
+
[starterKitCommandNames.undo]: asProseMirrorCommand(undo),
|
|
145
|
+
[starterKitCommandNames.redo]: asProseMirrorCommand(redo)
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
export const starterKitExtension: LexionExtension = {
|
|
149
|
+
key: "starter-kit",
|
|
150
|
+
schema: starterKitSchema,
|
|
151
|
+
commands: () => createStarterKitCommands(),
|
|
152
|
+
prosemirrorPlugins: () => [history(), keymap(baseKeymap)]
|
|
153
|
+
};
|
package/src/types.ts
ADDED