@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.
Files changed (78) hide show
  1. package/CHANGELOG.md +40 -293
  2. package/es/SourceCodeEditor/{SearchPanel.js → v1/SearchPanel.js} +2 -2
  3. package/es/SourceCodeEditor/{index.js → v1/index.js} +1 -1
  4. package/es/SourceCodeEditor/v2/SearchPanel.js +118 -0
  5. package/es/SourceCodeEditor/v2/customKeybinding.js +100 -0
  6. package/es/SourceCodeEditor/v2/index.js +535 -0
  7. package/es/SourceCodeEditor/v2/props.js +28 -0
  8. package/es/SourceCodeEditor/v2/styles.js +228 -0
  9. package/es/{index.js → exports/a.js} +1 -1
  10. package/{src/index.ts → es/exports/b.js} +1 -2
  11. package/lib/SourceCodeEditor/{SearchPanel.js → v1/SearchPanel.js} +8 -8
  12. package/lib/SourceCodeEditor/{index.js → v1/index.js} +1 -1
  13. package/lib/SourceCodeEditor/v2/SearchPanel.js +126 -0
  14. package/lib/SourceCodeEditor/v2/customKeybinding.js +105 -0
  15. package/lib/SourceCodeEditor/v2/index.js +541 -0
  16. package/lib/SourceCodeEditor/v2/props.js +33 -0
  17. package/lib/SourceCodeEditor/v2/styles.js +234 -0
  18. package/lib/{index.js → exports/a.js} +2 -2
  19. package/lib/exports/b.js +12 -0
  20. package/package.json +42 -20
  21. package/src/SourceCodeEditor/{SearchPanel.tsx → v1/SearchPanel.tsx} +2 -2
  22. package/src/SourceCodeEditor/{index.tsx → v1/index.tsx} +1 -1
  23. package/src/SourceCodeEditor/v2/README.md +799 -0
  24. package/src/SourceCodeEditor/v2/SearchPanel.tsx +150 -0
  25. package/src/SourceCodeEditor/v2/customKeybinding.ts +125 -0
  26. package/src/SourceCodeEditor/v2/index.tsx +685 -0
  27. package/src/SourceCodeEditor/v2/props.ts +263 -0
  28. package/src/SourceCodeEditor/v2/styles.ts +239 -0
  29. package/src/exports/a.ts +25 -0
  30. package/src/exports/b.ts +25 -0
  31. package/tsconfig.build.tsbuildinfo +1 -1
  32. package/types/SourceCodeEditor/v1/SearchPanel.d.ts.map +1 -0
  33. package/types/SourceCodeEditor/v1/customKeybinding.d.ts.map +1 -0
  34. package/types/SourceCodeEditor/v1/index.d.ts.map +1 -0
  35. package/types/SourceCodeEditor/v1/props.d.ts.map +1 -0
  36. package/types/SourceCodeEditor/v1/styles.d.ts.map +1 -0
  37. package/types/SourceCodeEditor/v1/theme.d.ts.map +1 -0
  38. package/types/SourceCodeEditor/v2/SearchPanel.d.ts +7 -0
  39. package/types/SourceCodeEditor/v2/SearchPanel.d.ts.map +1 -0
  40. package/types/SourceCodeEditor/v2/customKeybinding.d.ts +4 -0
  41. package/types/SourceCodeEditor/v2/customKeybinding.d.ts.map +1 -0
  42. package/types/SourceCodeEditor/v2/index.d.ts +103 -0
  43. package/types/SourceCodeEditor/v2/index.d.ts.map +1 -0
  44. package/types/SourceCodeEditor/v2/props.d.ts +142 -0
  45. package/types/SourceCodeEditor/v2/props.d.ts.map +1 -0
  46. package/types/SourceCodeEditor/v2/styles.d.ts +14 -0
  47. package/types/SourceCodeEditor/v2/styles.d.ts.map +1 -0
  48. package/types/exports/a.d.ts +3 -0
  49. package/types/exports/a.d.ts.map +1 -0
  50. package/types/exports/b.d.ts +3 -0
  51. package/types/exports/b.d.ts.map +1 -0
  52. package/types/SourceCodeEditor/SearchPanel.d.ts.map +0 -1
  53. package/types/SourceCodeEditor/customKeybinding.d.ts.map +0 -1
  54. package/types/SourceCodeEditor/index.d.ts.map +0 -1
  55. package/types/SourceCodeEditor/props.d.ts.map +0 -1
  56. package/types/SourceCodeEditor/styles.d.ts.map +0 -1
  57. package/types/SourceCodeEditor/theme.d.ts.map +0 -1
  58. package/types/index.d.ts +0 -3
  59. package/types/index.d.ts.map +0 -1
  60. /package/es/SourceCodeEditor/{customKeybinding.js → v1/customKeybinding.js} +0 -0
  61. /package/es/SourceCodeEditor/{props.js → v1/props.js} +0 -0
  62. /package/es/SourceCodeEditor/{styles.js → v1/styles.js} +0 -0
  63. /package/es/SourceCodeEditor/{theme.js → v1/theme.js} +0 -0
  64. /package/lib/SourceCodeEditor/{customKeybinding.js → v1/customKeybinding.js} +0 -0
  65. /package/lib/SourceCodeEditor/{props.js → v1/props.js} +0 -0
  66. /package/lib/SourceCodeEditor/{styles.js → v1/styles.js} +0 -0
  67. /package/lib/SourceCodeEditor/{theme.js → v1/theme.js} +0 -0
  68. /package/src/SourceCodeEditor/{README.md → v1/README.md} +0 -0
  69. /package/src/SourceCodeEditor/{customKeybinding.ts → v1/customKeybinding.ts} +0 -0
  70. /package/src/SourceCodeEditor/{props.ts → v1/props.ts} +0 -0
  71. /package/src/SourceCodeEditor/{styles.ts → v1/styles.ts} +0 -0
  72. /package/src/SourceCodeEditor/{theme.ts → v1/theme.ts} +0 -0
  73. /package/types/SourceCodeEditor/{SearchPanel.d.ts → v1/SearchPanel.d.ts} +0 -0
  74. /package/types/SourceCodeEditor/{customKeybinding.d.ts → v1/customKeybinding.d.ts} +0 -0
  75. /package/types/SourceCodeEditor/{index.d.ts → v1/index.d.ts} +0 -0
  76. /package/types/SourceCodeEditor/{props.d.ts → v1/props.d.ts} +0 -0
  77. /package/types/SourceCodeEditor/{styles.d.ts → v1/styles.d.ts} +0 -0
  78. /package/types/SourceCodeEditor/{theme.d.ts → v1/theme.d.ts} +0 -0
