@mongodb-js/compass-editor 0.6.0 → 0.8.0

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 (96) hide show
  1. package/dist/actions.d.ts +11 -0
  2. package/dist/actions.d.ts.map +1 -0
  3. package/dist/{multiline-editor.js → actions.js} +8 -57
  4. package/dist/actions.js.map +1 -0
  5. package/dist/autocompleter.d.ts +22 -7
  6. package/dist/autocompleter.d.ts.map +1 -1
  7. package/dist/autocompleter.js +28 -8
  8. package/dist/autocompleter.js.map +1 -1
  9. package/dist/codemirror/ace-compat-autocompleter.d.ts +26 -0
  10. package/dist/codemirror/ace-compat-autocompleter.d.ts.map +1 -0
  11. package/dist/codemirror/ace-compat-autocompleter.js +67 -0
  12. package/dist/codemirror/ace-compat-autocompleter.js.map +1 -0
  13. package/dist/codemirror/aggregation-autocompleter.d.ts +3 -0
  14. package/dist/codemirror/aggregation-autocompleter.d.ts.map +1 -0
  15. package/dist/codemirror/aggregation-autocompleter.js +58 -0
  16. package/dist/codemirror/aggregation-autocompleter.js.map +1 -0
  17. package/dist/codemirror/document-autocompleter.d.ts +3 -0
  18. package/dist/codemirror/document-autocompleter.d.ts.map +1 -0
  19. package/dist/codemirror/document-autocompleter.js +50 -0
  20. package/dist/codemirror/document-autocompleter.js.map +1 -0
  21. package/dist/codemirror/query-autocompleter.d.ts +4 -0
  22. package/dist/codemirror/query-autocompleter.d.ts.map +1 -0
  23. package/dist/codemirror/query-autocompleter.js +22 -0
  24. package/dist/codemirror/query-autocompleter.js.map +1 -0
  25. package/dist/codemirror/stage-autocompleter.d.ts +6 -0
  26. package/dist/codemirror/stage-autocompleter.d.ts.map +1 -0
  27. package/dist/codemirror/stage-autocompleter.js +58 -0
  28. package/dist/codemirror/stage-autocompleter.js.map +1 -0
  29. package/dist/codemirror/utils.d.ts +14 -0
  30. package/dist/codemirror/utils.d.ts.map +1 -0
  31. package/dist/codemirror/utils.js +80 -0
  32. package/dist/codemirror/utils.js.map +1 -0
  33. package/dist/codemirror/validation-autocompleter.d.ts +4 -0
  34. package/dist/codemirror/validation-autocompleter.d.ts.map +1 -0
  35. package/dist/codemirror/validation-autocompleter.js +123 -0
  36. package/dist/codemirror/validation-autocompleter.js.map +1 -0
  37. package/dist/index.d.ts +7 -13
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +14 -31
  40. package/dist/index.js.map +1 -1
  41. package/dist/json-editor.d.ts +101 -16
  42. package/dist/json-editor.d.ts.map +1 -1
  43. package/dist/json-editor.js +440 -76
  44. package/dist/json-editor.js.map +1 -1
  45. package/dist/prettify.d.ts +1 -1
  46. package/dist/prettify.d.ts.map +1 -1
  47. package/dist/types.d.ts +4 -36
  48. package/dist/types.d.ts.map +1 -1
  49. package/package.json +12 -14
  50. package/dist/ace/aggregation-autocompleter.d.ts +0 -15
  51. package/dist/ace/aggregation-autocompleter.d.ts.map +0 -1
  52. package/dist/ace/aggregation-autocompleter.js +0 -108
  53. package/dist/ace/aggregation-autocompleter.js.map +0 -1
  54. package/dist/ace/index.d.ts +0 -2
  55. package/dist/ace/index.d.ts.map +0 -1
  56. package/dist/ace/index.js +0 -37
  57. package/dist/ace/index.js.map +0 -1
  58. package/dist/ace/mode.d.ts +0 -2
  59. package/dist/ace/mode.d.ts.map +0 -1
  60. package/dist/ace/mode.js +0 -67
  61. package/dist/ace/mode.js.map +0 -1
  62. package/dist/ace/query-autocompleter.d.ts +0 -12
  63. package/dist/ace/query-autocompleter.d.ts.map +0 -1
  64. package/dist/ace/query-autocompleter.js +0 -38
  65. package/dist/ace/query-autocompleter.js.map +0 -1
  66. package/dist/ace/stage-autocompleter.d.ts +0 -16
  67. package/dist/ace/stage-autocompleter.d.ts.map +0 -1
  68. package/dist/ace/stage-autocompleter.js +0 -70
  69. package/dist/ace/stage-autocompleter.js.map +0 -1
  70. package/dist/ace/theme.d.ts +0 -4
  71. package/dist/ace/theme.d.ts.map +0 -1
  72. package/dist/ace/theme.js +0 -297
  73. package/dist/ace/theme.js.map +0 -1
  74. package/dist/ace/util.d.ts +0 -3
  75. package/dist/ace/util.d.ts.map +0 -1
  76. package/dist/ace/util.js +0 -12
  77. package/dist/ace/util.js.map +0 -1
  78. package/dist/ace/validation-autocompleter.d.ts +0 -12
  79. package/dist/ace/validation-autocompleter.d.ts.map +0 -1
  80. package/dist/ace/validation-autocompleter.js +0 -36
  81. package/dist/ace/validation-autocompleter.js.map +0 -1
  82. package/dist/base-editor.d.ts +0 -36
  83. package/dist/base-editor.d.ts.map +0 -1
  84. package/dist/base-editor.js +0 -156
  85. package/dist/base-editor.js.map +0 -1
  86. package/dist/inline-editor.d.ts +0 -8
  87. package/dist/inline-editor.d.ts.map +0 -1
  88. package/dist/inline-editor.js +0 -21
  89. package/dist/inline-editor.js.map +0 -1
  90. package/dist/multiline-editor.d.ts +0 -18
  91. package/dist/multiline-editor.d.ts.map +0 -1
  92. package/dist/multiline-editor.js.map +0 -1
  93. package/dist/syntax-highlight.d.ts +0 -10
  94. package/dist/syntax-highlight.d.ts.map +0 -1
  95. package/dist/syntax-highlight.js +0 -26
  96. package/dist/syntax-highlight.js.map +0 -1
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.JSONEditor = void 0;
26
+ exports.setCodemirrorEditorValue = exports.CodemirrorMultilineEditor = exports.CodemirrorInlineEditor = exports.BaseEditor = exports.languageName = exports.languages = exports.editorPalette = void 0;
27
27
  const react_1 = __importStar(require("react"));
