@kerebron/extension-basic-editor 0.4.31 → 0.5.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 +4 -4
- package/esm/BasicEditorKit.d.ts +5 -0
- package/esm/BasicEditorKit.d.ts.map +1 -0
- package/esm/BasicEditorKit.js +97 -0
- package/esm/BasicEditorKit.js.map +1 -0
- package/esm/ExtensionBasicEditor.d.ts +1 -44
- package/esm/ExtensionBasicEditor.d.ts.map +1 -1
- package/esm/ExtensionBasicEditor.js +3 -90
- package/esm/ExtensionBasicEditor.js.map +1 -1
- package/esm/NodeIframe.d.ts +8 -0
- package/esm/NodeIframe.d.ts.map +1 -0
- package/esm/NodeIframe.js +35 -0
- package/esm/NodeIframe.js.map +1 -0
- package/esm/NodeImage.d.ts +3 -0
- package/esm/NodeImage.d.ts.map +1 -1
- package/esm/NodeImage.js +190 -2
- package/esm/NodeImage.js.map +1 -1
- package/esm/NodeInlineShortCode.d.ts +3 -0
- package/esm/NodeInlineShortCode.d.ts.map +1 -1
- package/esm/NodeInlineShortCode.js +42 -4
- package/esm/NodeInlineShortCode.js.map +1 -1
- package/package.json +11 -5
- package/src/BasicEditorKit.ts +101 -0
- package/src/ExtensionBasicEditor.ts +3 -91
- package/src/NodeIframe.ts +37 -0
- package/src/NodeImage.ts +209 -2
- package/src/NodeInlineShortCode.ts +72 -7
|
@@ -1,11 +1,14 @@
|
|
|
1
|
-
import { Node as PmNode, NodeSpec, NodeType } from 'prosemirror-model';
|
|
1
|
+
import { Node as PmNode, NodeSpec, NodeType, Schema } from 'prosemirror-model';
|
|
2
|
+
import { EditorState, Transaction } from 'prosemirror-state';
|
|
3
|
+
|
|
2
4
|
import { Node } from '@kerebron/editor';
|
|
3
5
|
import {
|
|
4
6
|
type InputRule,
|
|
5
7
|
replaceInlineNode,
|
|
6
|
-
textblockTypeInputRule,
|
|
7
|
-
wrappingInputRule,
|
|
8
8
|
} from '@kerebron/editor/plugins/input-rules';
|
|
9
|
+
import { CoreEditor } from '@kerebron/editor';
|
|
10
|
+
import { CommandFactories } from '@kerebron/editor';
|
|
11
|
+
import { Command } from '@kerebron/editor/commands';
|
|
9
12
|
|
|
10
13
|
export function fixCharacters(text: string) {
|
|
11
14
|
return text
|
|
@@ -18,8 +21,35 @@ export function fixCharacters(text: string) {
|
|
|
18
21
|
.replace(/\u201c/g, '"');
|
|
19
22
|
}
|
|
20
23
|
|
|
24
|
+
type Factory = (oldNode: PmNode, schema: Schema) => PmNode;
|
|
25
|
+
|
|
26
|
+
function replaceAllNodesOfType(
|
|
27
|
+
tr: Transaction,
|
|
28
|
+
doc: PmNode,
|
|
29
|
+
oldType: NodeType,
|
|
30
|
+
factory: Factory,
|
|
31
|
+
) {
|
|
32
|
+
const replacements: Array<{ node: PmNode; pos: number }> = [];
|
|
33
|
+
|
|
34
|
+
doc.descendants((node, pos) => {
|
|
35
|
+
if (node.type === oldType) {
|
|
36
|
+
replacements.push({ node, pos });
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
console.log('replaceAllNodesOfType', replacements.length);
|
|
41
|
+
|
|
42
|
+
for (let i = replacements.length - 1; i >= 0; i--) {
|
|
43
|
+
const { node, pos } = replacements[i];
|
|
44
|
+
const newNode = factory(node, tr.doc.type.schema);
|
|
45
|
+
tr = tr.replaceWith(pos, pos + node.nodeSize, newNode);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return tr;
|
|
49
|
+
}
|
|
50
|
+
|
|
21
51
|
export class NodeInlineShortCode extends Node {
|
|
22
|
-
override name = '
|
|
52
|
+
override name = 'shortcode_inline';
|
|
23
53
|
requires = ['doc'];
|
|
24
54
|
|
|
25
55
|
override getNodeSpec(): NodeSpec {
|
|
@@ -28,12 +58,21 @@ export class NodeInlineShortCode extends Node {
|
|
|
28
58
|
group: 'inline',
|
|
29
59
|
selectable: true,
|
|
30
60
|
attrs: {
|
|
31
|
-
content: {
|
|
61
|
+
content: {
|
|
62
|
+
default: '',
|
|
63
|
+
},
|
|
32
64
|
},
|
|
33
65
|
atom: true,
|
|
34
|
-
parseDOM: [{
|
|
66
|
+
parseDOM: [{
|
|
67
|
+
tag: 'span.kb-shortcode-inline',
|
|
68
|
+
getAttrs: (dom) => ({ content: dom.textContent || null }),
|
|
69
|
+
}],
|
|
35
70
|
toDOM(node) {
|
|
36
|
-
return [
|
|
71
|
+
return [
|
|
72
|
+
'span',
|
|
73
|
+
{ class: 'kb-shortcode-inline' },
|
|
74
|
+
node.attrs.content || '',
|
|
75
|
+
];
|
|
37
76
|
},
|
|
38
77
|
};
|
|
39
78
|
}
|
|
@@ -52,4 +91,30 @@ export class NodeInlineShortCode extends Node {
|
|
|
52
91
|
),
|
|
53
92
|
];
|
|
54
93
|
}
|
|
94
|
+
|
|
95
|
+
override getCommandFactories(
|
|
96
|
+
editor: CoreEditor,
|
|
97
|
+
type: NodeType,
|
|
98
|
+
): Partial<CommandFactories> {
|
|
99
|
+
return {
|
|
100
|
+
'renderShortCode': (createReplacementNode: Factory): Command => {
|
|
101
|
+
return (state: EditorState, dispatch?: (tr: Transaction) => void) => {
|
|
102
|
+
let tr = state.tr;
|
|
103
|
+
|
|
104
|
+
tr = replaceAllNodesOfType(
|
|
105
|
+
tr,
|
|
106
|
+
state.doc,
|
|
107
|
+
type,
|
|
108
|
+
createReplacementNode,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (tr.docChanged && dispatch) {
|
|
112
|
+
dispatch(tr);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return true;
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
55
120
|
}
|