@rme-sdk/extension-codemirror6 1.1.0
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/LICENSE +21 -0
- package/dist/_tsup-dts-rollup.d.cts +179 -0
- package/dist/_tsup-dts-rollup.d.ts +179 -0
- package/dist/rme-sdk-extension-codemirror6.cjs +551 -0
- package/dist/rme-sdk-extension-codemirror6.d.cts +3 -0
- package/dist/rme-sdk-extension-codemirror6.d.ts +3 -0
- package/dist/rme-sdk-extension-codemirror6.js +545 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 drl990114
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { ApplySchemaAttributes } from '@rme-sdk/core';
|
|
2
|
+
import { CommandFunction } from '@rme-sdk/core';
|
|
3
|
+
import type { EditorView } from '@rme-sdk/pm';
|
|
4
|
+
import { Extension } from '@codemirror/state';
|
|
5
|
+
import { InputRule } from '@rme-sdk/core';
|
|
6
|
+
import { KeyBindingProps } from '@rme-sdk/core';
|
|
7
|
+
import type { LanguageDescription } from '@codemirror/language';
|
|
8
|
+
import type { LanguageSupport } from '@codemirror/language';
|
|
9
|
+
import { NodeExtension } from '@rme-sdk/core';
|
|
10
|
+
import { NodeExtensionSpec } from '@rme-sdk/core';
|
|
11
|
+
import { NodeSpecOverride } from '@rme-sdk/core';
|
|
12
|
+
import type { NodeView } from '@rme-sdk/pm';
|
|
13
|
+
import { NodeViewMethod } from '@rme-sdk/core';
|
|
14
|
+
import { PrioritizedKeyBindings } from '@rme-sdk/core';
|
|
15
|
+
import { ProsemirrorAttributes } from '@rme-sdk/core';
|
|
16
|
+
import type { ProsemirrorNode } from '@rme-sdk/pm';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Handling cursor motion from the outer to the inner editor must be done with a
|
|
20
|
+
* keymap on the outer editor. The `arrowHandler` function uses the
|
|
21
|
+
* `endOfTextblock` method to determine, in a bidi-text-aware way, whether the
|
|
22
|
+
* cursor is at the end of a given textblock. If it is, and the next block is a
|
|
23
|
+
* code block, the selection is moved into it.
|
|
24
|
+
*
|
|
25
|
+
* Adapted from https://prosemirror.net/examples/codemirror/
|
|
26
|
+
*/
|
|
27
|
+
export declare function arrowHandler(dir: 'left' | 'right' | 'up' | 'down'): CommandFunction;
|
|
28
|
+
|
|
29
|
+
export declare class CodeMirror6NodeView implements NodeView {
|
|
30
|
+
dom: HTMLElement;
|
|
31
|
+
private node;
|
|
32
|
+
private readonly view;
|
|
33
|
+
private readonly getPos;
|
|
34
|
+
private readonly schema;
|
|
35
|
+
private readonly cm;
|
|
36
|
+
private updating;
|
|
37
|
+
private readonly loadLanguage;
|
|
38
|
+
private readonly languageConf;
|
|
39
|
+
private languageName;
|
|
40
|
+
private readonly toggleName;
|
|
41
|
+
constructor({ node, view, getPos, extensions, loadLanguage, toggleName, }: {
|
|
42
|
+
node: ProsemirrorNode;
|
|
43
|
+
view: EditorView;
|
|
44
|
+
getPos: () => number;
|
|
45
|
+
extensions: Extension[] | null;
|
|
46
|
+
loadLanguage: LoadLanguage;
|
|
47
|
+
toggleName: string;
|
|
48
|
+
});
|
|
49
|
+
update(node: ProsemirrorNode): boolean;
|
|
50
|
+
private updateLanguage;
|
|
51
|
+
private setLanguage;
|
|
52
|
+
/**
|
|
53
|
+
* Synchronize the selections from ProseMirror to CodeMirrror
|
|
54
|
+
*/
|
|
55
|
+
setSelection(anchor: number, head: number): void;
|
|
56
|
+
selectNode(): void;
|
|
57
|
+
focus(): void;
|
|
58
|
+
stopEvent(): boolean;
|
|
59
|
+
destroy(): void;
|
|
60
|
+
/**
|
|
61
|
+
* When the code editor is focused, we can keep the selection of the outer
|
|
62
|
+
* editor synchronized with the inner one, so that any commands executed on
|
|
63
|
+
* the outer editor see an accurate selection.
|
|
64
|
+
*/
|
|
65
|
+
private forwardSelection;
|
|
66
|
+
/**
|
|
67
|
+
* This helper function translates from a CodeMirror selection to a
|
|
68
|
+
* ProseMirror selection.
|
|
69
|
+
*/
|
|
70
|
+
private asProseMirrorSelection;
|
|
71
|
+
/**
|
|
72
|
+
* A somewhat tricky aspect of nesting editor like this is handling cursor
|
|
73
|
+
* motion across the edges of the inner editor. This node view will have to
|
|
74
|
+
* take care of allowing the user to move the selection out of the code
|
|
75
|
+
* editor. For that purpose, it binds the arrow keys to handlers that check if
|
|
76
|
+
* further motion would ‘escape’ the editor, and if so, return the selection
|
|
77
|
+
* and focus to the outer editor.
|
|
78
|
+
*
|
|
79
|
+
* The keymap also binds ctrl-enter, which, in ProseMirror's base keymap,
|
|
80
|
+
* creates a new paragraph after a code block.
|
|
81
|
+
*/
|
|
82
|
+
private codeMirrorKeymap;
|
|
83
|
+
/**
|
|
84
|
+
* When the actual content of the code editor is changed, the event handler
|
|
85
|
+
* registered in the node view's constructor calls this method. It'll compare
|
|
86
|
+
* the code block node's current value to the value in the editor, and
|
|
87
|
+
* dispatch a transaction if there is a difference.
|
|
88
|
+
*/
|
|
89
|
+
private valueChanged;
|
|
90
|
+
private maybeEscape;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
declare class CodeMirrorExtension extends NodeExtension<CodeMirrorExtensionOptions> {
|
|
94
|
+
get name(): "codeMirror";
|
|
95
|
+
private languageMap;
|
|
96
|
+
createNodeSpec(extra: ApplySchemaAttributes, override: NodeSpecOverride): NodeExtensionSpec;
|
|
97
|
+
createNodeViews(): NodeViewMethod;
|
|
98
|
+
createKeymap(): PrioritizedKeyBindings;
|
|
99
|
+
/**
|
|
100
|
+
* Create an input rule that listens converts the code fence into a code block
|
|
101
|
+
* when typing triple back tick followed by a space.
|
|
102
|
+
*/
|
|
103
|
+
createInputRules(): InputRule[];
|
|
104
|
+
enterKey({ dispatch, tr }: KeyBindingProps): boolean;
|
|
105
|
+
private getLanguageMap;
|
|
106
|
+
private loadLanguage;
|
|
107
|
+
/**
|
|
108
|
+
* Creates a CodeMirror block at the current position.
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* commands.createCodeMirror({ language: 'js' });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
createCodeMirror(attributes: CodeMirrorExtensionAttributes): CommandFunction;
|
|
115
|
+
/**
|
|
116
|
+
* Update the code block at the current position. Primarily this is used
|
|
117
|
+
* to change the language.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* if (commands.updateCodeMirror.enabled()) {
|
|
121
|
+
* commands.updateCodeMirror({ language: 'markdown' });
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
updateCodeMirror(attributes: CodeMirrorExtensionAttributes): CommandFunction;
|
|
126
|
+
}
|
|
127
|
+
export { CodeMirrorExtension }
|
|
128
|
+
export { CodeMirrorExtension as CodeMirrorExtension_alias_1 }
|
|
129
|
+
|
|
130
|
+
declare interface CodeMirrorExtensionAttributes extends ProsemirrorAttributes {
|
|
131
|
+
/**
|
|
132
|
+
* A string to represent the language matching the language name or alias in
|
|
133
|
+
* [`LanguageDescription`](https://codemirror.net/docs/ref/#language.LanguageDescription)
|
|
134
|
+
*
|
|
135
|
+
* @defaultValue ''
|
|
136
|
+
*/
|
|
137
|
+
language?: string;
|
|
138
|
+
}
|
|
139
|
+
export { CodeMirrorExtensionAttributes }
|
|
140
|
+
export { CodeMirrorExtensionAttributes as CodeMirrorExtensionAttributes_alias_1 }
|
|
141
|
+
|
|
142
|
+
declare interface CodeMirrorExtensionOptions {
|
|
143
|
+
/**
|
|
144
|
+
* The CodeMirror extensions to use.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
*
|
|
148
|
+
* For package size reasons, no CodeMirror extensions are included by default.
|
|
149
|
+
* You might want to install and add the following two extensions:
|
|
150
|
+
*
|
|
151
|
+
* - [`basicSetup`](https://codemirror.net/6/docs/ref/#basic-setup.basicSetup)
|
|
152
|
+
* - [`oneDark`](https://github.com/codemirror/theme-one-dark)
|
|
153
|
+
*
|
|
154
|
+
* @defaultValue null
|
|
155
|
+
*/
|
|
156
|
+
extensions?: Extension[] | null;
|
|
157
|
+
/**
|
|
158
|
+
* The CodeMirror languages to use.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
*
|
|
162
|
+
* You can install [`@codemirror/language-data`](https://codemirror.net/6/docs/ref/#language-data) and import languages from there.
|
|
163
|
+
*
|
|
164
|
+
* @defaultValue null
|
|
165
|
+
*/
|
|
166
|
+
languages?: LanguageDescription[] | null;
|
|
167
|
+
/**
|
|
168
|
+
* The name of the node that the codeMirror block should toggle back and forth from.
|
|
169
|
+
*
|
|
170
|
+
* @defaultValue "paragraph"
|
|
171
|
+
*/
|
|
172
|
+
toggleName?: string;
|
|
173
|
+
}
|
|
174
|
+
export { CodeMirrorExtensionOptions }
|
|
175
|
+
export { CodeMirrorExtensionOptions as CodeMirrorExtensionOptions_alias_1 }
|
|
176
|
+
|
|
177
|
+
declare type LoadLanguage = (lang: string) => Promise<LanguageSupport> | LanguageSupport | void;
|
|
178
|
+
|
|
179
|
+
export { }
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { ApplySchemaAttributes } from '@rme-sdk/core';
|
|
2
|
+
import { CommandFunction } from '@rme-sdk/core';
|
|
3
|
+
import type { EditorView } from '@rme-sdk/pm';
|
|
4
|
+
import { Extension } from '@codemirror/state';
|
|
5
|
+
import { InputRule } from '@rme-sdk/core';
|
|
6
|
+
import { KeyBindingProps } from '@rme-sdk/core';
|
|
7
|
+
import type { LanguageDescription } from '@codemirror/language';
|
|
8
|
+
import type { LanguageSupport } from '@codemirror/language';
|
|
9
|
+
import { NodeExtension } from '@rme-sdk/core';
|
|
10
|
+
import { NodeExtensionSpec } from '@rme-sdk/core';
|
|
11
|
+
import { NodeSpecOverride } from '@rme-sdk/core';
|
|
12
|
+
import type { NodeView } from '@rme-sdk/pm';
|
|
13
|
+
import { NodeViewMethod } from '@rme-sdk/core';
|
|
14
|
+
import { PrioritizedKeyBindings } from '@rme-sdk/core';
|
|
15
|
+
import { ProsemirrorAttributes } from '@rme-sdk/core';
|
|
16
|
+
import type { ProsemirrorNode } from '@rme-sdk/pm';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Handling cursor motion from the outer to the inner editor must be done with a
|
|
20
|
+
* keymap on the outer editor. The `arrowHandler` function uses the
|
|
21
|
+
* `endOfTextblock` method to determine, in a bidi-text-aware way, whether the
|
|
22
|
+
* cursor is at the end of a given textblock. If it is, and the next block is a
|
|
23
|
+
* code block, the selection is moved into it.
|
|
24
|
+
*
|
|
25
|
+
* Adapted from https://prosemirror.net/examples/codemirror/
|
|
26
|
+
*/
|
|
27
|
+
export declare function arrowHandler(dir: 'left' | 'right' | 'up' | 'down'): CommandFunction;
|
|
28
|
+
|
|
29
|
+
export declare class CodeMirror6NodeView implements NodeView {
|
|
30
|
+
dom: HTMLElement;
|
|
31
|
+
private node;
|
|
32
|
+
private readonly view;
|
|
33
|
+
private readonly getPos;
|
|
34
|
+
private readonly schema;
|
|
35
|
+
private readonly cm;
|
|
36
|
+
private updating;
|
|
37
|
+
private readonly loadLanguage;
|
|
38
|
+
private readonly languageConf;
|
|
39
|
+
private languageName;
|
|
40
|
+
private readonly toggleName;
|
|
41
|
+
constructor({ node, view, getPos, extensions, loadLanguage, toggleName, }: {
|
|
42
|
+
node: ProsemirrorNode;
|
|
43
|
+
view: EditorView;
|
|
44
|
+
getPos: () => number;
|
|
45
|
+
extensions: Extension[] | null;
|
|
46
|
+
loadLanguage: LoadLanguage;
|
|
47
|
+
toggleName: string;
|
|
48
|
+
});
|
|
49
|
+
update(node: ProsemirrorNode): boolean;
|
|
50
|
+
private updateLanguage;
|
|
51
|
+
private setLanguage;
|
|
52
|
+
/**
|
|
53
|
+
* Synchronize the selections from ProseMirror to CodeMirrror
|
|
54
|
+
*/
|
|
55
|
+
setSelection(anchor: number, head: number): void;
|
|
56
|
+
selectNode(): void;
|
|
57
|
+
focus(): void;
|
|
58
|
+
stopEvent(): boolean;
|
|
59
|
+
destroy(): void;
|
|
60
|
+
/**
|
|
61
|
+
* When the code editor is focused, we can keep the selection of the outer
|
|
62
|
+
* editor synchronized with the inner one, so that any commands executed on
|
|
63
|
+
* the outer editor see an accurate selection.
|
|
64
|
+
*/
|
|
65
|
+
private forwardSelection;
|
|
66
|
+
/**
|
|
67
|
+
* This helper function translates from a CodeMirror selection to a
|
|
68
|
+
* ProseMirror selection.
|
|
69
|
+
*/
|
|
70
|
+
private asProseMirrorSelection;
|
|
71
|
+
/**
|
|
72
|
+
* A somewhat tricky aspect of nesting editor like this is handling cursor
|
|
73
|
+
* motion across the edges of the inner editor. This node view will have to
|
|
74
|
+
* take care of allowing the user to move the selection out of the code
|
|
75
|
+
* editor. For that purpose, it binds the arrow keys to handlers that check if
|
|
76
|
+
* further motion would ‘escape’ the editor, and if so, return the selection
|
|
77
|
+
* and focus to the outer editor.
|
|
78
|
+
*
|
|
79
|
+
* The keymap also binds ctrl-enter, which, in ProseMirror's base keymap,
|
|
80
|
+
* creates a new paragraph after a code block.
|
|
81
|
+
*/
|
|
82
|
+
private codeMirrorKeymap;
|
|
83
|
+
/**
|
|
84
|
+
* When the actual content of the code editor is changed, the event handler
|
|
85
|
+
* registered in the node view's constructor calls this method. It'll compare
|
|
86
|
+
* the code block node's current value to the value in the editor, and
|
|
87
|
+
* dispatch a transaction if there is a difference.
|
|
88
|
+
*/
|
|
89
|
+
private valueChanged;
|
|
90
|
+
private maybeEscape;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
declare class CodeMirrorExtension extends NodeExtension<CodeMirrorExtensionOptions> {
|
|
94
|
+
get name(): "codeMirror";
|
|
95
|
+
private languageMap;
|
|
96
|
+
createNodeSpec(extra: ApplySchemaAttributes, override: NodeSpecOverride): NodeExtensionSpec;
|
|
97
|
+
createNodeViews(): NodeViewMethod;
|
|
98
|
+
createKeymap(): PrioritizedKeyBindings;
|
|
99
|
+
/**
|
|
100
|
+
* Create an input rule that listens converts the code fence into a code block
|
|
101
|
+
* when typing triple back tick followed by a space.
|
|
102
|
+
*/
|
|
103
|
+
createInputRules(): InputRule[];
|
|
104
|
+
enterKey({ dispatch, tr }: KeyBindingProps): boolean;
|
|
105
|
+
private getLanguageMap;
|
|
106
|
+
private loadLanguage;
|
|
107
|
+
/**
|
|
108
|
+
* Creates a CodeMirror block at the current position.
|
|
109
|
+
*
|
|
110
|
+
* ```ts
|
|
111
|
+
* commands.createCodeMirror({ language: 'js' });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
createCodeMirror(attributes: CodeMirrorExtensionAttributes): CommandFunction;
|
|
115
|
+
/**
|
|
116
|
+
* Update the code block at the current position. Primarily this is used
|
|
117
|
+
* to change the language.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* if (commands.updateCodeMirror.enabled()) {
|
|
121
|
+
* commands.updateCodeMirror({ language: 'markdown' });
|
|
122
|
+
* }
|
|
123
|
+
* ```
|
|
124
|
+
*/
|
|
125
|
+
updateCodeMirror(attributes: CodeMirrorExtensionAttributes): CommandFunction;
|
|
126
|
+
}
|
|
127
|
+
export { CodeMirrorExtension }
|
|
128
|
+
export { CodeMirrorExtension as CodeMirrorExtension_alias_1 }
|
|
129
|
+
|
|
130
|
+
declare interface CodeMirrorExtensionAttributes extends ProsemirrorAttributes {
|
|
131
|
+
/**
|
|
132
|
+
* A string to represent the language matching the language name or alias in
|
|
133
|
+
* [`LanguageDescription`](https://codemirror.net/docs/ref/#language.LanguageDescription)
|
|
134
|
+
*
|
|
135
|
+
* @defaultValue ''
|
|
136
|
+
*/
|
|
137
|
+
language?: string;
|
|
138
|
+
}
|
|
139
|
+
export { CodeMirrorExtensionAttributes }
|
|
140
|
+
export { CodeMirrorExtensionAttributes as CodeMirrorExtensionAttributes_alias_1 }
|
|
141
|
+
|
|
142
|
+
declare interface CodeMirrorExtensionOptions {
|
|
143
|
+
/**
|
|
144
|
+
* The CodeMirror extensions to use.
|
|
145
|
+
*
|
|
146
|
+
* @remarks
|
|
147
|
+
*
|
|
148
|
+
* For package size reasons, no CodeMirror extensions are included by default.
|
|
149
|
+
* You might want to install and add the following two extensions:
|
|
150
|
+
*
|
|
151
|
+
* - [`basicSetup`](https://codemirror.net/6/docs/ref/#basic-setup.basicSetup)
|
|
152
|
+
* - [`oneDark`](https://github.com/codemirror/theme-one-dark)
|
|
153
|
+
*
|
|
154
|
+
* @defaultValue null
|
|
155
|
+
*/
|
|
156
|
+
extensions?: Extension[] | null;
|
|
157
|
+
/**
|
|
158
|
+
* The CodeMirror languages to use.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
*
|
|
162
|
+
* You can install [`@codemirror/language-data`](https://codemirror.net/6/docs/ref/#language-data) and import languages from there.
|
|
163
|
+
*
|
|
164
|
+
* @defaultValue null
|
|
165
|
+
*/
|
|
166
|
+
languages?: LanguageDescription[] | null;
|
|
167
|
+
/**
|
|
168
|
+
* The name of the node that the codeMirror block should toggle back and forth from.
|
|
169
|
+
*
|
|
170
|
+
* @defaultValue "paragraph"
|
|
171
|
+
*/
|
|
172
|
+
toggleName?: string;
|
|
173
|
+
}
|
|
174
|
+
export { CodeMirrorExtensionOptions }
|
|
175
|
+
export { CodeMirrorExtensionOptions as CodeMirrorExtensionOptions_alias_1 }
|
|
176
|
+
|
|
177
|
+
declare type LoadLanguage = (lang: string) => Promise<LanguageSupport> | LanguageSupport | void;
|
|
178
|
+
|
|
179
|
+
export { }
|