@instructure/ui-source-code-editor 11.7.10 → 11.7.11-pr-snapshot-1794841957152

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