@editora/toast 1.0.1
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/README.md +14 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +70 -0
- package/dist/packages/core/src/Editor.d.ts +15 -0
- package/dist/packages/core/src/Editor.js +37 -0
- package/dist/packages/core/src/EditorState.d.ts +13 -0
- package/dist/packages/core/src/EditorState.js +14 -0
- package/dist/packages/core/src/Selection.d.ts +126 -0
- package/dist/packages/core/src/Selection.js +232 -0
- package/dist/packages/core/src/commands/CommandManager.d.ts +56 -0
- package/dist/packages/core/src/commands/CommandManager.js +166 -0
- package/dist/packages/core/src/index.d.ts +7 -0
- package/dist/packages/core/src/index.js +4 -0
- package/dist/packages/core/src/model/Fragment.d.ts +80 -0
- package/dist/packages/core/src/model/Fragment.js +172 -0
- package/dist/packages/core/src/model/Mark.d.ts +108 -0
- package/dist/packages/core/src/model/Mark.js +138 -0
- package/dist/packages/core/src/model/Node.d.ts +172 -0
- package/dist/packages/core/src/model/Node.js +458 -0
- package/dist/packages/core/src/model/types.d.ts +29 -0
- package/dist/packages/core/src/model/types.js +1 -0
- package/dist/packages/core/src/plugins/Plugin.d.ts +37 -0
- package/dist/packages/core/src/plugins/Plugin.js +31 -0
- package/dist/packages/core/src/plugins/PluginManager.d.ts +75 -0
- package/dist/packages/core/src/plugins/PluginManager.js +185 -0
- package/dist/packages/core/src/schema/Node.d.ts +26 -0
- package/dist/packages/core/src/schema/Node.js +12 -0
- package/dist/packages/core/src/schema/Schema.d.ts +52 -0
- package/dist/packages/core/src/schema/Schema.js +296 -0
- package/dist/packages/core/src/security/Sanitizer.d.ts +81 -0
- package/dist/packages/core/src/security/Sanitizer.js +200 -0
- package/dist/packages/editora-toast/src/index.d.ts +18 -0
- package/dist/packages/editora-toast/src/index.js +70 -0
- package/dist/toast.css +42 -0
- package/package.json +27 -0
- package/src/index.ts +83 -0
- package/src/toast.css +42 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { Selection } from '../Selection';
|
|
2
|
+
import { Fragment } from '../model/Fragment';
|
|
3
|
+
/**
|
|
4
|
+
* Command manager for registering and executing commands.
|
|
5
|
+
*/
|
|
6
|
+
export class CommandManager {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.commands = new Map();
|
|
9
|
+
this.aliases = new Map();
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Register a command.
|
|
13
|
+
*/
|
|
14
|
+
register(name, command) {
|
|
15
|
+
this.commands.set(name, command);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Register multiple commands.
|
|
19
|
+
*/
|
|
20
|
+
registerAll(commands) {
|
|
21
|
+
Object.entries(commands).forEach(([name, command]) => {
|
|
22
|
+
this.register(name, command);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Create an alias for a command.
|
|
27
|
+
*/
|
|
28
|
+
alias(alias, commandName) {
|
|
29
|
+
this.aliases.set(alias, commandName);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Execute a command by name.
|
|
33
|
+
*/
|
|
34
|
+
execute(name, state, dispatch, view, ...args) {
|
|
35
|
+
const commandName = this.aliases.get(name) || name;
|
|
36
|
+
const command = this.commands.get(commandName);
|
|
37
|
+
if (!command) {
|
|
38
|
+
console.warn(`Command "${name}" not found`);
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
return command(state, dispatch, view, ...args);
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error(`Error executing command "${name}":`, error);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Check if a command can be executed.
|
|
51
|
+
*/
|
|
52
|
+
can(name, state, view, ...args) {
|
|
53
|
+
const commandName = this.aliases.get(name) || name;
|
|
54
|
+
const command = this.commands.get(commandName);
|
|
55
|
+
if (!command)
|
|
56
|
+
return false;
|
|
57
|
+
try {
|
|
58
|
+
return command(state, undefined, view, ...args);
|
|
59
|
+
}
|
|
60
|
+
catch (_a) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if a command exists.
|
|
66
|
+
*/
|
|
67
|
+
has(name) {
|
|
68
|
+
const commandName = this.aliases.get(name) || name;
|
|
69
|
+
return this.commands.has(commandName);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Get all registered commands.
|
|
73
|
+
*/
|
|
74
|
+
getCommands() {
|
|
75
|
+
const result = {};
|
|
76
|
+
this.commands.forEach((command, name) => {
|
|
77
|
+
result[name] = command;
|
|
78
|
+
});
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get all registered command names.
|
|
83
|
+
*/
|
|
84
|
+
getCommandNames() {
|
|
85
|
+
return Array.from(this.commands.keys());
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Unregister a command.
|
|
89
|
+
*/
|
|
90
|
+
unregister(name) {
|
|
91
|
+
this.commands.delete(name);
|
|
92
|
+
for (const [alias, commandName] of this.aliases.entries()) {
|
|
93
|
+
if (commandName === name) {
|
|
94
|
+
this.aliases.delete(alias);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Clear all commands.
|
|
100
|
+
*/
|
|
101
|
+
clear() {
|
|
102
|
+
this.commands.clear();
|
|
103
|
+
this.aliases.clear();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a command manager instance with built-in commands.
|
|
108
|
+
*/
|
|
109
|
+
export function createCommandManager() {
|
|
110
|
+
const manager = new CommandManager();
|
|
111
|
+
// Register built-in commands
|
|
112
|
+
manager.registerAll({
|
|
113
|
+
selectAll: selectAllCommand,
|
|
114
|
+
insertText: insertTextCommand,
|
|
115
|
+
deleteSelection: deleteSelectionCommand,
|
|
116
|
+
insertHardBreak: insertHardBreakCommand,
|
|
117
|
+
focus: focusCommand,
|
|
118
|
+
blur: blurCommand
|
|
119
|
+
});
|
|
120
|
+
return manager;
|
|
121
|
+
}
|
|
122
|
+
// Built-in commands
|
|
123
|
+
function selectAllCommand(state, dispatch) {
|
|
124
|
+
if (dispatch) {
|
|
125
|
+
const tr = state.tr.setSelection(Selection.create(state.doc, 0, state.doc.content.size));
|
|
126
|
+
dispatch(tr);
|
|
127
|
+
}
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
function insertTextCommand(text) {
|
|
131
|
+
return (state, dispatch) => {
|
|
132
|
+
if (dispatch) {
|
|
133
|
+
const tr = state.tr.insertText(state.selection.from, text, state.storedMarks);
|
|
134
|
+
dispatch(tr);
|
|
135
|
+
}
|
|
136
|
+
return true;
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function deleteSelectionCommand(state, dispatch) {
|
|
140
|
+
const { from, to } = state.selection;
|
|
141
|
+
if (from === to)
|
|
142
|
+
return false;
|
|
143
|
+
if (dispatch) {
|
|
144
|
+
const tr = state.tr.replace(from, to, Fragment.from([]));
|
|
145
|
+
dispatch(tr);
|
|
146
|
+
}
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
function insertHardBreakCommand(state, dispatch) {
|
|
150
|
+
const hardBreak = state.schema.nodes.hard_break;
|
|
151
|
+
if (!hardBreak)
|
|
152
|
+
return false;
|
|
153
|
+
if (dispatch) {
|
|
154
|
+
const tr = state.tr.replace(state.selection.from, state.selection.to, Fragment.from([hardBreak.create()]));
|
|
155
|
+
dispatch(tr);
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
function focusCommand(state, dispatch, view) {
|
|
160
|
+
view === null || view === void 0 ? void 0 : view.focus();
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
function blurCommand(state, dispatch, view) {
|
|
164
|
+
view === null || view === void 0 ? void 0 : view.blur();
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { Editor } from './Editor';
|
|
2
|
+
export { EditorState } from './EditorState';
|
|
3
|
+
export type { EditorSelection } from './EditorState';
|
|
4
|
+
export { Schema } from './schema/Node';
|
|
5
|
+
export type { Node, NodeSpec } from './schema/Node';
|
|
6
|
+
export { PluginManager } from './plugins/Plugin';
|
|
7
|
+
export type { Plugin, ToolbarItem } from './plugins/Plugin';
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Node } from './Node';
|
|
2
|
+
/**
|
|
3
|
+
* An immutable sequence of nodes.
|
|
4
|
+
* Fragments are the content of block nodes and represent the document structure.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Fragment {
|
|
7
|
+
private readonly content;
|
|
8
|
+
constructor(content?: readonly Node[]);
|
|
9
|
+
/**
|
|
10
|
+
* Create a fragment from an array of nodes.
|
|
11
|
+
*/
|
|
12
|
+
static from(nodes: readonly Node[]): Fragment;
|
|
13
|
+
/**
|
|
14
|
+
* Create a fragment from JSON.
|
|
15
|
+
*/
|
|
16
|
+
static fromJSON(schema: any, json: any[]): Fragment;
|
|
17
|
+
/**
|
|
18
|
+
* Get the number of child nodes.
|
|
19
|
+
*/
|
|
20
|
+
get childCount(): number;
|
|
21
|
+
/**
|
|
22
|
+
* Get the child nodes as an array.
|
|
23
|
+
*/
|
|
24
|
+
get children(): readonly Node[];
|
|
25
|
+
/**
|
|
26
|
+
* Get a child node by index.
|
|
27
|
+
*/
|
|
28
|
+
child(index: number): Node;
|
|
29
|
+
/**
|
|
30
|
+
* Get the first child node.
|
|
31
|
+
*/
|
|
32
|
+
get firstChild(): Node | null;
|
|
33
|
+
/**
|
|
34
|
+
* Get the last child node.
|
|
35
|
+
*/
|
|
36
|
+
get lastChild(): Node | null;
|
|
37
|
+
/**
|
|
38
|
+
* Find the child node at the given document position.
|
|
39
|
+
*/
|
|
40
|
+
findChildAt(pos: number): {
|
|
41
|
+
node: Node;
|
|
42
|
+
offset: number;
|
|
43
|
+
} | null;
|
|
44
|
+
/**
|
|
45
|
+
* Get the total size of this fragment in the document.
|
|
46
|
+
*/
|
|
47
|
+
get size(): number;
|
|
48
|
+
/**
|
|
49
|
+
* Create a new fragment with a node appended.
|
|
50
|
+
*/
|
|
51
|
+
append(node: Node): Fragment;
|
|
52
|
+
/**
|
|
53
|
+
* Create a new fragment by cutting out a slice.
|
|
54
|
+
*/
|
|
55
|
+
cut(from?: number, to?: number): Fragment;
|
|
56
|
+
/**
|
|
57
|
+
* Replace a range of this fragment with new content.
|
|
58
|
+
*/
|
|
59
|
+
replace(from: number, to: number, fragment: Fragment): Fragment;
|
|
60
|
+
/**
|
|
61
|
+
* Append another fragment to this one.
|
|
62
|
+
*/
|
|
63
|
+
appendFragment(other: Fragment): Fragment;
|
|
64
|
+
/**
|
|
65
|
+
* Check if this fragment equals another fragment.
|
|
66
|
+
*/
|
|
67
|
+
equals(other: Fragment): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Convert to JSON representation.
|
|
70
|
+
*/
|
|
71
|
+
toJSON(): any[];
|
|
72
|
+
/**
|
|
73
|
+
* Get the text content of this fragment.
|
|
74
|
+
*/
|
|
75
|
+
get textContent(): string;
|
|
76
|
+
/**
|
|
77
|
+
* Create a string representation for debugging.
|
|
78
|
+
*/
|
|
79
|
+
toString(): string;
|
|
80
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Node } from './Node';
|
|
2
|
+
/**
|
|
3
|
+
* An immutable sequence of nodes.
|
|
4
|
+
* Fragments are the content of block nodes and represent the document structure.
|
|
5
|
+
*/
|
|
6
|
+
export class Fragment {
|
|
7
|
+
constructor(content = []) {
|
|
8
|
+
this.content = content;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create a fragment from an array of nodes.
|
|
12
|
+
*/
|
|
13
|
+
static from(nodes) {
|
|
14
|
+
return new Fragment(nodes);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a fragment from JSON.
|
|
18
|
+
*/
|
|
19
|
+
static fromJSON(schema, json) {
|
|
20
|
+
const nodes = json.map(nodeJSON => Node.fromJSON(schema, nodeJSON));
|
|
21
|
+
return new Fragment(nodes);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Get the number of child nodes.
|
|
25
|
+
*/
|
|
26
|
+
get childCount() {
|
|
27
|
+
return this.content.length;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get the child nodes as an array.
|
|
31
|
+
*/
|
|
32
|
+
get children() {
|
|
33
|
+
return this.content;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Get a child node by index.
|
|
37
|
+
*/
|
|
38
|
+
child(index) {
|
|
39
|
+
if (index < 0 || index >= this.content.length) {
|
|
40
|
+
throw new Error(`Index ${index} out of bounds`);
|
|
41
|
+
}
|
|
42
|
+
return this.content[index];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the first child node.
|
|
46
|
+
*/
|
|
47
|
+
get firstChild() {
|
|
48
|
+
return this.content.length > 0 ? this.content[0] : null;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the last child node.
|
|
52
|
+
*/
|
|
53
|
+
get lastChild() {
|
|
54
|
+
return this.content.length > 0 ? this.content[this.content.length - 1] : null;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Find the child node at the given document position.
|
|
58
|
+
*/
|
|
59
|
+
findChildAt(pos) {
|
|
60
|
+
let offset = 0;
|
|
61
|
+
for (const child of this.content) {
|
|
62
|
+
const childSize = child.nodeSize;
|
|
63
|
+
if (pos >= offset && pos < offset + childSize) {
|
|
64
|
+
return { node: child, offset };
|
|
65
|
+
}
|
|
66
|
+
offset += childSize;
|
|
67
|
+
}
|
|
68
|
+
// Return last child if position is at the end
|
|
69
|
+
if (pos === offset && this.content.length > 0) {
|
|
70
|
+
const lastChild = this.content[this.content.length - 1];
|
|
71
|
+
return { node: lastChild, offset: offset - lastChild.nodeSize };
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Get the total size of this fragment in the document.
|
|
77
|
+
*/
|
|
78
|
+
get size() {
|
|
79
|
+
return this.content.reduce((size, node) => size + node.nodeSize, 0);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create a new fragment with a node appended.
|
|
83
|
+
*/
|
|
84
|
+
append(node) {
|
|
85
|
+
return new Fragment([...this.content, node]);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Create a new fragment by cutting out a slice.
|
|
89
|
+
*/
|
|
90
|
+
cut(from = 0, to) {
|
|
91
|
+
const actualTo = to !== null && to !== void 0 ? to : this.size;
|
|
92
|
+
if (from === 0 && actualTo === this.size) {
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
const result = [];
|
|
96
|
+
let pos = 0;
|
|
97
|
+
for (const child of this.content) {
|
|
98
|
+
const childSize = child.nodeSize;
|
|
99
|
+
if (pos + childSize <= from) {
|
|
100
|
+
// Child is entirely before the cut
|
|
101
|
+
pos += childSize;
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (pos >= actualTo) {
|
|
105
|
+
// Child is entirely after the cut
|
|
106
|
+
break;
|
|
107
|
+
}
|
|
108
|
+
// Child overlaps with the cut range
|
|
109
|
+
const childFrom = Math.max(0, from - pos);
|
|
110
|
+
const childTo = Math.min(childSize, actualTo - pos);
|
|
111
|
+
if (childFrom === 0 && childTo === childSize) {
|
|
112
|
+
// Include entire child
|
|
113
|
+
result.push(child);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
// Cut the child
|
|
117
|
+
const cutChild = child.cut(childFrom, childTo);
|
|
118
|
+
if (cutChild) {
|
|
119
|
+
result.push(cutChild);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
pos += childSize;
|
|
123
|
+
}
|
|
124
|
+
return new Fragment(result);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Replace a range of this fragment with new content.
|
|
128
|
+
*/
|
|
129
|
+
replace(from, to, fragment) {
|
|
130
|
+
const before = this.cut(0, from);
|
|
131
|
+
const after = this.cut(to);
|
|
132
|
+
return before.appendFragment(fragment).appendFragment(after);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Append another fragment to this one.
|
|
136
|
+
*/
|
|
137
|
+
appendFragment(other) {
|
|
138
|
+
return new Fragment([...this.content, ...other.content]);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Check if this fragment equals another fragment.
|
|
142
|
+
*/
|
|
143
|
+
equals(other) {
|
|
144
|
+
if (this.content.length !== other.content.length) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
for (let i = 0; i < this.content.length; i++) {
|
|
148
|
+
if (!this.content[i].equals(other.content[i])) {
|
|
149
|
+
return false;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Convert to JSON representation.
|
|
156
|
+
*/
|
|
157
|
+
toJSON() {
|
|
158
|
+
return this.content.map(node => node.toJSON());
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Get the text content of this fragment.
|
|
162
|
+
*/
|
|
163
|
+
get textContent() {
|
|
164
|
+
return this.content.map(node => node.textContent).join('');
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Create a string representation for debugging.
|
|
168
|
+
*/
|
|
169
|
+
toString() {
|
|
170
|
+
return this.content.map(node => node.toString()).join('');
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Attrs } from './Node';
|
|
2
|
+
/**
|
|
3
|
+
* A mark represents inline formatting like bold, italic, links, etc.
|
|
4
|
+
* Marks can be applied to text nodes and can span multiple characters.
|
|
5
|
+
*/
|
|
6
|
+
export declare class Mark {
|
|
7
|
+
readonly type: MarkType;
|
|
8
|
+
readonly attrs: Attrs;
|
|
9
|
+
constructor(type: MarkType, attrs?: Attrs);
|
|
10
|
+
/**
|
|
11
|
+
* Create a mark from a MarkType.
|
|
12
|
+
*/
|
|
13
|
+
static create(type: MarkType, attrs?: Attrs): Mark;
|
|
14
|
+
/**
|
|
15
|
+
* Create a mark from JSON representation.
|
|
16
|
+
*/
|
|
17
|
+
static fromJSON(schema: any, json: any): Mark;
|
|
18
|
+
/**
|
|
19
|
+
* Check if this mark is in a set of marks.
|
|
20
|
+
*/
|
|
21
|
+
isInSet(marks: readonly Mark[]): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Add this mark to a set of marks.
|
|
24
|
+
*/
|
|
25
|
+
addToSet(marks: readonly Mark[]): readonly Mark[];
|
|
26
|
+
/**
|
|
27
|
+
* Remove this mark from a set of marks.
|
|
28
|
+
*/
|
|
29
|
+
removeFromSet(marks: readonly Mark[]): readonly Mark[];
|
|
30
|
+
/**
|
|
31
|
+
* Remove marks of a specific type from a set.
|
|
32
|
+
*/
|
|
33
|
+
static removeFromSet(marks: readonly Mark[], markType: MarkType): readonly Mark[];
|
|
34
|
+
/**
|
|
35
|
+
* Check if attributes are equal.
|
|
36
|
+
*/
|
|
37
|
+
private attrsEqual;
|
|
38
|
+
/**
|
|
39
|
+
* Check if this mark equals another mark.
|
|
40
|
+
*/
|
|
41
|
+
equals(other: Mark): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Convert to JSON representation.
|
|
44
|
+
*/
|
|
45
|
+
toJSON(): any;
|
|
46
|
+
/**
|
|
47
|
+
* Create a string representation for debugging.
|
|
48
|
+
*/
|
|
49
|
+
toString(): string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Mark type definition with specifications.
|
|
53
|
+
*/
|
|
54
|
+
export declare class MarkType {
|
|
55
|
+
readonly name: string;
|
|
56
|
+
readonly spec: MarkSpec;
|
|
57
|
+
readonly schema: any;
|
|
58
|
+
constructor(name: string, spec: MarkSpec, schema: any);
|
|
59
|
+
/**
|
|
60
|
+
* Check if this mark type excludes another mark type.
|
|
61
|
+
*/
|
|
62
|
+
excludes(other: MarkType): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Create a mark of this type.
|
|
65
|
+
*/
|
|
66
|
+
create(attrs?: Attrs): Mark;
|
|
67
|
+
/**
|
|
68
|
+
* Check if this mark is active in a set of marks.
|
|
69
|
+
*/
|
|
70
|
+
isInSet(marks: readonly Mark[]): boolean;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Specification for a mark type.
|
|
74
|
+
*/
|
|
75
|
+
export interface MarkSpec {
|
|
76
|
+
attrs?: {
|
|
77
|
+
[key: string]: AttributeSpec;
|
|
78
|
+
};
|
|
79
|
+
inclusive?: boolean;
|
|
80
|
+
excludes?: string | string[];
|
|
81
|
+
group?: string;
|
|
82
|
+
spanning?: boolean;
|
|
83
|
+
parseDOM?: ParseRule[];
|
|
84
|
+
toDOM?: (mark: Mark) => DOMOutputSpec;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Specification for a node or mark attribute.
|
|
88
|
+
*/
|
|
89
|
+
export interface AttributeSpec {
|
|
90
|
+
default?: any;
|
|
91
|
+
validate?: (value: any) => boolean;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Parse rule for DOM parsing.
|
|
95
|
+
*/
|
|
96
|
+
export interface ParseRule {
|
|
97
|
+
tag?: string;
|
|
98
|
+
style?: string;
|
|
99
|
+
attrs?: {
|
|
100
|
+
[key: string]: any;
|
|
101
|
+
};
|
|
102
|
+
getAttrs?: (node: HTMLElement | string) => any | null;
|
|
103
|
+
clearMark?: (mark: Mark) => boolean;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* DOM output specification.
|
|
107
|
+
*/
|
|
108
|
+
export type DOMOutputSpec = string | [string, ...any[]];
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A mark represents inline formatting like bold, italic, links, etc.
|
|
3
|
+
* Marks can be applied to text nodes and can span multiple characters.
|
|
4
|
+
*/
|
|
5
|
+
export class Mark {
|
|
6
|
+
constructor(type, attrs = {}) {
|
|
7
|
+
this.type = type;
|
|
8
|
+
this.attrs = attrs;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create a mark from a MarkType.
|
|
12
|
+
*/
|
|
13
|
+
static create(type, attrs = {}) {
|
|
14
|
+
return new Mark(type, attrs);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Create a mark from JSON representation.
|
|
18
|
+
*/
|
|
19
|
+
static fromJSON(schema, json) {
|
|
20
|
+
const type = schema.marks[json.type];
|
|
21
|
+
if (!type) {
|
|
22
|
+
throw new Error(`Unknown mark type: ${json.type}`);
|
|
23
|
+
}
|
|
24
|
+
const attrs = json.attrs || {};
|
|
25
|
+
return new Mark(type, attrs);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check if this mark is in a set of marks.
|
|
29
|
+
*/
|
|
30
|
+
isInSet(marks) {
|
|
31
|
+
return marks.some(mark => mark.type === this.type && this.attrsEqual(mark.attrs));
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Add this mark to a set of marks.
|
|
35
|
+
*/
|
|
36
|
+
addToSet(marks) {
|
|
37
|
+
// Remove any existing marks of the same type first
|
|
38
|
+
const filtered = marks.filter(mark => mark.type !== this.type);
|
|
39
|
+
// Check if this mark excludes any existing marks
|
|
40
|
+
const compatible = filtered.filter(mark => !this.type.excludes(mark.type));
|
|
41
|
+
return [...compatible, this];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Remove this mark from a set of marks.
|
|
45
|
+
*/
|
|
46
|
+
removeFromSet(marks) {
|
|
47
|
+
return marks.filter(mark => !(mark.type === this.type && this.attrsEqual(mark.attrs)));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Remove marks of a specific type from a set.
|
|
51
|
+
*/
|
|
52
|
+
static removeFromSet(marks, markType) {
|
|
53
|
+
return marks.filter(mark => mark.type !== markType);
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Check if attributes are equal.
|
|
57
|
+
*/
|
|
58
|
+
attrsEqual(otherAttrs) {
|
|
59
|
+
const keys1 = Object.keys(this.attrs);
|
|
60
|
+
const keys2 = Object.keys(otherAttrs);
|
|
61
|
+
if (keys1.length !== keys2.length) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
for (const key of keys1) {
|
|
65
|
+
if (this.attrs[key] !== otherAttrs[key]) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if this mark equals another mark.
|
|
73
|
+
*/
|
|
74
|
+
equals(other) {
|
|
75
|
+
return this.type === other.type && this.attrsEqual(other.attrs);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Convert to JSON representation.
|
|
79
|
+
*/
|
|
80
|
+
toJSON() {
|
|
81
|
+
const result = {
|
|
82
|
+
type: this.type.name,
|
|
83
|
+
};
|
|
84
|
+
if (Object.keys(this.attrs).length > 0) {
|
|
85
|
+
result.attrs = this.attrs;
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Create a string representation for debugging.
|
|
91
|
+
*/
|
|
92
|
+
toString() {
|
|
93
|
+
const attrs = Object.keys(this.attrs).length > 0
|
|
94
|
+
? `(${Object.entries(this.attrs).map(([k, v]) => `${k}=${v}`).join(', ')})`
|
|
95
|
+
: '';
|
|
96
|
+
return `<${this.type.name}${attrs}>`;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Mark type definition with specifications.
|
|
101
|
+
*/
|
|
102
|
+
export class MarkType {
|
|
103
|
+
constructor(name, spec, schema) {
|
|
104
|
+
this.name = name;
|
|
105
|
+
this.spec = spec;
|
|
106
|
+
this.schema = schema;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if this mark type excludes another mark type.
|
|
110
|
+
*/
|
|
111
|
+
excludes(other) {
|
|
112
|
+
const excludes = this.spec.excludes;
|
|
113
|
+
if (excludes) {
|
|
114
|
+
if (typeof excludes === 'string') {
|
|
115
|
+
return excludes === other.name || excludes === (other.spec.group || '');
|
|
116
|
+
}
|
|
117
|
+
return excludes.includes(other.name) || excludes.includes(other.spec.group || '');
|
|
118
|
+
}
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a mark of this type.
|
|
123
|
+
*/
|
|
124
|
+
create(attrs = {}) {
|
|
125
|
+
return Mark.create(this, attrs);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Check if this mark is active in a set of marks.
|
|
129
|
+
*/
|
|
130
|
+
isInSet(marks) {
|
|
131
|
+
for (const mark of marks) {
|
|
132
|
+
if (mark.type === this) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
}
|