@instructure/ui-source-code-editor 11.6.0 → 11.6.1-snapshot-129
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 +40 -293
- package/es/SourceCodeEditor/{SearchPanel.js → v1/SearchPanel.js} +2 -2
- package/es/SourceCodeEditor/{index.js → v1/index.js} +1 -1
- package/es/SourceCodeEditor/v2/SearchPanel.js +118 -0
- package/es/SourceCodeEditor/v2/customKeybinding.js +100 -0
- package/es/SourceCodeEditor/v2/index.js +535 -0
- package/es/SourceCodeEditor/v2/props.js +28 -0
- package/es/SourceCodeEditor/v2/styles.js +228 -0
- package/es/{index.js → exports/a.js} +1 -1
- package/{src/index.ts → es/exports/b.js} +1 -2
- package/lib/SourceCodeEditor/{SearchPanel.js → v1/SearchPanel.js} +8 -8
- package/lib/SourceCodeEditor/{index.js → v1/index.js} +1 -1
- package/lib/SourceCodeEditor/v2/SearchPanel.js +126 -0
- package/lib/SourceCodeEditor/v2/customKeybinding.js +105 -0
- package/lib/SourceCodeEditor/v2/index.js +541 -0
- package/lib/SourceCodeEditor/v2/props.js +33 -0
- package/lib/SourceCodeEditor/v2/styles.js +234 -0
- package/lib/{index.js → exports/a.js} +2 -2
- package/lib/exports/b.js +12 -0
- package/package.json +42 -20
- package/src/SourceCodeEditor/{SearchPanel.tsx → v1/SearchPanel.tsx} +2 -2
- package/src/SourceCodeEditor/{index.tsx → v1/index.tsx} +1 -1
- package/src/SourceCodeEditor/v2/README.md +799 -0
- package/src/SourceCodeEditor/v2/SearchPanel.tsx +150 -0
- package/src/SourceCodeEditor/v2/customKeybinding.ts +125 -0
- package/src/SourceCodeEditor/v2/index.tsx +685 -0
- package/src/SourceCodeEditor/v2/props.ts +263 -0
- package/src/SourceCodeEditor/v2/styles.ts +239 -0
- package/src/exports/a.ts +25 -0
- package/src/exports/b.ts +25 -0
- package/tsconfig.build.tsbuildinfo +1 -1
- package/types/SourceCodeEditor/v1/SearchPanel.d.ts.map +1 -0
- package/types/SourceCodeEditor/v1/customKeybinding.d.ts.map +1 -0
- package/types/SourceCodeEditor/v1/index.d.ts.map +1 -0
- package/types/SourceCodeEditor/v1/props.d.ts.map +1 -0
- package/types/SourceCodeEditor/v1/styles.d.ts.map +1 -0
- package/types/SourceCodeEditor/v1/theme.d.ts.map +1 -0
- package/types/SourceCodeEditor/v2/SearchPanel.d.ts +7 -0
- package/types/SourceCodeEditor/v2/SearchPanel.d.ts.map +1 -0
- package/types/SourceCodeEditor/v2/customKeybinding.d.ts +4 -0
- package/types/SourceCodeEditor/v2/customKeybinding.d.ts.map +1 -0
- package/types/SourceCodeEditor/v2/index.d.ts +103 -0
- package/types/SourceCodeEditor/v2/index.d.ts.map +1 -0
- package/types/SourceCodeEditor/v2/props.d.ts +142 -0
- package/types/SourceCodeEditor/v2/props.d.ts.map +1 -0
- package/types/SourceCodeEditor/v2/styles.d.ts +14 -0
- package/types/SourceCodeEditor/v2/styles.d.ts.map +1 -0
- package/types/exports/a.d.ts +3 -0
- package/types/exports/a.d.ts.map +1 -0
- package/types/exports/b.d.ts +3 -0
- package/types/exports/b.d.ts.map +1 -0
- package/types/SourceCodeEditor/SearchPanel.d.ts.map +0 -1
- package/types/SourceCodeEditor/customKeybinding.d.ts.map +0 -1
- package/types/SourceCodeEditor/index.d.ts.map +0 -1
- package/types/SourceCodeEditor/props.d.ts.map +0 -1
- package/types/SourceCodeEditor/styles.d.ts.map +0 -1
- package/types/SourceCodeEditor/theme.d.ts.map +0 -1
- package/types/index.d.ts +0 -3
- package/types/index.d.ts.map +0 -1
- /package/es/SourceCodeEditor/{customKeybinding.js → v1/customKeybinding.js} +0 -0
- /package/es/SourceCodeEditor/{props.js → v1/props.js} +0 -0
- /package/es/SourceCodeEditor/{styles.js → v1/styles.js} +0 -0
- /package/es/SourceCodeEditor/{theme.js → v1/theme.js} +0 -0
- /package/lib/SourceCodeEditor/{customKeybinding.js → v1/customKeybinding.js} +0 -0
- /package/lib/SourceCodeEditor/{props.js → v1/props.js} +0 -0
- /package/lib/SourceCodeEditor/{styles.js → v1/styles.js} +0 -0
- /package/lib/SourceCodeEditor/{theme.js → v1/theme.js} +0 -0
- /package/src/SourceCodeEditor/{README.md → v1/README.md} +0 -0
- /package/src/SourceCodeEditor/{customKeybinding.ts → v1/customKeybinding.ts} +0 -0
- /package/src/SourceCodeEditor/{props.ts → v1/props.ts} +0 -0
- /package/src/SourceCodeEditor/{styles.ts → v1/styles.ts} +0 -0
- /package/src/SourceCodeEditor/{theme.ts → v1/theme.ts} +0 -0
- /package/types/SourceCodeEditor/{SearchPanel.d.ts → v1/SearchPanel.d.ts} +0 -0
- /package/types/SourceCodeEditor/{customKeybinding.d.ts → v1/customKeybinding.d.ts} +0 -0
- /package/types/SourceCodeEditor/{index.d.ts → v1/index.d.ts} +0 -0
- /package/types/SourceCodeEditor/{props.d.ts → v1/props.d.ts} +0 -0
- /package/types/SourceCodeEditor/{styles.d.ts → v1/styles.d.ts} +0 -0
- /package/types/SourceCodeEditor/{theme.d.ts → v1/theme.d.ts} +0 -0
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
2
|
+
const _excluded = ["label", "styles"];
|
|
3
|
+
var _dec, _dec2, _dec3, _class, _SourceCodeEditor;
|
|
4
|
+
/*
|
|
5
|
+
* The MIT License (MIT)
|
|
6
|
+
*
|
|
7
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
* copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
* SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { Component } from 'react';
|
|
29
|
+
import { deepEqual as isEqual } from '@instructure/ui-utils';
|
|
30
|
+
import { EditorState, StateEffect } from '@codemirror/state';
|
|
31
|
+
import { EditorView, highlightSpecialChars, highlightActiveLine, drawSelection, dropCursor, rectangularSelection, crosshairCursor, lineNumbers, highlightActiveLineGutter, keymap } from '@codemirror/view';
|
|
32
|
+
import { autocompletion, completionKeymap, closeBrackets, closeBracketsKeymap } from '@codemirror/autocomplete';
|
|
33
|
+
import { highlightSelectionMatches, searchKeymap } from '@codemirror/search';
|
|
34
|
+
import { indentSelection, defaultKeymap, indentWithTab, history, historyKeymap } from '@codemirror/commands';
|
|
35
|
+
import { lintKeymap } from '@codemirror/lint';
|
|
36
|
+
import { indentOnInput, indentRange, indentUnit, StreamLanguage, bracketMatching, foldGutter, foldKeymap, defaultHighlightStyle, syntaxHighlighting, HighlightStyle } from '@codemirror/language';
|
|
37
|
+
import { javascript } from '@codemirror/lang-javascript';
|
|
38
|
+
import { html } from '@codemirror/lang-html';
|
|
39
|
+
import { css } from '@codemirror/lang-css';
|
|
40
|
+
import { markdown } from '@codemirror/lang-markdown';
|
|
41
|
+
import { json } from '@codemirror/lang-json';
|
|
42
|
+
import { shell } from '@codemirror/legacy-modes/mode/shell';
|
|
43
|
+
import { yaml } from '@codemirror/legacy-modes/mode/yaml';
|
|
44
|
+
// import { oneDarkTheme, oneDarkHighlightStyle } from '@codemirror/theme-one-dark'
|
|
45
|
+
|
|
46
|
+
import { omitProps, passthroughProps, withDeterministicId } from '@instructure/ui-react-utils';
|
|
47
|
+
import { requestAnimationFrame } from '@instructure/ui-dom-utils';
|
|
48
|
+
import { ScreenReaderContent } from '@instructure/ui-a11y-content';
|
|
49
|
+
import { textDirectionContextConsumer } from '@instructure/ui-i18n';
|
|
50
|
+
import { withStyle } from '@instructure/emotion';
|
|
51
|
+
import customSearch from "./SearchPanel.js";
|
|
52
|
+
import generateStyle from "./styles.js";
|
|
53
|
+
import { rtlHorizontalArrowKeymap } from "./customKeybinding.js";
|
|
54
|
+
import { allowedProps } from "./props.js";
|
|
55
|
+
import { jsx as _jsx, jsxs as _jsxs } from "@emotion/react/jsx-runtime";
|
|
56
|
+
/**
|
|
57
|
+
---
|
|
58
|
+
category: components
|
|
59
|
+
---
|
|
60
|
+
**/
|
|
61
|
+
let SourceCodeEditor = (_dec = withDeterministicId(), _dec2 = withStyle(generateStyle), _dec3 = textDirectionContextConsumer(), _dec(_class = _dec2(_class = _dec3(_class = (_SourceCodeEditor = class SourceCodeEditor extends Component {
|
|
62
|
+
addAnimationFrame(callback) {
|
|
63
|
+
if (typeof callback === 'function') {
|
|
64
|
+
this._raf.push(requestAnimationFrame(callback));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
cancelAnimationFrames() {
|
|
68
|
+
this._raf.forEach(request => request.cancel());
|
|
69
|
+
this._raf = [];
|
|
70
|
+
}
|
|
71
|
+
focus() {
|
|
72
|
+
this.addAnimationFrame(() => {
|
|
73
|
+
var _this$_editorView;
|
|
74
|
+
(_this$_editorView = this._editorView) === null || _this$_editorView === void 0 ? void 0 : _this$_editorView.focus();
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
get hasFocus() {
|
|
78
|
+
var _this$_editorView2;
|
|
79
|
+
return (_this$_editorView2 = this._editorView) === null || _this$_editorView2 === void 0 ? void 0 : _this$_editorView2.hasFocus;
|
|
80
|
+
}
|
|
81
|
+
selectAll() {
|
|
82
|
+
if (this._editorView) {
|
|
83
|
+
this.addAnimationFrame(() => {
|
|
84
|
+
var _this$currentDocValue;
|
|
85
|
+
this.dispatchViewSelection({
|
|
86
|
+
anchor: 0,
|
|
87
|
+
head: (_this$currentDocValue = this.currentDocValue) === null || _this$currentDocValue === void 0 ? void 0 : _this$currentDocValue.length
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
deselectAll() {
|
|
93
|
+
if (this._editorView) {
|
|
94
|
+
this.addAnimationFrame(() => {
|
|
95
|
+
this.dispatchViewSelection({
|
|
96
|
+
anchor: 0,
|
|
97
|
+
head: 0
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
indentCurrentSelection() {
|
|
103
|
+
this.addAnimationFrame(() => {
|
|
104
|
+
if (this._editorView) {
|
|
105
|
+
indentSelection({
|
|
106
|
+
state: this._editorView.state,
|
|
107
|
+
dispatch: transaction => {
|
|
108
|
+
var _this$_editorView3;
|
|
109
|
+
(_this$_editorView3 = this._editorView) === null || _this$_editorView3 === void 0 ? void 0 : _this$_editorView3.update([transaction]);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
indentAll() {
|
|
116
|
+
this.addAnimationFrame(() => {
|
|
117
|
+
if (this._editorView && this.currentDocValue) {
|
|
118
|
+
this.indentCodeRange(0, this.currentDocValue.length);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
indentCodeRange(from, to) {
|
|
123
|
+
this.addAnimationFrame(() => {
|
|
124
|
+
if (this._editorView && this.currentDocValue) {
|
|
125
|
+
this.dispatchViewChanges({
|
|
126
|
+
changes: indentRange(this._editorView.state, from, to)
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Attach state effects
|
|
133
|
+
dispatchViewEffects(effects) {
|
|
134
|
+
if (!this._editorView || !effects) return;
|
|
135
|
+
this._editorView.dispatch({
|
|
136
|
+
effects
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Dispatch changes to the document
|
|
141
|
+
dispatchViewChanges({
|
|
142
|
+
changes,
|
|
143
|
+
selection,
|
|
144
|
+
userEvent
|
|
145
|
+
}) {
|
|
146
|
+
if (!this._editorView || !changes) return;
|
|
147
|
+
this._editorView.dispatch({
|
|
148
|
+
changes,
|
|
149
|
+
...(selection ? {
|
|
150
|
+
selection
|
|
151
|
+
} : void 0),
|
|
152
|
+
...(userEvent ? {
|
|
153
|
+
userEvent
|
|
154
|
+
} : void 0)
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Select a portion of the document
|
|
159
|
+
dispatchViewSelection(selection) {
|
|
160
|
+
if (!this._editorView || !selection) return;
|
|
161
|
+
this._editorView.dispatch({
|
|
162
|
+
selection
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
get currentDocValue() {
|
|
166
|
+
var _this$_editorView4;
|
|
167
|
+
return (_this$_editorView4 = this._editorView) === null || _this$_editorView4 === void 0 ? void 0 : _this$_editorView4.state.doc;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// when value is passed, the editor should be controlled
|
|
171
|
+
get isControlled() {
|
|
172
|
+
return typeof this.props.value === 'string';
|
|
173
|
+
}
|
|
174
|
+
constructor(props) {
|
|
175
|
+
super(props);
|
|
176
|
+
this._id = void 0;
|
|
177
|
+
this.ref = null;
|
|
178
|
+
this._containerRef = void 0;
|
|
179
|
+
this._editorView = void 0;
|
|
180
|
+
this._raf = [];
|
|
181
|
+
this._newSelectionAfterValueChange = void 0;
|
|
182
|
+
this.handleRef = el => {
|
|
183
|
+
const elementRef = this.props.elementRef;
|
|
184
|
+
this.ref = el;
|
|
185
|
+
if (typeof elementRef === 'function') {
|
|
186
|
+
elementRef(el);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
this.handleContainerRef = el => {
|
|
190
|
+
const containerRef = this.props.containerRef;
|
|
191
|
+
this._containerRef = el || void 0;
|
|
192
|
+
if (typeof containerRef === 'function') {
|
|
193
|
+
containerRef(el);
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
this.assignAriaLabel = () => {
|
|
197
|
+
if (this._containerRef) {
|
|
198
|
+
const editorDiv = this._containerRef.querySelector('[role="textbox"]');
|
|
199
|
+
if (editorDiv) {
|
|
200
|
+
editorDiv.setAttribute('aria-labelledby', `${this._id}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
this._id = props.deterministicId();
|
|
205
|
+
}
|
|
206
|
+
componentDidMount() {
|
|
207
|
+
var _this$props$makeStyle, _this$props2;
|
|
208
|
+
const _this$props = this.props,
|
|
209
|
+
value = _this$props.value,
|
|
210
|
+
defaultValue = _this$props.defaultValue,
|
|
211
|
+
autofocus = _this$props.autofocus,
|
|
212
|
+
indentOnLoad = _this$props.indentOnLoad;
|
|
213
|
+
(_this$props$makeStyle = (_this$props2 = this.props).makeStyles) === null || _this$props$makeStyle === void 0 ? void 0 : _this$props$makeStyle.call(_this$props2);
|
|
214
|
+
const state = EditorState.create({
|
|
215
|
+
doc: value || defaultValue,
|
|
216
|
+
extensions: this.extensions
|
|
217
|
+
});
|
|
218
|
+
this._editorView = new EditorView({
|
|
219
|
+
state,
|
|
220
|
+
parent: this._containerRef
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
// from the a11y team:
|
|
224
|
+
// axe devtools and other automated a11y tests both flagging this issue,
|
|
225
|
+
// which can be observed while navigating with keyboard:
|
|
226
|
+
// Ensure elements that have scrollable content are accessible by keyboard
|
|
227
|
+
// To solve this problem, you need to fix at least (1) of the following:
|
|
228
|
+
// Element should have focusable content, Element should be focusable
|
|
229
|
+
this._editorView.scrollDOM.tabIndex = 0;
|
|
230
|
+
if (autofocus) {
|
|
231
|
+
this.focus();
|
|
232
|
+
}
|
|
233
|
+
if (indentOnLoad) {
|
|
234
|
+
this.indentAll();
|
|
235
|
+
}
|
|
236
|
+
this.assignAriaLabel();
|
|
237
|
+
}
|
|
238
|
+
componentWillUnmount() {
|
|
239
|
+
var _this$_editorView5;
|
|
240
|
+
(_this$_editorView5 = this._editorView) === null || _this$_editorView5 === void 0 ? void 0 : _this$_editorView5.destroy();
|
|
241
|
+
this.cancelAnimationFrames();
|
|
242
|
+
}
|
|
243
|
+
componentDidUpdate(prevProps) {
|
|
244
|
+
var _this$props$makeStyle2, _this$props3;
|
|
245
|
+
(_this$props$makeStyle2 = (_this$props3 = this.props).makeStyles) === null || _this$props$makeStyle2 === void 0 ? void 0 : _this$props$makeStyle2.call(_this$props3);
|
|
246
|
+
if (this._editorView) {
|
|
247
|
+
if (this.props.value !== prevProps.value) {
|
|
248
|
+
this.refreshEditorValue();
|
|
249
|
+
}
|
|
250
|
+
if (this.shouldUpdateExtensions(prevProps)) {
|
|
251
|
+
this.refreshExtensions();
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
shouldUpdateExtensions(prevProps) {
|
|
256
|
+
const propsToObserve = ['styles',
|
|
257
|
+
// needed for theme update
|
|
258
|
+
'themeOverride', 'language', 'readOnly', 'editable', 'lineNumbers', 'highlightActiveLineGutter', 'foldGutter', 'lineWrapping', 'autofocus', 'spellcheck', 'direction', 'dir', 'rtlMoveVisually', 'indentOnLoad', 'indentWithTab', 'indentUnit', 'highlightActiveLine', 'attachment'];
|
|
259
|
+
for (const prop of propsToObserve) {
|
|
260
|
+
if (!isEqual(this.props[prop], prevProps[prop])) {
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return false;
|
|
265
|
+
}
|
|
266
|
+
get direction() {
|
|
267
|
+
// comes from the `direction` prop and
|
|
268
|
+
// falls back to the `dir` prop coming from the bidirectional decorator
|
|
269
|
+
return this.props.direction || this.props.dir;
|
|
270
|
+
}
|
|
271
|
+
get extensions() {
|
|
272
|
+
const extensions = [...this.baseExtensions,
|
|
273
|
+
// our custom extensions
|
|
274
|
+
this.languageExtension, this.onChangeExtension, this.focusListenerExtension, this.announceLineNumberExtension];
|
|
275
|
+
if (this.themeExtension) {
|
|
276
|
+
extensions.push(this.themeExtension);
|
|
277
|
+
}
|
|
278
|
+
if (this.props.lineNumbers) {
|
|
279
|
+
extensions.push(lineNumbers());
|
|
280
|
+
}
|
|
281
|
+
if (this.props.highlightActiveLine) {
|
|
282
|
+
extensions.push(highlightActiveLine());
|
|
283
|
+
}
|
|
284
|
+
if (this.props.highlightActiveLineGutter) {
|
|
285
|
+
extensions.push(highlightActiveLineGutter());
|
|
286
|
+
}
|
|
287
|
+
if (this.props.foldGutter) {
|
|
288
|
+
extensions.push(foldGutter());
|
|
289
|
+
}
|
|
290
|
+
if (this.props.lineWrapping) {
|
|
291
|
+
extensions.push(EditorView.lineWrapping);
|
|
292
|
+
}
|
|
293
|
+
if (this.props.editable === false) {
|
|
294
|
+
extensions.push(EditorView.editable.of(false));
|
|
295
|
+
}
|
|
296
|
+
if (this.props.readOnly) {
|
|
297
|
+
extensions.push(EditorState.readOnly.of(true));
|
|
298
|
+
}
|
|
299
|
+
if (this.props.spellcheck) {
|
|
300
|
+
extensions.push(EditorView.contentAttributes.of({
|
|
301
|
+
spellcheck: 'true'
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
if (this.direction) {
|
|
305
|
+
extensions.push(EditorView.contentAttributes.of({
|
|
306
|
+
dir: this.direction
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
if (this.props.indentUnit) {
|
|
310
|
+
extensions.push(indentUnit.of(this.props.indentUnit));
|
|
311
|
+
}
|
|
312
|
+
return extensions;
|
|
313
|
+
}
|
|
314
|
+
get baseExtensions() {
|
|
315
|
+
return [
|
|
316
|
+
// The extensions are based on codemirrors basic setup from 'codemirror'.
|
|
317
|
+
// It is recommended by CodeMirror, that if we want to configure
|
|
318
|
+
// our editor more precisely, we have to copy the source
|
|
319
|
+
// and adjust it as desired.
|
|
320
|
+
highlightSpecialChars(), history(), drawSelection(), dropCursor(), EditorState.allowMultipleSelections.of(true), syntaxHighlighting(defaultHighlightStyle, {
|
|
321
|
+
fallback: true
|
|
322
|
+
}), bracketMatching(), closeBrackets(), autocompletion(), rectangularSelection(), crosshairCursor(), highlightSelectionMatches(), indentOnInput(), customSearch(this.props.searchConfig), keymap.of(this.keymaps)];
|
|
323
|
+
}
|
|
324
|
+
get keymaps() {
|
|
325
|
+
// TODO: if more keymaps are added, list them in the docs as well (#Command keybinding)
|
|
326
|
+
const keymaps = [...closeBracketsKeymap, ...this.commandKeybinding, ...historyKeymap, ...foldKeymap, ...completionKeymap, ...lintKeymap, ...(this.props.searchConfig ? searchKeymap : [])];
|
|
327
|
+
if (this.props.indentWithTab) {
|
|
328
|
+
keymaps.push(indentWithTab);
|
|
329
|
+
}
|
|
330
|
+
return keymaps;
|
|
331
|
+
}
|
|
332
|
+
get commandKeybinding() {
|
|
333
|
+
const rtlMoveVisually = this.props.rtlMoveVisually;
|
|
334
|
+
if (this.direction === 'rtl' && !rtlMoveVisually) {
|
|
335
|
+
const overrideableKeys = rtlHorizontalArrowKeymap.map(binding => binding.key ? binding.key : binding.mac ? binding.mac : binding);
|
|
336
|
+
// we have to remove the binding we want to override from the original,
|
|
337
|
+
// otherwise all will be merged and the defaults will still apply
|
|
338
|
+
const filteredOriginal = defaultKeymap.filter(binding => binding.key ? overrideableKeys.indexOf(binding.key) < 0 : binding.mac ? overrideableKeys.indexOf(binding.mac) < 0 : false);
|
|
339
|
+
return [...filteredOriginal, ...rtlHorizontalArrowKeymap];
|
|
340
|
+
}
|
|
341
|
+
return defaultKeymap;
|
|
342
|
+
}
|
|
343
|
+
get themeExtension() {
|
|
344
|
+
const styles = this.props.styles;
|
|
345
|
+
if (!(styles !== null && styles !== void 0 && styles.theme) || !styles.highlightStyle) {
|
|
346
|
+
return void 0;
|
|
347
|
+
}
|
|
348
|
+
const theme = EditorView.theme(styles === null || styles === void 0 ? void 0 : styles.theme);
|
|
349
|
+
const highlightStyle = syntaxHighlighting(HighlightStyle.define(styles === null || styles === void 0 ? void 0 : styles.highlightStyle));
|
|
350
|
+
|
|
351
|
+
// see notes in props.ts
|
|
352
|
+
// if (darkTheme) {
|
|
353
|
+
// theme = oneDarkTheme
|
|
354
|
+
// highlightStyle = syntaxHighlighting(oneDarkHighlightStyle)
|
|
355
|
+
// }
|
|
356
|
+
|
|
357
|
+
return [theme, highlightStyle];
|
|
358
|
+
}
|
|
359
|
+
get languageExtension() {
|
|
360
|
+
const language = this.props.language;
|
|
361
|
+
switch (language) {
|
|
362
|
+
case 'json':
|
|
363
|
+
return json();
|
|
364
|
+
case 'js':
|
|
365
|
+
case 'jsx':
|
|
366
|
+
case 'javascript':
|
|
367
|
+
return javascript({
|
|
368
|
+
jsx: true,
|
|
369
|
+
typescript: true
|
|
370
|
+
});
|
|
371
|
+
case 'html':
|
|
372
|
+
return html({
|
|
373
|
+
matchClosingTags: true,
|
|
374
|
+
autoCloseTags: true
|
|
375
|
+
});
|
|
376
|
+
case 'css':
|
|
377
|
+
return css();
|
|
378
|
+
case 'markdown':
|
|
379
|
+
return markdown();
|
|
380
|
+
case 'sh':
|
|
381
|
+
case 'shell':
|
|
382
|
+
case 'bash':
|
|
383
|
+
// ????
|
|
384
|
+
return StreamLanguage.define(shell);
|
|
385
|
+
case 'yml':
|
|
386
|
+
case 'yaml':
|
|
387
|
+
return StreamLanguage.define(yaml);
|
|
388
|
+
default:
|
|
389
|
+
return javascript({
|
|
390
|
+
jsx: true,
|
|
391
|
+
typescript: true
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
callOnChangeHandler(newValue) {
|
|
396
|
+
const _this$props4 = this.props,
|
|
397
|
+
onChange = _this$props4.onChange,
|
|
398
|
+
value = _this$props4.value;
|
|
399
|
+
this.addAnimationFrame(() => {
|
|
400
|
+
if (typeof onChange === 'function' && newValue !== value) {
|
|
401
|
+
onChange(newValue);
|
|
402
|
+
}
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
get onChangeExtension() {
|
|
406
|
+
return EditorState.changeFilter.of(transaction => {
|
|
407
|
+
if (!this._editorView) {
|
|
408
|
+
return false;
|
|
409
|
+
}
|
|
410
|
+
if (transaction.docChanged) {
|
|
411
|
+
const newDoc = transaction.newDoc.toString();
|
|
412
|
+
if (this.isControlled) {
|
|
413
|
+
// the value will be changed by the onChange handler,
|
|
414
|
+
// refreshEditorValue has to run first
|
|
415
|
+
if (newDoc !== this.props.value) {
|
|
416
|
+
this._newSelectionAfterValueChange = transaction.selection;
|
|
417
|
+
this.cancelAnimationFrames();
|
|
418
|
+
this.callOnChangeHandler(newDoc);
|
|
419
|
+
return false;
|
|
420
|
+
} else {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
} else {
|
|
424
|
+
this.callOnChangeHandler(newDoc);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
return true;
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
get focusListenerExtension() {
|
|
431
|
+
const _this$props5 = this.props,
|
|
432
|
+
onFocus = _this$props5.onFocus,
|
|
433
|
+
onBlur = _this$props5.onBlur;
|
|
434
|
+
return EditorView.updateListener.of(update => {
|
|
435
|
+
if (update.focusChanged && this._editorView) {
|
|
436
|
+
if (this.hasFocus) {
|
|
437
|
+
if (typeof onFocus === 'function') {
|
|
438
|
+
onFocus();
|
|
439
|
+
}
|
|
440
|
+
} else {
|
|
441
|
+
if (typeof onBlur === 'function') {
|
|
442
|
+
onBlur();
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
get announceLineNumberExtension() {
|
|
449
|
+
return EditorState.transactionExtender.of(tr => {
|
|
450
|
+
const selection = tr.selection;
|
|
451
|
+
const oldSelection = tr.startState.selection.main;
|
|
452
|
+
if (selection && selection.main.empty && oldSelection.empty) {
|
|
453
|
+
const oldLine = tr.startState.doc.lineAt(oldSelection.head);
|
|
454
|
+
const newLine = tr.newDoc.lineAt(selection.main.head);
|
|
455
|
+
if (oldLine.number != newLine.number) return {
|
|
456
|
+
effects: EditorView.announce.of(tr.startState.phrase('line ') + newLine.number + '.')
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
refreshExtensions() {
|
|
463
|
+
this.dispatchViewEffects(StateEffect.reconfigure.of(this.extensions));
|
|
464
|
+
}
|
|
465
|
+
refreshEditorValue() {
|
|
466
|
+
if (!this._editorView) return;
|
|
467
|
+
const value = this.props.value;
|
|
468
|
+
const currentValue = this._editorView.state.doc.toString();
|
|
469
|
+
if (typeof value !== 'undefined' && currentValue !== value) {
|
|
470
|
+
let userEvent;
|
|
471
|
+
const lengthDiff = value.length - currentValue.length;
|
|
472
|
+
|
|
473
|
+
// setting user events are needed for the autocomplete to work
|
|
474
|
+
// (only these 2 events, autocomplete doesn't work on paste, etc.)
|
|
475
|
+
if (lengthDiff === 1) {
|
|
476
|
+
userEvent = 'input.type';
|
|
477
|
+
} else if (lengthDiff === -1) {
|
|
478
|
+
userEvent = 'delete.backward';
|
|
479
|
+
}
|
|
480
|
+
this.dispatchViewChanges({
|
|
481
|
+
changes: {
|
|
482
|
+
from: 0,
|
|
483
|
+
to: currentValue.length,
|
|
484
|
+
insert: value || ''
|
|
485
|
+
},
|
|
486
|
+
selection: this._newSelectionAfterValueChange,
|
|
487
|
+
userEvent: userEvent
|
|
488
|
+
});
|
|
489
|
+
this._newSelectionAfterValueChange = void 0;
|
|
490
|
+
}
|
|
491
|
+
if (this.props.indentOnLoad) {
|
|
492
|
+
this.indentAll();
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
render() {
|
|
496
|
+
const _this$props6 = this.props,
|
|
497
|
+
label = _this$props6.label,
|
|
498
|
+
styles = _this$props6.styles,
|
|
499
|
+
restProps = _objectWithoutProperties(_this$props6, _excluded);
|
|
500
|
+
return _jsx("div", {
|
|
501
|
+
"data-cid": "SourceCodeEditor",
|
|
502
|
+
ref: this.handleRef,
|
|
503
|
+
css: styles === null || styles === void 0 ? void 0 : styles.codeEditor,
|
|
504
|
+
...passthroughProps(omitProps(restProps, SourceCodeEditor.allowedProps)),
|
|
505
|
+
children: _jsxs("label", {
|
|
506
|
+
css: styles === null || styles === void 0 ? void 0 : styles.label,
|
|
507
|
+
id: this._id,
|
|
508
|
+
children: [_jsx(ScreenReaderContent, {
|
|
509
|
+
children: label
|
|
510
|
+
}), _jsx("div", {
|
|
511
|
+
ref: this.handleContainerRef,
|
|
512
|
+
css: styles === null || styles === void 0 ? void 0 : styles.codeEditorContainer
|
|
513
|
+
})]
|
|
514
|
+
})
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
}, _SourceCodeEditor.displayName = "SourceCodeEditor", _SourceCodeEditor.componentId = 'SourceCodeEditor', _SourceCodeEditor.allowedProps = allowedProps, _SourceCodeEditor.defaultProps = {
|
|
518
|
+
language: 'jsx',
|
|
519
|
+
readOnly: false,
|
|
520
|
+
editable: true,
|
|
521
|
+
lineNumbers: false,
|
|
522
|
+
foldGutter: false,
|
|
523
|
+
highlightActiveLine: false,
|
|
524
|
+
highlightActiveLineGutter: false,
|
|
525
|
+
lineWrapping: false,
|
|
526
|
+
autofocus: false,
|
|
527
|
+
spellcheck: false,
|
|
528
|
+
rtlMoveVisually: true,
|
|
529
|
+
indentOnLoad: false,
|
|
530
|
+
indentWithTab: false,
|
|
531
|
+
defaultValue: '',
|
|
532
|
+
height: 'auto'
|
|
533
|
+
}, _SourceCodeEditor)) || _class) || _class) || _class);
|
|
534
|
+
export default SourceCodeEditor;
|
|
535
|
+
export { SourceCodeEditor };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License (MIT)
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2015 - present Instructure, Inc.
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const allowedProps = ['label', 'language', 'readOnly', 'editable', 'lineNumbers', 'foldGutter', 'highlightActiveLineGutter', 'highlightActiveLine', 'lineWrapping', 'autofocus', 'spellcheck', 'direction', 'rtlMoveVisually', 'indentOnLoad', 'indentWithTab', 'indentUnit', 'defaultValue', 'value', 'onChange', 'onFocus', 'onBlur', 'attachment', 'height', 'width',
|
|
26
|
+
// 'darkTheme',
|
|
27
|
+
'elementRef', 'containerRef', 'searchConfig'];
|
|
28
|
+
export { allowedProps };
|