@neo4j-cypher/react-codemirror 2.0.0-alpha.0 → 2.0.0-next.1

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 (59) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE.md +201 -0
  3. package/README.md +24 -4
  4. package/dist/{index.cjs → cjs/index.cjs} +187 -74
  5. package/dist/cjs/index.cjs.map +7 -0
  6. package/{esm → dist/esm}/index.mjs +196 -66
  7. package/dist/esm/index.mjs.map +7 -0
  8. package/dist/types/CypherEditor.d.ts +118 -0
  9. package/dist/types/e2e_tests/auto-completion.spec.d.ts +1 -0
  10. package/dist/types/e2e_tests/e2e-utils.d.ts +12 -0
  11. package/dist/types/e2e_tests/extra-keybindings.spec.d.ts +1 -0
  12. package/dist/types/e2e_tests/history-navigation.spec.d.ts +1 -0
  13. package/dist/types/e2e_tests/mock-data.d.ts +3779 -0
  14. package/dist/types/e2e_tests/performance-test.spec.d.ts +1 -0
  15. package/dist/types/e2e_tests/sanity-checks.spec.d.ts +1 -0
  16. package/dist/types/e2e_tests/syntax-highlighting.spec.d.ts +1 -0
  17. package/dist/types/e2e_tests/syntax-validation.spec.d.ts +1 -0
  18. package/dist/types/icons.d.ts +2 -0
  19. package/dist/types/index.d.ts +4 -0
  20. package/dist/types/lang-cypher/ParserAdapter.d.ts +14 -0
  21. package/dist/types/lang-cypher/autocomplete.d.ts +3 -0
  22. package/dist/types/lang-cypher/constants.d.ts +31 -0
  23. package/dist/types/lang-cypher/contants.test.d.ts +1 -0
  24. package/dist/types/lang-cypher/create-cypher-theme.d.ts +26 -0
  25. package/dist/types/lang-cypher/lang-cypher.d.ts +7 -0
  26. package/dist/types/lang-cypher/syntax-validation.d.ts +3 -0
  27. package/dist/types/lang-cypher/theme-icons.d.ts +7 -0
  28. package/dist/types/ndl-tokens-copy.d.ts +379 -0
  29. package/dist/types/ndl-tokens-copy.test.d.ts +1 -0
  30. package/dist/types/neo4j-setup.d.ts +2 -0
  31. package/dist/types/repl-mode.d.ts +8 -0
  32. package/dist/types/themes.d.ts +11 -0
  33. package/dist/types/tsconfig.tsbuildinfo +1 -0
  34. package/package.json +48 -15
  35. package/src/CypherEditor.tsx +316 -0
  36. package/src/e2e_tests/auto-completion.spec.tsx +232 -0
  37. package/src/e2e_tests/e2e-utils.ts +75 -0
  38. package/src/e2e_tests/extra-keybindings.spec.tsx +57 -0
  39. package/src/e2e_tests/history-navigation.spec.tsx +144 -0
  40. package/src/e2e_tests/mock-data.ts +4310 -0
  41. package/src/e2e_tests/performance-test.spec.tsx +71 -0
  42. package/src/e2e_tests/sanity-checks.spec.tsx +87 -0
  43. package/src/e2e_tests/syntax-highlighting.spec.tsx +198 -0
  44. package/src/e2e_tests/syntax-validation.spec.tsx +157 -0
  45. package/src/icons.ts +87 -0
  46. package/src/index.ts +4 -0
  47. package/src/lang-cypher/ParserAdapter.ts +92 -0
  48. package/src/lang-cypher/autocomplete.ts +65 -0
  49. package/src/lang-cypher/constants.ts +61 -0
  50. package/src/lang-cypher/contants.test.ts +104 -0
  51. package/src/lang-cypher/create-cypher-theme.ts +207 -0
  52. package/src/lang-cypher/lang-cypher.ts +32 -0
  53. package/src/lang-cypher/syntax-validation.ts +24 -0
  54. package/src/lang-cypher/theme-icons.ts +27 -0
  55. package/src/ndl-tokens-copy.test.ts +11 -0
  56. package/src/ndl-tokens-copy.ts +379 -0
  57. package/src/neo4j-setup.tsx +129 -0
  58. package/src/repl-mode.ts +214 -0
  59. package/src/themes.ts +130 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # @neo4j-cypher/react-codemirror
