@instructure/ui-source-code-editor 11.7.8 → 11.7.9-pr-snapshot-1792147648392

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