@kerebron/extension-basic-editor 0.5.3 → 0.5.4
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/BasicEditorKit.d.ts.map +1 -1
- package/esm/BasicEditorKit.js +5 -1
- package/esm/BasicEditorKit.js.map +1 -1
- package/esm/ExtensionBasicCodeEditor.d.ts.map +1 -1
- package/esm/ExtensionBasicCodeEditor.js +4 -2
- package/esm/ExtensionBasicCodeEditor.js.map +1 -1
- package/esm/MarkLink.d.ts.map +1 -1
- package/esm/MarkLink.js +3 -1
- package/esm/MarkLink.js.map +1 -1
- package/esm/MarkUnderline.d.ts.map +1 -1
- package/esm/MarkUnderline.js +0 -5
- package/esm/MarkUnderline.js.map +1 -1
- package/esm/NodeBookmark.d.ts.map +1 -1
- package/esm/NodeBookmark.js +5 -1
- package/esm/NodeBookmark.js.map +1 -1
- package/esm/NodeBulletList.d.ts +8 -0
- package/esm/NodeBulletList.d.ts.map +1 -1
- package/esm/NodeBulletList.js +8 -0
- package/esm/NodeBulletList.js.map +1 -1
- package/esm/NodeCodeBlock.js +1 -1
- package/esm/NodeCodeBlock.js.map +1 -1
- package/esm/NodeCommentAnchor.d.ts +15 -0
- package/esm/NodeCommentAnchor.d.ts.map +1 -0
- package/esm/NodeCommentAnchor.js +133 -0
- package/esm/NodeCommentAnchor.js.map +1 -0
- package/esm/NodeHorizontalRule.d.ts.map +1 -1
- package/esm/NodeHorizontalRule.js +2 -1
- package/esm/NodeHorizontalRule.js.map +1 -1
- package/esm/NodeImage.d.ts.map +1 -1
- package/esm/NodeImage.js +2 -0
- package/esm/NodeImage.js.map +1 -1
- package/esm/NodeInlineShortCode.d.ts +1 -3
- package/esm/NodeInlineShortCode.d.ts.map +1 -1
- package/esm/NodeInlineShortCode.js +11 -2
- package/esm/NodeInlineShortCode.js.map +1 -1
- package/esm/NodeOrderedList.d.ts +3 -0
- package/esm/NodeOrderedList.d.ts.map +1 -1
- package/esm/NodeOrderedList.js +3 -0
- package/esm/NodeOrderedList.js.map +1 -1
- package/esm/NodeParagraph.d.ts +1 -0
- package/esm/NodeParagraph.d.ts.map +1 -1
- package/esm/NodeParagraph.js +4 -4
- package/esm/NodeParagraph.js.map +1 -1
- package/esm/pairing/ExtensionPairing.d.ts +7 -0
- package/esm/pairing/ExtensionPairing.d.ts.map +1 -0
- package/esm/pairing/ExtensionPairing.js +11 -0
- package/esm/pairing/ExtensionPairing.js.map +1 -0
- package/esm/pairing/PairNodesPlugin.d.ts +3 -0
- package/esm/pairing/PairNodesPlugin.d.ts.map +1 -0
- package/esm/pairing/PairNodesPlugin.js +45 -0
- package/esm/pairing/PairNodesPlugin.js.map +1 -0
- package/esm/pairing/pairNodes.d.ts +5 -0
- package/esm/pairing/pairNodes.d.ts.map +1 -0
- package/esm/pairing/pairNodes.js +81 -0
- package/esm/pairing/pairNodes.js.map +1 -0
- package/package.json +2 -2
- package/src/BasicEditorKit.ts +5 -1
- package/src/ExtensionBasicCodeEditor.ts +4 -2
- package/src/MarkLink.ts +3 -1
- package/src/MarkUnderline.ts +0 -7
- package/src/NodeBookmark.ts +5 -1
- package/src/NodeBulletList.ts +9 -0
- package/src/NodeCodeBlock.ts +1 -1
- package/src/NodeCommentAnchor.ts +170 -0
- package/src/NodeHorizontalRule.ts +2 -1
- package/src/NodeImage.ts +2 -0
- package/src/NodeInlineShortCode.ts +17 -5
- package/src/NodeOrderedList.ts +3 -0
- package/src/NodeParagraph.ts +5 -5
- package/src/pairing/ExtensionPairing.ts +14 -0
- package/src/pairing/PairNodesPlugin.ts +56 -0
- package/src/pairing/pairNodes.ts +108 -0
package/src/NodeParagraph.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
type CommandShortcuts,
|
|
6
6
|
} from '@kerebron/editor/commands';
|
|
7
7
|
|
|
8
|
-
type TextAlign = 'left' | 'center' | 'right' | 'justify';
|
|
8
|
+
export type TextAlign = 'left' | 'center' | 'right' | 'justify' | undefined;
|
|
9
9
|
|
|
10
10
|
export class NodeParagraph extends Node {
|
|
11
11
|
override name = 'paragraph';
|
|
@@ -16,7 +16,7 @@ export class NodeParagraph extends Node {
|
|
|
16
16
|
content: 'inline*',
|
|
17
17
|
group: 'block',
|
|
18
18
|
attrs: {
|
|
19
|
-
textAlign: { default:
|
|
19
|
+
textAlign: { default: undefined },
|
|
20
20
|
},
|
|
21
21
|
parseDOM: [
|
|
22
22
|
{
|
|
@@ -29,13 +29,13 @@ export class NodeParagraph extends Node {
|
|
|
29
29
|
) {
|
|
30
30
|
return { textAlign: style };
|
|
31
31
|
}
|
|
32
|
-
return
|
|
32
|
+
return false;
|
|
33
33
|
},
|
|
34
34
|
},
|
|
35
35
|
],
|
|
36
36
|
toDOM(node) {
|
|
37
|
-
const align = node.attrs.textAlign
|
|
38
|
-
if (
|
|
37
|
+
const align = node.attrs.textAlign || 'left';
|
|
38
|
+
if (['center', 'right', 'justify'].includes(align)) {
|
|
39
39
|
return ['p', { style: `text-align: ${align}` }, 0];
|
|
40
40
|
}
|
|
41
41
|
return ['p', 0];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Plugin } from 'prosemirror-state';
|
|
2
|
+
|
|
3
|
+
import { Extension } from '@kerebron/editor';
|
|
4
|
+
import { createPairingPlugin } from './PairNodesPlugin.js';
|
|
5
|
+
|
|
6
|
+
export class ExtensionPairing extends Extension {
|
|
7
|
+
name = 'pairing-nodes';
|
|
8
|
+
|
|
9
|
+
override getProseMirrorPlugins(): Plugin[] {
|
|
10
|
+
return [
|
|
11
|
+
createPairingPlugin(['shortcode_inline']),
|
|
12
|
+
];
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Plugin, Transaction } from 'prosemirror-state';
|
|
2
|
+
import { ReplaceAroundStep, ReplaceStep } from 'prosemirror-transform';
|
|
3
|
+
import { buildPairingTransaction } from './pairNodes.js';
|
|
4
|
+
|
|
5
|
+
export const createPairingPlugin = (types: string[]) => {
|
|
6
|
+
return new Plugin({
|
|
7
|
+
appendTransaction(
|
|
8
|
+
transactions: readonly Transaction[],
|
|
9
|
+
oldState,
|
|
10
|
+
newState,
|
|
11
|
+
) {
|
|
12
|
+
const typesToRebuild: Set<string> = new Set();
|
|
13
|
+
|
|
14
|
+
for (const tr of transactions) {
|
|
15
|
+
if (!tr.docChanged) continue;
|
|
16
|
+
|
|
17
|
+
for (const step of tr.steps) {
|
|
18
|
+
if (
|
|
19
|
+
!(step instanceof ReplaceStep || step instanceof ReplaceAroundStep)
|
|
20
|
+
) {
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (step.slice) {
|
|
25
|
+
step.slice.content.descendants((node) => {
|
|
26
|
+
if (types.includes(node.type.name)) {
|
|
27
|
+
typesToRebuild.add(node.type.name);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (step.from != null && step.to != null && step.from !== step.to) {
|
|
33
|
+
oldState.doc.nodesBetween(step.from, step.to, (node) => {
|
|
34
|
+
if (types.includes(node.type.name)) {
|
|
35
|
+
typesToRebuild.add(node.type.name);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (typesToRebuild.size === types.length) break;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (typesToRebuild.size === types.length) break;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typesToRebuild.size === 0) return null;
|
|
47
|
+
|
|
48
|
+
const tr = newState.tr;
|
|
49
|
+
for (const type of typesToRebuild) {
|
|
50
|
+
buildPairingTransaction(newState, type, tr);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return tr;
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Node } from 'prosemirror-model';
|
|
2
|
+
|
|
3
|
+
import { EditorState, Transaction } from 'prosemirror-state';
|
|
4
|
+
import { CommandFactory } from '@kerebron/editor/commands';
|
|
5
|
+
import {
|
|
6
|
+
NESTING_CLOSING,
|
|
7
|
+
NESTING_OPENING,
|
|
8
|
+
NESTING_SELF_CLOSING,
|
|
9
|
+
} from '@kerebron/editor';
|
|
10
|
+
|
|
11
|
+
function generateId(): string {
|
|
12
|
+
return String(Math.random());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type NodeAndPos = { node: Node; pos: number };
|
|
16
|
+
|
|
17
|
+
export function buildPairingTransaction(
|
|
18
|
+
state: EditorState,
|
|
19
|
+
type: string,
|
|
20
|
+
tr: Transaction,
|
|
21
|
+
) {
|
|
22
|
+
const { doc } = state;
|
|
23
|
+
|
|
24
|
+
const stackMap: Map<string, Array<NodeAndPos>> = new Map();
|
|
25
|
+
const nodes: Array<NodeAndPos> = [];
|
|
26
|
+
|
|
27
|
+
doc.descendants((node, pos) => {
|
|
28
|
+
if (node.type.name === type) {
|
|
29
|
+
nodes.push({ node, pos });
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
let modified = false;
|
|
34
|
+
|
|
35
|
+
for (const { node, pos } of nodes) {
|
|
36
|
+
const raw = node.attrs.content?.trim();
|
|
37
|
+
if (!raw) continue;
|
|
38
|
+
|
|
39
|
+
const match = raw.match(/^[^0-9a-z\/]*(\/?)[\s]*([\w]+)/i);
|
|
40
|
+
const isClosing = match[1] === '/';
|
|
41
|
+
const name = match[2];
|
|
42
|
+
|
|
43
|
+
if (!isClosing) {
|
|
44
|
+
if (!stackMap.has(name)) {
|
|
45
|
+
stackMap.set(name, []);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
stackMap.get(name)!.push({ node, pos });
|
|
49
|
+
} else {
|
|
50
|
+
const stack = stackMap.get(name);
|
|
51
|
+
|
|
52
|
+
if (stack && stack.length > 0) {
|
|
53
|
+
const opening = stack.pop()!;
|
|
54
|
+
const id = generateId();
|
|
55
|
+
|
|
56
|
+
tr.setNodeMarkup(opening.pos, undefined, {
|
|
57
|
+
...opening.node.attrs,
|
|
58
|
+
id,
|
|
59
|
+
nesting: NESTING_OPENING,
|
|
60
|
+
error: undefined,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
64
|
+
...node.attrs,
|
|
65
|
+
id,
|
|
66
|
+
nesting: NESTING_CLOSING,
|
|
67
|
+
error: undefined,
|
|
68
|
+
});
|
|
69
|
+
} else {
|
|
70
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
71
|
+
...node.attrs,
|
|
72
|
+
error: 'Closing shortcode without matching opening',
|
|
73
|
+
nesting: NESTING_CLOSING,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
modified = true;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Any leftover openings are self-closing (no matching close)
|
|
82
|
+
for (const [, stack] of stackMap.entries()) {
|
|
83
|
+
for (const { node, pos } of stack) {
|
|
84
|
+
tr.setNodeMarkup(pos, undefined, {
|
|
85
|
+
...node.attrs,
|
|
86
|
+
nesting: NESTING_SELF_CLOSING,
|
|
87
|
+
id: undefined,
|
|
88
|
+
});
|
|
89
|
+
modified = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return modified ? tr : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const pairNodes: CommandFactory = (type: string) => {
|
|
97
|
+
return (state: EditorState, dispatch) => {
|
|
98
|
+
const tr = buildPairingTransaction(state, type, state.tr);
|
|
99
|
+
if (!tr) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (dispatch) {
|
|
104
|
+
dispatch(tr);
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
};
|
|
108
|
+
};
|