28
28
  const view_1 = require("@codemirror/view");
29
29
  const language_1 = require("@codemirror/language");
@@ -38,16 +38,42 @@ const language_2 = require("@codemirror/language");
38
38
  const highlight_1 = require("@lezer/highlight");
39
39
  const polished_1 = require("polished");
40
40
  const prettify_1 = require("./prettify");
41
+ const actions_1 = require("./actions");
41
42
  const editorStyle = (0, compass_components_1.css)({
42
43
  fontSize: 13,
43
44
  fontFamily: compass_components_1.fontFamilies.code,
44
45
  });
45
- const editorPalette = {
46
+ const hiddenScrollStyle = (0, compass_components_1.css)({
47
+ '& .cm-scroller': {
48
+ overflow: '-moz-scrollbars-none',
49
+ msOverflowStyle: 'none',
50
+ },
51
+ '& .cm-scroller::-webkit-scrollbar': {
52
+ display: 'none',
53
+ },
54
+ });
55
+ function isReadOnly(state) {
56
+ return state.facet(state_1.EditorState.readOnly);
57
+ }
58
+ const breakFocusOutBinding = {
59
+ ...commands_1.indentWithTab,
60
+ run({ state, dispatch }) {
61
+ if (isReadOnly(state)) {
62
+ return false;
63
+ }
64
+ dispatch(state.update(state.replaceSelection('\t'), {
65
+ scrollIntoView: true,
66
+ userEvent: 'input',
67
+ }));
68
+ return true;
69
+ },
70
+ };
71
+ exports.editorPalette = {
46
72
  light: {
47
73
  color: compass_components_1.codePalette.light[3],
48
74
  backgroundColor: compass_components_1.codePalette.light[0],
49
- gutterColor: compass_components_1.palette.gray.base,
50
- gutterBackgroundColor: compass_components_1.palette.gray.light3,
75
+ gutterColor: compass_components_1.codePalette.light[3],
76
+ gutterBackgroundColor: compass_components_1.codePalette.light[0],
51
77
  gutterActiveLineBackgroundColor: (0, polished_1.rgba)(compass_components_1.palette.gray.light2, 0.5),
52
78
  gutterFoldButtonColor: compass_components_1.palette.black,
53
79
  cursorColor: compass_components_1.palette.gray.base,
@@ -59,12 +85,17 @@ const editorPalette = {
59
85
  errorGutterIconColor: encodeURIComponent(compass_components_1.palette.red.base),
60
86
  foldPlaceholderColor: compass_components_1.palette.gray.base,
61
87
  foldPlaceholderBackgroundColor: compass_components_1.palette.gray.light3,
88
+ autocompleteColor: compass_components_1.palette.black,
89
+ autocompleteBackgroundColor: compass_components_1.palette.gray.light3,
90
+ autocompleteBorderColor: compass_components_1.palette.gray.light2,
91
+ autocompleteMatchColor: compass_components_1.palette.green.dark1,
92
+ autocompleteSelectedBackgroundColor: compass_components_1.palette.gray.light2,
62
93
  },
63
94
  dark: {
64
95
  color: compass_components_1.codePalette.dark[3],
65
96
  backgroundColor: compass_components_1.codePalette.dark[0],
66
- gutterColor: compass_components_1.palette.gray.light3,
67
- gutterBackgroundColor: compass_components_1.palette.gray.dark4,
97
+ gutterColor: compass_components_1.codePalette.dark[3],
98
+ gutterBackgroundColor: compass_components_1.codePalette.dark[0],
68
99
  gutterActiveLineBackgroundColor: (0, polished_1.rgba)(compass_components_1.palette.gray.dark2, 0.5),
69
100
  gutterFoldButtonColor: compass_components_1.palette.white,
70
101
  cursorColor: compass_components_1.palette.green.base,
@@ -76,44 +107,45 @@ const editorPalette = {
76
107
  errorGutterIconColor: encodeURIComponent(compass_components_1.palette.red.light1),
77
108
  foldPlaceholderColor: compass_components_1.palette.gray.base,
78
109
  foldPlaceholderBackgroundColor: compass_components_1.palette.gray.dark3,
110
+ autocompleteColor: compass_components_1.palette.gray.light1,
111
+ autocompleteBackgroundColor: compass_components_1.palette.gray.dark4,
112
+ autocompleteBorderColor: compass_components_1.palette.gray.dark1,
113
+ autocompleteMatchColor: compass_components_1.palette.gray.light3,
114
+ autocompleteSelectedBackgroundColor: compass_components_1.palette.gray.dark2,
79
115
  },
80
116
  };
81
117
  function getStylesForTheme(theme) {
82
118
  return view_1.EditorView.theme({
83
119
  '&': {
84
- color: editorPalette[theme].color,
85
- backgroundColor: editorPalette[theme].backgroundColor,
120
+ color: exports.editorPalette[theme].color,
121
+ backgroundColor: exports.editorPalette[theme].backgroundColor,
122
+ maxHeight: '100%',
86
123
  },
87
124
  '& .cm-scroller': {
88
125
  fontSize: '13px',
89
126
  fontFamily: compass_components_1.fontFamilies.code,
90
- lineHeight: `${compass_components_1.spacing[3]}px`,
91
127
  },
92
128
  '&.cm-editor.cm-focused': {
93
129
  outline: 'none',
94
130
  },
95
131
  '& .cm-content': {
96
- caretColor: editorPalette[theme].cursorColor,
132
+ paddingTop: `${compass_components_1.spacing[1]}px`,
133
+ paddingBottom: `${compass_components_1.spacing[1]}px`,
134
+ caretColor: exports.editorPalette[theme].cursorColor,
97
135
  },
98
136
  '& .cm-activeLine': {
99
137
  background: 'none',
100
138
  },
101
139
  '&.cm-focused .cm-activeLine': {
102
- backgroundColor: editorPalette[theme].activeLineBackgroundColor,
140
+ backgroundColor: exports.editorPalette[theme].activeLineBackgroundColor,
103
141
  },
104
142
  '& .cm-gutters': {
105
- color: editorPalette[theme].gutterColor,
106
- backgroundColor: editorPalette[theme].gutterBackgroundColor,
143
+ color: exports.editorPalette[theme].gutterColor,
144
+ backgroundColor: exports.editorPalette[theme].gutterBackgroundColor,
107
145
  border: 'none',
108
146
  },
109
- '& .cm-gutters > .cm-gutter:first-child > .cm-gutterElement': {
110
- paddingLeft: `${compass_components_1.spacing[2]}px`,
111
- },
112
- '& .cm-gutters > .cm-gutter:last-child > .cm-gutterElement': {
113
- paddingRight: `${compass_components_1.spacing[2]}px`,
114
- },
115
147
  '& .cm-gutter-lint': {
116
- width: 'auto',
148
+ width: `${compass_components_1.spacing[3]}px`,
117
149
  },
118
150
  '& .cm-gutter-lint .cm-gutterElement': {
119
151
  padding: '0',
@@ -124,26 +156,26 @@ function getStylesForTheme(theme) {
124
156
  padding: '2px',
125
157
  },
126
158
  '& .cm-gutter-lint .cm-lint-marker-info': {
127
- content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${editorPalette[theme].infoGutterIconColor}' fill-rule='evenodd' d='M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14ZM9 4a1 1 0 1 1-2 0 1 1 0 0 1 2 0ZM8 6a1 1 0 0 1 1 1v4h.5a.5.5 0 0 1 0 1h-3a.5.5 0 0 1 0-1H7V7h-.5a.5.5 0 0 1 0-1H8Z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
159
+ content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${exports.editorPalette[theme].infoGutterIconColor}' fill-rule='evenodd' d='M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14ZM9 4a1 1 0 1 1-2 0 1 1 0 0 1 2 0ZM8 6a1 1 0 0 1 1 1v4h.5a.5.5 0 0 1 0 1h-3a.5.5 0 0 1 0-1H7V7h-.5a.5.5 0 0 1 0-1H8Z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
128
160
  },
129
161
  '& .cm-gutter-lint .cm-lint-marker-warning': {
130
- content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${editorPalette[theme].warningGutterIconColor}' fill-rule='evenodd' d='M8.864 2.514a.983.983 0 0 0-1.728 0L1.122 13.539A.987.987 0 0 0 1.986 15h12.028a.987.987 0 0 0 .864-1.461L8.864 2.514ZM7 6a1 1 0 0 1 2 0v4a1 1 0 1 1-2 0V6Zm2 7a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
162
+ content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${exports.editorPalette[theme].warningGutterIconColor}' fill-rule='evenodd' d='M8.864 2.514a.983.983 0 0 0-1.728 0L1.122 13.539A.987.987 0 0 0 1.986 15h12.028a.987.987 0 0 0 .864-1.461L8.864 2.514ZM7 6a1 1 0 0 1 2 0v4a1 1 0 1 1-2 0V6Zm2 7a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
131
163
  },
132
164
  '& .cm-gutter-lint .cm-lint-marker-error': {
133
- content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${editorPalette[theme].errorGutterIconColor}' fill-rule='evenodd' d='M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14zm1.414-9.828a1 1 0 1 1 1.414 1.414L9.414 8l1.414 1.414a1 1 0 1 1-1.414 1.414L8 9.414l-1.414 1.414a1 1 0 1 1-1.414-1.414L6.586 8 5.172 6.586a1 1 0 0 1 1.414-1.414L8 6.586l1.414-1.414z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
165
+ content: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 16 16'%3E%3Cpath fill='${exports.editorPalette[theme].errorGutterIconColor}' fill-rule='evenodd' d='M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14zm1.414-9.828a1 1 0 1 1 1.414 1.414L9.414 8l1.414 1.414a1 1 0 1 1-1.414 1.414L8 9.414l-1.414 1.414a1 1 0 1 1-1.414-1.414L6.586 8 5.172 6.586a1 1 0 0 1 1.414-1.414L8 6.586l1.414-1.414z' clip-rule='evenodd'/%3E%3C/svg%3E%0A")`,
134
166
  },
135
167
  '& .cm-activeLineGutter': {
136
168
  background: 'none',
137
169
  },
138
170
  '&.cm-focused .cm-activeLineGutter': {
139
- backgroundColor: editorPalette[theme].gutterActiveLineBackgroundColor,
171
+ backgroundColor: exports.editorPalette[theme].gutterActiveLineBackgroundColor,
140
172
  },
141
173
  '& .cm-foldPlaceholder': {
142
174
  display: 'inline-block',
143
175
  border: 'none',
144
- color: editorPalette[theme].foldPlaceholderColor,
145
- backgroundColor: editorPalette[theme].foldPlaceholderBackgroundColor,
146
- boxShadow: `inset 0 0 0 1px ${editorPalette[theme].foldPlaceholderColor}`,
176
+ color: exports.editorPalette[theme].foldPlaceholderColor,
177
+ backgroundColor: exports.editorPalette[theme].foldPlaceholderBackgroundColor,
178
+ boxShadow: `inset 0 0 0 1px ${exports.editorPalette[theme].foldPlaceholderColor}`,
147
179
  padding: '0 2px',
148
180
  },
149
181
  '& .foldMarker': {
@@ -158,14 +190,14 @@ function getStylesForTheme(theme) {
158
190
  width: '12px',
159
191
  height: '12px',
160
192
  display: 'block',
161
- color: editorPalette[theme].gutterFoldButtonColor,
193
+ color: exports.editorPalette[theme].gutterFoldButtonColor,
162
194
  },
163
195
  '& .cm-selectionBackground': {
164
- backgroundColor: editorPalette[theme].selectionBackgroundColor,
196
+ backgroundColor: exports.editorPalette[theme].selectionBackgroundColor,
165
197
  },
166
198
  '&.cm-focused .cm-matchingBracket': {
167
199
  background: 'none',
168
- boxShadow: `0 0 0 1px ${editorPalette[theme].bracketBorderColor}`,
200
+ boxShadow: `0 0 0 1px ${exports.editorPalette[theme].bracketBorderColor}`,
169
201
  borderRadius: '2px',
170
202
  },
171
203
  '& .cm-selectionBackground:first-child': {
@@ -178,9 +210,48 @@ function getStylesForTheme(theme) {
178
210
  },
179
211
  '& .cm-cursor': {
180
212
  borderLeft: '2px solid',
181
- borderColor: editorPalette[theme].cursorColor,
213
+ borderColor: exports.editorPalette[theme].cursorColor,
182
214
  marginLeft: '-1px',
183
215
  },
216
+ '& .cm-tooltip': {
217
+ color: exports.editorPalette[theme].autocompleteColor,
218
+ backgroundColor: exports.editorPalette[theme].autocompleteBackgroundColor,
219
+ border: `1px solid ${exports.editorPalette[theme].autocompleteBorderColor}`,
220
+ },
221
+ '& .cm-tooltip.cm-tooltip-autocomplete > ul': {
222
+ fontFamily: compass_components_1.fontFamilies.code,
223
+ },
224
+ '& .cm-tooltip-autocomplete ul li[aria-selected]': {
225
+ color: exports.editorPalette[theme].autocompleteColor,
226
+ backgroundColor: exports.editorPalette[theme].autocompleteSelectedBackgroundColor,
227
+ },
228
+ '& .cm-completionIcon': {
229
+ display: 'none',
230
+ },
231
+ '& .cm-completionDetail': {
232
+ color: (0, polished_1.rgba)(exports.editorPalette[theme].autocompleteColor, 0.5),
233
+ fontStyle: 'normal',
234
+ marginRight: '1em',
235
+ },
236
+ '& .cm-completionMatchedText': {
237
+ color: exports.editorPalette[theme].autocompleteMatchColor,
238
+ fontWeight: 'bold',
239
+ textDecoration: 'none',
240
+ },
241
+ '& .cm-tooltip .completion-info p': {
242
+ margin: 0,
243
+ marginTop: `${compass_components_1.spacing[2]}px`,
244
+ marginBottom: `${compass_components_1.spacing[2]}px`,
245
+ },
246
+ '& .cm-tooltip .completion-info p:first-child': {
247
+ marginTop: 0,
248
+ },
249
+ '& .cm-tooltip .completion-info p:last-child': {
250
+ marginBottom: 0,
251
+ },
252
+ '& .cm-widgetBuffer': {
253
+ verticalAlign: 'top',
254
+ },
184
255
  }, { dark: theme === 'dark' });
185
256
  }
186
257
  const themeStyles = {
@@ -265,13 +336,30 @@ function createFoldGutterExtension() {
265
336
  const javascriptExpression = lang_javascript_1.javascriptLanguage.configure({
266
337
  top: 'SingleExpression',
267
338
  });
268
- const languages = {
339
+ exports.languages = {
269
340
  json: lang_json_1.json,
270
- javascript() {
341
+ javascript: lang_javascript_1.javascript,
342
+ 'javascript-expression': () => {
271
343
  return new language_2.LanguageSupport(javascriptExpression);
272
344
  },
273
345
  };
274
- const languageName = state_1.Facet.define({});
346
+ exports.languageName = state_1.Facet.define({});
347
+ async function wait(ms) {
348
+ return new Promise((resolve) => {
349
+ setTimeout(resolve, ms);
350
+ });
351
+ }
352
+ async function sheduleDispatch(editorView, transactions, signal) {
353
+ while (editorView.updateState != 0) {
354
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
355
+ return false;
356
+ }
357
+ await wait();
358
+ }
359
+ transactions = Array.isArray(transactions) ? transactions : [transactions];
360
+ editorView.dispatch(...transactions);
361
+ return true;
362
+ }
275
363
  function useCodemirrorExtensionCompartment(fn, value, editorViewRef) {
276
364
  var _a, _b;
277
365
  const extensionCreatorRef = (0, react_1.useRef)(fn);
@@ -281,16 +369,22 @@ function useCodemirrorExtensionCompartment(fn, value, editorViewRef) {
281
369
  const initialExtensionRef = (0, react_1.useRef)();
282
370
  (_b = initialExtensionRef.current) !== null && _b !== void 0 ? _b : (initialExtensionRef.current = compartmentRef.current.of(extensionCreatorRef.current()));
283
371
  (0, compass_components_1.useEffectOnChange)(() => {
284
- var _a, _b;
285
- (_a = editorViewRef.current) === null || _a === void 0 ? void 0 : _a.dispatch({
286
- effects: (_b = compartmentRef.current) === null || _b === void 0 ? void 0 : _b.reconfigure(extensionCreatorRef.current()),
287
- });
372
+ var _a;
373
+ if (!editorViewRef.current) {
374
+ return;
375
+ }
376
+ const controller = new AbortController();
377
+ void sheduleDispatch(editorViewRef.current, {
378
+ effects: (_a = compartmentRef.current) === null || _a === void 0 ? void 0 : _a.reconfigure(extensionCreatorRef.current()),
379
+ }, controller.signal);
380
+ return () => {
381
+ controller.abort();
382
+ };
288
383
  }, value);
289
384
  return initialExtensionRef.current;
290
385
  }
291
- const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
292
- }, language = 'json', showLineNumbers = true, showFoldGutter = true, annotations = [], completer, darkMode: _darkMode, className, readOnly = false, onLoad = () => {
293
- }, ...props }) => {
386
+ const BaseEditor = react_1.default.forwardRef(function BaseEditor({ initialText: _initialText, text, onChangeText, language = 'json', showLineNumbers = true, showFoldGutter = true, showAnnotationsGutter = true, showScroll = true, highlightActiveLine: shouldHighlightActiveLine = true, annotations, completer, darkMode: _darkMode, className, readOnly = false, onLoad = () => {
387
+ }, minLines, maxLines, lineHeight = 16, placeholder: placeholderString, commands, initialJSONFoldAll: _initialJSONFoldAll = true, ...props }, ref) {
294
388
  const darkMode = (0, compass_components_1.useDarkMode)(_darkMode);
295
389
  const onChangeTextRef = (0, react_1.useRef)(onChangeText);
296
390
  const onLoadRef = (0, react_1.useRef)(onLoad);
@@ -298,28 +392,101 @@ const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
298
392
  const initialText = (0, react_1.useRef)(_initialText !== null && _initialText !== void 0 ? _initialText : text);
299
393
  const initialLanguage = (0, react_1.useRef)(language);
300
394
  const containerRef = (0, react_1.useRef)(null);
395
+ const editorContainerRef = (0, react_1.useRef)(null);
301
396
  const editorViewRef = (0, react_1.useRef)();
397
+ const [{ height: contentHeight, hasScroll }, setContentHeight] = (0, react_1.useState)({
398
+ height: 0,
399
+ hasScroll: false,
400
+ });
401
+ const initialJSONFoldAll = (0, react_1.useRef)(_initialJSONFoldAll);
302
402
  onChangeTextRef.current = onChangeText;
303
403
  onLoadRef.current = onLoad;
404
+ (0, react_1.useImperativeHandle)(ref, () => {
405
+ return {
406
+ foldAll() {
407
+ if (!editorViewRef.current) {
408
+ return false;
409
+ }
410
+ return foldAll(editorViewRef.current);
411
+ },
412
+ unfoldAll() {
413
+ if (!editorViewRef.current) {
414
+ return false;
415
+ }
416
+ return (0, language_1.unfoldAll)(editorViewRef.current);
417
+ },
418
+ copyAll() {
419
+ if (!editorViewRef.current) {
420
+ return false;
421
+ }
422
+ return copyAll(editorViewRef.current);
423
+ },
424
+ prettify() {
425
+ if (!editorViewRef.current) {
426
+ return false;
427
+ }
428
+ return prettify(editorViewRef.current);
429
+ },
430
+ applySnippet(template) {
431
+ if (!editorViewRef.current) {
432
+ return false;
433
+ }
434
+ return applySnippet(editorViewRef.current, template);
435
+ },
436
+ focus() {
437
+ if (!editorViewRef.current) {
438
+ return false;
439
+ }
440
+ editorViewRef.current.focus();
441
+ return true;
442
+ },
443
+ get editor() {
444
+ var _a;
445
+ return (_a = editorViewRef.current) !== null && _a !== void 0 ? _a : null;
446
+ },
447
+ };
448
+ }, []);
304
449
  const languageExtension = useCodemirrorExtensionCompartment(() => {
305
- return [languages[language](), languageName.of(language)];
450
+ return [exports.languages[language](), exports.languageName.of(language)];
306
451
  }, language, editorViewRef);
452
+ const activeLineExtension = useCodemirrorExtensionCompartment(() => {
453
+ return !readOnly && shouldHighlightActiveLine
454
+ ? [(0, view_1.highlightActiveLine)(), (0, view_1.highlightActiveLineGutter)()]
455
+ : [];
456
+ }, [readOnly, shouldHighlightActiveLine], editorViewRef);
307
457
  const readOnlyExtension = useCodemirrorExtensionCompartment(() => {
308
- return [state_1.EditorState.readOnly.of(readOnly)].concat(readOnly ? [] : [(0, view_1.highlightActiveLine)(), (0, view_1.highlightActiveLineGutter)()]);
458
+ return state_1.EditorState.readOnly.of(readOnly);
309
459
  }, readOnly, editorViewRef);
310
460
  const themeConfigExtension = useCodemirrorExtensionCompartment(() => {
311
461
  return themeStyles[darkMode ? 'dark' : 'light'];
312
462
  }, darkMode, editorViewRef);
463
+ const lineHeightExtension = useCodemirrorExtensionCompartment(() => {
464
+ return view_1.EditorView.theme({
465
+ '& .cm-scroller': {
466
+ lineHeight: `${lineHeight}px`,
467
+ ...(maxLines &&
468
+ maxLines < Infinity && {
469
+ maxHeight: `${maxLines * lineHeight}px`,
470
+ }),
471
+ height: '100%',
472
+ overflowY: 'auto',
473
+ },
474
+ '& .cm-content, & .cm-gutter': {
475
+ ...(minLines && { minHeight: `${minLines * lineHeight}px` }),
476
+ },
477
+ });
478
+ }, [minLines, maxLines, lineHeight], editorViewRef);
313
479
  const lineNumbersExtension = useCodemirrorExtensionCompartment(() => {
314
480
  return showLineNumbers ? (0, view_1.lineNumbers)() : [];
315
481
  }, showLineNumbers, editorViewRef);
316
482
  const foldGutterExtension = useCodemirrorExtensionCompartment(() => {
317
483
  return showFoldGutter ? createFoldGutterExtension() : [];
318
484
  }, showFoldGutter, editorViewRef);
485
+ const shouldShowAnnotationsGutter = showAnnotationsGutter || (annotations && annotations.length === 0);
319
486
  const annotationsGutterExtension = useCodemirrorExtensionCompartment(() => {
320
- return annotations.length === 0 ? [] : (0, lint_1.lintGutter)();
321
- }, annotations.length === 0, editorViewRef);
322
- const autocomletionExtension = useCodemirrorExtensionCompartment(() => {
487
+ return shouldShowAnnotationsGutter ? (0, lint_1.lintGutter)() : [];
488
+ }, shouldShowAnnotationsGutter, editorViewRef);
489
+ const autocompletionExtension = useCodemirrorExtensionCompartment(() => {
323
490
  return completer
324
491
  ? (0, autocomplete_1.autocompletion)({
325
492
  activateOnTyping: true,
@@ -327,28 +494,67 @@ const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
327
494
  })
328
495
  : [];
329
496
  }, completer, editorViewRef);
497
+ const placeholderExtension = useCodemirrorExtensionCompartment(() => {
498
+ return placeholderString ? (0, view_1.placeholder)(placeholderString) : [];
499
+ }, placeholderString, editorViewRef);
500
+ const commandsExtension = useCodemirrorExtensionCompartment(() => {
501
+ return commands && commands.length > 0 ? view_1.keymap.of(commands) : [];
502
+ }, commands, editorViewRef);
503
+ const updateEditorContentHeight = (0, react_1.useCallback)(() => {
504
+ var _a;
505
+ (_a = editorViewRef.current) === null || _a === void 0 ? void 0 : _a.requestMeasure({
506
+ read(view) {
507
+ return [
508
+ view.contentHeight,
509
+ view.scrollDOM.scrollWidth > view.scrollDOM.clientWidth,
510
+ ];
511
+ },
512
+ write([height, hasScroll]) {
513
+ setContentHeight((state) => {
514
+ if (height !== state.height || hasScroll !== state.hasScroll) {
515
+ return { height, hasScroll };
516
+ }
517
+ return state;
518
+ });
519
+ },
520
+ });
521
+ }, []);
330
522
  (0, react_1.useLayoutEffect)(() => {
331
- if (!containerRef.current) {
523
+ if (!editorContainerRef.current) {
332
524
  throw new Error("Can't mount editor: DOM node is missing");
333
525
  }
334
- const domNode = containerRef.current;
526
+ const domNode = editorContainerRef.current;
335
527
  const editor = (editorViewRef.current = new view_1.EditorView({
336
528
  doc: initialText.current,
337
529
  extensions: [
530
+ state_1.EditorState.tabSize.of(2),
531
+ annotationsGutterExtension,
338
532
  lineNumbersExtension,
339
- (0, commands_1.history)(),
340
533
  foldGutterExtension,
534
+ (0, commands_1.history)(),
341
535
  (0, view_1.drawSelection)(),
342
536
  (0, language_1.indentOnInput)(),
343
537
  (0, language_1.bracketMatching)(),
344
538
  (0, autocomplete_1.closeBrackets)(),
345
- autocomletionExtension,
346
- annotationsGutterExtension,
539
+ autocompletionExtension,
347
540
  languageExtension,
348
541
  (0, language_2.syntaxHighlighting)(highlightStyles['light']),
349
542
  (0, language_2.syntaxHighlighting)(highlightStyles['dark']),
543
+ activeLineExtension,
544
+ lineHeightExtension,
350
545
  themeConfigExtension,
546
+ placeholderExtension,
547
+ commandsExtension,
351
548
  view_1.keymap.of([
549
+ {
550
+ key: 'Tab',
551
+ run(context) {
552
+ if (isReadOnly(context.state)) {
553
+ return false;
554
+ }
555
+ return (0, autocomplete_1.acceptCompletion)(context);
556
+ },
557
+ },
352
558
  {
353
559
  key: 'Ctrl-Shift-b',
354
560
  run: prettify,
@@ -362,14 +568,15 @@ const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
362
568
  ...commands_1.historyKeymap,
363
569
  ...language_1.foldKeymap,
364
570
  ...autocomplete_1.completionKeymap,
365
- commands_1.indentWithTab,
571
+ breakFocusOutBinding,
366
572
  ]),
367
573
  readOnlyExtension,
368
574
  view_1.EditorView.updateListener.of((update) => {
369
- var _a;
575
+ var _a, _b;
576
+ updateEditorContentHeight();
370
577
  if (update.docChanged) {
371
578
  const text = (_a = editor.state.sliceDoc()) !== null && _a !== void 0 ? _a : '';
372
- onChangeTextRef.current(text, update);
579
+ (_b = onChangeTextRef.current) === null || _b === void 0 ? void 0 : _b.call(onChangeTextRef, text, update);
373
580
  }
374
581
  }),
375
582
  ],
@@ -381,12 +588,13 @@ const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
381
588
  const docLength = editor.state.doc.length;
382
589
  const isSyntaxTreeAvailable = (0, language_1.syntaxTreeAvailable)(editor.state, docLength) ||
383
590
  (0, language_1.forceParsing)(editor, docLength, 150);
384
- if (isSyntaxTreeAvailable) {
591
+ if (initialJSONFoldAll.current && isSyntaxTreeAvailable) {
385
592
  foldAll(editor);
386
593
  }
387
594
  else {
388
595
  }
389
596
  }
597
+ updateEditorContentHeight();
390
598
  return () => {
391
599
  delete domNode._cm;
392
600
  editor.destroy();
@@ -398,32 +606,66 @@ const BaseEditor = ({ initialText: _initialText, text, onChangeText = () => {
398
606
  lineNumbersExtension,
399
607
  readOnlyExtension,
400
608
  themeConfigExtension,
401
- autocomletionExtension,
609
+ autocompletionExtension,
610
+ lineHeightExtension,
611
+ activeLineExtension,
612
+ placeholderExtension,
613
+ commandsExtension,
614
+ updateEditorContentHeight,
402
615
  ]);
403
616
  (0, react_1.useEffect)(() => {
404
- var _a, _b;
617
+ var _a;
405
618
  if (initialTextProvided.current) {
406
619
  return;
407
620
  }
408
- if (text !== ((_a = editorViewRef.current) === null || _a === void 0 ? void 0 : _a.state.sliceDoc())) {
409
- (_b = editorViewRef.current) === null || _b === void 0 ? void 0 : _b.dispatch({
621
+ if (editorViewRef.current &&
622
+ text !== ((_a = editorViewRef.current.state.sliceDoc()) !== null && _a !== void 0 ? _a : '')) {
623
+ const controller = new AbortController();
624
+ void sheduleDispatch(editorViewRef.current, {
410
625
  changes: {
411
626
  from: 0,
412
627
  to: editorViewRef.current.state.doc.length,
413
628
  insert: text,
414
629
  },
415
- });
630
+ }, controller.signal);
631
+ return () => {
632
+ controller.abort();
633
+ };
416
634
  }
417
635
  }, [text]);
418
636
  (0, react_1.useEffect)(() => {
419
- var _a;
420
- (_a = editorViewRef.current) === null || _a === void 0 ? void 0 : _a.dispatch({
421
- effects: lint_1.setDiagnosticsEffect.of(annotations),
422
- });
637
+ if (!editorViewRef.current) {
638
+ return;
639
+ }
640
+ const controller = new AbortController();
641
+ void sheduleDispatch(editorViewRef.current, {
642
+ effects: lint_1.setDiagnosticsEffect.of(annotations !== null && annotations !== void 0 ? annotations : []),
643
+ }, controller.signal);
644
+ return () => {
645
+ controller.abort();
646
+ };
423
647
  }, [annotations]);
424
- return (react_1.default.createElement("div", { ref: containerRef, className: (0, compass_components_1.cx)(editorStyle, className), "data-codemirror": "true", ...props }));
425
- };
426
- exports.JSONEditor = BaseEditor;
648
+ (0, react_1.useLayoutEffect)(() => {
649
+ if (containerRef.current) {
650
+ const scrollHeight = hasScroll && showScroll ? 10 : 0;
651
+ const height = Math.min(contentHeight, (maxLines !== null && maxLines !== void 0 ? maxLines : Infinity) * lineHeight) +
652
+ scrollHeight;
653
+ containerRef.current.style.height = `${height}px`;
654
+ }
655
+ }, [maxLines, contentHeight, hasScroll, lineHeight, showScroll]);
656
+ return (react_1.default.createElement("div", { ref: containerRef, className: className, style: {
657
+ width: '100%',
658
+ minHeight: Math.max(lineHeight, (minLines !== null && minLines !== void 0 ? minLines : 0) * lineHeight),
659
+ position: 'relative',
660
+ maxHeight: '100%',
661
+ } },
662
+ react_1.default.createElement("div", { ref: editorContainerRef, className: (0, compass_components_1.cx)(editorStyle, !showScroll && hiddenScrollStyle), style: {
663
+ position: 'absolute',
664
+ width: '100%',
665
+ height: '100%',
666
+ }, "data-codemirror": "true", ...props })));
667
+ });
668
+ exports.BaseEditor = BaseEditor;
427
669
  function isTopNode(node) {
428
670
  return !node
429
671
  ? true
@@ -433,6 +675,19 @@ function isTopNode(node) {
433
675
  ? true
434
676
  : isTopNode(node.parent);
435
677
  }
678
+ const applySnippet = (editor, template) => {
679
+ if (isReadOnly(editor.state)) {
680
+ return false;
681
+ }
682
+ const completion = (0, autocomplete_1.snippetCompletion)(template, { label: template });
683
+ if (typeof completion.apply === 'function') {
684
+ completion.apply(editor, completion, 0, editor.state.doc.length);
685
+ return true;
686
+ }
687
+ else {
688
+ return false;
689
+ }
690
+ };
436
691
  const foldAll = (editor) => {
437
692
  const foldableProperties = [];
438
693
  (0, language_1.syntaxTree)(editor.state).iterate({
@@ -452,7 +707,7 @@ const foldAll = (editor) => {
452
707
  },
453
708
  });
454
709
  if (foldableProperties.length > 0) {
455
- editor.dispatch({
710
+ void sheduleDispatch(editor, {
456
711
  effects: foldableProperties.map((range) => {
457
712
  return language_1.foldEffect.of(range);
458
713
  }),
@@ -465,20 +720,21 @@ const copyAll = (editorView) => {
465
720
  return true;
466
721
  };
467
722
  const prettify = (editorView) => {
468
- if (editorView.state.facet(state_1.EditorState.readOnly)) {
723
+ if (isReadOnly(editorView.state)) {
469
724
  return false;
470
725
  }
471
- const language = editorView.state.facet(languageName)[0];
726
+ const language = editorView.state.facet(exports.languageName)[0];
472
727
  const doc = editorView.state.sliceDoc();
473
728
  try {
474
- const formatted = (0, prettify_1.prettify)(doc, language === 'json' ? 'json' : 'javascript-expression');
729
+ const formatted = (0, prettify_1.prettify)(doc, language);
475
730
  if (formatted !== doc) {
476
- editorView.dispatch({
731
+ void sheduleDispatch(editorView, {
477
732
  changes: {
478
733
  from: 0,
479
734
  to: editorView.state.doc.length,
480
735
  insert: formatted,
481
736
  },
737
+ scrollIntoView: true,
482
738
  });
483
739
  return true;
484
740
  }
@@ -487,8 +743,116 @@ const prettify = (editorView) => {
487
743
  }
488
744
  return false;
489
745
  };
490
- BaseEditor.foldAll = foldAll;
491
- BaseEditor.unfoldAll = language_1.unfoldAll;
492
- BaseEditor.copyAll = copyAll;
493
- BaseEditor.prettify = prettify;
746
+ const inlineStyles = (0, compass_components_1.css)({
747
+ '& .cm-editor': {
748
+ backgroundColor: 'transparent',
749
+ },
750
+ });
751
+ const InlineEditor = react_1.default.forwardRef(function InlineEditor({ className, ...props }, forwardRef) {
752
+ return (react_1.default.createElement(BaseEditor, { ref: forwardRef, maxLines: 10, showFoldGutter: false, showLineNumbers: false, showAnnotationsGutter: false, showScroll: false, highlightActiveLine: false, className: (0, compass_components_1.cx)(inlineStyles, className), language: "javascript-expression", ...props }));
753
+ });
754
+ exports.CodemirrorInlineEditor = InlineEditor;
755
+ const multilineEditorContainerStyle = (0, compass_components_1.css)({
756
+ position: 'relative',
757
+ height: '100%',
758
+ backgroundColor: exports.editorPalette.light.backgroundColor,
759
+ [`&:focus-within > .multiline-editor-actions,
760
+ &:hover > .multiline-editor-actions`]: {
761
+ display: 'flex',
762
+ },
763
+ });
764
+ const multilineEditorContainerDarkModeStyle = (0, compass_components_1.css)({
765
+ backgroundColor: exports.editorPalette.dark.backgroundColor,
766
+ });
767
+ const actionsContainerStyle = (0, compass_components_1.css)({
768
+ position: 'absolute',
769
+ top: compass_components_1.spacing[1],
770
+ right: compass_components_1.spacing[2],
771
+ display: 'none',
772
+ gap: compass_components_1.spacing[2],
773
+ });
774
+ const MultilineEditor = react_1.default.forwardRef(function MultilineEditor({ customActions, copyable = true, formattable = true, className, editorClassName, actionsClassName, darkMode: _darkMode, ...props }, ref) {
775
+ const darkMode = (0, compass_components_1.useDarkMode)(_darkMode);
776
+ const containerRef = (0, react_1.useRef)(null);
777
+ const editorRef = (0, react_1.useRef)(null);
778
+ (0, react_1.useImperativeHandle)(ref, () => {
779
+ return {
780
+ foldAll() {
781
+ var _a, _b;
782
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.foldAll()) !== null && _b !== void 0 ? _b : false;
783
+ },
784
+ unfoldAll() {
785
+ var _a, _b;
786
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.unfoldAll()) !== null && _b !== void 0 ? _b : false;
787
+ },
788
+ copyAll() {
789
+ var _a, _b;
790
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.copyAll()) !== null && _b !== void 0 ? _b : false;
791
+ },
792
+ prettify() {
793
+ var _a, _b;
794
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.prettify()) !== null && _b !== void 0 ? _b : false;
795
+ },
796
+ focus() {
797
+ var _a, _b;
798
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.focus()) !== null && _b !== void 0 ? _b : false;
799
+ },
800
+ applySnippet(template) {
801
+ var _a, _b;
802
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.applySnippet(template)) !== null && _b !== void 0 ? _b : false;
803
+ },
804
+ get editor() {
805
+ var _a, _b;
806
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.editor) !== null && _b !== void 0 ? _b : null;
807
+ },
808
+ };
809
+ }, []);
810
+ const actions = (0, react_1.useMemo)(() => {
811
+ return [
812
+ copyable && (react_1.default.createElement(actions_1.ActionButton, { key: "Copy", label: "Copy", icon: "Copy", onClick: () => {
813
+ var _a, _b;
814
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.copyAll()) !== null && _b !== void 0 ? _b : false;
815
+ } })),
816
+ formattable && (react_1.default.createElement(actions_1.ActionButton, { key: "Format", label: "Format", icon: react_1.default.createElement(actions_1.FormatIcon, { size: 14, role: "presentation" }), onClick: () => {
817
+ var _a, _b;
818
+ return (_b = (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.prettify()) !== null && _b !== void 0 ? _b : false;
819
+ } })),
820
+ ...(customActions !== null && customActions !== void 0 ? customActions : []).map((action) => {
821
+ return (react_1.default.createElement(actions_1.ActionButton, { key: action.label, icon: action.icon, label: action.label, onClick: () => {
822
+ var _a;
823
+ if (!((_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.editor)) {
824
+ return false;
825
+ }
826
+ return action.action(editorRef.current.editor);
827
+ } }));
828
+ }),
829
+ ];
830
+ }, [copyable, formattable, customActions]);
831
+ return (react_1.default.createElement("div", { ref: containerRef, className: (0, compass_components_1.cx)(multilineEditorContainerStyle, darkMode && multilineEditorContainerDarkModeStyle, className), onClick: (evt) => {
832
+ var _a;
833
+ if (evt.target === containerRef.current) {
834
+ (_a = editorRef.current) === null || _a === void 0 ? void 0 : _a.focus();
835
+ }
836
+ } },
837
+ react_1.default.createElement(BaseEditor, { ref: editorRef, className: editorClassName, language: "javascript-expression", minLines: 10, ...props }),
838
+ actions.length > 0 && (react_1.default.createElement("div", { className: (0, compass_components_1.cx)('multiline-editor-actions', actionsContainerStyle, actionsClassName) }, actions))));
839
+ });
840
+ exports.CodemirrorMultilineEditor = MultilineEditor;
841
+ async function setCodemirrorEditorValue(element, text) {
842
+ if (typeof element === 'string') {
843
+ element = document.querySelector(`[data-testid="${element}"]`);
844
+ }
845
+ if (!element || !element.hasAttribute('data-codemirror')) {
846
+ throw new Error('Cannot find editor container');
847
+ }
848
+ const editorView = element._cm;
849
+ await sheduleDispatch(editorView, {
850
+ changes: {
851
+ from: 0,
852
+ to: editorView.state.doc.length,
853
+ insert: text,
854
+ },
855
+ });
856
+ }
857
+ exports.setCodemirrorEditorValue = setCodemirrorEditorValue;
494
858
  //# sourceMappingURL=json-editor.js.map