2
+
3
+ ## 2.0.0-next.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix incorrect exports
8
+
9
+ ## 2.0.0-next.0
10
+
11
+ ### Major Changes
12
+
13
+ - 5819f6385: First alpha release of the new Neo4j's Cypher Language Support, including syntax highlighting, auto-completion and linting as features
package/LICENSE.md ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ https://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2023 Neo4j Inc.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ https://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -2,10 +2,30 @@
2
2
 
3
3
  This package can be built with `npm run build` and then published to npm with `npm publish`.
4
4
 
5
- ### Completion Icons
5
+ ## Usage
6
6
 
7
- We use unmodified copies of Visual Studio Code Icons from microsofts repository [here](https://github.com/microsoft/vscode-icons) licensed under creative commons.
7
+ `npm install @neo4j-cypher/reactcodemirror`
8
+
9
+ ```tsx
10
+ import { useState } from 'react';
11
+
12
+ import { CypherEditor } from '@neo4j-cypher/react-codemirror@next';
13
+
14
+ export function CodeEditor() {
15
+ const [value, setValue] = useState('');
16
+ // can be used to access underlying codemirror state or call for example `focus`
17
+ const editorRef = useRef<CypherEditor>(null);
18
+
19
+ return <CypherEditor value={value} onChange={setValue} ref={editorRef} />;
20
+ }
21
+ ```
8
22
 
9
- ### Build notes
23
+ For a full example, see the [react-codemirror-playground](https://github.com/neo4j/cypher-language-support/tree/main/packages/react-codemirror-playground).
10
24
 
11
- We need to bundle the other project dependencies into the build until we've published those packages. When we do publish them, we can just do the same "tsc" build step as for example `language-support`.
25
+ ## Usage without react
26
+
27
+ Currently we only support using the codemirror editor via the react wrapper, but we plan to release the codemirror extensions separately as well.
28
+
29
+ ### Completion Icons
30
+
31
+ We use unmodified copies of Visual Studio Code Icons from microsofts repository [here](https://github.com/microsoft/vscode-icons) licensed under creative commons.
@@ -1,8 +1,6 @@
1
- var __create = Object.create;
2
1
  var __defProp = Object.defineProperty;
3
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf;
6
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
5
  var __export = (target, all) => {
8
6
  for (var name in all)
@@ -16,14 +14,6 @@ var __copyProps = (to, from, except, desc) => {
16
14
  }
17
15
  return to;
18
16
  };
19
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
- // If the importer is in node compatibility mode or this is not an ESM
21
- // file that has been converted to a CommonJS file using a Babel-
22
- // compatible transform (i.e. "__esModule" has not been set), then set
23
- // "default" to the CommonJS "module.exports" for node compatibility.
24
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
- mod
26
- ));
27
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
18
 
29
19
  // src/index.ts
@@ -31,7 +21,6 @@ var src_exports = {};
31
21
  __export(src_exports, {
32
22
  CypherEditor: () => CypherEditor,
33
23
  CypherParser: () => import_language_support4.CypherParser,
34
- ReactCodemirror: () => ReactCodemirror,
35
24
  cypher: () => cypher,
36
25
  darkThemeConstants: () => darkThemeConstants,
37
26
  lightThemeConstants: () => lightThemeConstants,
@@ -39,12 +28,11 @@ __export(src_exports, {
39
28
  });
40
29
  module.exports = __toCommonJS(src_exports);
41
30
  var import_language_support4 = require("@neo4j-cypher/language-support");
42
- var ReactCodemirror = __toESM(require("@uiw/react-codemirror"));
43
31
 
44
32
  // src/CypherEditor.tsx
33
+ var import_state4 = require("@codemirror/state");
45
34
  var import_view4 = require("@codemirror/view");
46
- var import_react_codemirror = __toESM(require("@uiw/react-codemirror"));
47
- var import_react = __toESM(require("react"));
35
+ var import_react = require("react");
48
36
 
49
37
  // src/lang-cypher/lang-cypher.ts
50
38
  var import_language2 = require("@codemirror/language");
@@ -82,8 +70,7 @@ var completionKindToCodemirrorIcon = (c) => {
82
70
  };
83
71
  return map[c];
84
72
  };
85
- var emptySchema = {};
86
- var cypherAutocomplete = (schema) => (context) => {
73
+ var cypherAutocomplete = (config) => (context) => {
87
74
  const textUntilCursor = context.state.doc.toString().slice(0, context.pos);
88
75
  const triggerCharacters = [".", ":", "{", "$"];
89
76
  const lastCharacter = textUntilCursor.slice(-1);
@@ -93,7 +80,7 @@ var cypherAutocomplete = (schema) => (context) => {
93
80
  if (!shouldTriggerCompletion) {
94
81
  return null;
95
82
  }
96
- const options = (0, import_language_support.autocomplete)(textUntilCursor, schema ?? emptySchema);
83
+ const options = (0, import_language_support.autocomplete)(textUntilCursor, config.schema ?? {});
97
84
  return {
98
85
  from: context.matchBefore(/(\w|\$)*$/).from,
99
86
  options: options.map((o) => ({
@@ -235,17 +222,18 @@ var ParserAdapter = class extends import_common2.Parser {
235
222
  var import_lint = require("@codemirror/lint");
236
223
  var import_language_support3 = require("@neo4j-cypher/language-support");
237
224
  var import_vscode_languageserver_types2 = require("vscode-languageserver-types");
238
- var cypherLinter = (schema) => (0, import_lint.linter)((view) => {
239
- const diagnostics = [];
240
- (0, import_language_support3.validateSyntax)(view.state.doc.toString(), schema).forEach((diagnostic) => {
241
- diagnostics.push({
225
+ var cypherLinter = (config) => (0, import_lint.linter)((view) => {
226
+ if (!config.lint) {
227
+ return [];
228
+ }
229
+ return (0, import_language_support3.validateSyntax)(view.state.doc.toString(), config.schema).map(
230
+ (diagnostic) => ({
242
231
  from: diagnostic.offsets.start,
243
232
  to: diagnostic.offsets.end,
244
233
  severity: diagnostic.severity === import_vscode_languageserver_types2.DiagnosticSeverity.Error ? "error" : "warning",
245
234
  message: diagnostic.message
246
- });
247
- });
248
- return diagnostics;
235
+ })
236
+ );
249
237
  });
250
238
 
251
239
  // src/lang-cypher/lang-cypher.ts
@@ -255,12 +243,12 @@ var facet = (0, import_language2.defineLanguageFacet)({
255
243
  });
256
244
  var parserAdapter = new ParserAdapter(facet);
257
245
  var cypherLanguage = new import_language2.Language(facet, parserAdapter, [], "cypher");
258
- function cypher({ lint, schema = {} }) {
246
+ function cypher(config) {
259
247
  return new import_language2.LanguageSupport(cypherLanguage, [
260
248
  cypherLanguage.data.of({
261
- autocomplete: cypherAutocomplete(schema)
249
+ autocomplete: cypherAutocomplete(config)
262
250
  }),
263
- ...lint ? [cypherLinter(schema)] : []
251
+ cypherLinter(config)
264
252
  ]);
265
253
  }
266
254
 
@@ -352,7 +340,7 @@ var insertTab = (cmd) => {
352
340
  }
353
341
  return true;
354
342
  };
355
- var basicNeo4jSetup = (prompt) => {
343
+ var basicNeo4jSetup = () => {
356
344
  const keymaps = [
357
345
  import_autocomplete2.closeBracketsKeymap,
358
346
  import_commands.defaultKeymap,
@@ -378,16 +366,6 @@ var basicNeo4jSetup = (prompt) => {
378
366
  }
379
367
  ].flat();
380
368
  const extensions = [];
381
- extensions.push(
382
- (0, import_view.lineNumbers)({
383
- formatNumber(a, state) {
384
- if (state.doc.lines === 1 && prompt !== void 0) {
385
- return prompt;
386
- }
387
- return a.toString();
388
- }
389
- })
390
- );
391
369
  extensions.push((0, import_view.highlightSpecialChars)());
392
370
  extensions.push((0, import_commands.history)());
393
371
  extensions.push((0, import_view.drawSelection)());
@@ -402,6 +380,7 @@ var basicNeo4jSetup = (prompt) => {
402
380
  extensions.push(
403
381
  (0, import_autocomplete2.autocompletion)({
404
382
  icons: false,
383
+ interactionDelay: 5,
405
384
  addToOptions: [
406
385
  {
407
386
  render(completion, state) {
@@ -544,8 +523,7 @@ var replMode = ({
544
523
  onExecute?.(doc);
545
524
  onNewHistoryEntry?.(doc);
546
525
  view.dispatch({
547
- effects: pushToHistory.of(doc),
548
- changes: { from: 0, to: view.state.doc.length, insert: "" }
526
+ effects: pushToHistory.of(doc)
549
527
  });
550
528
  }
551
529
  return true;
@@ -1289,42 +1267,177 @@ function getThemeExtension(theme, inheritBgColor) {
1289
1267
 
1290
1268
  // src/CypherEditor.tsx
1291
1269
  var import_jsx_runtime = require("react/jsx-runtime");
1292
- var CypherEditor = import_react.default.forwardRef((props, ref) => {
1293
- const {
1294
- theme = "light",
1295
- extensions = [],
1296
- prompt,
1297
- onExecute,
1298
- initialHistory = [],
1299
- onNewHistoryEntry,
1300
- extraKeybindings = [],
1301
- lineWrap = false,
1302
- overrideThemeBackgroundColor = false,
1303
- schema = {},
1304
- lint = true,
1305
- ...rest
1306
- } = props;
1307
- const maybeReplMode = onExecute ? replMode({
1308
- onExecute,
1309
- initialHistory,
1310
- onNewHistoryEntry
1311
- }) : [];
1312
- return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1313
- import_react_codemirror.default,
1314
- {
1315
- ref,
1316
- theme: getThemeExtension(theme, overrideThemeBackgroundColor),
1270
+ var themeCompartment = new import_state4.Compartment();
1271
+ var keyBindingCompartment = new import_state4.Compartment();
1272
+ var ExternalEdit = import_state4.Annotation.define();
1273
+ var CypherEditor = class extends import_react.Component {
1274
+ /**
1275
+ * The codemirror editor container.
1276
+ */
1277
+ editorContainer = (0, import_react.createRef)();
1278
+ /**
1279
+ * The codemirror editor state.
1280
+ */
1281
+ editorState = (0, import_react.createRef)();
1282
+ /**
1283
+ * The codemirror editor view.
1284
+ */
1285
+ editorView = (0, import_react.createRef)();
1286
+ schemaRef = (0, import_react.createRef)();
1287
+ /**
1288
+ * Focus the editor
1289
+ */
1290
+ focus() {
1291
+ this.editorView.current?.focus();
1292
+ }
1293
+ /**
1294
+ * Move the cursor to the supplied position.
1295
+ * For example, to move the cursor to the end of the editor, use `value.length`
1296
+ */
1297
+ updateCursorPosition(position) {
1298
+ this.editorView.current?.dispatch({
1299
+ selection: { anchor: position, head: position }
1300
+ });
1301
+ }
1302
+ /**
1303
+ * Externally set the editor value and focus the editor.
1304
+ */
1305
+ setValueAndFocus(value = "") {
1306
+ const currentCmValue = this.editorView.current.state?.doc.toString() ?? "";
1307
+ this.editorView.current.dispatch({
1308
+ changes: {
1309
+ from: 0,
1310
+ to: currentCmValue.length,
1311
+ insert: value
1312
+ },
1313
+ selection: { anchor: value.length, head: value.length }
1314
+ });
1315
+ this.editorView.current?.focus();
1316
+ }
1317
+ static defaultProps = {
1318
+ lint: true,
1319
+ schema: {},
1320
+ overrideThemeBackgroundColor: false,
1321
+ lineWrap: false,
1322
+ extraKeybindings: [],
1323
+ initialHistory: [],
1324
+ theme: "light"
1325
+ };
1326
+ componentDidMount() {
1327
+ const {
1328
+ theme,
1329
+ onExecute,
1330
+ initialHistory,
1331
+ onNewHistoryEntry,
1332
+ extraKeybindings,
1333
+ lineWrap,
1334
+ overrideThemeBackgroundColor,
1335
+ schema,
1336
+ lint,
1337
+ onChange
1338
+ } = this.props;
1339
+ this.schemaRef.current = { schema, lint };
1340
+ const maybeReplMode = onExecute ? replMode({
1341
+ onExecute,
1342
+ initialHistory,
1343
+ onNewHistoryEntry
1344
+ }) : [];
1345
+ const themeExtension = getThemeExtension(
1346
+ theme,
1347
+ overrideThemeBackgroundColor
1348
+ );
1349
+ const changeListener = onChange ? [
1350
+ import_view4.EditorView.updateListener.of((upt) => {
1351
+ const wasUserEdit = !upt.transactions.some(
1352
+ (tr) => tr.annotation(ExternalEdit)
1353
+ );
1354
+ if (upt.docChanged && wasUserEdit) {
1355
+ const doc = upt.state.doc;
1356
+ const value = doc.toString();
1357
+ onChange(value, upt);
1358
+ }
1359
+ })
1360
+ ] : [];
1361
+ this.editorState.current = import_state4.EditorState.create({
1317
1362
  extensions: [
1318
- cypher({ lint, schema }),
1319
- import_view4.keymap.of(extraKeybindings),
1320
1363
  maybeReplMode,
1321
- basicNeo4jSetup(prompt),
1364
+ basicNeo4jSetup(),
1365
+ themeCompartment.of(themeExtension),
1366
+ changeListener,
1367
+ cypher(this.schemaRef.current),
1368
+ keyBindingCompartment.of(import_view4.keymap.of(extraKeybindings)),
1322
1369
  lineWrap ? import_view4.EditorView.lineWrapping : [],
1323
- ...extensions
1370
+ (0, import_view4.lineNumbers)({
1371
+ formatNumber: (a, state) => {
1372
+ if (state.doc.lines === 1 && this.props.prompt !== void 0) {
1373
+ return this.props.prompt;
1374
+ }
1375
+ return a.toString();
1376
+ }
1377
+ })
1324
1378
  ],
1325
- basicSetup: false,
1326
- indentWithTab: false,
1327
- ...rest
1379
+ doc: this.props.value
1380
+ });
1381
+ this.editorView.current = new import_view4.EditorView({
1382
+ state: this.editorState.current,
1383
+ parent: this.editorContainer.current
1384
+ });
1385
+ if (this.props.autofocus) {
1386
+ this.focus();
1387
+ if (this.props.value) {
1388
+ this.updateCursorPosition(this.props.value.length);
1389
+ }
1328
1390
  }
1329
- );
1330
- });
1391
+ }
1392
+ componentDidUpdate(prevProps) {
1393
+ if (!this.editorView.current) {
1394
+ return;
1395
+ }
1396
+ const currentCmValue = this.editorView.current.state?.doc.toString() ?? "";
1397
+ if (this.props.value !== void 0 && currentCmValue !== this.props.value) {
1398
+ this.editorView.current.dispatch({
1399
+ changes: {
1400
+ from: 0,
1401
+ to: currentCmValue.length,
1402
+ insert: this.props.value ?? ""
1403
+ },
1404
+ annotations: [ExternalEdit.of(true)]
1405
+ });
1406
+ }
1407
+ const didChangeTheme = prevProps.theme !== this.props.theme || prevProps.overrideThemeBackgroundColor !== this.props.overrideThemeBackgroundColor;
1408
+ if (didChangeTheme) {
1409
+ this.editorView.current.dispatch({
1410
+ effects: themeCompartment.reconfigure(
1411
+ getThemeExtension(
1412
+ this.props.theme,
1413
+ this.props.overrideThemeBackgroundColor
1414
+ )
1415
+ )
1416
+ });
1417
+ }
1418
+ if (prevProps.extraKeybindings !== this.props.extraKeybindings) {
1419
+ this.editorView.current.dispatch({
1420
+ effects: keyBindingCompartment.reconfigure(
1421
+ import_view4.keymap.of(this.props.extraKeybindings)
1422
+ )
1423
+ });
1424
+ }
1425
+ this.schemaRef.current.schema = this.props.schema;
1426
+ this.schemaRef.current.lint = this.props.lint;
1427
+ }
1428
+ componentWillUnmount() {
1429
+ this.editorView.current?.destroy();
1430
+ }
1431
+ render() {
1432
+ const { className, theme } = this.props;
1433
+ const themeClass = typeof theme === "string" ? `cm-theme-${theme}` : "cm-theme";
1434
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
1435
+ "div",
1436
+ {
1437
+ ref: this.editorContainer,
1438
+ className: `${themeClass}${className ? ` ${className}` : ""}`
1439
+ }
1440
+ );
1441
+ }
1442
+ };
1443
+ //# sourceMappingURL=index.cjs.map