@kerebron/extension-menu 0.0.12 → 0.0.13
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.
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EditorView } from 'prosemirror-view';
|
|
2
2
|
import { Fragment, Node as ProseMirrorNode, type Schema } from 'prosemirror-model';
|
|
3
3
|
import type { EditorOptions, JSONContent } from './types.js';
|
|
4
|
-
import { EditorState } from 'prosemirror-state';
|
|
4
|
+
import { EditorState, Transaction } from 'prosemirror-state';
|
|
5
5
|
import { ChainedCommands } from './commands/CommandManager.js';
|
|
6
6
|
export declare function getHTMLFromFragment(fragment: Fragment, schema: Schema): string;
|
|
7
7
|
export declare class CoreEditor extends EventTarget {
|
|
@@ -15,7 +15,7 @@ export declare class CoreEditor extends EventTarget {
|
|
|
15
15
|
chain(): ChainedCommands;
|
|
16
16
|
can(): ChainedCommands;
|
|
17
17
|
private createView;
|
|
18
|
-
|
|
18
|
+
dispatchTransaction(transaction: Transaction): void;
|
|
19
19
|
private setupPlugins;
|
|
20
20
|
getJSON(): JSONContent;
|
|
21
21
|
/**
|
|
@@ -24,5 +24,7 @@ export declare class CoreEditor extends EventTarget {
|
|
|
24
24
|
getHTML(): string;
|
|
25
25
|
setDocument(content?: any, mediaType?: string): void;
|
|
26
26
|
getDocument(mediaType?: string): void | ProseMirrorNode;
|
|
27
|
+
clone(options?: Partial<EditorOptions>): CoreEditor;
|
|
28
|
+
debug(doc?: ProseMirrorNode): void;
|
|
27
29
|
}
|
|
28
30
|
//# sourceMappingURL=CoreEditor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CoreEditor.d.ts","sourceRoot":"","sources":["../../../src/editor/src/CoreEditor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAEL,QAAQ,EACR,IAAI,IAAI,eAAe,EAEvB,KAAK,MAAM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,MAAM;IAc9B,KAAK,CAAC,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM,GAAG,UAAU;IAOvD,KAAK,CAAC,GAAG,CAAC,EAAE,eAAe;CAMnC"}
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { EditorView } from 'prosemirror-view';
|
|
2
|
-
import { DOMSerializer, } from 'prosemirror-model';
|
|
2
|
+
import { DOMSerializer, Node as ProseMirrorNode, } from 'prosemirror-model';
|
|
3
3
|
import { ExtensionManager } from './ExtensionManager.js';
|
|
4
4
|
import { EditorState } from 'prosemirror-state';
|
|
5
5
|
import { createNodeFromContent } from './utilities/createNodeFromContent.js';
|
|
6
6
|
import { CommandManager } from './commands/CommandManager.js';
|
|
7
|
+
import { debugDoc } from './debugDoc.js';
|
|
7
8
|
export function getHTMLFromFragment(fragment, schema) {
|
|
8
9
|
const documentFragment = DOMSerializer.fromSchema(schema).serializeFragment(fragment);
|
|
9
10
|
const temporaryDocument = document.implementation.createHTMLDocument();
|
|
@@ -18,6 +19,33 @@ function createDocument(content, schema, parseOptions = {}, options = {}) {
|
|
|
18
19
|
errorOnInvalidContent: options.errorOnInvalidContent,
|
|
19
20
|
});
|
|
20
21
|
}
|
|
22
|
+
function ensureDocSchema(doc, schema) {
|
|
23
|
+
if (doc.type.schema != schema) {
|
|
24
|
+
const findNode = (nodeName) => {
|
|
25
|
+
if (!schema.nodes[nodeName]) {
|
|
26
|
+
throw new Error(`Not able to rewrite schema for node '${nodeName}'`);
|
|
27
|
+
}
|
|
28
|
+
return schema.nodes[nodeName];
|
|
29
|
+
};
|
|
30
|
+
const findMark = (markName) => {
|
|
31
|
+
if (!schema.marks[markName]) {
|
|
32
|
+
throw new Error(`Not able to rewrite schema for mark '${markName}'`);
|
|
33
|
+
}
|
|
34
|
+
return schema.marks[markName];
|
|
35
|
+
};
|
|
36
|
+
// TODO fix readonly warnings
|
|
37
|
+
doc.type = findNode(doc.type.name);
|
|
38
|
+
doc.marks.forEach(mark => {
|
|
39
|
+
mark.type = findMark(mark.type.name);
|
|
40
|
+
});
|
|
41
|
+
doc.descendants(node => {
|
|
42
|
+
node.type = findNode(node.type.name);
|
|
43
|
+
node.marks.forEach(mark => {
|
|
44
|
+
mark.type = findMark(mark.type.name);
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
21
49
|
export class CoreEditor extends EventTarget {
|
|
22
50
|
constructor(options = {}) {
|
|
23
51
|
super();
|
|
@@ -141,6 +169,7 @@ export class CoreEditor extends EventTarget {
|
|
|
141
169
|
else {
|
|
142
170
|
doc = createDocument(content, this.schema, this.options.parseOptions, { errorOnInvalidContent: false });
|
|
143
171
|
}
|
|
172
|
+
ensureDocSchema(doc, this.schema);
|
|
144
173
|
this.state = EditorState.create({
|
|
145
174
|
doc,
|
|
146
175
|
plugins: this.state.plugins,
|
|
@@ -161,9 +190,23 @@ export class CoreEditor extends EventTarget {
|
|
|
161
190
|
if (mediaType) {
|
|
162
191
|
const converter = this.extensionManager.converters[mediaType];
|
|
163
192
|
if (converter) {
|
|
164
|
-
|
|
193
|
+
const json = this.state.doc.toJSON();
|
|
194
|
+
const clonedDoc = ProseMirrorNode.fromJSON(this.state.schema, json);
|
|
195
|
+
return converter.fromDoc(clonedDoc);
|
|
165
196
|
}
|
|
166
197
|
}
|
|
167
198
|
return this.state.doc;
|
|
168
199
|
}
|
|
200
|
+
clone(options = {}) {
|
|
201
|
+
return new CoreEditor({
|
|
202
|
+
...options,
|
|
203
|
+
extensions: [...this.options.extensions]
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
debug(doc) {
|
|
207
|
+
if (!doc) {
|
|
208
|
+
doc = this.state.doc;
|
|
209
|
+
}
|
|
210
|
+
debugDoc(doc);
|
|
211
|
+
}
|
|
169
212
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"debugDoc.d.ts","sourceRoot":"","sources":["../../../src/editor/src/debugDoc.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,IAAI,EAAC,MAAM,mBAAmB,CAAC;AAYvC,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,EAAE,KAAK,SAAI,EAAE,UAAU,SAAI,QAgC7D"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
function trimText(str, maxLen = 20) {
|
|
2
|
+
str = str.replaceAll('\n', '\\n');
|
|
3
|
+
if (str.length <= maxLen) {
|
|
4
|
+
return str;
|
|
5
|
+
}
|
|
6
|
+
return str.slice(0, maxLen) + '...';
|
|
7
|
+
}
|
|
8
|
+
export function debugDoc(node, level = 0, currentPos = 0) {
|
|
9
|
+
let delim = '';
|
|
10
|
+
for (let i = 0; i < level; i++) {
|
|
11
|
+
delim += ' ';
|
|
12
|
+
}
|
|
13
|
+
// https://prosemirror.net/docs/guide/#doc.indexing
|
|
14
|
+
let line = '';
|
|
15
|
+
line += ` - [${node.type.name}] `;
|
|
16
|
+
line += `pos: ${currentPos}, `;
|
|
17
|
+
line += `nodeSize: ${node.nodeSize}, `; // isLeaf ? 1 : 2 + this.content.size
|
|
18
|
+
line += `fragment.size: ${node.content.size}, `;
|
|
19
|
+
console.log(delim + line);
|
|
20
|
+
let marksLine = '';
|
|
21
|
+
for (const mark of node.marks) {
|
|
22
|
+
marksLine += `(${mark.type.name}), `;
|
|
23
|
+
}
|
|
24
|
+
if (marksLine) {
|
|
25
|
+
console.log(delim + ' ' + marksLine);
|
|
26
|
+
}
|
|
27
|
+
if (node.text) {
|
|
28
|
+
console.log(delim + ' "' + trimText(node.text) + '"');
|
|
29
|
+
}
|
|
30
|
+
node.forEach((child, offset) => {
|
|
31
|
+
debugDoc(child, level + 1, currentPos + offset); // + (node.isLeaf ? 1 : 2)
|
|
32
|
+
});
|
|
33
|
+
}
|