@kerebron/extension-menu 0.0.11 → 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.
- package/esm/editor/src/CoreEditor.d.ts +4 -2
- package/esm/editor/src/CoreEditor.d.ts.map +1 -1
- package/esm/editor/src/CoreEditor.js +45 -2
- package/esm/editor/src/debugDoc.d.ts +3 -0
- package/esm/editor/src/debugDoc.d.ts.map +1 -0
- package/esm/editor/src/debugDoc.js +33 -0
- package/esm/extension-menu/src/ExtensionMenu.d.ts +8 -1
- package/esm/extension-menu/src/ExtensionMenu.d.ts.map +1 -1
- package/esm/extension-menu/src/ExtensionMenu.js +42 -30
- package/package.json +1 -1
|
@@ -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,EACZ,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAW,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,WAAW,
|
|
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,EACZ,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAW,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAE7D,OAAO,EAAE,eAAe,EAAkB,MAAM,8BAA8B,CAAC;AAG/E,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,GACb,MAAM,CAWR;AA4CD,qBAAa,UAAW,SAAQ,WAAW;IACzC,SAAgB,OAAO,EAAE,OAAO,CAAC,aAAa,CAAC,CAG7C;IACF,OAAO,CAAC,gBAAgB,CAAmB;IAC3C,OAAO,CAAC,cAAc,CAAiB;IAChC,IAAI,EAAG,UAAU,CAAC;IAClB,KAAK,EAAG,WAAW,CAAC;gBAEf,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM;IAyBhD,IAAW,MAAM,qBAEhB;IAEM,KAAK,IAAI,eAAe;IAIxB,GAAG,IAAI,eAAe;IAI7B,OAAO,CAAC,UAAU;IAkBX,mBAAmB,CAAC,WAAW,EAAE,WAAW;IAcnD,OAAO,CAAC,YAAY;IAcb,OAAO,IAAI,WAAW;IAI7B;;OAEG;IACI,OAAO,IAAI,MAAM;IAIjB,WAAW,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,EAAE,MAAM;IA8C7C,WAAW,CAAC,SAAS,CAAC,EAAE,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
|
+
}
|
|
@@ -1,10 +1,17 @@
|
|
|
1
|
-
import { MenuElement } from './menu.js';
|
|
2
1
|
import { Plugin } from 'prosemirror-state';
|
|
3
2
|
import { Schema } from 'prosemirror-model';
|
|
4
3
|
import { type CoreEditor, Extension } from '../../editor/src/mod.js';
|
|
4
|
+
import { MenuElement } from './menu.js';
|
|
5
|
+
export { blockTypeItem, Dropdown, DropdownSubmenu, MenuItem, wrapItem, } from './menu.js';
|
|
5
6
|
export declare function buildMenu(editor: CoreEditor, schema: Schema): MenuElement[][];
|
|
7
|
+
export interface MenuConfig {
|
|
8
|
+
modifyMenu?(menus: MenuElement[][]): MenuElement[][];
|
|
9
|
+
floating: boolean;
|
|
10
|
+
}
|
|
6
11
|
export declare class ExtensionMenu extends Extension {
|
|
12
|
+
protected config: MenuConfig;
|
|
7
13
|
name: string;
|
|
14
|
+
constructor(config?: MenuConfig);
|
|
8
15
|
getProseMirrorPlugins(editor: CoreEditor, schema: Schema): Plugin[];
|
|
9
16
|
}
|
|
10
17
|
//# sourceMappingURL=ExtensionMenu.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ExtensionMenu.d.ts","sourceRoot":"","sources":["../../../src/extension-menu/src/ExtensionMenu.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"ExtensionMenu.d.ts","sourceRoot":"","sources":["../../../src/extension-menu/src/ExtensionMenu.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuC,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAChF,OAAO,EAAsB,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE/D,OAAO,EAAE,KAAK,UAAU,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAGrE,OAAO,EAIL,WAAW,EAIZ,MAAM,WAAW,CAAC;AAKnB,OAAO,EACL,aAAa,EACb,QAAQ,EACR,eAAe,EAEf,QAAQ,EAER,QAAQ,GACT,MAAM,WAAW,CAAC;AAkDnB,wBAAgB,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,WAAW,EAAE,EAAE,CAuR7E;AAED,MAAM,WAAW,UAAU;IACzB,UAAU,CAAC,CAAC,KAAK,EAAE,WAAW,EAAE,EAAE,GAAG,WAAW,EAAE,EAAE,CAAC;IACrD,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,qBAAa,aAAc,SAAQ,SAAS;IAG9B,SAAS,CAAC,MAAM,EAAE,UAAU;IAFxC,IAAI,SAAU;gBAEQ,MAAM,GAAE,UAA+B;IAIpD,qBAAqB,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;CAY7E"}
|
|
@@ -1,10 +1,15 @@
|
|
|
1
|
-
import { blockTypeItem, Dropdown, DropdownSubmenu, MenuItem, wrapItem, } from './menu.js';
|
|
2
1
|
import { NodeSelection } from 'prosemirror-state';
|
|
3
2
|
import { Extension } from '../../editor/src/mod.js';
|
|
4
3
|
import { toggleMark, wrapInList } from '../../editor/src/commands/mod.js';
|
|
4
|
+
import { blockTypeItem, Dropdown, DropdownSubmenu, MenuItem, wrapItem, } from './menu.js';
|
|
5
5
|
import { MenuPlugin } from './MenuPlugin.js';
|
|
6
6
|
import { icons } from './icons.js';
|
|
7
7
|
import { openPrompt, TextField } from './prompt.js';
|
|
8
|
+
export { blockTypeItem, Dropdown, DropdownSubmenu,
|
|
9
|
+
// MenuElement,
|
|
10
|
+
MenuItem,
|
|
11
|
+
// MenuItemSpec,
|
|
12
|
+
wrapItem, } from './menu.js';
|
|
8
13
|
function canInsert(state, nodeType) {
|
|
9
14
|
let $from = state.selection.$from;
|
|
10
15
|
for (let d = $from.depth; d >= 0; d--) {
|
|
@@ -240,10 +245,6 @@ export function buildMenu(editor, schema) {
|
|
|
240
245
|
selectParentNodeItem,
|
|
241
246
|
]),
|
|
242
247
|
];
|
|
243
|
-
r.fullMenu = r.inlineMenu.concat([[r.insertMenu, r.typeMenu]], [[
|
|
244
|
-
undoItem,
|
|
245
|
-
redoItem,
|
|
246
|
-
]], r.blockMenu);
|
|
247
248
|
*/
|
|
248
249
|
menu.push(new MenuItem({
|
|
249
250
|
title: 'Undo last change',
|
|
@@ -257,33 +258,40 @@ export function buildMenu(editor, schema) {
|
|
|
257
258
|
enable: () => editor.can().redo().run(),
|
|
258
259
|
icon: icons.redo,
|
|
259
260
|
}));
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
261
|
+
if (schema.nodes.table) {
|
|
262
|
+
const item = (label, cmdName) => {
|
|
263
|
+
return new MenuItem({ label, enable: () => editor.can()[cmdName]().run(), run: () => editor.chain()[cmdName]().run() });
|
|
264
|
+
};
|
|
265
|
+
const tableMenu = [
|
|
266
|
+
item('Insert table', 'insertTable'),
|
|
267
|
+
item('Insert column before', 'addColumnBefore'),
|
|
268
|
+
item('Insert column after', 'addColumnAfter'),
|
|
269
|
+
item('Delete column', 'deleteColumn'),
|
|
270
|
+
item('Insert row before', 'addRowBefore'),
|
|
271
|
+
item('Insert row after', 'addRowAfter'),
|
|
272
|
+
item('Delete row', 'deleteRow'),
|
|
273
|
+
item('Delete table', 'deleteTable'),
|
|
274
|
+
item('Merge cells', 'mergeCells'),
|
|
275
|
+
item('Split cell', 'splitCell'),
|
|
276
|
+
item('Toggle header column', 'toggleHeaderColumn'),
|
|
277
|
+
item('Toggle header row', 'toggleHeaderRow'),
|
|
278
|
+
item('Toggle header cells', 'toggleHeaderCell'),
|
|
279
|
+
// item('Make cell green', setCellAttr('background', '#dfd')),
|
|
280
|
+
// item('Make cell not-green', setCellAttr('background', null)),
|
|
281
|
+
];
|
|
282
|
+
menu.push(new Dropdown(tableMenu, { label: 'Table' }));
|
|
263
283
|
}
|
|
264
|
-
const tableMenu = [
|
|
265
|
-
item('Insert column before', addColumnBefore),
|
|
266
|
-
item('Insert column after', addColumnAfter),
|
|
267
|
-
item('Delete column', deleteColumn),
|
|
268
|
-
item('Insert row before', addRowBefore),
|
|
269
|
-
item('Insert row after', addRowAfter),
|
|
270
|
-
item('Delete row', deleteRow),
|
|
271
|
-
item('Delete table', deleteTable),
|
|
272
|
-
item('Merge cells', mergeCells),
|
|
273
|
-
item('Split cell', splitCell),
|
|
274
|
-
item('Toggle header column', toggleHeaderColumn),
|
|
275
|
-
item('Toggle header row', toggleHeaderRow),
|
|
276
|
-
item('Toggle header cells', toggleHeaderCell),
|
|
277
|
-
item('Make cell green', setCellAttr('background', '#dfd')),
|
|
278
|
-
item('Make cell not-green', setCellAttr('background', null)),
|
|
279
|
-
];
|
|
280
|
-
menu.splice(2, 0, [new Dropdown(tableMenu, { label: 'Table' })]);
|
|
281
|
-
*/
|
|
282
284
|
return [menu, blockMenu];
|
|
283
285
|
}
|
|
284
286
|
export class ExtensionMenu extends Extension {
|
|
285
|
-
constructor() {
|
|
286
|
-
super(
|
|
287
|
+
constructor(config = { floating: true }) {
|
|
288
|
+
super(config);
|
|
289
|
+
Object.defineProperty(this, "config", {
|
|
290
|
+
enumerable: true,
|
|
291
|
+
configurable: true,
|
|
292
|
+
writable: true,
|
|
293
|
+
value: config
|
|
294
|
+
});
|
|
287
295
|
Object.defineProperty(this, "name", {
|
|
288
296
|
enumerable: true,
|
|
289
297
|
configurable: true,
|
|
@@ -292,10 +300,14 @@ export class ExtensionMenu extends Extension {
|
|
|
292
300
|
});
|
|
293
301
|
}
|
|
294
302
|
getProseMirrorPlugins(editor, schema) {
|
|
303
|
+
let content = buildMenu(editor, schema);
|
|
304
|
+
if (this.config.modifyMenu) {
|
|
305
|
+
content = this.config.modifyMenu(content);
|
|
306
|
+
}
|
|
295
307
|
return [
|
|
296
308
|
new MenuPlugin({
|
|
297
|
-
content
|
|
298
|
-
floating:
|
|
309
|
+
content,
|
|
310
|
+
floating: this.config.floating,
|
|
299
311
|
}),
|
|
300
312
|
];
|
|
301
313
|
}
|