@kerebron/extension-codemirror 0.2.0 → 0.2.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/esm/editor/src/CoreEditor.d.ts +25 -0
- package/esm/editor/src/CoreEditor.d.ts.map +1 -0
- package/esm/editor/src/CoreEditor.js +197 -0
- package/esm/editor/src/Extension.d.ts +26 -0
- package/esm/editor/src/Extension.d.ts.map +1 -0
- package/esm/editor/src/Extension.js +33 -0
- package/esm/editor/src/ExtensionManager.d.ts +32 -0
- package/esm/editor/src/ExtensionManager.d.ts.map +1 -0
- package/esm/editor/src/ExtensionManager.js +253 -0
- package/esm/editor/src/Mark.d.ts +18 -0
- package/esm/editor/src/Mark.d.ts.map +1 -0
- package/esm/editor/src/Mark.js +34 -0
- package/esm/editor/src/Node.d.ts +27 -0
- package/esm/editor/src/Node.d.ts.map +1 -0
- package/esm/editor/src/Node.js +43 -0
- package/esm/editor/src/commands/CommandManager.d.ts +20 -0
- package/esm/editor/src/commands/CommandManager.d.ts.map +1 -0
- package/esm/editor/src/commands/CommandManager.js +60 -0
- package/esm/editor/src/commands/createChainableState.d.ts +3 -0
- package/esm/editor/src/commands/createChainableState.d.ts.map +1 -0
- package/esm/editor/src/commands/createChainableState.js +29 -0
- package/esm/editor/src/commands/mod.d.ts +49 -0
- package/esm/editor/src/commands/mod.d.ts.map +1 -0
- package/esm/editor/src/commands/mod.js +928 -0
- package/esm/editor/src/mod.d.ts +6 -0
- package/esm/editor/src/mod.d.ts.map +1 -0
- package/esm/editor/src/mod.js +5 -0
- package/esm/editor/src/nodeToTreeString.d.ts +4 -0
- package/esm/editor/src/nodeToTreeString.d.ts.map +1 -0
- package/esm/editor/src/nodeToTreeString.js +56 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts +23 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.d.ts.map +1 -0
- package/esm/editor/src/plugins/input-rules/InputRulesPlugin.js +163 -0
- package/esm/editor/src/plugins/input-rules/mod.d.ts +3 -0
- package/esm/editor/src/plugins/input-rules/mod.d.ts.map +1 -0
- package/esm/editor/src/plugins/input-rules/mod.js +2 -0
- package/esm/editor/src/plugins/input-rules/rulebuilders.d.ts +5 -0
- package/esm/editor/src/plugins/input-rules/rulebuilders.d.ts.map +1 -0
- package/esm/editor/src/plugins/input-rules/rulebuilders.js +50 -0
- package/esm/editor/src/types.d.ts +29 -0
- package/esm/editor/src/types.d.ts.map +1 -0
- package/esm/editor/src/types.js +1 -0
- package/esm/editor/src/utilities/createNodeFromContent.d.ts +8 -0
- package/esm/editor/src/utilities/createNodeFromContent.d.ts.map +1 -0
- package/esm/editor/src/utilities/createNodeFromContent.js +33 -0
- package/esm/editor/src/utilities/getHtmlAttributes.d.ts +4 -0
- package/esm/editor/src/utilities/getHtmlAttributes.d.ts.map +1 -0
- package/esm/editor/src/utilities/getHtmlAttributes.js +47 -0
- package/esm/editor/src/utilities/mod.d.ts +3 -0
- package/esm/editor/src/utilities/mod.d.ts.map +1 -0
- package/esm/editor/src/utilities/mod.js +2 -0
- package/esm/extension-codemirror/src/ExtensionCodeMirror.d.ts +5 -0
- package/esm/extension-codemirror/src/ExtensionCodeMirror.d.ts.map +1 -0
- package/esm/extension-codemirror/src/ExtensionCodeMirror.js +4 -0
- package/esm/extension-codemirror/src/NodeCodeMirror.d.ts +27 -0
- package/esm/extension-codemirror/src/NodeCodeMirror.d.ts.map +1 -0
- package/esm/extension-codemirror/src/NodeCodeMirror.js +198 -0
- package/esm/extension-codemirror/src/NodeDocumentCode.d.ts +7 -0
- package/esm/extension-codemirror/src/NodeDocumentCode.d.ts.map +1 -0
- package/esm/extension-codemirror/src/NodeDocumentCode.js +37 -0
- package/esm/extension-codemirror/src/codeMirrorBlockNodeView.d.ts +6 -0
- package/esm/extension-codemirror/src/codeMirrorBlockNodeView.d.ts.map +1 -0
- package/esm/extension-codemirror/src/codeMirrorBlockNodeView.js +267 -0
- package/esm/extension-codemirror/src/defaults.d.ts +6 -0
- package/esm/extension-codemirror/src/defaults.d.ts.map +1 -0
- package/esm/extension-codemirror/src/defaults.js +57 -0
- package/esm/extension-codemirror/src/languageLoaders.d.ts +5 -0
- package/esm/extension-codemirror/src/languageLoaders.d.ts.map +1 -0
- package/esm/extension-codemirror/src/languageLoaders.js +113 -0
- package/esm/extension-codemirror/src/languages.d.ts +109 -0
- package/esm/extension-codemirror/src/languages.d.ts.map +1 -0
- package/esm/extension-codemirror/src/languages.js +110 -0
- package/esm/extension-codemirror/src/types.d.ts +30 -0
- package/esm/extension-codemirror/src/types.d.ts.map +1 -0
- package/esm/extension-codemirror/src/types.js +1 -0
- package/esm/extension-codemirror/src/utils.d.ts +39 -0
- package/esm/extension-codemirror/src/utils.d.ts.map +1 -0
- package/esm/extension-codemirror/src/utils.js +199 -0
- package/esm/extension-codemirror/src/y-remote-selections.d.ts +15 -0
- package/esm/extension-codemirror/src/y-remote-selections.d.ts.map +1 -0
- package/esm/extension-codemirror/src/y-remote-selections.js +273 -0
- package/esm/extension-codemirror/src/y-sync.d.ts +11 -0
- package/esm/extension-codemirror/src/y-sync.d.ts.map +1 -0
- package/esm/extension-codemirror/src/y-sync.js +29 -0
- package/esm/package.json +3 -0
- package/package.json +1 -4
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import { Plugin, PluginKey, Selection } from 'prosemirror-state';
|
|
2
|
+
import { Node } from '../../editor/src/mod.js';
|
|
3
|
+
import { setBlockType, } from '../../editor/src/commands/mod.js';
|
|
4
|
+
import { textblockTypeInputRule, } from '../../editor/src/plugins/input-rules/mod.js';
|
|
5
|
+
import { codeMirrorBlockNodeView } from './codeMirrorBlockNodeView.js';
|
|
6
|
+
import { defaultSettings } from './defaults.js';
|
|
7
|
+
import languageLoaders, { legacyLanguageLoaders } from './languageLoaders.js';
|
|
8
|
+
import { createNodeFromObject } from '../../editor/src/utilities/mod.js';
|
|
9
|
+
export const codeMirrorBlockKey = new PluginKey('codemirror-block');
|
|
10
|
+
function arrowHandler(dir) {
|
|
11
|
+
return (state, dispatch, view) => {
|
|
12
|
+
if (state.selection.empty && view.endOfTextblock(dir)) {
|
|
13
|
+
let side = dir == 'left' || dir == 'up' ? -1 : 1;
|
|
14
|
+
let $head = state.selection.$head;
|
|
15
|
+
let nextPos = Selection.near(state.doc.resolve(side > 0 ? $head.after() : $head.before()), side);
|
|
16
|
+
if (nextPos.$head && nextPos.$head.parent.type.name == 'code_block') {
|
|
17
|
+
dispatch(state.tr.setSelection(nextPos));
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
const LANGS = [
|
|
25
|
+
'diff',
|
|
26
|
+
'dockerfile',
|
|
27
|
+
'http',
|
|
28
|
+
'nginx',
|
|
29
|
+
'properties',
|
|
30
|
+
'shell',
|
|
31
|
+
'toml',
|
|
32
|
+
'yaml',
|
|
33
|
+
'sql',
|
|
34
|
+
'javascript',
|
|
35
|
+
'cpp',
|
|
36
|
+
'css',
|
|
37
|
+
'xml',
|
|
38
|
+
'java',
|
|
39
|
+
'json',
|
|
40
|
+
'markdown',
|
|
41
|
+
'rust',
|
|
42
|
+
'html',
|
|
43
|
+
];
|
|
44
|
+
export class NodeCodeMirror extends Node {
|
|
45
|
+
constructor() {
|
|
46
|
+
super(...arguments);
|
|
47
|
+
Object.defineProperty(this, "name", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: 'code_block'
|
|
52
|
+
});
|
|
53
|
+
// requires = ['doc'];
|
|
54
|
+
Object.defineProperty(this, "automerge", {
|
|
55
|
+
enumerable: true,
|
|
56
|
+
configurable: true,
|
|
57
|
+
writable: true,
|
|
58
|
+
value: {
|
|
59
|
+
block: this.name,
|
|
60
|
+
attrParsers: {
|
|
61
|
+
fromAutomerge: (block) => ({ lang: block.attrs.lang }),
|
|
62
|
+
fromProsemirror: (node) => ({ lang: node.attrs.lang }),
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
getConverters(editor, schema) {
|
|
68
|
+
return {
|
|
69
|
+
'text/code-only': {
|
|
70
|
+
fromDoc(document) {
|
|
71
|
+
const retVal = [];
|
|
72
|
+
if (document.content) {
|
|
73
|
+
for (const node of document.content.toJSON()) {
|
|
74
|
+
if ('code_block' === node.type && Array.isArray(node.content)) {
|
|
75
|
+
for (const content of node.content) {
|
|
76
|
+
retVal.push(content.text);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return retVal.join('');
|
|
82
|
+
},
|
|
83
|
+
toDoc(code) {
|
|
84
|
+
const content = {
|
|
85
|
+
'type': 'doc_code',
|
|
86
|
+
'content': [
|
|
87
|
+
{
|
|
88
|
+
'type': 'code_block',
|
|
89
|
+
'attrs': {
|
|
90
|
+
'lang': schema.topNodeType.spec.defaultAttrs?.lang,
|
|
91
|
+
},
|
|
92
|
+
'content': [
|
|
93
|
+
{
|
|
94
|
+
'type': 'text',
|
|
95
|
+
'text': code,
|
|
96
|
+
},
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
};
|
|
101
|
+
return createNodeFromObject(content, schema, {
|
|
102
|
+
slice: false,
|
|
103
|
+
errorOnInvalidContent: false,
|
|
104
|
+
});
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
getNodeSpec() {
|
|
110
|
+
const langs = this.config.languageWhitelist || LANGS;
|
|
111
|
+
return {
|
|
112
|
+
content: 'text*',
|
|
113
|
+
marks: '',
|
|
114
|
+
group: 'block',
|
|
115
|
+
code: true,
|
|
116
|
+
defining: true,
|
|
117
|
+
parseDOM: [
|
|
118
|
+
{
|
|
119
|
+
tag: 'pre',
|
|
120
|
+
preserveWhitespace: 'full',
|
|
121
|
+
getAttrs(dom) {
|
|
122
|
+
let lang = dom.getAttribute('lang');
|
|
123
|
+
if (!lang) {
|
|
124
|
+
for (const className of dom.classList) {
|
|
125
|
+
if (className.startsWith('lang-') &&
|
|
126
|
+
langs.indexOf(className.substring('lang-'.length)) > -1) {
|
|
127
|
+
lang = className.substring('lang-'.length);
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
lang,
|
|
134
|
+
};
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
],
|
|
138
|
+
attrs: { lang: { default: null } },
|
|
139
|
+
toDOM(node) {
|
|
140
|
+
const { lang } = node.attrs;
|
|
141
|
+
return ['pre', { lang }, ['code', 0]];
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
getInputRules(type) {
|
|
146
|
+
/// Given a code block node type, returns an input rule that turns a
|
|
147
|
+
/// textblock starting with three backticks into a code block.
|
|
148
|
+
return [
|
|
149
|
+
textblockTypeInputRule(/^```$/, type),
|
|
150
|
+
];
|
|
151
|
+
}
|
|
152
|
+
getCommands(editor, type) {
|
|
153
|
+
return {
|
|
154
|
+
'setCodeBlock': (lang) => setBlockType(type, { lang }),
|
|
155
|
+
ArrowLeft: () => arrowHandler('left'),
|
|
156
|
+
ArrowRight: () => arrowHandler('right'),
|
|
157
|
+
ArrowUp: () => arrowHandler('up'),
|
|
158
|
+
ArrowDown: () => arrowHandler('down'),
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
getKeyboardShortcuts() {
|
|
162
|
+
return {
|
|
163
|
+
'Shift-Ctrl-"': 'setCodeBlock',
|
|
164
|
+
'ArrowLeft': 'ArrowLeft',
|
|
165
|
+
'ArrowRight': 'ArrowRight',
|
|
166
|
+
'ArrowUp': 'ArrowUp',
|
|
167
|
+
'ArrowDown': 'ArrowDown',
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
getProseMirrorPlugins(editor) {
|
|
171
|
+
const codeMirrorBlockPlugin = (settings) => new Plugin({
|
|
172
|
+
key: codeMirrorBlockKey,
|
|
173
|
+
props: {
|
|
174
|
+
nodeViews: {
|
|
175
|
+
[this.name]: codeMirrorBlockNodeView(settings),
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
});
|
|
179
|
+
return [
|
|
180
|
+
codeMirrorBlockPlugin({
|
|
181
|
+
provider: this.config.provider,
|
|
182
|
+
languageWhitelist: this.config.languageWhitelist || LANGS,
|
|
183
|
+
shadowRoot: this.config.shadowRoot,
|
|
184
|
+
...defaultSettings,
|
|
185
|
+
readOnly: this.config.readOnly,
|
|
186
|
+
languageLoaders: { ...languageLoaders, ...legacyLanguageLoaders },
|
|
187
|
+
undo: () => {
|
|
188
|
+
editor.chain().undo().run();
|
|
189
|
+
},
|
|
190
|
+
redo: () => {
|
|
191
|
+
editor.chain().redo().run();
|
|
192
|
+
},
|
|
193
|
+
theme: [...(this.config.theme || [])],
|
|
194
|
+
}),
|
|
195
|
+
// keymap(codeBlockKeymap),
|
|
196
|
+
];
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NodeDocumentCode.d.ts","sourceRoot":"","sources":["../../../src/extension-codemirror/src/NodeDocumentCode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,mBAAmB,CAAC;AAE7C,OAAO,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAE/C,qBAAa,gBAAiB,SAAQ,IAAI;IAC/B,IAAI,SAAc;IAElB,WAAW,IAAI,QAAQ;CAyBjC"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Node } from '../../editor/src/mod.js';
|
|
2
|
+
export class NodeDocumentCode extends Node {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
Object.defineProperty(this, "name", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: 'doc_code'
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
getNodeSpec() {
|
|
13
|
+
return {
|
|
14
|
+
content: 'code_block',
|
|
15
|
+
defaultAttrs: {
|
|
16
|
+
'lang': this.config.lang || null,
|
|
17
|
+
},
|
|
18
|
+
EMPTY_DOC: {
|
|
19
|
+
'type': this.name,
|
|
20
|
+
'content': [
|
|
21
|
+
{
|
|
22
|
+
'type': 'code_block',
|
|
23
|
+
'attrs': {
|
|
24
|
+
'lang': this.config.lang || null,
|
|
25
|
+
},
|
|
26
|
+
'content': [
|
|
27
|
+
// {
|
|
28
|
+
// "type": "text",
|
|
29
|
+
// "text": ""
|
|
30
|
+
// }
|
|
31
|
+
],
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Node } from 'prosemirror-model';
|
|
2
|
+
import { EditorView as PMEditorView, NodeView } from 'prosemirror-view';
|
|
3
|
+
import { CodeBlockSettings } from './types.js';
|
|
4
|
+
export declare const themeCallbacks: Array<(theme: string) => void>;
|
|
5
|
+
export declare const codeMirrorBlockNodeView: (settings: CodeBlockSettings) => (pmNode: Node, view: PMEditorView, getPos: (() => number) | boolean) => NodeView;
|
|
6
|
+
//# sourceMappingURL=codeMirrorBlockNodeView.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codeMirrorBlockNodeView.d.ts","sourceRoot":"","sources":["../../../src/extension-codemirror/src/codeMirrorBlockNodeView.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAEL,UAAU,IAAI,YAAY,EAC1B,QAAQ,EACT,MAAM,kBAAkB,CAAC;AA0C1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAO/C,eAAO,MAAM,cAAc,EAAE,KAAK,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAM,CAAC;AA0QjE,eAAO,MAAM,uBAAuB,EAAE,CACpC,QAAQ,EAAE,iBAAiB,KACxB,CACH,MAAM,EAAE,IAAI,EACZ,IAAI,EAAE,YAAY,EAClB,MAAM,EAAE,CAAC,MAAM,MAAM,CAAC,GAAG,OAAO,KAC7B,QAQJ,CAAC"}
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { exitCode, selectAll } from '../../editor/src/commands/mod.js';
|
|
2
|
+
import { drawSelection, EditorView as CodeMirror, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, rectangularSelection, } from '@codemirror/view';
|
|
3
|
+
import { highlightSelectionMatches, selectNextOccurrence, } from '@codemirror/search';
|
|
4
|
+
import { bracketMatching, defaultHighlightStyle, foldGutter, foldKeymap, indentOnInput, syntaxHighlighting, } from '@codemirror/language';
|
|
5
|
+
import { autocompletion, closeBrackets, closeBracketsKeymap, completionKeymap, } from '@codemirror/autocomplete';
|
|
6
|
+
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
|
|
7
|
+
import { Compartment, EditorState } from '@codemirror/state';
|
|
8
|
+
import { backspaceHandler, computeChange, forwardSelection, maybeEscape, setMode, setTheme, valueChanged, } from './utils.js';
|
|
9
|
+
import { YSyncConfig, ySyncFacet } from './y-sync.js';
|
|
10
|
+
import { yRemoteSelections, yRemoteSelectionsTheme, } from './y-remote-selections.js';
|
|
11
|
+
export const themeCallbacks = [];
|
|
12
|
+
class CodeMirrorBlockNodeView {
|
|
13
|
+
constructor(node, view, getPos, settings) {
|
|
14
|
+
Object.defineProperty(this, "node", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: node
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "view", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: view
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "getPos", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: getPos
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "settings", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: settings
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "dom", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
Object.defineProperty(this, "codeMirrorView", {
|
|
45
|
+
enumerable: true,
|
|
46
|
+
configurable: true,
|
|
47
|
+
writable: true,
|
|
48
|
+
value: void 0
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(this, "updating", {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: true,
|
|
53
|
+
writable: true,
|
|
54
|
+
value: void 0
|
|
55
|
+
});
|
|
56
|
+
Object.defineProperty(this, "createCopyButtonCB", {
|
|
57
|
+
enumerable: true,
|
|
58
|
+
configurable: true,
|
|
59
|
+
writable: true,
|
|
60
|
+
value: void 0
|
|
61
|
+
});
|
|
62
|
+
Object.defineProperty(this, "selectDeleteCB", {
|
|
63
|
+
enumerable: true,
|
|
64
|
+
configurable: true,
|
|
65
|
+
writable: true,
|
|
66
|
+
value: void 0
|
|
67
|
+
});
|
|
68
|
+
Object.defineProperty(this, "languageConf", {
|
|
69
|
+
enumerable: true,
|
|
70
|
+
configurable: true,
|
|
71
|
+
writable: true,
|
|
72
|
+
value: void 0
|
|
73
|
+
});
|
|
74
|
+
this.updating = false;
|
|
75
|
+
const dom = document.createElement('div');
|
|
76
|
+
this.dom = dom;
|
|
77
|
+
dom.className = 'codeblock-root';
|
|
78
|
+
this.languageConf = new Compartment();
|
|
79
|
+
const themeConfig = new Compartment();
|
|
80
|
+
const yCollab = () => {
|
|
81
|
+
const plugins = [];
|
|
82
|
+
if (settings.provider?.awareness) {
|
|
83
|
+
const ySyncConfig = new YSyncConfig(() => this.node, getPos, settings.provider.awareness);
|
|
84
|
+
plugins.push(ySyncFacet.of(ySyncConfig));
|
|
85
|
+
plugins.push(yRemoteSelectionsTheme, yRemoteSelections);
|
|
86
|
+
}
|
|
87
|
+
return plugins;
|
|
88
|
+
};
|
|
89
|
+
const state = EditorState.create({
|
|
90
|
+
extensions: [
|
|
91
|
+
EditorState.readOnly.of(settings.readOnly),
|
|
92
|
+
CodeMirror.editable.of(!settings.readOnly),
|
|
93
|
+
lineNumbers(),
|
|
94
|
+
highlightActiveLineGutter(),
|
|
95
|
+
foldGutter(),
|
|
96
|
+
bracketMatching(),
|
|
97
|
+
closeBrackets(),
|
|
98
|
+
highlightSelectionMatches(),
|
|
99
|
+
autocompletion(),
|
|
100
|
+
rectangularSelection(),
|
|
101
|
+
drawSelection({ cursorBlinkRate: 1000 }), // broken focus
|
|
102
|
+
EditorState.allowMultipleSelections.of(true),
|
|
103
|
+
highlightActiveLine(),
|
|
104
|
+
syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
|
|
105
|
+
this.languageConf.of([]),
|
|
106
|
+
indentOnInput(),
|
|
107
|
+
keymap.of([
|
|
108
|
+
{ key: 'Mod-d', run: selectNextOccurrence, preventDefault: true },
|
|
109
|
+
{
|
|
110
|
+
key: 'ArrowUp',
|
|
111
|
+
run: (cmView) => maybeEscape('line', -1, cmView, view, getPos),
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
key: 'ArrowLeft',
|
|
115
|
+
run: (cmView) => maybeEscape('char', -1, cmView, view, getPos),
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
key: 'ArrowDown',
|
|
119
|
+
run: (cmView) => maybeEscape('line', 1, cmView, view, getPos),
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
key: 'ArrowRight',
|
|
123
|
+
run: (cmView) => maybeEscape('char', 1, cmView, view, getPos),
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
key: 'Ctrl-Enter',
|
|
127
|
+
run: () => {
|
|
128
|
+
if (!exitCode(view.state, view.dispatch))
|
|
129
|
+
return false;
|
|
130
|
+
view.focus();
|
|
131
|
+
return true;
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
key: 'Mod-z',
|
|
136
|
+
run: () => settings.undo?.(view.state, view.dispatch) || true,
|
|
137
|
+
shift: () => settings.redo?.(view.state, view.dispatch) || true,
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
key: 'Mod-y',
|
|
141
|
+
run: () => settings.redo?.(view.state, view.dispatch) || true,
|
|
142
|
+
},
|
|
143
|
+
{ key: 'Backspace', run: (cmView) => backspaceHandler(view, cmView) },
|
|
144
|
+
{
|
|
145
|
+
key: 'Mod-Backspace',
|
|
146
|
+
run: (cmView) => backspaceHandler(view, cmView),
|
|
147
|
+
},
|
|
148
|
+
{
|
|
149
|
+
key: 'Mod-a',
|
|
150
|
+
run: () => {
|
|
151
|
+
const result = selectAll(view.state, view.dispatch);
|
|
152
|
+
view.focus();
|
|
153
|
+
return result;
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
{
|
|
157
|
+
key: 'Enter',
|
|
158
|
+
run: (cmView) => {
|
|
159
|
+
const sel = cmView.state.selection.main;
|
|
160
|
+
if (cmView.state.doc.line(cmView.state.doc.lines).text === '' &&
|
|
161
|
+
sel.from === sel.to &&
|
|
162
|
+
sel.from === cmView.state.doc.length) {
|
|
163
|
+
exitCode(view.state, view.dispatch);
|
|
164
|
+
view.focus();
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
...defaultKeymap,
|
|
171
|
+
...foldKeymap,
|
|
172
|
+
...closeBracketsKeymap,
|
|
173
|
+
...completionKeymap,
|
|
174
|
+
indentWithTab,
|
|
175
|
+
]),
|
|
176
|
+
...(settings.theme ? settings.theme : []),
|
|
177
|
+
themeConfig.of([]),
|
|
178
|
+
yCollab(),
|
|
179
|
+
],
|
|
180
|
+
doc: node.textContent,
|
|
181
|
+
});
|
|
182
|
+
const root = settings.shadowRoot || document;
|
|
183
|
+
this.codeMirrorView = new CodeMirror({
|
|
184
|
+
state,
|
|
185
|
+
root,
|
|
186
|
+
dispatch: (tr) => {
|
|
187
|
+
this.codeMirrorView.update([tr]);
|
|
188
|
+
if (!this.updating) {
|
|
189
|
+
const textUpdate = tr.state.toJSON().doc;
|
|
190
|
+
valueChanged(textUpdate, this.node, getPos, view);
|
|
191
|
+
forwardSelection(this.codeMirrorView, view, getPos);
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
});
|
|
195
|
+
dom.append(this.codeMirrorView.dom);
|
|
196
|
+
if (!(Array.isArray(settings.languageWhitelist) &&
|
|
197
|
+
settings.languageWhitelist.length === 1)) {
|
|
198
|
+
this.selectDeleteCB = settings.createSelect(settings, dom, node, view, getPos);
|
|
199
|
+
}
|
|
200
|
+
this.createCopyButtonCB = settings.createCopyButton(settings, dom, node, view, this.codeMirrorView, getPos);
|
|
201
|
+
setMode(node.attrs.lang, this.codeMirrorView, settings, this.languageConf);
|
|
202
|
+
const currentTheme = settings.getCurrentTheme?.();
|
|
203
|
+
setTheme(this.codeMirrorView, themeConfig, [
|
|
204
|
+
settings.themes.find((t) => t.name === currentTheme),
|
|
205
|
+
]);
|
|
206
|
+
this.updateTheme = (theme) => {
|
|
207
|
+
setTheme(this.codeMirrorView, themeConfig, [
|
|
208
|
+
settings.themes.find((t) => t.name === theme),
|
|
209
|
+
]);
|
|
210
|
+
};
|
|
211
|
+
themeCallbacks.push(this.updateTheme);
|
|
212
|
+
}
|
|
213
|
+
updateTheme(updateTheme) {
|
|
214
|
+
throw new Error('Method not implemented.');
|
|
215
|
+
}
|
|
216
|
+
selectNode() {
|
|
217
|
+
this.codeMirrorView.focus();
|
|
218
|
+
}
|
|
219
|
+
stopEvent(e) {
|
|
220
|
+
return this.settings.stopEvent(e, this.node, this.getPos, this.view, this.dom);
|
|
221
|
+
}
|
|
222
|
+
setSelection(anchor, head) {
|
|
223
|
+
this.codeMirrorView.focus();
|
|
224
|
+
this.updating = true;
|
|
225
|
+
this.codeMirrorView.dispatch({ selection: { anchor, head } });
|
|
226
|
+
this.updating = false;
|
|
227
|
+
}
|
|
228
|
+
update(updateNode) {
|
|
229
|
+
if (updateNode.type.name !== this.node.type.name)
|
|
230
|
+
return false;
|
|
231
|
+
if (updateNode.attrs.lang !== this.node.attrs.lang) {
|
|
232
|
+
setMode(updateNode.attrs.lang, this.codeMirrorView, this.settings, this.languageConf);
|
|
233
|
+
}
|
|
234
|
+
const oldNode = this.node;
|
|
235
|
+
this.node = updateNode;
|
|
236
|
+
const change = computeChange(this.codeMirrorView.state.doc.toString(), this.node.textContent);
|
|
237
|
+
if (change) {
|
|
238
|
+
this.updating = true;
|
|
239
|
+
this.codeMirrorView.dispatch({
|
|
240
|
+
changes: {
|
|
241
|
+
from: change.from,
|
|
242
|
+
to: change.to,
|
|
243
|
+
insert: change.text,
|
|
244
|
+
},
|
|
245
|
+
selection: { anchor: change.from + change.text.length },
|
|
246
|
+
});
|
|
247
|
+
this.updating = false;
|
|
248
|
+
}
|
|
249
|
+
this.settings.updateSelect(this.settings, this.dom, updateNode, this.view, this.getPos, oldNode);
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
ignoreMutation() {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
destroy() {
|
|
256
|
+
if (this.selectDeleteCB) {
|
|
257
|
+
this.selectDeleteCB();
|
|
258
|
+
}
|
|
259
|
+
this.createCopyButtonCB();
|
|
260
|
+
themeCallbacks.splice(themeCallbacks.indexOf(this.updateTheme), 1);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
export const codeMirrorBlockNodeView = (settings) => {
|
|
264
|
+
return (pmNode, view, getPos) => {
|
|
265
|
+
return new CodeMirrorBlockNodeView(pmNode, view, getPos, settings);
|
|
266
|
+
};
|
|
267
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Node } from 'prosemirror-model';
|
|
2
|
+
import { EditorView } from 'prosemirror-view';
|
|
3
|
+
import { CodeBlockSettings } from './types.js';
|
|
4
|
+
export declare const defaultCreateSelect: (settings: CodeBlockSettings, dom: HTMLElement, node: Node, view: EditorView, getPos: (() => number) | boolean) => () => void;
|
|
5
|
+
export declare const defaultSettings: CodeBlockSettings;
|
|
6
|
+
//# sourceMappingURL=defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../../../src/extension-codemirror/src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C,eAAO,MAAM,mBAAmB,aACpB,iBAAiB,OACtB,WAAW,QACV,IAAI,QACJ,UAAU,UACR,CAAC,MAAM,MAAM,CAAC,GAAG,OAAO,eAwCjC,CAAC;AAmBF,eAAO,MAAM,eAAe,EAAE,iBAS7B,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export const defaultCreateSelect = (settings, dom, node, view, getPos) => {
|
|
2
|
+
if (!settings.languageLoaders)
|
|
3
|
+
return () => { };
|
|
4
|
+
const { languageLoaders } = settings;
|
|
5
|
+
const select = document.createElement('select');
|
|
6
|
+
select.className = 'codeblock-select';
|
|
7
|
+
const noneOption = document.createElement('option');
|
|
8
|
+
noneOption.value = 'none';
|
|
9
|
+
noneOption.textContent = settings.languageNameMap?.none || 'none';
|
|
10
|
+
select.append(noneOption);
|
|
11
|
+
Object.keys(languageLoaders)
|
|
12
|
+
.sort()
|
|
13
|
+
.forEach((lang) => {
|
|
14
|
+
if (settings.languageWhitelist &&
|
|
15
|
+
!settings.languageWhitelist.includes(lang)) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const option = document.createElement('option');
|
|
19
|
+
option.value = lang;
|
|
20
|
+
option.textContent = settings.languageNameMap?.[lang] || lang;
|
|
21
|
+
select.append(option);
|
|
22
|
+
});
|
|
23
|
+
select.value = node.attrs.lang || 'none';
|
|
24
|
+
dom.prepend(select);
|
|
25
|
+
select.onchange = async (e) => {
|
|
26
|
+
if (!(e.target instanceof HTMLSelectElement))
|
|
27
|
+
return;
|
|
28
|
+
const lang = e.target.value;
|
|
29
|
+
if (typeof getPos === 'function') {
|
|
30
|
+
view.dispatch(view.state.tr.setNodeMarkup(getPos(), undefined, {
|
|
31
|
+
...node.attrs,
|
|
32
|
+
lang,
|
|
33
|
+
}));
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
// Delete code.
|
|
37
|
+
return () => { };
|
|
38
|
+
};
|
|
39
|
+
const defaultUpdateSelect = (settings, dom, node, view, getPos, oldNode) => {
|
|
40
|
+
if (oldNode.attrs.lang !== node.attrs.lang) {
|
|
41
|
+
const select = dom.querySelector('.codeblock-select');
|
|
42
|
+
if (!(select instanceof HTMLSelectElement))
|
|
43
|
+
return;
|
|
44
|
+
select.value = node.attrs.lang || 'none';
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const defaultStopEvent = () => true;
|
|
48
|
+
export const defaultSettings = {
|
|
49
|
+
createSelect: defaultCreateSelect,
|
|
50
|
+
updateSelect: defaultUpdateSelect,
|
|
51
|
+
createCopyButton: () => {
|
|
52
|
+
return () => { };
|
|
53
|
+
},
|
|
54
|
+
stopEvent: defaultStopEvent,
|
|
55
|
+
readOnly: false,
|
|
56
|
+
themes: [],
|
|
57
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"languageLoaders.d.ts","sourceRoot":"","sources":["../../../src/extension-codemirror/src/languageLoaders.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAE7C,QAAA,MAAM,eAAe,EAAE,eA6BtB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,eAyWnC,CAAC;AAEF,eAAe,eAAe,CAAC"}
|