@atlaskit/editor-plugin-code-block-advanced 1.0.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/CHANGELOG.md +13 -0
- package/LICENSE.md +11 -0
- package/README.md +1 -0
- package/afm-cc/tsconfig.json +36 -0
- package/build/tsconfig.json +17 -0
- package/dist/cjs/codeBlockAdvancedPlugin.js +33 -0
- package/dist/cjs/codeBlockAdvancedPluginType.js +5 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/nodeviews/codeBlockAdvanced.js +212 -0
- package/dist/cjs/nodeviews/codeBlockNodeWithToDOMFixed.js +55 -0
- package/dist/cjs/nodeviews/codemirrorSync/syncCMWithPM.js +38 -0
- package/dist/cjs/nodeviews/codemirrorSync/updateCMSelection.js +36 -0
- package/dist/cjs/nodeviews/extensions/bidiCharWarning.js +83 -0
- package/dist/cjs/nodeviews/extensions/keymap/backspace.js +33 -0
- package/dist/cjs/nodeviews/extensions/keymap/index.js +129 -0
- package/dist/cjs/nodeviews/extensions/keymap/maybeEscape.js +63 -0
- package/dist/cjs/nodeviews/languages/languageMap.js +100 -0
- package/dist/cjs/nodeviews/languages/loader.js +50 -0
- package/dist/cjs/nodeviews/lazyCodeBlockAdvanced.js +29 -0
- package/dist/cjs/pm-plugins/main.js +17 -0
- package/dist/cjs/ui/syntaxHighlightingTheme.js +76 -0
- package/dist/cjs/ui/theme.js +55 -0
- package/dist/es2019/codeBlockAdvancedPlugin.js +26 -0
- package/dist/es2019/codeBlockAdvancedPluginType.js +1 -0
- package/dist/es2019/index.js +4 -0
- package/dist/es2019/nodeviews/codeBlockAdvanced.js +170 -0
- package/dist/es2019/nodeviews/codeBlockNodeWithToDOMFixed.js +42 -0
- package/dist/es2019/nodeviews/codemirrorSync/syncCMWithPM.js +35 -0
- package/dist/es2019/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
- package/dist/es2019/nodeviews/extensions/bidiCharWarning.js +53 -0
- package/dist/es2019/nodeviews/extensions/keymap/backspace.js +28 -0
- package/dist/es2019/nodeviews/extensions/keymap/index.js +107 -0
- package/dist/es2019/nodeviews/extensions/keymap/maybeEscape.js +57 -0
- package/dist/es2019/nodeviews/languages/languageMap.js +82 -0
- package/dist/es2019/nodeviews/languages/loader.js +37 -0
- package/dist/es2019/nodeviews/lazyCodeBlockAdvanced.js +18 -0
- package/dist/es2019/pm-plugins/main.js +11 -0
- package/dist/es2019/ui/syntaxHighlightingTheme.js +70 -0
- package/dist/es2019/ui/theme.js +49 -0
- package/dist/esm/codeBlockAdvancedPlugin.js +27 -0
- package/dist/esm/codeBlockAdvancedPluginType.js +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/nodeviews/codeBlockAdvanced.js +205 -0
- package/dist/esm/nodeviews/codeBlockNodeWithToDOMFixed.js +48 -0
- package/dist/esm/nodeviews/codemirrorSync/syncCMWithPM.js +32 -0
- package/dist/esm/nodeviews/codemirrorSync/updateCMSelection.js +30 -0
- package/dist/esm/nodeviews/extensions/bidiCharWarning.js +77 -0
- package/dist/esm/nodeviews/extensions/keymap/backspace.js +27 -0
- package/dist/esm/nodeviews/extensions/keymap/index.js +122 -0
- package/dist/esm/nodeviews/extensions/keymap/maybeEscape.js +56 -0
- package/dist/esm/nodeviews/languages/languageMap.js +86 -0
- package/dist/esm/nodeviews/languages/loader.js +44 -0
- package/dist/esm/nodeviews/lazyCodeBlockAdvanced.js +17 -0
- package/dist/esm/pm-plugins/main.js +11 -0
- package/dist/esm/ui/syntaxHighlightingTheme.js +70 -0
- package/dist/esm/ui/theme.js +49 -0
- package/dist/types/codeBlockAdvancedPlugin.d.ts +2 -0
- package/dist/types/codeBlockAdvancedPluginType.d.ts +11 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/nodeviews/codeBlockAdvanced.d.ts +38 -0
- package/dist/types/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
- package/dist/types/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
- package/dist/types/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
- package/dist/types/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
- package/dist/types/nodeviews/extensions/keymap/backspace.d.ts +12 -0
- package/dist/types/nodeviews/extensions/keymap/index.d.ts +14 -0
- package/dist/types/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
- package/dist/types/nodeviews/languages/languageMap.d.ts +5 -0
- package/dist/types/nodeviews/languages/loader.d.ts +11 -0
- package/dist/types/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
- package/dist/types/pm-plugins/main.d.ts +10 -0
- package/dist/types/ui/syntaxHighlightingTheme.d.ts +2 -0
- package/dist/types/ui/theme.d.ts +1 -0
- package/dist/types-ts4.5/codeBlockAdvancedPlugin.d.ts +2 -0
- package/dist/types-ts4.5/codeBlockAdvancedPluginType.d.ts +15 -0
- package/dist/types-ts4.5/index.d.ts +2 -0
- package/dist/types-ts4.5/nodeviews/codeBlockAdvanced.d.ts +38 -0
- package/dist/types-ts4.5/nodeviews/codeBlockNodeWithToDOMFixed.d.ts +2 -0
- package/dist/types-ts4.5/nodeviews/codemirrorSync/syncCMWithPM.d.ts +17 -0
- package/dist/types-ts4.5/nodeviews/codemirrorSync/updateCMSelection.d.ts +11 -0
- package/dist/types-ts4.5/nodeviews/extensions/bidiCharWarning.d.ts +8 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/backspace.d.ts +12 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/index.d.ts +14 -0
- package/dist/types-ts4.5/nodeviews/extensions/keymap/maybeEscape.d.ts +17 -0
- package/dist/types-ts4.5/nodeviews/languages/languageMap.d.ts +5 -0
- package/dist/types-ts4.5/nodeviews/languages/loader.d.ts +12 -0
- package/dist/types-ts4.5/nodeviews/lazyCodeBlockAdvanced.d.ts +9 -0
- package/dist/types-ts4.5/pm-plugins/main.d.ts +10 -0
- package/dist/types-ts4.5/ui/syntaxHighlightingTheme.d.ts +2 -0
- package/dist/types-ts4.5/ui/theme.d.ts +1 -0
- package/docs/0-intro.tsx +56 -0
- package/package.json +96 -0
- package/src/codeBlockAdvancedPlugin.tsx +25 -0
- package/src/codeBlockAdvancedPluginType.ts +18 -0
- package/src/index.ts +5 -0
- package/src/nodeviews/codeBlockAdvanced.ts +223 -0
- package/src/nodeviews/codeBlockNodeWithToDOMFixed.ts +59 -0
- package/src/nodeviews/codemirrorSync/syncCMWithPM.ts +40 -0
- package/src/nodeviews/codemirrorSync/updateCMSelection.ts +40 -0
- package/src/nodeviews/extensions/bidiCharWarning.ts +72 -0
- package/src/nodeviews/extensions/keymap/backspace.ts +47 -0
- package/src/nodeviews/extensions/keymap/index.ts +118 -0
- package/src/nodeviews/extensions/keymap/maybeEscape.ts +69 -0
- package/src/nodeviews/languages/languageMap.ts +91 -0
- package/src/nodeviews/languages/loader.ts +44 -0
- package/src/nodeviews/lazyCodeBlockAdvanced.ts +32 -0
- package/src/pm-plugins/main.ts +22 -0
- package/src/ui/syntaxHighlightingTheme.ts +50 -0
- package/src/ui/theme.ts +52 -0
- package/tsconfig.json +10 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import { closeBrackets } from '@codemirror/autocomplete';
|
|
3
|
+
import { syntaxHighlighting } from '@codemirror/language';
|
|
4
|
+
import { Compartment, EditorSelection } from '@codemirror/state';
|
|
5
|
+
import { EditorView as CodeMirror, lineNumbers } from '@codemirror/view';
|
|
6
|
+
import { isCodeBlockWordWrapEnabled } from '@atlaskit/editor-common/code-block';
|
|
7
|
+
import { NodeSelection } from '@atlaskit/editor-prosemirror/state';
|
|
8
|
+
import { highlightStyle } from '../ui/syntaxHighlightingTheme';
|
|
9
|
+
import { cmTheme } from '../ui/theme';
|
|
10
|
+
import { syncCMWithPM } from './codemirrorSync/syncCMWithPM';
|
|
11
|
+
import { updateCMSelection } from './codemirrorSync/updateCMSelection';
|
|
12
|
+
import { bidiCharWarningExtension } from './extensions/bidiCharWarning';
|
|
13
|
+
import { keymapExtension } from './extensions/keymap';
|
|
14
|
+
import { LanguageLoader } from './languages/loader';
|
|
15
|
+
// Based on: https://prosemirror.net/examples/codemirror/
|
|
16
|
+
class CodeBlockAdvancedNodeView {
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
18
|
+
constructor(node, view, getPos, config) {
|
|
19
|
+
var _config$api, _config$api$selection, _config$api2, _config$api2$editorDi;
|
|
20
|
+
_defineProperty(this, "lineWrappingCompartment", new Compartment());
|
|
21
|
+
_defineProperty(this, "languageCompartment", new Compartment());
|
|
22
|
+
_defineProperty(this, "readOnlyCompartment", new Compartment());
|
|
23
|
+
_defineProperty(this, "maybeTryingToReachNodeSelection", false);
|
|
24
|
+
_defineProperty(this, "wordWrappingEnabled", false);
|
|
25
|
+
this.node = node;
|
|
26
|
+
this.view = view;
|
|
27
|
+
this.getPos = getPos;
|
|
28
|
+
this.selectionAPI = (_config$api = config.api) === null || _config$api === void 0 ? void 0 : (_config$api$selection = _config$api.selection) === null || _config$api$selection === void 0 ? void 0 : _config$api$selection.actions;
|
|
29
|
+
const getNode = () => this.node;
|
|
30
|
+
const onMaybeNodeSelection = () => this.maybeTryingToReachNodeSelection = true;
|
|
31
|
+
this.cleanupDisabledState = (_config$api2 = config.api) === null || _config$api2 === void 0 ? void 0 : (_config$api2$editorDi = _config$api2.editorDisabled) === null || _config$api2$editorDi === void 0 ? void 0 : _config$api2$editorDi.sharedState.onChange(() => {
|
|
32
|
+
this.updateReadonlyState();
|
|
33
|
+
});
|
|
34
|
+
this.languageLoader = new LanguageLoader(lang => {
|
|
35
|
+
this.updating = true;
|
|
36
|
+
this.cm.dispatch({
|
|
37
|
+
effects: this.languageCompartment.reconfigure(lang)
|
|
38
|
+
});
|
|
39
|
+
this.updating = false;
|
|
40
|
+
});
|
|
41
|
+
this.cm = new CodeMirror({
|
|
42
|
+
doc: this.node.textContent,
|
|
43
|
+
extensions: [...config.extensions, this.lineWrappingCompartment.of([]), this.languageCompartment.of([]), keymapExtension({
|
|
44
|
+
view,
|
|
45
|
+
getPos,
|
|
46
|
+
getNode,
|
|
47
|
+
selectCodeBlockNode: this.selectCodeBlockNode.bind(this),
|
|
48
|
+
onMaybeNodeSelection
|
|
49
|
+
}), cmTheme, syntaxHighlighting(highlightStyle), lineNumbers(), CodeMirror.updateListener.of(update => this.forwardUpdate(update)), this.readOnlyCompartment.of(CodeMirror.editable.of(this.view.editable)), closeBrackets(), CodeMirror.editorAttributes.of({
|
|
50
|
+
class: 'code-block'
|
|
51
|
+
}), bidiCharWarningExtension]
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// The editor's outer node is our DOM representation
|
|
55
|
+
this.dom = this.cm.dom;
|
|
56
|
+
|
|
57
|
+
// This flag is used to avoid an update loop between the outer and
|
|
58
|
+
// inner editor
|
|
59
|
+
this.updating = false;
|
|
60
|
+
this.updateLanguage();
|
|
61
|
+
}
|
|
62
|
+
destroy() {
|
|
63
|
+
var _this$cleanupDisabled;
|
|
64
|
+
(_this$cleanupDisabled = this.cleanupDisabledState) === null || _this$cleanupDisabled === void 0 ? void 0 : _this$cleanupDisabled.call(this);
|
|
65
|
+
}
|
|
66
|
+
forwardUpdate(update) {
|
|
67
|
+
var _this$getPos, _this$getPos2;
|
|
68
|
+
if (this.updating || !this.cm.hasFocus) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const offset = ((_this$getPos = (_this$getPos2 = this.getPos) === null || _this$getPos2 === void 0 ? void 0 : _this$getPos2.call(this)) !== null && _this$getPos !== void 0 ? _this$getPos : 0) + 1;
|
|
72
|
+
syncCMWithPM({
|
|
73
|
+
view: this.view,
|
|
74
|
+
update,
|
|
75
|
+
offset
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
setSelection(anchor, head) {
|
|
79
|
+
if (!this.maybeTryingToReachNodeSelection) {
|
|
80
|
+
this.cm.focus();
|
|
81
|
+
}
|
|
82
|
+
this.updating = true;
|
|
83
|
+
this.cm.dispatch({
|
|
84
|
+
selection: {
|
|
85
|
+
anchor,
|
|
86
|
+
head
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
this.updating = false;
|
|
90
|
+
}
|
|
91
|
+
updateReadonlyState() {
|
|
92
|
+
this.updating = true;
|
|
93
|
+
this.cm.dispatch({
|
|
94
|
+
effects: this.readOnlyCompartment.reconfigure(CodeMirror.editable.of(this.view.editable))
|
|
95
|
+
});
|
|
96
|
+
this.updating = false;
|
|
97
|
+
}
|
|
98
|
+
updateLanguage() {
|
|
99
|
+
this.languageLoader.updateLanguage(this.node.attrs.language);
|
|
100
|
+
}
|
|
101
|
+
selectCodeBlockNode(relativeSelectionPos) {
|
|
102
|
+
var _this$selectionAPI, _this$getPos3, _this$getPos4;
|
|
103
|
+
const tr = (_this$selectionAPI = this.selectionAPI) === null || _this$selectionAPI === void 0 ? void 0 : _this$selectionAPI.selectNearNode({
|
|
104
|
+
selectionRelativeToNode: relativeSelectionPos,
|
|
105
|
+
selection: NodeSelection.create(this.view.state.doc, (_this$getPos3 = (_this$getPos4 = this.getPos) === null || _this$getPos4 === void 0 ? void 0 : _this$getPos4.call(this)) !== null && _this$getPos3 !== void 0 ? _this$getPos3 : 0)
|
|
106
|
+
})(this.view.state);
|
|
107
|
+
if (tr) {
|
|
108
|
+
this.view.dispatch(tr);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
updateWordWrap(node) {
|
|
112
|
+
if (this.wordWrappingEnabled !== isCodeBlockWordWrapEnabled(node)) {
|
|
113
|
+
this.updating = true;
|
|
114
|
+
this.cm.dispatch({
|
|
115
|
+
effects: [this.lineWrappingCompartment.reconfigure(isCodeBlockWordWrapEnabled(node) ? CodeMirror.lineWrapping : [])]
|
|
116
|
+
});
|
|
117
|
+
this.updating = false;
|
|
118
|
+
this.wordWrappingEnabled = !this.wordWrappingEnabled;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
update(node) {
|
|
122
|
+
this.maybeTryingToReachNodeSelection = false;
|
|
123
|
+
if (node.type !== this.node.type) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
this.updateWordWrap(node);
|
|
127
|
+
this.node = node;
|
|
128
|
+
if (this.updating) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
this.updateLanguage();
|
|
132
|
+
const newText = node.textContent,
|
|
133
|
+
curText = this.cm.state.doc.toString();
|
|
134
|
+
updateCMSelection(curText, newText, tr => {
|
|
135
|
+
this.updating = true;
|
|
136
|
+
this.cm.dispatch(tr);
|
|
137
|
+
this.updating = false;
|
|
138
|
+
});
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
stopEvent(e) {
|
|
142
|
+
var _this$getPos5;
|
|
143
|
+
if (e instanceof MouseEvent && e.type === 'mousedown') {
|
|
144
|
+
// !Warning: Side effect!
|
|
145
|
+
// CodeMirror on blur updates the dom observer with a `setTimeout(..., 10);`
|
|
146
|
+
// We need to select the nodeview after this has taken place to ensure
|
|
147
|
+
// ProseMirror takes over
|
|
148
|
+
// https://github.com/codemirror/view/commit/70a9a253df04a57004247b9463198c17832f92f4#diff-cb8cbffa623ff0975389e7e8c315e69d5e10345239ffe2c9b4b7986a56ad95efR720
|
|
149
|
+
setTimeout(() => {
|
|
150
|
+
// Ensure the CM selection is reset - if we have a ranged selection when we do node selection can
|
|
151
|
+
// cause funky behaviour
|
|
152
|
+
this.updating = true;
|
|
153
|
+
this.cm.dispatch({
|
|
154
|
+
selection: EditorSelection.create([EditorSelection.cursor(0)], 0)
|
|
155
|
+
});
|
|
156
|
+
this.updating = false;
|
|
157
|
+
this.selectCodeBlockNode(undefined);
|
|
158
|
+
this.view.focus();
|
|
159
|
+
}, 20);
|
|
160
|
+
}
|
|
161
|
+
// If we have selected the node we should not stop these events
|
|
162
|
+
if (e instanceof KeyboardEvent && this.view.state.selection instanceof NodeSelection && this.view.state.selection.from === ((_this$getPos5 = this.getPos) === null || _this$getPos5 === void 0 ? void 0 : _this$getPos5.call(this))) {
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export const getCodeBlockAdvancedNodeView = props => (node, view, getPos) => {
|
|
169
|
+
return new CodeBlockAdvancedNodeView(node, view, getPos, props);
|
|
170
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { codeBlock } from '@atlaskit/adf-schema';
|
|
2
|
+
import { CodeBlockSharedCssClassName } from '@atlaskit/editor-common/styles';
|
|
3
|
+
const codeBlockClassNames = {
|
|
4
|
+
container: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER,
|
|
5
|
+
start: CodeBlockSharedCssClassName.CODEBLOCK_START,
|
|
6
|
+
end: CodeBlockSharedCssClassName.CODEBLOCK_END,
|
|
7
|
+
contentWrapper: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPER,
|
|
8
|
+
contentWrapped: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT_WRAPPED,
|
|
9
|
+
gutter: CodeBlockSharedCssClassName.CODEBLOCK_LINE_NUMBER_GUTTER,
|
|
10
|
+
content: CodeBlockSharedCssClassName.CODEBLOCK_CONTENT,
|
|
11
|
+
lineNumberWidget: CodeBlockSharedCssClassName.CODEBLOCK_CONTAINER_LINE_NUMBER_WIDGET
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
// From: `packages/editor/editor-plugin-code-block/src/nodeviews/code-block.ts`
|
|
15
|
+
const toDOM = (node, contentEditable, formattedAriaLabel) => ['div', {
|
|
16
|
+
class: codeBlockClassNames.container
|
|
17
|
+
}, ['div', {
|
|
18
|
+
class: codeBlockClassNames.start,
|
|
19
|
+
contenteditable: 'false'
|
|
20
|
+
}], ['div', {
|
|
21
|
+
class: codeBlockClassNames.contentWrapper
|
|
22
|
+
}, ['div', {
|
|
23
|
+
class: codeBlockClassNames.gutter,
|
|
24
|
+
contenteditable: 'false'
|
|
25
|
+
}], ['div', {
|
|
26
|
+
class: codeBlockClassNames.content
|
|
27
|
+
}, ['code', {
|
|
28
|
+
'data-language': node.attrs.language || '',
|
|
29
|
+
spellcheck: 'false',
|
|
30
|
+
contenteditable: contentEditable ? 'true' : 'false',
|
|
31
|
+
'data-testid': 'code-block--code',
|
|
32
|
+
'aria-label': formattedAriaLabel
|
|
33
|
+
}, 0]]], ['div', {
|
|
34
|
+
class: codeBlockClassNames.end,
|
|
35
|
+
contenteditable: 'false'
|
|
36
|
+
}]];
|
|
37
|
+
export const codeBlockNodeWithFixedToDOM = () => {
|
|
38
|
+
return {
|
|
39
|
+
...codeBlock,
|
|
40
|
+
toDOM: node => toDOM(node, false, '')
|
|
41
|
+
};
|
|
42
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
|
|
2
|
+
/**
|
|
3
|
+
*
|
|
4
|
+
* Synchronises the CodeMirror update changes with the Prosemirror editor
|
|
5
|
+
*
|
|
6
|
+
* @param props.view EditorView - Prosemirror EditorView
|
|
7
|
+
* @param props.update ViewUpdate - CodeMirror ViewUpdate
|
|
8
|
+
* @param props.offset number - position where the code block starts in prosemirror
|
|
9
|
+
*/
|
|
10
|
+
export const syncCMWithPM = ({
|
|
11
|
+
view,
|
|
12
|
+
update,
|
|
13
|
+
offset
|
|
14
|
+
}) => {
|
|
15
|
+
const {
|
|
16
|
+
main
|
|
17
|
+
} = update.state.selection;
|
|
18
|
+
const selFrom = offset + main.from;
|
|
19
|
+
const selTo = offset + main.to;
|
|
20
|
+
const pmSel = view.state.selection;
|
|
21
|
+
if (update.docChanged || pmSel.from !== selFrom || pmSel.to !== selTo) {
|
|
22
|
+
const tr = view.state.tr;
|
|
23
|
+
// eslint-disable-next-line @typescript-eslint/max-params
|
|
24
|
+
update.changes.iterChanges((fromA, toA, fromB, toB, text) => {
|
|
25
|
+
if (text.length) {
|
|
26
|
+
tr.replaceWith(offset + fromA, offset + toA, view.state.schema.text(text.toString()));
|
|
27
|
+
} else {
|
|
28
|
+
tr.delete(offset + fromA, offset + toA);
|
|
29
|
+
}
|
|
30
|
+
offset += toB - fromB - (toA - fromA);
|
|
31
|
+
});
|
|
32
|
+
tr.setSelection(TextSelection.create(tr.doc, selFrom, selTo));
|
|
33
|
+
view.dispatch(tr);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* Compares the updated text with the current to determine the transaction to fire
|
|
4
|
+
* in the CodeMirror editor.
|
|
5
|
+
*
|
|
6
|
+
* @param curText string - the current CodeMirror text
|
|
7
|
+
* @param newText string - the new CodeMirror text
|
|
8
|
+
* @param updateCallback Callback to process the CodeMirror transaction
|
|
9
|
+
*/
|
|
10
|
+
export const updateCMSelection = (curText, newText, updateCallback) => {
|
|
11
|
+
if (newText !== curText) {
|
|
12
|
+
let start = 0,
|
|
13
|
+
curEnd = curText.length,
|
|
14
|
+
newEnd = newText.length;
|
|
15
|
+
while (start < curEnd && curText.charCodeAt(start) === newText.charCodeAt(start)) {
|
|
16
|
+
++start;
|
|
17
|
+
}
|
|
18
|
+
while (curEnd > start && newEnd > start && curText.charCodeAt(curEnd - 1) === newText.charCodeAt(newEnd - 1)) {
|
|
19
|
+
curEnd--;
|
|
20
|
+
newEnd--;
|
|
21
|
+
}
|
|
22
|
+
updateCallback({
|
|
23
|
+
changes: {
|
|
24
|
+
from: start,
|
|
25
|
+
to: curEnd,
|
|
26
|
+
insert: newText.slice(start, newEnd)
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Code based on warning from @atlaskit/code
|
|
3
|
+
*/
|
|
4
|
+
import { EditorView as CodeMirror, ViewPlugin, Decoration, MatchDecorator, WidgetType } from '@codemirror/view';
|
|
5
|
+
const bidiCharacterRegex = /[\u202A-\u202E\u2066-\u2069]/gu;
|
|
6
|
+
const placeholderMatcher = new MatchDecorator({
|
|
7
|
+
regexp: bidiCharacterRegex,
|
|
8
|
+
decoration: match => Decoration.replace({
|
|
9
|
+
widget: new PlaceholderWidget(match[0])
|
|
10
|
+
})
|
|
11
|
+
});
|
|
12
|
+
export const bidiCharWarningExtension = ViewPlugin.fromClass(class {
|
|
13
|
+
constructor(view) {
|
|
14
|
+
this.placeholders = placeholderMatcher.createDeco(view);
|
|
15
|
+
}
|
|
16
|
+
update(update) {
|
|
17
|
+
this.placeholders = placeholderMatcher.updateDeco(update, this.placeholders);
|
|
18
|
+
}
|
|
19
|
+
}, {
|
|
20
|
+
decorations: instance => instance.placeholders,
|
|
21
|
+
provide: plugin => CodeMirror.atomicRanges.of(view => {
|
|
22
|
+
var _view$plugin;
|
|
23
|
+
return ((_view$plugin = view.plugin(plugin)) === null || _view$plugin === void 0 ? void 0 : _view$plugin.placeholders) || Decoration.none;
|
|
24
|
+
})
|
|
25
|
+
});
|
|
26
|
+
function getBidiCharacterCode(bidiCharacter) {
|
|
27
|
+
var _bidiCharacter$codePo;
|
|
28
|
+
const bidiCharacterCode = (_bidiCharacter$codePo = bidiCharacter.codePointAt(0)) === null || _bidiCharacter$codePo === void 0 ? void 0 : _bidiCharacter$codePo.toString(16);
|
|
29
|
+
return `U+${bidiCharacterCode}`;
|
|
30
|
+
}
|
|
31
|
+
class PlaceholderWidget extends WidgetType {
|
|
32
|
+
constructor(name) {
|
|
33
|
+
super();
|
|
34
|
+
this.name = name;
|
|
35
|
+
}
|
|
36
|
+
eq(other) {
|
|
37
|
+
return this.name === other.name;
|
|
38
|
+
}
|
|
39
|
+
toDOM() {
|
|
40
|
+
const elt = document.createElement('span');
|
|
41
|
+
elt.setAttribute('data-bidi-character-code', this.name);
|
|
42
|
+
elt.style.cssText = `
|
|
43
|
+
padding: 0 3px;
|
|
44
|
+
color: ${"var(--ds-text-warning, #A54800)"};
|
|
45
|
+
background: ${"var(--ds-background-warning, #FFF7D6)"};
|
|
46
|
+
aria-hidden="true"`;
|
|
47
|
+
elt.textContent = `<${getBidiCharacterCode(this.name)}>`;
|
|
48
|
+
return elt;
|
|
49
|
+
}
|
|
50
|
+
ignoreEvent() {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TextSelection } from '@atlaskit/editor-prosemirror/state';
|
|
2
|
+
export const backspaceKeymap = ({
|
|
3
|
+
cm,
|
|
4
|
+
view,
|
|
5
|
+
getPos,
|
|
6
|
+
getNode
|
|
7
|
+
}) => {
|
|
8
|
+
var _getPos;
|
|
9
|
+
const ranges = cm.state.selection.ranges;
|
|
10
|
+
if (ranges.length > 1) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
const selection = ranges[0];
|
|
14
|
+
if (selection && (!selection.empty || selection.anchor > 0)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
if (cm.state.doc.lines >= 2) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const state = view.state;
|
|
21
|
+
const pos = (_getPos = getPos()) !== null && _getPos !== void 0 ? _getPos : 0;
|
|
22
|
+
const node = getNode();
|
|
23
|
+
const tr = state.tr.replaceWith(pos, pos + node.nodeSize, state.schema.nodes.paragraph.createChecked({}, node.content));
|
|
24
|
+
tr.setSelection(TextSelection.near(tr.doc.resolve(pos)));
|
|
25
|
+
view.dispatch(tr);
|
|
26
|
+
view.focus();
|
|
27
|
+
return true;
|
|
28
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { defaultKeymap, indentWithTab } from '@codemirror/commands';
|
|
2
|
+
import { keymap as cmKeymap } from '@codemirror/view';
|
|
3
|
+
import { exitCode } from '@atlaskit/editor-prosemirror/commands';
|
|
4
|
+
import { undo, redo } from '@atlaskit/editor-prosemirror/history';
|
|
5
|
+
import { backspaceKeymap } from './backspace';
|
|
6
|
+
import { maybeEscapeKeymap } from './maybeEscape';
|
|
7
|
+
export const keymapExtension = ({
|
|
8
|
+
view,
|
|
9
|
+
getNode,
|
|
10
|
+
getPos,
|
|
11
|
+
selectCodeBlockNode,
|
|
12
|
+
onMaybeNodeSelection
|
|
13
|
+
}) => {
|
|
14
|
+
return cmKeymap.of(codeBlockKeymap({
|
|
15
|
+
view,
|
|
16
|
+
getNode,
|
|
17
|
+
getPos,
|
|
18
|
+
selectCodeBlockNode,
|
|
19
|
+
onMaybeNodeSelection
|
|
20
|
+
}));
|
|
21
|
+
};
|
|
22
|
+
const codeBlockKeymap = ({
|
|
23
|
+
view,
|
|
24
|
+
getNode,
|
|
25
|
+
getPos,
|
|
26
|
+
selectCodeBlockNode,
|
|
27
|
+
onMaybeNodeSelection
|
|
28
|
+
}) => {
|
|
29
|
+
return [{
|
|
30
|
+
key: 'ArrowUp',
|
|
31
|
+
run: cm => maybeEscapeKeymap({
|
|
32
|
+
unit: 'line',
|
|
33
|
+
dir: -1,
|
|
34
|
+
cm,
|
|
35
|
+
view,
|
|
36
|
+
getNode,
|
|
37
|
+
getPos,
|
|
38
|
+
selectCodeBlockNode,
|
|
39
|
+
onMaybeNodeSelection
|
|
40
|
+
})
|
|
41
|
+
}, {
|
|
42
|
+
key: 'ArrowLeft',
|
|
43
|
+
run: cm => maybeEscapeKeymap({
|
|
44
|
+
unit: 'char',
|
|
45
|
+
dir: -1,
|
|
46
|
+
cm,
|
|
47
|
+
view,
|
|
48
|
+
getNode,
|
|
49
|
+
getPos,
|
|
50
|
+
selectCodeBlockNode,
|
|
51
|
+
onMaybeNodeSelection
|
|
52
|
+
})
|
|
53
|
+
}, {
|
|
54
|
+
key: 'ArrowDown',
|
|
55
|
+
run: cm => maybeEscapeKeymap({
|
|
56
|
+
unit: 'line',
|
|
57
|
+
dir: 1,
|
|
58
|
+
cm,
|
|
59
|
+
view,
|
|
60
|
+
getNode,
|
|
61
|
+
getPos,
|
|
62
|
+
selectCodeBlockNode,
|
|
63
|
+
onMaybeNodeSelection
|
|
64
|
+
})
|
|
65
|
+
}, {
|
|
66
|
+
key: 'ArrowRight',
|
|
67
|
+
run: cm => maybeEscapeKeymap({
|
|
68
|
+
unit: 'char',
|
|
69
|
+
dir: 1,
|
|
70
|
+
cm,
|
|
71
|
+
view,
|
|
72
|
+
getNode,
|
|
73
|
+
getPos,
|
|
74
|
+
selectCodeBlockNode,
|
|
75
|
+
onMaybeNodeSelection
|
|
76
|
+
})
|
|
77
|
+
}, {
|
|
78
|
+
key: 'Ctrl-Enter',
|
|
79
|
+
run: () => {
|
|
80
|
+
if (!exitCode(view.state, view.dispatch)) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
view.focus();
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
}, {
|
|
87
|
+
key: 'Ctrl-z',
|
|
88
|
+
mac: 'Cmd-z',
|
|
89
|
+
run: () => undo(view.state, view.dispatch)
|
|
90
|
+
}, {
|
|
91
|
+
key: 'Shift-Ctrl-z',
|
|
92
|
+
mac: 'Shift-Cmd-z',
|
|
93
|
+
run: () => redo(view.state, view.dispatch)
|
|
94
|
+
}, {
|
|
95
|
+
key: 'Ctrl-y',
|
|
96
|
+
mac: 'Cmd-y',
|
|
97
|
+
run: () => redo(view.state, view.dispatch)
|
|
98
|
+
}, {
|
|
99
|
+
key: 'Backspace',
|
|
100
|
+
run: cm => backspaceKeymap({
|
|
101
|
+
cm,
|
|
102
|
+
view,
|
|
103
|
+
getNode,
|
|
104
|
+
getPos
|
|
105
|
+
})
|
|
106
|
+
}, ...defaultKeymap.concat(indentWithTab)];
|
|
107
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { RelativeSelectionPos } from '@atlaskit/editor-common/selection';
|
|
2
|
+
import { Selection } from '@atlaskit/editor-prosemirror/state';
|
|
3
|
+
export const maybeEscapeKeymap = ({
|
|
4
|
+
unit,
|
|
5
|
+
dir,
|
|
6
|
+
view,
|
|
7
|
+
cm,
|
|
8
|
+
getPos,
|
|
9
|
+
getNode,
|
|
10
|
+
onMaybeNodeSelection,
|
|
11
|
+
selectCodeBlockNode
|
|
12
|
+
}) => {
|
|
13
|
+
var _getPos;
|
|
14
|
+
if (unit === 'char') {
|
|
15
|
+
onMaybeNodeSelection();
|
|
16
|
+
}
|
|
17
|
+
const node = getNode();
|
|
18
|
+
const {
|
|
19
|
+
state
|
|
20
|
+
} = cm;
|
|
21
|
+
let main = state.selection.main;
|
|
22
|
+
if (!main.empty) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
if (unit === 'line') {
|
|
26
|
+
main = {
|
|
27
|
+
...state.doc.lineAt(main.head),
|
|
28
|
+
head: 0,
|
|
29
|
+
empty: false
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
if (dir < 0 ? main.from > 0 : main.to < state.doc.length) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const targetPos = ((_getPos = getPos === null || getPos === void 0 ? void 0 : getPos()) !== null && _getPos !== void 0 ? _getPos : 0) + (dir < 0 ? 0 : node.nodeSize);
|
|
36
|
+
if (unit === 'char') {
|
|
37
|
+
view.focus();
|
|
38
|
+
selectCodeBlockNode(dir === -1 ? RelativeSelectionPos.Start : RelativeSelectionPos.End);
|
|
39
|
+
return true;
|
|
40
|
+
} else {
|
|
41
|
+
createParagraphIfEndOfDoc(view, targetPos);
|
|
42
|
+
const tr = view.state.tr;
|
|
43
|
+
const selection = Selection.near(tr.doc.resolve(targetPos), dir);
|
|
44
|
+
tr.setSelection(selection).scrollIntoView();
|
|
45
|
+
view.dispatch(tr);
|
|
46
|
+
view.focus();
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const createParagraphIfEndOfDoc = (view, targetPos) => {
|
|
51
|
+
if (targetPos === view.state.doc.content.size) {
|
|
52
|
+
const paragraph = view.state.schema.nodes.paragraph.createChecked({});
|
|
53
|
+
const tr = view.state.tr.insert(targetPos, paragraph);
|
|
54
|
+
// Note: we purposefully do a multi-dispatch here, otherwise we get stuck in codemirror
|
|
55
|
+
view.dispatch(tr);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { LanguageDescription } from '@codemirror/language';
|
|
2
|
+
import { languages } from '@codemirror/language-data';
|
|
3
|
+
// getLanguageIdentifier defines `language.alias[0]`
|
|
4
|
+
export const mapLanguageToCodeMirror = language => {
|
|
5
|
+
if (!language || language === 'none') {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
switch (language) {
|
|
9
|
+
case 'coldfusion':
|
|
10
|
+
return languages.find(l => {
|
|
11
|
+
return l.name.toLowerCase() === 'xml';
|
|
12
|
+
});
|
|
13
|
+
case 'matlab':
|
|
14
|
+
return languages.find(l => {
|
|
15
|
+
return l.name.toLowerCase() === 'octave';
|
|
16
|
+
});
|
|
17
|
+
case 'javafx':
|
|
18
|
+
return languages.find(l => {
|
|
19
|
+
return l.name.toLowerCase() === 'java';
|
|
20
|
+
});
|
|
21
|
+
case 'vbnet':
|
|
22
|
+
return languages.find(l => {
|
|
23
|
+
return l.name === 'VB.NET';
|
|
24
|
+
});
|
|
25
|
+
case 'pas':
|
|
26
|
+
return languages.find(l => {
|
|
27
|
+
return l.name.toLowerCase() === 'pascal';
|
|
28
|
+
});
|
|
29
|
+
case 'cuda':
|
|
30
|
+
return languages.find(l => {
|
|
31
|
+
return l.name.toLowerCase() === 'c++';
|
|
32
|
+
});
|
|
33
|
+
case 'racket':
|
|
34
|
+
return languages.find(l => {
|
|
35
|
+
return l.name === 'Common Lisp';
|
|
36
|
+
});
|
|
37
|
+
case 'abap':
|
|
38
|
+
return languages.find(l => {
|
|
39
|
+
return l.name === 'SQL';
|
|
40
|
+
});
|
|
41
|
+
case 'standardmL':
|
|
42
|
+
return languages.find(l => {
|
|
43
|
+
return l.name === 'SML';
|
|
44
|
+
});
|
|
45
|
+
case 'objective-j':
|
|
46
|
+
return languages.find(l => {
|
|
47
|
+
return l.name === 'Objective-C';
|
|
48
|
+
});
|
|
49
|
+
case 'docker':
|
|
50
|
+
return languages.find(l => {
|
|
51
|
+
return l.name === 'Dockerfile';
|
|
52
|
+
});
|
|
53
|
+
case 'arduino':
|
|
54
|
+
return languages.find(l => {
|
|
55
|
+
return l.name === 'C++';
|
|
56
|
+
});
|
|
57
|
+
case 'visualbasic':
|
|
58
|
+
return languages.find(l => {
|
|
59
|
+
return l.name === 'VB.NET';
|
|
60
|
+
});
|
|
61
|
+
case 'elixir':
|
|
62
|
+
return LanguageDescription.of({
|
|
63
|
+
name: 'Elixir',
|
|
64
|
+
load() {
|
|
65
|
+
return import( /* webpackChunkName: "@atlaskit-internal_@atlaskit/editor-plugin-code-block-advanced-lang-elixir" */
|
|
66
|
+
'codemirror-lang-elixir').then(m => m.elixir());
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
case 'graphql':
|
|
70
|
+
return LanguageDescription.of({
|
|
71
|
+
name: 'GraphQL',
|
|
72
|
+
load() {
|
|
73
|
+
return import( /* webpackChunkName: "@atlaskit-internal_@atlaskit/editor-plugin-code-block-advanced-lang-graphql" */
|
|
74
|
+
'cm6-graphql').then(m => m.graphqlLanguageSupport());
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
default:
|
|
78
|
+
return languages.find(l => {
|
|
79
|
+
return l.alias.includes(language) || l.name.toLowerCase() === (language === null || language === void 0 ? void 0 : language.toLowerCase());
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
import { mapLanguageToCodeMirror } from './languageMap';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Manages loading the languages (for syntax highlighting, etc.)
|
|
6
|
+
* from CodeMirror and updating the language in the CodeMirror view
|
|
7
|
+
*/
|
|
8
|
+
export class LanguageLoader {
|
|
9
|
+
constructor(updateLanguageCompartment) {
|
|
10
|
+
_defineProperty(this, "languageName", '');
|
|
11
|
+
this.updateLanguageCompartment = updateLanguageCompartment;
|
|
12
|
+
}
|
|
13
|
+
updateLanguage(languageName) {
|
|
14
|
+
if (languageName === this.languageName) {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
const language = mapLanguageToCodeMirror(languageName);
|
|
18
|
+
const configureEmpty = () => {
|
|
19
|
+
this.updateLanguageCompartment([]);
|
|
20
|
+
this.languageName = '';
|
|
21
|
+
};
|
|
22
|
+
if (!language) {
|
|
23
|
+
configureEmpty();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
language.load().then(lang => {
|
|
27
|
+
if (lang) {
|
|
28
|
+
this.updateLanguageCompartment(lang);
|
|
29
|
+
this.languageName = languageName;
|
|
30
|
+
} else {
|
|
31
|
+
configureEmpty();
|
|
32
|
+
}
|
|
33
|
+
}).catch(() => {
|
|
34
|
+
configureEmpty();
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { withLazyLoading } from '@atlaskit/editor-common/lazy-node-view';
|
|
2
|
+
export const lazyCodeBlockView = props => {
|
|
3
|
+
return withLazyLoading({
|
|
4
|
+
nodeName: 'codeBlock',
|
|
5
|
+
getNodeViewOptions: () => {},
|
|
6
|
+
loader: () => {
|
|
7
|
+
const result = import( /* webpackChunkName: "@atlaskit-internal_editor-plugin-code-block-advanced-nodeview" */
|
|
8
|
+
'./codeBlockAdvanced').then(({
|
|
9
|
+
getCodeBlockAdvancedNodeView
|
|
10
|
+
}) => {
|
|
11
|
+
return (node, view, getPos) => {
|
|
12
|
+
return getCodeBlockAdvancedNodeView(props)(node, view, getPos);
|
|
13
|
+
};
|
|
14
|
+
});
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
2
|
+
import { lazyCodeBlockView } from '../nodeviews/lazyCodeBlockAdvanced';
|
|
3
|
+
export const createPlugin = props => {
|
|
4
|
+
return new SafePlugin({
|
|
5
|
+
props: {
|
|
6
|
+
nodeViews: {
|
|
7
|
+
codeBlock: lazyCodeBlockView(props)
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
};
|