@kerebron/editor 0.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.
Files changed (52) hide show
  1. package/LICENSE +23 -0
  2. package/README.md +21 -0
  3. package/esm/CoreEditor.d.ts +29 -0
  4. package/esm/CoreEditor.d.ts.map +1 -0
  5. package/esm/CoreEditor.js +162 -0
  6. package/esm/Extension.d.ts +26 -0
  7. package/esm/Extension.d.ts.map +1 -0
  8. package/esm/Extension.js +33 -0
  9. package/esm/ExtensionManager.d.ts +32 -0
  10. package/esm/ExtensionManager.d.ts.map +1 -0
  11. package/esm/ExtensionManager.js +253 -0
  12. package/esm/Mark.d.ts +18 -0
  13. package/esm/Mark.d.ts.map +1 -0
  14. package/esm/Mark.js +34 -0
  15. package/esm/Node.d.ts +27 -0
  16. package/esm/Node.d.ts.map +1 -0
  17. package/esm/Node.js +43 -0
  18. package/esm/commands/CommandManager.d.ts +20 -0
  19. package/esm/commands/CommandManager.d.ts.map +1 -0
  20. package/esm/commands/CommandManager.js +60 -0
  21. package/esm/commands/createChainableState.d.ts +3 -0
  22. package/esm/commands/createChainableState.d.ts.map +1 -0
  23. package/esm/commands/createChainableState.js +29 -0
  24. package/esm/commands/mod.d.ts +49 -0
  25. package/esm/commands/mod.d.ts.map +1 -0
  26. package/esm/commands/mod.js +928 -0
  27. package/esm/mod.d.ts +5 -0
  28. package/esm/mod.d.ts.map +1 -0
  29. package/esm/mod.js +4 -0
  30. package/esm/package.json +3 -0
  31. package/esm/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
  32. package/esm/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
  33. package/esm/plugins/input-rules/InputRulesPlugin.js +163 -0
  34. package/esm/plugins/input-rules/mod.d.ts +3 -0
  35. package/esm/plugins/input-rules/mod.d.ts.map +1 -0
  36. package/esm/plugins/input-rules/mod.js +2 -0
  37. package/esm/plugins/input-rules/rulebuilders.d.ts +5 -0
  38. package/esm/plugins/input-rules/rulebuilders.d.ts.map +1 -0
  39. package/esm/plugins/input-rules/rulebuilders.js +50 -0
  40. package/esm/types.d.ts +29 -0
  41. package/esm/types.d.ts.map +1 -0
  42. package/esm/types.js +1 -0
  43. package/esm/utilities/createNodeFromContent.d.ts +12 -0
  44. package/esm/utilities/createNodeFromContent.d.ts.map +1 -0
  45. package/esm/utilities/createNodeFromContent.js +118 -0
  46. package/esm/utilities/getHtmlAttributes.d.ts +4 -0
  47. package/esm/utilities/getHtmlAttributes.d.ts.map +1 -0
  48. package/esm/utilities/getHtmlAttributes.js +47 -0
  49. package/esm/utilities/mod.d.ts +3 -0
  50. package/esm/utilities/mod.d.ts.map +1 -0
  51. package/esm/utilities/mod.js +2 -0
  52. package/package.json +24 -0
