@instructure/ui-source-code-editor 11.7.3-snapshot-43 → 11.7.4-pr-snapshot-1781695314229

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