@@ -0,0 +1,685 @@
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
+ import { Component } from 'react'
26
+ import { deepEqual as isEqual } from '@instructure/ui-utils'
27
+
28
+ import { EditorSelection, EditorState, StateEffect } from '@codemirror/state'
29
+ import type { Transaction, TransactionSpec } from '@codemirror/state'
30
+ import {
31
+ EditorView,
32
+ highlightSpecialChars,
33
+ highlightActiveLine,
34
+ drawSelection,
35
+ dropCursor,
36
+ rectangularSelection,
37
+ crosshairCursor,
38
+ lineNumbers,
39
+ highlightActiveLineGutter,
40
+ keymap
41
+ } from '@codemirror/view'
42
+ import type { KeyBinding } from '@codemirror/view'
43
+ import {
44
+ autocompletion,
45
+ completionKeymap,
46
+ closeBrackets,
47
+ closeBracketsKeymap
48
+ } from '@codemirror/autocomplete'
49
+ import { highlightSelectionMatches, searchKeymap } from '@codemirror/search'
50
+ import {
51
+ indentSelection,
52
+ defaultKeymap,
53
+ indentWithTab,
54
+ history,
55
+ historyKeymap
56
+ } from '@codemirror/commands'
57
+ import { lintKeymap } from '@codemirror/lint'
58
+ import {
59
+ indentOnInput,
60
+ indentRange,
61
+ indentUnit,
62
+ StreamLanguage,
63
+ bracketMatching,
64
+ foldGutter,
65
+ foldKeymap,
66
+ defaultHighlightStyle,
67
+ syntaxHighlighting,
68
+ HighlightStyle
69
+ } from '@codemirror/language'
70
+ import { javascript } from '@codemirror/lang-javascript'
71
+ import { html } from '@codemirror/lang-html'
72
+ import { css } from '@codemirror/lang-css'
73
+ import { markdown } from '@codemirror/lang-markdown'
74
+ import { json } from '@codemirror/lang-json'
75
+ import { shell } from '@codemirror/legacy-modes/mode/shell'
76
+ import { yaml } from '@codemirror/legacy-modes/mode/yaml'
77
+ // import { oneDarkTheme, oneDarkHighlightStyle } from '@codemirror/theme-one-dark'
78
+
79
+ import {
80
+ omitProps,
81
+ passthroughProps,
82
+ withDeterministicId
83
+ } from '@instructure/ui-react-utils'
84
+ import { requestAnimationFrame } from '@instructure/ui-dom-utils'
85
+ import type { RequestAnimationFrameType } from '@instructure/ui-dom-utils'
86
+
87
+ import { ScreenReaderContent } from '@instructure/ui-a11y-content'
88
+ import { textDirectionContextConsumer } from '@instructure/ui-i18n'
89
+
90
+ import { withStyle } from '@instructure/emotion'
91
+
92
+ import customSearch from './SearchPanel'
93
+
94
+ import generateStyle from './styles'
95
+
96
+ import { rtlHorizontalArrowKeymap } from './customKeybinding'
97
+
98
+ import { allowedProps } from './props'
99
+ import type { SourceCodeEditorProps } from './props'
100
+
101
+ /**
102
+ ---
103
+ category: components
104
+ ---
105
+ **/
106
+ @withDeterministicId()
107
+ @withStyle(generateStyle)
108
+ @textDirectionContextConsumer()
109
+ class SourceCodeEditor extends Component<SourceCodeEditorProps> {
110
+ static readonly componentId = 'SourceCodeEditor'
111
+
112
+ static allowedProps = allowedProps
113
+ static defaultProps = {
114
+ language: 'jsx',
115
+ readOnly: false,
116
+ editable: true,
117
+ lineNumbers: false,
118
+ foldGutter: false,
119
+ highlightActiveLine: false,
120
+ highlightActiveLineGutter: false,
121
+ lineWrapping: false,
122
+ autofocus: false,
123
+ spellcheck: false,
124
+ rtlMoveVisually: true,
125
+ indentOnLoad: false,
126
+ indentWithTab: false,
127
+ defaultValue: '',
128
+ height: 'auto'
129
+ }
130
+
131
+ private readonly _id: string
132
+
133
+ ref: HTMLDivElement | null = null
134
+
135
+ private _containerRef?: HTMLDivElement
136
+ private _editorView?: EditorView
137
+
138
+ private _raf: RequestAnimationFrameType[] = []
139
+
140
+ private _newSelectionAfterValueChange?: EditorSelection
141
+
142
+ handleRef = (el: HTMLDivElement | null) => {
143
+ const { elementRef } = this.props
144
+
145
+ this.ref = el
146
+
147
+ if (typeof elementRef === 'function') {
148
+ elementRef(el)
149
+ }
150
+ }
151
+
152
+ handleContainerRef = (el: HTMLDivElement | null) => {
153
+ const { containerRef } = this.props
154
+
155
+ this._containerRef = el || undefined
156
+
157
+ if (typeof containerRef === 'function') {
158
+ containerRef(el)
159
+ }
160
+ }
161
+
162
+ private addAnimationFrame(callback?: FrameRequestCallback) {
163
+ if (typeof callback === 'function') {
164
+ this._raf.push(requestAnimationFrame(callback))
165
+ }
166
+ }
167
+
168
+ private cancelAnimationFrames() {
169
+ this._raf.forEach((request) => request.cancel())
170
+ this._raf = []
171
+ }
172
+
173
+ public focus() {
174
+ this.addAnimationFrame(() => {
175
+ this._editorView?.focus()
176
+ })
177
+ }
178
+
179
+ public get hasFocus() {
180
+ return this._editorView?.hasFocus
181
+ }
182
+
183
+ public selectAll() {
184
+ if (this._editorView) {
185
+ this.addAnimationFrame(() => {
186
+ this.dispatchViewSelection({
187
+ anchor: 0,
188
+ head: this.currentDocValue?.length
189
+ })
190
+ })
191
+ }
192
+ }
193
+
194
+ public deselectAll() {
195
+ if (this._editorView) {
196
+ this.addAnimationFrame(() => {
197
+ this.dispatchViewSelection({
198
+ anchor: 0,
199
+ head: 0
200
+ })
201
+ })
202
+ }
203
+ }
204
+
205
+ public indentCurrentSelection() {
206
+ this.addAnimationFrame(() => {
207
+ if (this._editorView) {
208
+ indentSelection({
209
+ state: this._editorView.state,
210
+ dispatch: (transaction) => {
211
+ this._editorView?.update([transaction])
212
+ }
213
+ })
214
+ }
215
+ })
216
+ }
217
+
218
+ public indentAll() {
219
+ this.addAnimationFrame(() => {
220
+ if (this._editorView && this.currentDocValue) {
221
+ this.indentCodeRange(0, this.currentDocValue.length)
222
+ }
223
+ })
224
+ }
225
+
226
+ private indentCodeRange(from: number, to: number) {
227
+ this.addAnimationFrame(() => {
228
+ if (this._editorView && this.currentDocValue) {
229
+ this.dispatchViewChanges({
230
+ changes: indentRange(this._editorView.state, from, to)
231
+ })
232
+ }
233
+ })
234
+ }
235
+
236
+ // Attach state effects
237
+ private dispatchViewEffects(effects?: TransactionSpec['effects']) {
238
+ if (!this._editorView || !effects) return
239
+
240
+ this._editorView.dispatch({ effects })
241
+ }
242
+
243
+ // Dispatch changes to the document
244
+ private dispatchViewChanges({
245
+ changes,
246
+ selection,
247
+ userEvent
248
+ }: TransactionSpec) {
249
+ if (!this._editorView || !changes) return
250
+
251
+ this._editorView.dispatch({
252
+ changes,
253
+ ...(selection ? { selection } : undefined),
254
+ ...(userEvent ? { userEvent } : undefined)
255
+ })
256
+ }
257
+
258
+ // Select a portion of the document
259
+ private dispatchViewSelection(selection?: TransactionSpec['selection']) {
260
+ if (!this._editorView || !selection) return
261
+
262
+ this._editorView.dispatch({ selection })
263
+ }
264
+
265
+ get currentDocValue() {
266
+ return this._editorView?.state.doc
267
+ }
268
+
269
+ // when value is passed, the editor should be controlled
270
+ get isControlled() {
271
+ return typeof this.props.value === 'string'
272
+ }
273
+
274
+ constructor(props: SourceCodeEditorProps) {
275
+ super(props)
276
+ this._id = props.deterministicId!()
277
+ }
278
+
279
+ componentDidMount() {
280
+ const { value, defaultValue, autofocus, indentOnLoad } = this.props
281
+
282
+ this.props.makeStyles?.()
283
+
284
+ const state = EditorState.create({
285
+ doc: value || defaultValue,
286
+ extensions: this.extensions
287
+ })
288
+ this._editorView = new EditorView({
289
+ state,
290
+ parent: this._containerRef
291
+ })
292
+
293
+ // from the a11y team:
294
+ // axe devtools and other automated a11y tests both flagging this issue,
295
+ // which can be observed while navigating with keyboard:
296
+ // Ensure elements that have scrollable content are accessible by keyboard
297
+ // To solve this problem, you need to fix at least (1) of the following:
298
+ // Element should have focusable content, Element should be focusable
299
+ this._editorView.scrollDOM.tabIndex = 0
300
+
301
+ if (autofocus) {
302
+ this.focus()
303
+ }
304
+
305
+ if (indentOnLoad) {
306
+ this.indentAll()
307
+ }
308
+ this.assignAriaLabel()
309
+ }
310
+
311
+ componentWillUnmount() {
312
+ this._editorView?.destroy()
313
+
314
+ this.cancelAnimationFrames()
315
+ }
316
+
317
+ componentDidUpdate(prevProps: SourceCodeEditorProps) {
318
+ this.props.makeStyles?.()
319
+
320
+ if (this._editorView) {
321
+ if (this.props.value !== prevProps.value) {
322
+ this.refreshEditorValue()
323
+ }
324
+
325
+ if (this.shouldUpdateExtensions(prevProps)) {
326
+ this.refreshExtensions()
327
+ }
328
+ }
329
+ }
330
+
331
+ private shouldUpdateExtensions(prevProps: SourceCodeEditorProps) {
332
+ const propsToObserve: (keyof SourceCodeEditorProps)[] = [
333
+ 'styles', // needed for theme update
334
+ 'themeOverride',
335
+ 'language',
336
+ 'readOnly',
337
+ 'editable',
338
+ 'lineNumbers',
339
+ 'highlightActiveLineGutter',
340
+ 'foldGutter',
341
+ 'lineWrapping',
342
+ 'autofocus',
343
+ 'spellcheck',
344
+ 'direction',
345
+ 'dir',
346
+ 'rtlMoveVisually',
347
+ 'indentOnLoad',
348
+ 'indentWithTab',
349
+ 'indentUnit',
350
+ 'highlightActiveLine',
351
+ 'attachment'
352
+ ]
353
+
354
+ for (const prop of propsToObserve) {
355
+ if (!isEqual(this.props[prop], prevProps[prop])) {
356
+ return true
357
+ }
358
+ }
359
+
360
+ return false
361
+ }
362
+
363
+ get direction() {
364
+ // comes from the `direction` prop and
365
+ // falls back to the `dir` prop coming from the bidirectional decorator
366
+ return this.props.direction || this.props.dir
367
+ }
368
+
369
+ get extensions() {
370
+ const extensions = [
371
+ ...this.baseExtensions,
372
+
373
+ // our custom extensions
374
+ this.languageExtension,
375
+ this.onChangeExtension,
376
+ this.focusListenerExtension,
377
+ this.announceLineNumberExtension
378
+ ]
379
+
380
+ if (this.themeExtension) {
381
+ extensions.push(this.themeExtension)
382
+ }
383
+ if (this.props.lineNumbers) {
384
+ extensions.push(lineNumbers())
385
+ }
386
+ if (this.props.highlightActiveLine) {
387
+ extensions.push(highlightActiveLine())
388
+ }
389
+ if (this.props.highlightActiveLineGutter) {
390
+ extensions.push(highlightActiveLineGutter())
391
+ }
392
+ if (this.props.foldGutter) {
393
+ extensions.push(foldGutter())
394
+ }
395
+ if (this.props.lineWrapping) {
396
+ extensions.push(EditorView.lineWrapping)
397
+ }
398
+ if (this.props.editable === false) {
399
+ extensions.push(EditorView.editable.of(false))
400
+ }
401
+ if (this.props.readOnly) {
402
+ extensions.push(EditorState.readOnly.of(true))
403
+ }
404
+ if (this.props.spellcheck) {
405
+ extensions.push(EditorView.contentAttributes.of({ spellcheck: 'true' }))
406
+ }
407
+ if (this.direction) {
408
+ extensions.push(
409
+ EditorView.contentAttributes.of({
410
+ dir: this.direction
411
+ })
412
+ )
413
+ }
414
+ if (this.props.indentUnit) {
415
+ extensions.push(indentUnit.of(this.props.indentUnit))
416
+ }
417
+
418
+ return extensions
419
+ }
420
+
421
+ get baseExtensions() {
422
+ return [
423
+ // The extensions are based on codemirrors basic setup from 'codemirror'.
424
+ // It is recommended by CodeMirror, that if we want to configure
425
+ // our editor more precisely, we have to copy the source
426
+ // and adjust it as desired.
427
+ highlightSpecialChars(),
428
+ history(),
429
+ drawSelection(),
430
+ dropCursor(),
431
+ EditorState.allowMultipleSelections.of(true),
432
+ syntaxHighlighting(defaultHighlightStyle, { fallback: true }),
433
+ bracketMatching(),
434
+ closeBrackets(),
435
+ autocompletion(),
436
+ rectangularSelection(),
437
+ crosshairCursor(),
438
+ highlightSelectionMatches(),
439
+ indentOnInput(),
440
+ customSearch(this.props.searchConfig),
441
+ keymap.of(this.keymaps)
442
+ ]
443
+ }
444
+
445
+ get keymaps(): KeyBinding[] {
446
+ // TODO: if more keymaps are added, list them in the docs as well (#Command keybinding)
447
+ const keymaps: KeyBinding[] = [
448
+ ...closeBracketsKeymap,
449
+ ...this.commandKeybinding,
450
+ ...historyKeymap,
451
+ ...foldKeymap,
452
+ ...completionKeymap,
453
+ ...lintKeymap,
454
+ ...(this.props.searchConfig ? searchKeymap : [])
455
+ ]
456
+
457
+ if (this.props.indentWithTab) {
458
+ keymaps.push(indentWithTab)
459
+ }
460
+
461
+ return keymaps
462
+ }
463
+
464
+ get commandKeybinding() {
465
+ const { rtlMoveVisually } = this.props
466
+
467
+ if (this.direction === 'rtl' && !rtlMoveVisually) {
468
+ const overrideableKeys = rtlHorizontalArrowKeymap.map((binding) =>
469
+ binding.key ? binding.key : binding.mac ? binding.mac : binding
470
+ )
471
+ // we have to remove the binding we want to override from the original,
472
+ // otherwise all will be merged and the defaults will still apply
473
+ const filteredOriginal = defaultKeymap.filter((binding) =>
474
+ binding.key
475
+ ? overrideableKeys.indexOf(binding.key) < 0
476
+ : binding.mac
477
+ ? overrideableKeys.indexOf(binding.mac) < 0
478
+ : false
479
+ )
480
+ return [...filteredOriginal, ...rtlHorizontalArrowKeymap]
481
+ }
482
+
483
+ return defaultKeymap
484
+ }
485
+
486
+ get themeExtension() {
487
+ const { styles } = this.props
488
+
489
+ if (!styles?.theme || !styles.highlightStyle) {
490
+ return undefined
491
+ }
492
+
493
+ const theme = EditorView.theme(styles?.theme)
494
+ const highlightStyle = syntaxHighlighting(
495
+ HighlightStyle.define(styles?.highlightStyle)
496
+ )
497
+
498
+ // see notes in props.ts
499
+ // if (darkTheme) {
500
+ // theme = oneDarkTheme
501
+ // highlightStyle = syntaxHighlighting(oneDarkHighlightStyle)
502
+ // }
503
+
504
+ return [theme, highlightStyle]
505
+ }
506
+
507
+ get languageExtension() {
508
+ const { language } = this.props
509
+
510
+ switch (language) {
511
+ case 'json':
512
+ return json()
513
+ case 'js':
514
+ case 'jsx':
515
+ case 'javascript':
516
+ return javascript({ jsx: true, typescript: true })
517
+ case 'html':
518
+ return html({ matchClosingTags: true, autoCloseTags: true })
519
+ case 'css':
520
+ return css()
521
+ case 'markdown':
522
+ return markdown()
523
+ case 'sh':
524
+ case 'shell':
525
+ case 'bash': // ????
526
+ return StreamLanguage.define(shell)
527
+ case 'yml':
528
+ case 'yaml':
529
+ return StreamLanguage.define(yaml)
530
+ default:
531
+ return javascript({ jsx: true, typescript: true })
532
+ }
533
+ }
534
+
535
+ callOnChangeHandler(newValue: string) {
536
+ const { onChange, value } = this.props
537
+
538
+ this.addAnimationFrame(() => {
539
+ if (typeof onChange === 'function' && newValue !== value) {
540
+ onChange(newValue)
541
+ }
542
+ })
543
+ }
544
+
545
+ get onChangeExtension() {
546
+ return EditorState.changeFilter.of((transaction: Transaction) => {
547
+ if (!this._editorView) {
548
+ return false
549
+ }
550
+
551
+ if (transaction.docChanged) {
552
+ const newDoc = transaction.newDoc.toString()
553
+
554
+ if (this.isControlled) {
555
+ // the value will be changed by the onChange handler,
556
+ // refreshEditorValue has to run first
557
+ if (newDoc !== this.props.value) {
558
+ this._newSelectionAfterValueChange = transaction.selection
559
+ this.cancelAnimationFrames()
560
+ this.callOnChangeHandler(newDoc)
561
+ return false
562
+ } else {
563
+ return true
564
+ }
565
+ } else {
566
+ this.callOnChangeHandler(newDoc)
567
+ }
568
+ }
569
+
570
+ return true
571
+ })
572
+ }
573
+
574
+ get focusListenerExtension() {
575
+ const { onFocus, onBlur } = this.props
576
+
577
+ return EditorView.updateListener.of((update) => {
578
+ if (update.focusChanged && this._editorView) {
579
+ if (this.hasFocus) {
580
+ if (typeof onFocus === 'function') {
581
+ onFocus()
582
+ }
583
+ } else {
584
+ if (typeof onBlur === 'function') {
585
+ onBlur()
586
+ }
587
+ }
588
+ }
589
+ })
590
+ }
591
+
592
+ get announceLineNumberExtension() {
593
+ return EditorState.transactionExtender.of((tr) => {
594
+ const selection = tr.selection
595
+ const oldSelection = tr.startState.selection.main
596
+
597
+ if (selection && selection.main.empty && oldSelection.empty) {
598
+ const oldLine = tr.startState.doc.lineAt(oldSelection.head)
599
+ const newLine = tr.newDoc.lineAt(selection.main.head)
600
+ if (oldLine.number != newLine.number)
601
+ return {
602
+ effects: EditorView.announce.of(
603
+ tr.startState.phrase('line ') + newLine.number + '.'
604
+ )
605
+ }
606
+ }
607
+ return null
608
+ })
609
+ }
610
+
611
+ refreshExtensions() {
612
+ this.dispatchViewEffects(StateEffect.reconfigure.of(this.extensions))
613
+ }
614
+
615
+ refreshEditorValue() {
616
+ if (!this._editorView) return
617
+
618
+ const { value } = this.props
619
+
620
+ const currentValue = this._editorView.state.doc!.toString()
621
+
622
+ if (typeof value !== 'undefined' && currentValue !== value) {
623
+ let userEvent
624
+ const lengthDiff = value.length - currentValue.length
625
+
626
+ // setting user events are needed for the autocomplete to work
627
+ // (only these 2 events, autocomplete doesn't work on paste, etc.)
628
+ if (lengthDiff === 1) {
629
+ userEvent = 'input.type'
630
+ } else if (lengthDiff === -1) {
631
+ userEvent = 'delete.backward'
632
+ }
633
+
634
+ this.dispatchViewChanges({
635
+ changes: {
636
+ from: 0,
637
+ to: currentValue.length,
638
+ insert: value || ''
639
+ },
640
+ selection: this._newSelectionAfterValueChange,
641
+ userEvent: userEvent
642
+ })
643
+ this._newSelectionAfterValueChange = undefined
644
+ }
645
+
646
+ if (this.props.indentOnLoad) {
647
+ this.indentAll()
648
+ }
649
+ }
650
+
651
+ assignAriaLabel = () => {
652
+ if (this._containerRef) {
653
+ const editorDiv = this._containerRef.querySelector('[role="textbox"]')
654
+ if (editorDiv) {
655
+ editorDiv.setAttribute('aria-labelledby', `${this._id}`)
656
+ }
657
+ }
658
+ }
659
+
660
+ render() {
661
+ const { label, styles, ...restProps } = this.props
662
+
663
+ return (
664
+ <div
665
+ data-cid="SourceCodeEditor"
666
+ ref={this.handleRef}
667
+ css={styles?.codeEditor}
668
+ {...passthroughProps(
669
+ omitProps(restProps, SourceCodeEditor.allowedProps)
670
+ )}
671
+ >
672
+ <label css={styles?.label} id={this._id}>
673
+ <ScreenReaderContent>{label}</ScreenReaderContent>
674
+ <div
675
+ ref={this.handleContainerRef}
676
+ css={styles?.codeEditorContainer}
677
+ />
678
+ </label>
679
+ </div>
680
+ )
681
+ }
682
+ }
683
+
684
+ export default SourceCodeEditor
685
+ export { SourceCodeEditor }