@react-fabric/monaco 1.0.0-beta.5 → 1.0.0-beta.52

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.
@@ -23,70 +23,74 @@
23
23
 
24
24
  /* stylelint-disable custom-property-pattern */
25
25
 
26
- @layer util {
27
- .codeEditor {
26
+ @layer utilities {
27
+ .fabric-codeEditor {
28
28
  direction: ltr;
29
29
  overflow: hidden;
30
30
  border: 1px solid transparent;
31
31
  border-bottom-width: 2px;
32
32
 
33
- &:has(:global(.monaco-editor.focused)) {
34
- border-color: theme(borderColor.primary.500);
33
+ &:has(.monaco-editor.focused) {
34
+ border-color: var(--border-primary-500);
35
35
  }
36
36
 
37
- .editor {
38
- & > :global(.monaco-editor) {
39
- --vscode-menu-background: theme(colors.tint.50) !important;
40
- --vscode-menu-border: theme(borderColor.base) !important;
37
+ .fabric-editor {
38
+ & > .monaco-editor {
39
+ --vscode-menu-background: var(--color-tint.50) !important;
40
+ --vscode-menu-border: var(--border-color-base) !important;
41
41
  --vscode-focusBorder: transparent !important;
42
42
  --vscode-menu-selectionBackground: color(
43
- from var(--primary) srgb r g b / 80%
43
+ from var(--color-primary) srgb r g b / 80%
44
44
  ) !important;
45
45
  --vscode-editor-background: var(--fabric-bg) !important;
46
46
  --vscode-editor-foreground: var(--fabric-text) !important;
47
47
  --vscode-breadcrumb-background: var(--fabric-bg) !important;
48
48
  --vscode-editorGutter-background: color(
49
- from theme(colors.tint.50) srgb r g g / 50%
49
+ from var(--color-tint.50) srgb r g g / 50%
50
50
  ) !important;
51
51
  --vscode-minimapSlider-background: color(
52
- from var(--tint) srgb r g b / 20%
52
+ from var(--color-tint) srgb r g b / 20%
53
53
  ) !important;
54
54
  --vscode-minimapSlider-hoverBackground: color(
55
- from var(--tint) srgb r g b / 25%
55
+ from var(--color-tint) srgb r g b / 25%
56
56
  ) !important;
57
57
  --vscode-minimapSlider-activeBackground: color(
58
- from var(--tint) srgb r g b / 30%
58
+ from var(--color-tint) srgb r g b / 30%
59
59
  ) !important;
60
- --vscode-input-border: var(--primary) !important;
60
+ --vscode-input-border: var(--color-primary) !important;
61
61
  --vscode-editorStickyScroll-background: var(--fabric-bg) !important;
62
62
  --vscode-editorMarkerNavigation-background: var(--fabric-bg) !important;
63
63
  --vscode-editor-wordHighlightTextBackground: color(
64
- from theme(colors.accent.700) srgb r g b / 20%
64
+ from var(--color-accent.700) srgb r g b / 20%
65
65
  ) !important;
66
66
  }
67
67
 
68
- & :global(.minimap-decorations-layer) {
68
+ & .minimap-decorations-layer {
69
69
  background-color: color(
70
- from theme(colors.tint.50) srgb r g g / 50%
70
+ from var(--color-tint.50) srgb r g g / 50%
71
71
  ) !important;
72
72
  }
73
73
 
74
- & :global(.folded-background) {
74
+ & .folded-background {
75
75
  opacity: 0.5;
76
76
  }
77
77
 
78
- & :global(.monaco-findInput) textarea {
78
+ & .monaco-findInput textarea {
79
79
  resize: initial;
80
80
  min-height: unset;
81
81
  }
82
82
 
83
+ & .editor-widget.suggest-widget .message {
84
+ font-size: 0.75rem;
85
+ }
86
+
83
87
  & span {
84
88
  padding: 0;
85
89
  }
86
90
  }
87
91
  }
88
92
 
89
- .editorTools {
90
- @apply bg-base;
93
+ .fabric-editorTools {
94
+ background-color: var(--bg-color-base);
91
95
  }
92
96
  }
package/css/styles.css ADDED
@@ -0,0 +1,24 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2025 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ @import "./_codeEditor.css";
@@ -9,15 +9,42 @@ var utilities = require('@react-fabric/utilities');
9
9
  var classNames = require('classnames');
10
10
  var react = require('react');
11
11
  var MonacoEditor = require('react-monaco-editor');
12
- var CodeEditor_module = require('./CodeEditor.module.css');
13
12
  var jsxRuntime = require('react/jsx-runtime');
14
13
 
15
14
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
16
15
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
+ /**
17
+ * CodeEditor component to render a code editor using Monaco Editor.
18
+ * It supports JSON and HTML languages with optional schema validation and handlebar suggestions.
19
+ * The editor can be configured to be read-only, minimal, and can handle changes in the code.
20
+ * It provides a ref to access the editor's value and validation methods.
21
+ *
22
+ * @param {EditorProps} props - The properties for the CodeEditor component.
23
+ * @param {RefProp<MonacoEditorRef>} ref - The ref to access the editor methods.
24
+ * @return {JSX.Element} The rendered CodeEditor component.
25
+ *
26
+ * @example
27
+ * ```jsx
28
+ * <CodeEditor
29
+ * value={initialValue}
30
+ * schema={[{ uri: "http://example.com/schema.json", schema: {} }]}
31
+ * handlebarSuggestions={[{ text: "example", description: "An example suggestion" }]}
32
+ * required={true}
33
+ * readOnly={false}
34
+ * minimal={false}
35
+ * onChange={(value) => console.log("Code changed:", value)}
36
+ * language="json"
37
+ * />
38
+ * ```
39
+ *
40
+ * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/story/monaco-playground--playground}
41
+ * @see {@link https://microsoft.github.io/monaco-editor/}
42
+ */
17
43
  var CodeEditor = function CodeEditor(_ref) {
18
44
  var ref = _ref.ref,
19
45
  value = _ref.value,
20
46
  schema = _ref.schema,
47
+ handlebarSuggestions = _ref.handlebarSuggestions,
21
48
  required = _ref.required,
22
49
  readOnly = _ref.readOnly,
23
50
  minimal = _ref.minimal,
@@ -90,13 +117,15 @@ var CodeEditor = function CodeEditor(_ref) {
90
117
  }
91
118
  };
92
119
  }, [required, language]);
120
+ var disposeRef = react.useRef(null);
93
121
  var schemaUri = react.useCallback(function (monaco) {
94
- if (schema) {
122
+ if (language === "json" && schema) {
123
+ var _monaco$languages;
95
124
  var _schema = _toArray(schema),
96
125
  main = _schema[0],
97
126
  rest = _schema.slice(1);
98
127
  // configure the JSON language support with schemas and schema associations
99
- monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
128
+ (_monaco$languages = monaco.languages) === null || _monaco$languages === void 0 || (_monaco$languages = _monaco$languages.json) === null || _monaco$languages === void 0 || _monaco$languages.jsonDefaults.setDiagnosticsOptions({
100
129
  validate: true,
101
130
  allowComments: false,
102
131
  schemas: [_objectSpread({
@@ -104,7 +133,47 @@ var CodeEditor = function CodeEditor(_ref) {
104
133
  }, main)].concat(_toConsumableArray(rest))
105
134
  });
106
135
  }
107
- }, [schema]);
136
+ if (language === "html" && handlebarSuggestions) {
137
+ var _disposeRef$current, _disposeRef$current$d;
138
+ (_disposeRef$current = disposeRef.current) === null || _disposeRef$current === void 0 || (_disposeRef$current$d = _disposeRef$current.dispose) === null || _disposeRef$current$d === void 0 || _disposeRef$current$d.call(_disposeRef$current);
139
+ disposeRef.current = monaco.languages.registerCompletionItemProvider("html", {
140
+ triggerCharacters: ["{{"],
141
+ provideCompletionItems: function provideCompletionItems(model, position) {
142
+ // find out if we are completing a property in the 'dependencies' object.
143
+ var textUntilPosition = model.getValueInRange({
144
+ startLineNumber: 1,
145
+ startColumn: 1,
146
+ endLineNumber: position.lineNumber,
147
+ endColumn: position.column
148
+ });
149
+ var match = textUntilPosition.match(/\{\{(.*)([^}])?$/);
150
+ if (!match) {
151
+ return {
152
+ suggestions: []
153
+ };
154
+ }
155
+ var word = model.getWordUntilPosition(position);
156
+ var range = {
157
+ startLineNumber: position.lineNumber,
158
+ endLineNumber: position.lineNumber,
159
+ startColumn: word.startColumn,
160
+ endColumn: word.endColumn
161
+ };
162
+ return {
163
+ suggestions: handlebarSuggestions.map(function (hs) {
164
+ return {
165
+ label: hs.text,
166
+ kind: monaco.languages.CompletionItemKind.Text,
167
+ documentation: hs.description,
168
+ insertText: hs.text,
169
+ range: range
170
+ };
171
+ })
172
+ };
173
+ }
174
+ });
175
+ }
176
+ }, [schema, language]);
108
177
  var resetSchema = react.useCallback(function (_, monaco) {
109
178
  monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
110
179
  validate: true,
@@ -113,9 +182,11 @@ var CodeEditor = function CodeEditor(_ref) {
113
182
  });
114
183
  }, []);
115
184
  return /*#__PURE__*/jsxRuntime.jsx(core.Section, {
185
+ className: "relative",
116
186
  children: /*#__PURE__*/jsxRuntime.jsx("div", {
117
187
  ref: containerRef,
118
- className: classNames(CodeEditor_module.codeEditor, "area-content"),
188
+ "data-ref": "codeEditor",
189
+ className: classNames("fabric-codeEditor", "absolute inset-0"),
119
190
  children: /*#__PURE__*/jsxRuntime.jsx(MonacoEditor, {
120
191
  editorDidMount: function editorDidMount(e, m) {
121
192
  return [!!e && setEditorRef(e), !!m && setMonacoRef(m)];
@@ -126,7 +197,7 @@ var CodeEditor = function CodeEditor(_ref) {
126
197
  language: language,
127
198
  onChange: handleChange,
128
199
  theme: theme,
129
- className: CodeEditor_module.editor,
200
+ className: "fabric-editor",
130
201
  options: {
131
202
  readOnly: readOnly,
132
203
  minimap: {
@@ -7,17 +7,44 @@ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
7
7
  import { useIsDark, useResizeObserver, useDebounce, Section } from '@react-fabric/core';
8
8
  import { isString, isEmpty } from '@react-fabric/utilities';
9
9
  import classNames from 'classnames';
10
- import { useState, useLayoutEffect, useCallback, useDeferredValue, useMemo, useImperativeHandle } from 'react';
10
+ import { useState, useLayoutEffect, useCallback, useDeferredValue, useMemo, useImperativeHandle, useRef } from 'react';
11
11
  import MonacoEditor from 'react-monaco-editor';
12
- import classes from './CodeEditor.module.css';
13
12
  import { jsx } from 'react/jsx-runtime';
14
13
 
15
14
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
16
15
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
16
+ /**
17
+ * CodeEditor component to render a code editor using Monaco Editor.
18
+ * It supports JSON and HTML languages with optional schema validation and handlebar suggestions.
19
+ * The editor can be configured to be read-only, minimal, and can handle changes in the code.
20
+ * It provides a ref to access the editor's value and validation methods.
21
+ *
22
+ * @param {EditorProps} props - The properties for the CodeEditor component.
23
+ * @param {RefProp<MonacoEditorRef>} ref - The ref to access the editor methods.
24
+ * @return {JSX.Element} The rendered CodeEditor component.
25
+ *
26
+ * @example
27
+ * ```jsx
28
+ * <CodeEditor
29
+ * value={initialValue}
30
+ * schema={[{ uri: "http://example.com/schema.json", schema: {} }]}
31
+ * handlebarSuggestions={[{ text: "example", description: "An example suggestion" }]}
32
+ * required={true}
33
+ * readOnly={false}
34
+ * minimal={false}
35
+ * onChange={(value) => console.log("Code changed:", value)}
36
+ * language="json"
37
+ * />
38
+ * ```
39
+ *
40
+ * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/story/monaco-playground--playground}
41
+ * @see {@link https://microsoft.github.io/monaco-editor/}
42
+ */
17
43
  var CodeEditor = function CodeEditor(_ref) {
18
44
  var ref = _ref.ref,
19
45
  value = _ref.value,
20
46
  schema = _ref.schema,
47
+ handlebarSuggestions = _ref.handlebarSuggestions,
21
48
  required = _ref.required,
22
49
  readOnly = _ref.readOnly,
23
50
  minimal = _ref.minimal,
@@ -90,13 +117,15 @@ var CodeEditor = function CodeEditor(_ref) {
90
117
  }
91
118
  };
92
119
  }, [required, language]);
120
+ var disposeRef = useRef(null);
93
121
  var schemaUri = useCallback(function (monaco) {
94
- if (schema) {
122
+ if (language === "json" && schema) {
123
+ var _monaco$languages;
95
124
  var _schema = _toArray(schema),
96
125
  main = _schema[0],
97
126
  rest = _schema.slice(1);
98
127
  // configure the JSON language support with schemas and schema associations
99
- monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
128
+ (_monaco$languages = monaco.languages) === null || _monaco$languages === void 0 || (_monaco$languages = _monaco$languages.json) === null || _monaco$languages === void 0 || _monaco$languages.jsonDefaults.setDiagnosticsOptions({
100
129
  validate: true,
101
130
  allowComments: false,
102
131
  schemas: [_objectSpread({
@@ -104,7 +133,47 @@ var CodeEditor = function CodeEditor(_ref) {
104
133
  }, main)].concat(_toConsumableArray(rest))
105
134
  });
106
135
  }
107
- }, [schema]);
136
+ if (language === "html" && handlebarSuggestions) {
137
+ var _disposeRef$current, _disposeRef$current$d;
138
+ (_disposeRef$current = disposeRef.current) === null || _disposeRef$current === void 0 || (_disposeRef$current$d = _disposeRef$current.dispose) === null || _disposeRef$current$d === void 0 || _disposeRef$current$d.call(_disposeRef$current);
139
+ disposeRef.current = monaco.languages.registerCompletionItemProvider("html", {
140
+ triggerCharacters: ["{{"],
141
+ provideCompletionItems: function provideCompletionItems(model, position) {
142
+ // find out if we are completing a property in the 'dependencies' object.
143
+ var textUntilPosition = model.getValueInRange({
144
+ startLineNumber: 1,
145
+ startColumn: 1,
146
+ endLineNumber: position.lineNumber,
147
+ endColumn: position.column
148
+ });
149
+ var match = textUntilPosition.match(/\{\{(.*)([^}])?$/);
150
+ if (!match) {
151
+ return {
152
+ suggestions: []
153
+ };
154
+ }
155
+ var word = model.getWordUntilPosition(position);
156
+ var range = {
157
+ startLineNumber: position.lineNumber,
158
+ endLineNumber: position.lineNumber,
159
+ startColumn: word.startColumn,
160
+ endColumn: word.endColumn
161
+ };
162
+ return {
163
+ suggestions: handlebarSuggestions.map(function (hs) {
164
+ return {
165
+ label: hs.text,
166
+ kind: monaco.languages.CompletionItemKind.Text,
167
+ documentation: hs.description,
168
+ insertText: hs.text,
169
+ range: range
170
+ };
171
+ })
172
+ };
173
+ }
174
+ });
175
+ }
176
+ }, [schema, language]);
108
177
  var resetSchema = useCallback(function (_, monaco) {
109
178
  monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
110
179
  validate: true,
@@ -113,9 +182,11 @@ var CodeEditor = function CodeEditor(_ref) {
113
182
  });
114
183
  }, []);
115
184
  return /*#__PURE__*/jsx(Section, {
185
+ className: "relative",
116
186
  children: /*#__PURE__*/jsx("div", {
117
187
  ref: containerRef,
118
- className: classNames(classes.codeEditor, "area-content"),
188
+ "data-ref": "codeEditor",
189
+ className: classNames("fabric-codeEditor", "absolute inset-0"),
119
190
  children: /*#__PURE__*/jsx(MonacoEditor, {
120
191
  editorDidMount: function editorDidMount(e, m) {
121
192
  return [!!e && setEditorRef(e), !!m && setMonacoRef(m)];
@@ -126,7 +197,7 @@ var CodeEditor = function CodeEditor(_ref) {
126
197
  language: language,
127
198
  onChange: handleChange,
128
199
  theme: theme,
129
- className: classes.editor,
200
+ className: "fabric-editor",
130
201
  options: {
131
202
  readOnly: readOnly,
132
203
  minimap: {
@@ -3,16 +3,58 @@ export interface MonacoEditorRef {
3
3
  validate: () => boolean;
4
4
  getValue: () => string | undefined;
5
5
  }
6
- export interface EditorProps {
6
+ export interface BaseEditorProps {
7
7
  value?: string | AnyObject;
8
8
  required?: boolean;
9
9
  readOnly?: boolean;
10
10
  minimal?: boolean;
11
+ onChange?: (value: string) => void;
12
+ }
13
+ type EditorProps = BaseEditorProps & ({
14
+ language: "json";
11
15
  schema?: Array<{
12
16
  uri: string;
13
17
  schema: KeyValue;
14
18
  }>;
15
- language?: "json" | "css" | "html" | "text" | "markdown";
16
- onChange?: (value: string) => void;
17
- }
18
- export declare const CodeEditor: ({ ref, value, schema, required, readOnly, minimal, onChange, language, }: EditorProps & RefProp<MonacoEditorRef>) => import("react/jsx-runtime").JSX.Element;
19
+ handlebarSuggestions?: never;
20
+ } | {
21
+ language: "html";
22
+ schema?: never;
23
+ handlebarSuggestions?: Array<{
24
+ text: string;
25
+ description?: string;
26
+ }>;
27
+ } | {
28
+ language?: "css" | "text" | "markdown";
29
+ schema?: any;
30
+ handlebarSuggestions?: never;
31
+ });
32
+ /**
33
+ * CodeEditor component to render a code editor using Monaco Editor.
34
+ * It supports JSON and HTML languages with optional schema validation and handlebar suggestions.
35
+ * The editor can be configured to be read-only, minimal, and can handle changes in the code.
36
+ * It provides a ref to access the editor's value and validation methods.
37
+ *
38
+ * @param {EditorProps} props - The properties for the CodeEditor component.
39
+ * @param {RefProp<MonacoEditorRef>} ref - The ref to access the editor methods.
40
+ * @return {JSX.Element} The rendered CodeEditor component.
41
+ *
42
+ * @example
43
+ * ```jsx
44
+ * <CodeEditor
45
+ * value={initialValue}
46
+ * schema={[{ uri: "http://example.com/schema.json", schema: {} }]}
47
+ * handlebarSuggestions={[{ text: "example", description: "An example suggestion" }]}
48
+ * required={true}
49
+ * readOnly={false}
50
+ * minimal={false}
51
+ * onChange={(value) => console.log("Code changed:", value)}
52
+ * language="json"
53
+ * />
54
+ * ```
55
+ *
56
+ * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/story/monaco-playground--playground}
57
+ * @see {@link https://microsoft.github.io/monaco-editor/}
58
+ */
59
+ export declare const CodeEditor: ({ ref, value, schema, handlebarSuggestions, required, readOnly, minimal, onChange, language, }: EditorProps & RefProp<MonacoEditorRef>) => import("react/jsx-runtime").JSX.Element;
60
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/monaco",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.52",
4
4
  "description": "RUF: Monaco editor",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -11,9 +11,11 @@
11
11
  "types": "dist/types/index.d.ts",
12
12
  "directories": {
13
13
  "src": "src",
14
+ "styles": "css",
14
15
  "test": "tests"
15
16
  },
16
17
  "files": [
18
+ "css",
17
19
  "src",
18
20
  "dist"
19
21
  ],
@@ -33,23 +35,21 @@
33
35
  "watch": "npm-run-all -p watch:rollup watch:dts"
34
36
  },
35
37
  "dependencies": {
36
- "@react-fabric/core": "^1.0.0-beta.5",
37
- "@react-fabric/utilities": "^1.0.0-beta.5",
38
+ "@react-fabric/core": "^1.0.0-beta.52",
39
+ "@react-fabric/utilities": "^1.0.0-beta.52",
38
40
  "classnames": "^2.5.1",
39
- "react-monaco-editor": "^0.56.2"
41
+ "monaco-editor": "^0.52.2",
42
+ "react-monaco-editor": "^0.58.0"
40
43
  },
41
44
  "peerDependencies": {
42
- "i18next": "^23.15.1",
43
- "react": ">=18.0.0 <20.0.0",
44
- "react-dom": ">=18.0.0 <20.0.0",
45
- "react-i18next": "^15.0.2"
46
- },
47
- "devDependencies": {
48
- "monaco-editor": "^0.52.0"
45
+ "i18next": "^25.2.1",
46
+ "react": "^19.0.0",
47
+ "react-dom": "^19.0.0",
48
+ "react-i18next": "^15.5.2"
49
49
  },
50
50
  "engines": {
51
51
  "node": ">=18.12.0",
52
52
  "npm": ">=8.19.0"
53
53
  },
54
- "gitHead": "7d4a7ed1ea9890652826bef58d01c31280d761c8"
54
+ "gitHead": "0eabb9d88d91af93d657bcc209b226fd576e8260"
55
55
  }
@@ -37,17 +37,17 @@ import {
37
37
  useImperativeHandle,
38
38
  useLayoutEffect,
39
39
  useMemo,
40
+ useRef,
40
41
  useState,
41
42
  } from "react";
42
43
  import MonacoEditor from "react-monaco-editor";
43
- import classes from "./CodeEditor.module.css";
44
44
 
45
45
  export interface MonacoEditorRef {
46
46
  validate: () => boolean;
47
47
  getValue: () => string | undefined;
48
48
  }
49
49
 
50
- export interface EditorProps {
50
+ export interface BaseEditorProps {
51
51
  value?: string | AnyObject;
52
52
 
53
53
  required?: boolean;
@@ -55,17 +55,60 @@ export interface EditorProps {
55
55
 
56
56
  minimal?: boolean;
57
57
 
58
- schema?: Array<{ uri: string; schema: KeyValue }>;
59
-
60
- language?: "json" | "css" | "html" | "text" | "markdown";
61
-
62
58
  onChange?: (value: string) => void;
63
59
  }
64
60
 
61
+ type EditorProps = BaseEditorProps &
62
+ (
63
+ | {
64
+ language: "json";
65
+ schema?: Array<{ uri: string; schema: KeyValue }>;
66
+ handlebarSuggestions?: never;
67
+ }
68
+ | {
69
+ language: "html";
70
+ schema?: never;
71
+ handlebarSuggestions?: Array<{ text: string; description?: string }>;
72
+ }
73
+ | {
74
+ language?: "css" | "text" | "markdown";
75
+ schema?: any;
76
+ handlebarSuggestions?: never;
77
+ }
78
+ );
79
+
80
+ /**
81
+ * CodeEditor component to render a code editor using Monaco Editor.
82
+ * It supports JSON and HTML languages with optional schema validation and handlebar suggestions.
83
+ * The editor can be configured to be read-only, minimal, and can handle changes in the code.
84
+ * It provides a ref to access the editor's value and validation methods.
85
+ *
86
+ * @param {EditorProps} props - The properties for the CodeEditor component.
87
+ * @param {RefProp<MonacoEditorRef>} ref - The ref to access the editor methods.
88
+ * @return {JSX.Element} The rendered CodeEditor component.
89
+ *
90
+ * @example
91
+ * ```jsx
92
+ * <CodeEditor
93
+ * value={initialValue}
94
+ * schema={[{ uri: "http://example.com/schema.json", schema: {} }]}
95
+ * handlebarSuggestions={[{ text: "example", description: "An example suggestion" }]}
96
+ * required={true}
97
+ * readOnly={false}
98
+ * minimal={false}
99
+ * onChange={(value) => console.log("Code changed:", value)}
100
+ * language="json"
101
+ * />
102
+ * ```
103
+ *
104
+ * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/story/monaco-playground--playground}
105
+ * @see {@link https://microsoft.github.io/monaco-editor/}
106
+ */
65
107
  export const CodeEditor = ({
66
108
  ref,
67
109
  value,
68
110
  schema,
111
+ handlebarSuggestions,
69
112
  required,
70
113
  readOnly,
71
114
  minimal,
@@ -140,12 +183,13 @@ export const CodeEditor = ({
140
183
  [required, language],
141
184
  );
142
185
 
186
+ const disposeRef = useRef<AnyObject>(null);
143
187
  const schemaUri = useCallback(
144
188
  (monaco: typeof monacoEditor) => {
145
- if (schema) {
189
+ if (language === "json" && schema) {
146
190
  const [main, ...rest] = schema;
147
191
  // configure the JSON language support with schemas and schema associations
148
- monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
192
+ monaco.languages?.json?.jsonDefaults.setDiagnosticsOptions({
149
193
  validate: true,
150
194
  allowComments: false,
151
195
  schemas: [
@@ -157,8 +201,46 @@ export const CodeEditor = ({
157
201
  ],
158
202
  });
159
203
  }
204
+ if (language === "html" && handlebarSuggestions) {
205
+ disposeRef.current?.dispose?.();
206
+ disposeRef.current = monaco.languages.registerCompletionItemProvider(
207
+ "html",
208
+ {
209
+ triggerCharacters: ["{{"],
210
+ provideCompletionItems: function (model, position) {
211
+ // find out if we are completing a property in the 'dependencies' object.
212
+ const textUntilPosition = model.getValueInRange({
213
+ startLineNumber: 1,
214
+ startColumn: 1,
215
+ endLineNumber: position.lineNumber,
216
+ endColumn: position.column,
217
+ });
218
+ const match = textUntilPosition.match(/\{\{(.*)([^}])?$/);
219
+ if (!match) {
220
+ return { suggestions: [] };
221
+ }
222
+ const word = model.getWordUntilPosition(position);
223
+ const range = {
224
+ startLineNumber: position.lineNumber,
225
+ endLineNumber: position.lineNumber,
226
+ startColumn: word.startColumn,
227
+ endColumn: word.endColumn,
228
+ };
229
+ return {
230
+ suggestions: handlebarSuggestions.map((hs) => ({
231
+ label: hs.text,
232
+ kind: monaco.languages.CompletionItemKind.Text,
233
+ documentation: hs.description,
234
+ insertText: hs.text,
235
+ range,
236
+ })),
237
+ };
238
+ },
239
+ },
240
+ );
241
+ }
160
242
  },
161
- [schema],
243
+ [schema, language],
162
244
  );
163
245
 
164
246
  const resetSchema = useCallback((_: unknown, monaco: typeof monacoEditor) => {
@@ -170,10 +252,11 @@ export const CodeEditor = ({
170
252
  }, []);
171
253
 
172
254
  return (
173
- <Section>
255
+ <Section className="relative">
174
256
  <div
175
257
  ref={containerRef}
176
- className={classNames(classes.codeEditor, "area-content")}
258
+ data-ref="codeEditor"
259
+ className={classNames("fabric-codeEditor", "absolute inset-0")}
177
260
  >
178
261
  <MonacoEditor
179
262
  editorDidMount={(e, m) => [
@@ -186,7 +269,7 @@ export const CodeEditor = ({
186
269
  language={language}
187
270
  onChange={handleChange}
188
271
  theme={theme}
189
- className={classes.editor}
272
+ className={"fabric-editor"}
190
273
  options={{
191
274
  readOnly,
192
275
  minimap: {
@@ -1 +0,0 @@
1
- @layer util{.codeEditor{border:solid transparent;border-width:1px 1px 2px;direction:ltr;overflow:hidden;&:has(:global(.monaco-editor.focused)){border-color:theme(borderColor.primary.500)}.editor{&>:global(.monaco-editor){--vscode-menu-background:theme(colors.tint.50)!important;--vscode-menu-border:theme(borderColor.base)!important;--vscode-focusBorder:transparent!important;--vscode-menu-selectionBackground:color(from var(--primary) srgb r g b/80%)!important;--vscode-editor-background:var(--fabric-bg)!important;--vscode-editor-foreground:var(--fabric-text)!important;--vscode-breadcrumb-background:var(--fabric-bg)!important;--vscode-editorGutter-background:color(from theme(colors.tint.50) srgb r g g/50%)!important;--vscode-minimapSlider-background:color(from var(--tint) srgb r g b/20%)!important;--vscode-minimapSlider-hoverBackground:color(from var(--tint) srgb r g b/25%)!important;--vscode-minimapSlider-activeBackground:color(from var(--tint) srgb r g b/30%)!important;--vscode-input-border:var(--primary)!important;--vscode-editorStickyScroll-background:var(--fabric-bg)!important;--vscode-editorMarkerNavigation-background:var(--fabric-bg)!important;--vscode-editor-wordHighlightTextBackground:color(from theme(colors.accent.700) srgb r g b/20%)!important}& :global(.minimap-decorations-layer){background-color:color(from theme(colors.tint.50) srgb r g g/50%)!important}& :global(.folded-background){opacity:.5}& :global(.monaco-findInput) textarea{min-height:unset;resize:none}& span{padding:0}}}.editorTools{@apply bg-base}}
@@ -1 +0,0 @@
1
- @layer util{.codeEditor{border:solid transparent;border-width:1px 1px 2px;direction:ltr;overflow:hidden;&:has(:global(.monaco-editor.focused)){border-color:theme(borderColor.primary.500)}.editor{&>:global(.monaco-editor){--vscode-menu-background:theme(colors.tint.50)!important;--vscode-menu-border:theme(borderColor.base)!important;--vscode-focusBorder:transparent!important;--vscode-menu-selectionBackground:color(from var(--primary) srgb r g b/80%)!important;--vscode-editor-background:var(--fabric-bg)!important;--vscode-editor-foreground:var(--fabric-text)!important;--vscode-breadcrumb-background:var(--fabric-bg)!important;--vscode-editorGutter-background:color(from theme(colors.tint.50) srgb r g g/50%)!important;--vscode-minimapSlider-background:color(from var(--tint) srgb r g b/20%)!important;--vscode-minimapSlider-hoverBackground:color(from var(--tint) srgb r g b/25%)!important;--vscode-minimapSlider-activeBackground:color(from var(--tint) srgb r g b/30%)!important;--vscode-input-border:var(--primary)!important;--vscode-editorStickyScroll-background:var(--fabric-bg)!important;--vscode-editorMarkerNavigation-background:var(--fabric-bg)!important;--vscode-editor-wordHighlightTextBackground:color(from theme(colors.accent.700) srgb r g b/20%)!important}& :global(.minimap-decorations-layer){background-color:color(from theme(colors.tint.50) srgb r g g/50%)!important}& :global(.folded-background){opacity:.5}& :global(.monaco-findInput) textarea{min-height:unset;resize:none}& span{padding:0}}}.editorTools{@apply bg-base}}