package/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Permission is hereby granted, free of charge, to any
2
+ person obtaining a copy of this software and associated
3
+ documentation files (the "Software"), to deal in the
4
+ Software without restriction, including without
5
+ limitation the rights to use, copy, modify, merge,
6
+ publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software
8
+ is furnished to do so, subject to the following
9
+ conditions:
10
+
11
+ The above copyright notice and this permission notice
12
+ shall be included in all copies or substantial portions
13
+ of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16
+ ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17
+ TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
18
+ PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
19
+ SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
22
+ IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
+ DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Kerebron core editor
2
+
3
+ Core editor is basic agnostic. All you need is an HTML element.
4
+
5
+ ## Usage
6
+
7
+ ```js
8
+ import { CoreEditor } from '@kerebron/editor';
9
+ import { ExtensionBasicEditor } from '@kerebron/extension-basic-editor';
10
+ import { ExtensionHistory } from '@kerebron/extension-basic-editor/ExtensionHistory';
11
+ import { ExtensionMenu } from '@kerebron/extension-menu';
12
+
13
+ this.editor = new CoreEditor({
14
+ element: document.querySelector('div#editor'),
15
+ extensions: [
16
+ new ExtensionBasicEditor(),
17
+ new ExtensionHistory(),
18
+ new ExtensionMenu(),
19
+ ],
20
+ });
21
+ ```
@@ -0,0 +1,29 @@
1
+ import { EditorView } from 'prosemirror-view';
2
+ import { Fragment, type Schema } from 'prosemirror-model';
3
+ import type { EditorOptions, JSONContent } from './types.js';
4
+ import { EditorState } from 'prosemirror-state';
5
+ import { ChainedCommands } from './commands/CommandManager.js';
6
+ export declare function getHTMLFromFragment(fragment: Fragment, schema: Schema): string;
7
+ export declare class CoreEditor extends EventTarget {
8
+ readonly options: Partial<EditorOptions>;
9
+ private extensionManager;
10
+ private commandManager;
11
+ view: EditorView;
12
+ constructor(options?: Partial<EditorOptions>);
13
+ private createExtensionManager;
14
+ get schema(): Schema;
15
+ get state(): EditorState;
16
+ chain(): ChainedCommands;
17
+ can(): ChainedCommands;
18
+ private createView;
19
+ private dispatchTransaction;
20
+ private setupPlugins;
21
+ getJSON(): JSONContent;
22
+ /**
23
+ * Get the document as HTML.
24
+ */
25
+ getHTML(): string;
26
+ setDocument(content?: any, mediaType?: string): void;
27
+ getDocument(mediaType?: string): any;
28
+ }
29
+ //# sourceMappingURL=CoreEditor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CoreEditor.d.ts","sourceRoot":"","sources":["../src/CoreEditor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAEL,QAAQ,EAGR,KAAK,MAAM,EACZ,MAAM,mBAAmB,CAAC;AAG3B,OAAO,KAAK,EAAW,aAAa,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AACtE,OAAO,EAAE,WAAW,EAAe,MAAM,mBAAmB,CAAC;AAE7D,OAAO,EAAE,eAAe,EAAkB,MAAM,8BAA8B,CAAC;AAE/E,wBAAgB,mBAAmB,CACjC,QAAQ,EAAE,QAAQ,EAClB,MAAM,EAAE,MAAM,GACb,MAAM,CAWR;AAeD,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;gBAEb,OAAO,GAAE,OAAO,CAAC,aAAa,CAAM;IA0BhD,OAAO,CAAC,sBAAsB;IAI9B,IAAW,MAAM,WAEhB;IAED,IAAW,KAAK,IAAI,WAAW,CAE9B;IAEM,KAAK,IAAI,eAAe;IAIxB,GAAG,IAAI,eAAe;IAI7B,OAAO,CAAC,UAAU;IAgBlB,OAAO,CAAC,mBAAmB;IAY3B,OAAO,CAAC,YAAY;IAYb,OAAO,IAAI,WAAW;IAI7B;;OAEG;IACI,OAAO,IAAI,MAAM;IAIjB,WAAW,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,SAAS,CAAC,EAAE,MAAM;IA0C7C,WAAW,CAAC,SAAS,CAAC,EAAE,MAAM;CAUtC"}
@@ -0,0 +1,162 @@
1
+ import { EditorView } from 'prosemirror-view';
2
+ import { DOMSerializer, } from 'prosemirror-model';
3
+ import { ExtensionManager } from './ExtensionManager.js';
4
+ import { EditorState } from 'prosemirror-state';
5
+ import { createNodeFromContent } from './utilities/createNodeFromContent.js';
6
+ import { CommandManager } from './commands/CommandManager.js';
7
+ export function getHTMLFromFragment(fragment, schema) {
8
+ const documentFragment = DOMSerializer.fromSchema(schema).serializeFragment(fragment);
9
+ const temporaryDocument = document.implementation.createHTMLDocument();
10
+ const container = temporaryDocument.createElement('div');
11
+ container.appendChild(documentFragment);
12
+ return container.innerHTML;
13
+ }
14
+ function createDocument(content, schema, parseOptions = {}, options = {}) {
15
+ return createNodeFromContent(content, schema, {
16
+ slice: false,
17
+ parseOptions,
18
+ errorOnInvalidContent: options.errorOnInvalidContent,
19
+ });
20
+ }
21
+ export class CoreEditor extends EventTarget {
22
+ constructor(options = {}) {
23
+ super();
24
+ Object.defineProperty(this, "options", {
25
+ enumerable: true,
26
+ configurable: true,
27
+ writable: true,
28
+ value: {
29
+ element: document.createElement('div'),
30
+ extensions: [],
31
+ }
32
+ });
33
+ Object.defineProperty(this, "extensionManager", {
34
+ enumerable: true,
35
+ configurable: true,
36
+ writable: true,
37
+ value: void 0
38
+ });
39
+ Object.defineProperty(this, "commandManager", {
40
+ enumerable: true,
41
+ configurable: true,
42
+ writable: true,
43
+ value: void 0
44
+ });
45
+ Object.defineProperty(this, "view", {
46
+ enumerable: true,
47
+ configurable: true,
48
+ writable: true,
49
+ value: void 0
50
+ });
51
+ this.options = {
52
+ ...this.options,
53
+ ...options,
54
+ };
55
+ this.extensionManager = this.createExtensionManager();
56
+ // const content = this.options.content ? this.options.content : {
57
+ // type: this.extensionManager.schema.topNodeType.name,
58
+ // content: this.extensionManager.schema.topNodeType.spec.EMPTY_DOC,
59
+ // };
60
+ const content = this.options.content
61
+ ? this.options.content
62
+ : this.extensionManager.schema.topNodeType.spec.EMPTY_DOC;
63
+ this.createView(structuredClone(content));
64
+ // this.createView('');
65
+ this.commandManager = new CommandManager(this, this.extensionManager.commandConstructors);
66
+ this.setupPlugins();
67
+ }
68
+ createExtensionManager() {
69
+ return new ExtensionManager(this.options.extensions, this);
70
+ }
71
+ get schema() {
72
+ return this.extensionManager.schema;
73
+ }
74
+ get state() {
75
+ return this.view.state;
76
+ }
77
+ chain() {
78
+ return this.commandManager.chain();
79
+ }
80
+ can() {
81
+ return this.commandManager.can();
82
+ }
83
+ createView(content) {
84
+ let doc = createDocument(content, this.schema, this.options.parseOptions, { errorOnInvalidContent: false });
85
+ const state = EditorState.create({ doc });
86
+ this.view = new EditorView(this.options.element, {
87
+ state,
88
+ dispatchTransaction: (tx) => this.dispatchTransaction(tx),
89
+ });
90
+ }
91
+ dispatchTransaction(transaction) {
92
+ const nextState = this.state.apply(transaction);
93
+ this.view.updateState(nextState);
94
+ const event = new CustomEvent('transaction', {
95
+ detail: {
96
+ editor: this,
97
+ transaction,
98
+ },
99
+ });
100
+ this.dispatchEvent(event);
101
+ }
102
+ setupPlugins() {
103
+ const newState = this.state.reconfigure({
104
+ plugins: this.extensionManager.plugins,
105
+ });
106
+ this.view.updateState(newState);
107
+ this.view.setProps({
108
+ nodeViews: this.extensionManager.nodeViews,
109
+ });
110
+ }
111
+ getJSON() {
112
+ return this.state.doc.toJSON();
113
+ }
114
+ /**
115
+ * Get the document as HTML.
116
+ */
117
+ getHTML() {
118
+ return getHTMLFromFragment(this.state.doc.content, this.schema);
119
+ }
120
+ setDocument(content, mediaType) {
121
+ if (!content) { // clear
122
+ content = {
123
+ type: this.extensionManager.schema.topNodeType.name,
124
+ content: this.extensionManager.schema.topNodeType.spec.EMPTY_DOC.content,
125
+ // content: this.extensionManager.schema.topNodeType.createAndFill(),
126
+ };
127
+ mediaType = undefined;
128
+ }
129
+ let doc;
130
+ if (mediaType) {
131
+ const converter = this.extensionManager.converters[mediaType];
132
+ if (converter) {
133
+ doc = converter.toDoc(content);
134
+ }
135
+ }
136
+ else {
137
+ doc = createDocument(content, this.schema, this.options.parseOptions, { errorOnInvalidContent: false });
138
+ }
139
+ const newState = EditorState.create({
140
+ doc,
141
+ plugins: this.view.state.plugins,
142
+ storedMarks: this.view.state.storedMarks,
143
+ });
144
+ this.view.updateState(newState);
145
+ const event = new CustomEvent('doc:loaded', {
146
+ detail: {
147
+ editor: this,
148
+ doc,
149
+ },
150
+ });
151
+ this.dispatchEvent(event);
152
+ }
153
+ getDocument(mediaType) {
154
+ if (mediaType) {
155
+ const converter = this.extensionManager.converters[mediaType];
156
+ if (converter) {
157
+ return converter.fromDoc(this.state.doc);
158
+ }
159
+ }
160
+ return this.state.doc;
161
+ }
162
+ }
@@ -0,0 +1,26 @@
1
+ import { Plugin } from 'prosemirror-state';
2
+ import { type CoreEditor } from './CoreEditor.js';
3
+ import { InputRule } from './plugins/input-rules/InputRulesPlugin.js';
4
+ import { Commands, CommandShortcuts } from './commands/mod.js';
5
+ import { Schema, type SchemaSpec } from 'prosemirror-model';
6
+ export interface ExtensionConfig {
7
+ [key: string]: any;
8
+ requires: Array<Extension | string>;
9
+ }
10
+ export interface Converter {
11
+ fromDoc(document: unknown): void;
12
+ toDoc(content: unknown): any;
13
+ }
14
+ export declare abstract class Extension {
15
+ protected config: Partial<ExtensionConfig>;
16
+ readonly type = "extension";
17
+ abstract name: string;
18
+ protected constructor(config?: Partial<ExtensionConfig>);
19
+ getInputRules(): InputRule[];
20
+ getProseMirrorPlugins(editor: CoreEditor, schema: Schema): Plugin[];
21
+ getCommands(editor: CoreEditor): Partial<Commands>;
22
+ getKeyboardShortcuts(): Partial<CommandShortcuts>;
23
+ getConverters(): Record<string, Converter>;
24
+ setupSpec(spec: SchemaSpec): void;
25
+ }
26
+ //# sourceMappingURL=Extension.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Extension.d.ts","sourceRoot":"","sources":["../src/Extension.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,2CAA2C,CAAC;AACtE,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,MAAM,EAAE,KAAK,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE5D,MAAM,WAAW,eAAe;IAE9B,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;IAEnB,QAAQ,EAAE,KAAK,CAAC,SAAS,GAAG,MAAM,CAAC,CAAC;CACrC;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI,CAAC;IACjC,KAAK,CAAC,OAAO,EAAE,OAAO,GAAG,GAAG,CAAC;CAC9B;AAED,8BAAsB,SAAS;IAIP,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC;IAHhE,QAAQ,CAAC,IAAI,eAAe;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,SAAS,aAAuB,MAAM,GAAE,OAAO,CAAC,eAAe,CAAM;IAGrE,aAAa,IAAI,SAAS,EAAE;IAI5B,qBAAqB,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE;IAInE,WAAW,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC;IAIlD,oBAAoB,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAIjD,aAAa,IAAI,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC;IAI1C,SAAS,CAAC,IAAI,EAAE,UAAU;CAE3B"}
@@ -0,0 +1,33 @@
1
+ export class Extension {
2
+ constructor(config = {}) {
3
+ Object.defineProperty(this, "config", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: config
8
+ });
9
+ Object.defineProperty(this, "type", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: 'extension'
14
+ });
15
+ }
16
+ getInputRules() {
17
+ return [];
18
+ }
19
+ getProseMirrorPlugins(editor, schema) {
20
+ return [];
21
+ }
22
+ getCommands(editor) {
23
+ return {};
24
+ }
25
+ getKeyboardShortcuts() {
26
+ return {};
27
+ }
28
+ getConverters() {
29
+ return {};
30
+ }
31
+ setupSpec(spec) {
32
+ }
33
+ }
@@ -0,0 +1,32 @@
1
+ import { Schema } from 'prosemirror-model';
2
+ import { Plugin } from 'prosemirror-state';
3
+ import { NodeViewConstructor } from 'prosemirror-view';
4
+ import { Extension } from './Extension.js';
5
+ import { AnyExtension } from './types.js';
6
+ import { CoreEditor } from './CoreEditor.js';
7
+ import { Mark } from './Mark.js';
8
+ import { Node } from './Node.js';
9
+ import { type Command } from 'prosemirror-state';
10
+ export declare function findDuplicates(items: any[]): any[];
11
+ export declare function splitExtensions(extensions: Iterable<AnyExtension>): {
12
+ baseExtensions: Extension[];
13
+ nodeExtensions: Node[];
14
+ markExtensions: Mark[];
15
+ };
16
+ export declare class ExtensionManager {
17
+ private editor;
18
+ readonly schema: Schema;
19
+ private extensions;
20
+ readonly plugins: Plugin[];
21
+ readonly nodeViews: Record<string, NodeViewConstructor>;
22
+ readonly commandConstructors: {
23
+ [key: string]: () => Command;
24
+ };
25
+ private converters;
26
+ private debug;
27
+ constructor(extensions: Set<AnyExtension>, editor: CoreEditor);
28
+ private getPlugins;
29
+ private setupExtensions;
30
+ getSchemaByResolvedExtensions(editor: CoreEditor): Schema;
31
+ }
32
+ //# sourceMappingURL=ExtensionManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ExtensionManager.d.ts","sourceRoot":"","sources":["../src/ExtensionManager.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,EAAa,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAMjC,OAAO,EAAE,KAAK,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAGjD,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,GAAG,EAAE,CAIlD;AAED,wBAAgB,eAAe,CAAC,UAAU,EAAE,QAAQ,CAAC,YAAY,CAAC;;;;EAgBjE;AAED,qBAAa,gBAAgB;IAYgB,OAAO,CAAC,MAAM;IAXzD,SAAgB,MAAM,EAAE,MAAM,CAAC;IAE/B,OAAO,CAAC,UAAU,CAAgC;IAClD,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAC3B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,mBAAmB,CAAC,CAAM;IAE7D,QAAQ,CAAC,mBAAmB,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,OAAO,CAAA;KAAE,CAAM;IACpE,OAAO,CAAC,UAAU,CAAiC;IAEnD,OAAO,CAAC,KAAK,CAAQ;gBAET,UAAU,EAAE,GAAG,CAAC,YAAY,CAAC,EAAU,MAAM,EAAE,UAAU;IAcrE,OAAO,CAAC,UAAU;IAkIlB,OAAO,CAAC,eAAe;IA6DvB,6BAA6B,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM;CA6C1D"}
@@ -0,0 +1,253 @@
1
+ import { Schema } from 'prosemirror-model';
2
+ import { keymap } from 'prosemirror-keymap';
3
+ import { InputRulesPlugin, } from './plugins/input-rules/InputRulesPlugin.js';
4
+ import { chainCommands } from './commands/mod.js';
5
+ import { addAttributesToSchema } from './utilities/getHtmlAttributes.js';
6
+ export function findDuplicates(items) {
7
+ const filtered = items.filter((el, index) => items.indexOf(el) !== index);
8
+ return Array.from(new Set(filtered));
9
+ }
10
+ export function splitExtensions(extensions) {
11
+ const baseExtensions = Array.from(extensions).filter((extension) => extension.type === 'extension');
12
+ const nodeExtensions = Array.from(extensions).filter((extension) => extension.type === 'node');
13
+ const markExtensions = Array.from(extensions).filter((extension) => extension.type === 'mark');
14
+ return {
15
+ baseExtensions,
16
+ nodeExtensions,
17
+ markExtensions,
18
+ };
19
+ }
20
+ export class ExtensionManager {
21
+ constructor(extensions, editor) {
22
+ Object.defineProperty(this, "editor", {
23
+ enumerable: true,
24
+ configurable: true,
25
+ writable: true,
26
+ value: editor
27
+ });
28
+ Object.defineProperty(this, "schema", {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ value: void 0
33
+ });
34
+ Object.defineProperty(this, "extensions", {
35
+ enumerable: true,
36
+ configurable: true,
37
+ writable: true,
38
+ value: new Set()
39
+ });
40
+ Object.defineProperty(this, "plugins", {
41
+ enumerable: true,
42
+ configurable: true,
43
+ writable: true,
44
+ value: void 0
45
+ });
46
+ Object.defineProperty(this, "nodeViews", {
47
+ enumerable: true,
48
+ configurable: true,
49
+ writable: true,
50
+ value: {}
51
+ });
52
+ Object.defineProperty(this, "commandConstructors", {
53
+ enumerable: true,
54
+ configurable: true,
55
+ writable: true,
56
+ value: {}
57
+ });
58
+ Object.defineProperty(this, "converters", {
59
+ enumerable: true,
60
+ configurable: true,
61
+ writable: true,
62
+ value: {}
63
+ });
64
+ Object.defineProperty(this, "debug", {
65
+ enumerable: true,
66
+ configurable: true,
67
+ writable: true,
68
+ value: true
69
+ });
70
+ this.setupExtensions(extensions);
71
+ this.schema = this.getSchemaByResolvedExtensions(editor);
72
+ const event = new CustomEvent('schema:ready', {
73
+ detail: {
74
+ editor,
75
+ schema: this.schema,
76
+ },
77
+ });
78
+ editor.dispatchEvent(event);
79
+ this.plugins = this.getPlugins();
80
+ }
81
+ getPlugins() {
82
+ const plugins = [];
83
+ const inputRules = [];
84
+ const commands = new Map();
85
+ const keyBindings = new Map();
86
+ const mergeCommands = (toInsert, extName) => {
87
+ for (const key in toInsert) {
88
+ const commandConstructor = toInsert[key];
89
+ if (this.debug) {
90
+ const wrappedConstructor = () => {
91
+ const realCommand = commandConstructor();
92
+ const command = (state, dispatch, view) => {
93
+ if (dispatch) {
94
+ console.debug(`Command: ${extName}.${key}`);
95
+ }
96
+ return realCommand(state, dispatch, view);
97
+ };
98
+ return command;
99
+ };
100
+ commands.set(key, wrappedConstructor);
101
+ this.commandConstructors[key] = wrappedConstructor;
102
+ }
103
+ else {
104
+ commands.set(key, commandConstructor);
105
+ this.commandConstructors[key] = commandConstructor;
106
+ }
107
+ }
108
+ };
109
+ function mergeShortcuts(toInsert, extName) {
110
+ for (const key in toInsert) {
111
+ const commandConstructor = commands.get(toInsert[key]);
112
+ if (!commandConstructor) {
113
+ console.warn(`No command constructor: ${toInsert[key]}`);
114
+ continue;
115
+ }
116
+ const command = commandConstructor();
117
+ const keyBinding = keyBindings.get(key);
118
+ if (keyBinding) {
119
+ keyBindings.set(key, chainCommands(keyBinding, command));
120
+ }
121
+ else {
122
+ keyBindings.set(key, command);
123
+ }
124
+ }
125
+ }
126
+ let converters = {};
127
+ for (const extension of this.extensions) {
128
+ if (extension.type === 'node') {
129
+ const nodeType = this.schema.nodes[extension.name];
130
+ inputRules.push(...extension.getInputRules(nodeType));
131
+ plugins.push(...extension.getProseMirrorPlugins(this.editor, this.schema));
132
+ mergeCommands(extension.getCommands(this.editor, nodeType), extension.name);
133
+ mergeShortcuts(extension.getKeyboardShortcuts(this.editor), extension.name);
134
+ converters = {
135
+ ...converters,
136
+ ...extension.getConverters(this.editor, this.schema),
137
+ };
138
+ const nodeView = extension.getNodeView();
139
+ if (nodeView) {
140
+ this.nodeViews[extension.name] = nodeView;
141
+ }
142
+ }
143
+ if (extension.type === 'mark') {
144
+ const markType = this.schema.marks[extension.name];
145
+ inputRules.push(...extension.getInputRules(markType));
146
+ mergeCommands(extension.getCommands(this.editor, markType), extension.name);
147
+ mergeShortcuts(extension.getKeyboardShortcuts(this.editor), extension.name);
148
+ }
149
+ if (extension.type === 'extension') {
150
+ plugins.push(...extension.getProseMirrorPlugins(this.editor, this.schema));
151
+ mergeCommands(extension.getCommands(this.editor), extension.name);
152
+ mergeShortcuts(extension.getKeyboardShortcuts(this.editor), extension.name);
153
+ converters = {
154
+ ...converters,
155
+ ...extension.getConverters(this.editor, this.schema),
156
+ };
157
+ }
158
+ }
159
+ if (this.debug) {
160
+ for (const key in keyBindings) {
161
+ const wrapperCommand = (state, dispatch, view) => {
162
+ console.debug(`Key: ${key}`);
163
+ return true;
164
+ };
165
+ keyBindings.set(key, chainCommands(wrapperCommand, keyBindings.get(key)));
166
+ }
167
+ }
168
+ this.converters = converters;
169
+ plugins.push(new InputRulesPlugin(inputRules));
170
+ plugins.push(keymap(Object.fromEntries(keyBindings)));
171
+ return plugins;
172
+ }
173
+ setupExtensions(extensions) {
174
+ const allExtensions = new Map();
175
+ const createMap = (extensions) => {
176
+ for (const extension of extensions) {
177
+ allExtensions.set(extension.name, extension);
178
+ if (extension.requires) {
179
+ const childExtensions = Array.from(extension.requires).filter((e) => typeof e !== 'string');
180
+ createMap(new Set(childExtensions));
181
+ }
182
+ }
183
+ };
184
+ createMap(extensions);
185
+ const initialized = new Set();
186
+ const initializeExtension = (extension) => {
187
+ console.info(`Initialize ${extension.type} ${extension.name}`);
188
+ this.extensions.add(extension);
189
+ };
190
+ function recursiveInitializeExtension(extension) {
191
+ if (initialized.has(extension.name)) {
192
+ return;
193
+ }
194
+ const requires = (extension.requires || []).map((e) => typeof e === 'string' ? e : e.name);
195
+ for (const require of requires) {
196
+ if (!initialized.has(require)) {
197
+ const requiredExtension = allExtensions.get(require);
198
+ if (!requiredExtension) {
199
+ throw new Error('Required extension not found: ' + require);
200
+ }
201
+ recursiveInitializeExtension(requiredExtension);
202
+ }
203
+ }
204
+ initializeExtension(extension);
205
+ initialized.add(extension.name);
206
+ allExtensions.delete(extension.name);
207
+ }
208
+ for (const extension of allExtensions.values()) {
209
+ recursiveInitializeExtension(extension);
210
+ }
211
+ if (allExtensions.size > 0) {
212
+ throw new Error('Not all extensions initialized: ' +
213
+ Array.from(allExtensions.keys()).join(', '));
214
+ }
215
+ }
216
+ getSchemaByResolvedExtensions(editor) {
217
+ const { nodeExtensions, markExtensions, baseExtensions } = splitExtensions(this.extensions);
218
+ const nodes = {};
219
+ for (const extension of nodeExtensions) {
220
+ nodes[extension.name] = extension.getNodeSpec();
221
+ addAttributesToSchema(nodes[extension.name], extension);
222
+ if ('automerge' in extension) {
223
+ nodes[extension.name].automerge = extension.automerge;
224
+ }
225
+ }
226
+ const marks = {};
227
+ for (const extension of markExtensions) {
228
+ marks[extension.name] = extension.getMarkSpec();
229
+ addAttributesToSchema(marks[extension.name], extension);
230
+ if ('automerge' in extension) {
231
+ marks[extension.name].automerge = extension.automerge;
232
+ }
233
+ }
234
+ const spec = {
235
+ topNode: this.editor.options.topNode || 'doc',
236
+ nodes,
237
+ marks,
238
+ };
239
+ for (const extension of baseExtensions) {
240
+ if ('setupSpec' in baseExtensions) {
241
+ baseExtensions.setupSpec(spec);
242
+ }
243
+ }
244
+ const event = new CustomEvent('schema:spec', {
245
+ detail: {
246
+ editor,
247
+ spec,
248
+ },
249
+ });
250
+ editor.dispatchEvent(event);
251
+ return new Schema(spec);
252
+ }
253
+ }
package/esm/Mark.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { MarkSpec, MarkType } from 'prosemirror-model';
2
+ import { InputRule } from './plugins/input-rules/InputRulesPlugin.js';
3
+ import { CoreEditor } from './CoreEditor.js';
4
+ import { Commands, CommandShortcuts } from './commands/mod.js';
5
+ export interface MarkConfig {
6
+ [key: string]: any;
7
+ }
8
+ export declare abstract class Mark {
9
+ protected config: Partial<MarkConfig>;
10
+ readonly type = "mark";
11
+ name: string;
12
+ constructor(config?: Partial<MarkConfig>);
13
+ getMarkSpec(): MarkSpec;
14
+ getInputRules(type: MarkType): InputRule[];
15
+ getCommands(editor: CoreEditor, type: MarkType): Partial<Commands>;
16
+ getKeyboardShortcuts(): Partial<CommandShortcuts>;
17
+ }
18
+ //# sourceMappingURL=Mark.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Mark.d.ts","sourceRoot":"","sources":["../src/Mark.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,SAAS,EAAE,MAAM,2CAA2C,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE/D,MAAM,WAAW,UAAU;IAEzB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,8BAAsB,IAAI;IAIL,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC;IAHxD,QAAQ,CAAC,IAAI,UAAU;IACvB,IAAI,EAAE,MAAM,CAAU;gBAEO,MAAM,GAAE,OAAO,CAAC,UAAU,CAAM;IAE7D,WAAW,IAAI,QAAQ;IAIvB,aAAa,CAAC,IAAI,EAAE,QAAQ,GAAG,SAAS,EAAE;IAI1C,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAIlE,oBAAoB,IAAI,OAAO,CAAC,gBAAgB,CAAC;CAGlD"}
package/esm/Mark.js ADDED
@@ -0,0 +1,34 @@
1
+ export class Mark {
2
+ constructor(config = {}) {
3
+ Object.defineProperty(this, "config", {
4
+ enumerable: true,
5
+ configurable: true,
6
+ writable: true,
7
+ value: config
8
+ });
9
+ Object.defineProperty(this, "type", {
10
+ enumerable: true,
11
+ configurable: true,
12
+ writable: true,
13
+ value: 'mark'
14
+ });
15
+ Object.defineProperty(this, "name", {
16
+ enumerable: true,
17
+ configurable: true,
18
+ writable: true,
19
+ value: 'node'
20
+ });
21
+ }
22
+ getMarkSpec() {
23
+ throw new Error('MarkSpec not defined: ' + this.name);
24
+ }
25
+ getInputRules(type) {
26
+ return [];
27
+ }
28
+ getCommands(editor, type) {
29
+ return {};
30
+ }
31
+ getKeyboardShortcuts() {
32
+ return {};
33
+ }
34
+ }