@sqlrooms/monaco-editor 0.26.1-rc.1 → 0.26.1-rc.12
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MonacoEditor.d.ts","sourceRoot":"","sources":["../../src/components/MonacoEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,sBAAsB,CAAC;AAE5E,OAAO,
|
|
1
|
+
{"version":3,"file":"MonacoEditor.d.ts","sourceRoot":"","sources":["../../src/components/MonacoEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAS,WAAW,EAAE,QAAQ,EAAE,OAAO,EAAC,MAAM,sBAAsB,CAAC;AAE5E,OAAO,KAMN,MAAM,OAAO,CAAC;AAOf,OAAO,KAAK,KAAK,MAAM,MAAM,eAAe,CAAC;AAE7C,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,WAAW,EAAE,SAAS,CAAC;IACrE;;OAEG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,QAAQ,CAAC,EAAE,QAAQ,CAAC;IACpB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;;OAIG;IACH,KAAK,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;IAC5B;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,oCAAoC,CAAC;CAC9D;AAUD;;GAEG;AACH,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CAuOpD,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Editor } from '@monaco-editor/react';
|
|
3
3
|
import { Spinner, cn, useTheme } from '@sqlrooms/ui';
|
|
4
|
-
import
|
|
4
|
+
import { useEffect, useMemo, useRef, useState, useSyncExternalStore, } from 'react';
|
|
5
5
|
import { getCssColor, getJsonEditorTheme, getMenuColors, getMonospaceFont, } from '../utils/color-utils';
|
|
6
6
|
const DEFAULT_MONACO_OPTIONS = {
|
|
7
7
|
minimap: { enabled: false },
|
|
@@ -15,16 +15,26 @@ const DEFAULT_MONACO_OPTIONS = {
|
|
|
15
15
|
*/
|
|
16
16
|
export const MonacoEditor = ({ className, language = 'javascript', theme: explicitTheme, value = '', readOnly = false, onMount, onChange, options = {}, ...props }) => {
|
|
17
17
|
const { theme: appTheme } = useTheme();
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
// Track system dark preference without setState-in-effect
|
|
19
|
+
const systemPrefersDark = useSyncExternalStore((onStoreChange) => {
|
|
20
|
+
if (appTheme !== 'system' || typeof window === 'undefined') {
|
|
21
|
+
return () => { };
|
|
22
|
+
}
|
|
23
|
+
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
|
|
24
|
+
mediaQuery.addEventListener('change', onStoreChange);
|
|
25
|
+
return () => mediaQuery.removeEventListener('change', onStoreChange);
|
|
26
|
+
}, () => {
|
|
27
|
+
if (appTheme !== 'system' || typeof window === 'undefined')
|
|
28
|
+
return false;
|
|
29
|
+
return window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
30
|
+
}, () => false);
|
|
31
|
+
const isDark = explicitTheme === 'vs-dark'
|
|
32
|
+
? true
|
|
33
|
+
: explicitTheme === 'light'
|
|
34
|
+
? false
|
|
35
|
+
: appTheme === 'dark' || (appTheme === 'system' && systemPrefersDark);
|
|
36
|
+
// Determine editor theme name passed to the Editor component
|
|
37
|
+
const theme = isDark ? 'vs-dark' : 'light';
|
|
28
38
|
const editorRef = useRef(null);
|
|
29
39
|
const monacoRef = useRef(null);
|
|
30
40
|
const handleEditorDidMount = (editor, monaco) => {
|
|
@@ -83,15 +93,19 @@ export const MonacoEditor = ({ className, language = 'javascript', theme: explic
|
|
|
83
93
|
monaco.editor.defineTheme('sqlrooms-json-light', getJsonEditorTheme(false));
|
|
84
94
|
monaco.editor.defineTheme('sqlrooms-json-dark', getJsonEditorTheme(true));
|
|
85
95
|
// Apply the custom theme based on content type
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
const isDarkMount = explicitTheme === 'vs-dark'
|
|
97
|
+
? true
|
|
98
|
+
: explicitTheme === 'light'
|
|
99
|
+
? false
|
|
100
|
+
: appTheme === 'dark' ||
|
|
101
|
+
(appTheme === 'system' &&
|
|
102
|
+
window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
89
103
|
// Use JSON-specific theme for JSON files
|
|
90
104
|
if (language === 'json') {
|
|
91
|
-
monaco.editor.setTheme(
|
|
105
|
+
monaco.editor.setTheme(isDarkMount ? 'sqlrooms-json-dark' : 'sqlrooms-json-light');
|
|
92
106
|
}
|
|
93
107
|
else {
|
|
94
|
-
monaco.editor.setTheme(
|
|
108
|
+
monaco.editor.setTheme(isDarkMount ? 'sqlrooms-dark' : 'sqlrooms-light');
|
|
95
109
|
}
|
|
96
110
|
if (onMount) {
|
|
97
111
|
onMount(editor, monaco);
|
|
@@ -105,21 +119,21 @@ export const MonacoEditor = ({ className, language = 'javascript', theme: explic
|
|
|
105
119
|
}, [readOnly]);
|
|
106
120
|
// Update the editor theme when app theme changes
|
|
107
121
|
useEffect(() => {
|
|
108
|
-
if (editorRef.current && monacoRef.current
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
122
|
+
if (editorRef.current && monacoRef.current) {
|
|
123
|
+
const targetDark = explicitTheme === 'vs-dark'
|
|
124
|
+
? true
|
|
125
|
+
: explicitTheme === 'light'
|
|
126
|
+
? false
|
|
127
|
+
: isDark;
|
|
112
128
|
// Use JSON-specific theme for JSON files
|
|
113
129
|
if (language === 'json') {
|
|
114
|
-
monacoRef.current.editor.setTheme(
|
|
130
|
+
monacoRef.current.editor.setTheme(targetDark ? 'sqlrooms-json-dark' : 'sqlrooms-json-light');
|
|
115
131
|
}
|
|
116
132
|
else {
|
|
117
|
-
monacoRef.current.editor.setTheme(
|
|
133
|
+
monacoRef.current.editor.setTheme(targetDark ? 'sqlrooms-dark' : 'sqlrooms-light');
|
|
118
134
|
}
|
|
119
|
-
// Force re-render to apply the new theme
|
|
120
|
-
setRenderKey((key) => key + 1);
|
|
121
135
|
}
|
|
122
|
-
}, [appTheme, explicitTheme, language]);
|
|
136
|
+
}, [appTheme, explicitTheme, isDark, language]);
|
|
123
137
|
// Listen for system theme changes if using system theme
|
|
124
138
|
useEffect(() => {
|
|
125
139
|
if (appTheme === 'system' && !explicitTheme && monacoRef.current) {
|
|
@@ -133,8 +147,6 @@ export const MonacoEditor = ({ className, language = 'javascript', theme: explic
|
|
|
133
147
|
else {
|
|
134
148
|
monacoRef.current.editor.setTheme(mediaQuery.matches ? 'sqlrooms-dark' : 'sqlrooms-light');
|
|
135
149
|
}
|
|
136
|
-
// Force re-render to apply the new theme
|
|
137
|
-
setRenderKey((key) => key + 1);
|
|
138
150
|
}
|
|
139
151
|
};
|
|
140
152
|
// Add listener for theme changes
|
|
@@ -153,7 +165,11 @@ export const MonacoEditor = ({ className, language = 'javascript', theme: explic
|
|
|
153
165
|
fontFamily,
|
|
154
166
|
...options,
|
|
155
167
|
}), [options, fontFamily, readOnly]);
|
|
156
|
-
|
|
168
|
+
// Force remount when theme or language changes so Monaco applies the scheme.
|
|
169
|
+
const [renderKey, setRenderKey] = useState(`${theme}-${language}`);
|
|
170
|
+
useEffect(() => {
|
|
171
|
+
setRenderKey(`${theme}-${language}`);
|
|
172
|
+
}, [theme, language]);
|
|
157
173
|
return (_jsx("div", { className: cn('h-[300px] w-full', className), children: _jsx(Editor, { height: "100%", width: "100%", language: language, theme: theme, value: value, options: combinedOptions, onMount: handleEditorDidMount, onChange: onChange, loading: _jsx(Spinner, {}), ...props }, renderKey) }));
|
|
158
174
|
};
|
|
159
175
|
//# sourceMappingURL=MonacoEditor.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MonacoEditor.js","sourceRoot":"","sources":["../../src/components/MonacoEditor.tsx"],"names":[],"mappings":";AAAA,OAAO,EAAC,MAAM,EAAiC,MAAM,sBAAsB,CAAC;AAC5E,OAAO,EAAC,OAAO,EAAE,EAAE,EAAE,QAAQ,EAAC,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,EAAC,SAAS,EAAE,OAAO,EAAE,MAAM,EAAC,MAAM,OAAO,CAAC;AACxD,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,aAAa,EACb,gBAAgB,GACjB,MAAM,sBAAsB,CAAC;AA2C9B,MAAM,sBAAsB,GAC1B;IACE,OAAO,EAAE,EAAC,OAAO,EAAE,KAAK,EAAC;IACzB,oBAAoB,EAAE,KAAK;IAC3B,eAAe,EAAE,IAAI;IACrB,aAAa,EAAE,IAAI;IACnB,oBAAoB,EAAE,IAAI;CAC3B,CAAC;AACJ;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAgC,CAAC,EACxD,SAAS,EACT,QAAQ,GAAG,YAAY,EACvB,KAAK,EAAE,aAAa,EACpB,KAAK,GAAG,EAAE,EACV,QAAQ,GAAG,KAAK,EAChB,OAAO,EACP,QAAQ,EACR,OAAO,GAAG,EAAE,EACZ,GAAG,KAAK,EACT,EAAE,EAAE;IACH,MAAM,EAAC,KAAK,EAAE,QAAQ,EAAC,GAAG,QAAQ,EAAE,CAAC;IACrC,MAAM,CAAC,SAAS,EAAE,YAAY,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAEpD,4CAA4C;IAC5C,0FAA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H,CAAC,EAAE,CAAC,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,CAAC,CAAC;IAExC,qCAAqC;IACrC,MAAM,UAAU,GAAG,gBAAgB,EAAE,CAAC;IAEtC,MAAM,eAAe,GAAG,OAAO,CAC7B,GAAG,EAAE,CAAC,CAAC;QACL,GAAG,sBAAsB;QACzB,QAAQ;QACR,UAAU;QACV,GAAG,OAAO;KACX,CAAC,EACF,CAAC,OAAO,EAAE,UAAU,EAAE,QAAQ,CAAC,CAChC,CAAC;IACF,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAE7B,OAAO,CACL,cAAK,SAAS,EAAE,EAAE,CAAC,kBAAkB,EAAE,SAAS,CAAC,YAC/C,KAAC,MAAM,IACL,MAAM,EAAC,MAAM,EACb,KAAK,EAAC,MAAM,EACZ,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,KAAK,EACZ,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,eAAe,EACxB,OAAO,EAAE,oBAAoB,EAC7B,QAAQ,EAAE,QAAQ,EAClB,OAAO,EAAE,KAAC,OAAO,KAAG,KAEhB,KAAK,IADJ,SAAS,CAEd,GACE,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {Editor, EditorProps, OnChange, OnMount} from '@monaco-editor/react';\nimport {Spinner, cn, useTheme} from '@sqlrooms/ui';\nimport React, {useEffect, useMemo, useRef} from 'react';\nimport {\n getCssColor,\n getJsonEditorTheme,\n getMenuColors,\n getMonospaceFont,\n} from '../utils/color-utils';\nimport type * as Monaco from 'monaco-editor';\n\nexport interface MonacoEditorProps extends Omit<EditorProps, 'onMount'> {\n /**\n * Callback when the editor is mounted\n */\n onMount?: OnMount;\n /**\n * Callback when the editor content changes\n */\n onChange?: OnChange;\n /**\n * CSS class name for the editor container\n * @default ''\n */\n className?: string;\n /**\n * The language of the editor\n * @default 'javascript'\n */\n language?: string;\n /**\n * The theme of the editor\n * Can be explicitly set or will automatically use the app theme if not provided\n * @default 'vs-dark'\n */\n theme?: 'vs-dark' | 'light';\n /**\n * The value of the editor\n */\n value?: string;\n /**\n * Whether the editor is read-only\n * @default false\n */\n readOnly?: boolean;\n /**\n * Additional options for the editor\n */\n options?: Monaco.editor.IStandaloneEditorConstructionOptions;\n}\n\nconst DEFAULT_MONACO_OPTIONS: Monaco.editor.IStandaloneEditorConstructionOptions =\n {\n minimap: {enabled: false},\n scrollBeyondLastLine: false,\n automaticLayout: true,\n fontLigatures: true,\n fixedOverflowWidgets: true,\n };\n/**\n * A wrapper around the Monaco Editor component\n */\nexport const MonacoEditor: React.FC<MonacoEditorProps> = ({\n className,\n language = 'javascript',\n theme: explicitTheme,\n value = '',\n readOnly = false,\n onMount,\n onChange,\n options = {},\n ...props\n}) => {\n const {theme: appTheme} = useTheme();\n const [renderKey, setRenderKey] = React.useState(0);\n\n // Determine editor theme based on app theme\n // Use typeof window check to avoid SSR errors in Next.js when accessing window.matchMedia\n const theme =\n explicitTheme ||\n (appTheme === 'dark' ||\n (appTheme === 'system' &&\n typeof window !== 'undefined' &&\n window.matchMedia('(prefers-color-scheme: dark)').matches)\n ? 'vs-dark'\n : 'light');\n\n const editorRef = useRef<any>(null);\n const monacoRef = useRef<any>(null);\n\n const handleEditorDidMount: OnMount = (editor, monaco) => {\n editorRef.current = editor;\n monacoRef.current = monaco;\n\n // Special language configuration for JSON\n if (language === 'json') {\n // Define a more robust tokenizer for JSON with improved rules\n monaco.languages.setMonarchTokensProvider('json', {\n tokenizer: {\n root: [\n // Property keys (strings followed by a colon)\n [/\"([^\"]*)\"(?=\\s*:)/, 'string.key.json'],\n\n // Regular string values (any quoted string not followed by a colon)\n [/\"([^\"]*)\"(?!\\s*:)/, 'string.value.json'],\n\n // Numbers (integers, decimals, and scientific notation)\n [/-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?/, 'number'],\n\n // Keywords\n [/\\b(?:true|false|null)\\b/, 'keyword'],\n\n // Punctuation and delimiters\n [/[{}[\\],:]/, 'delimiter'],\n ],\n },\n });\n }\n\n // Define editor themes with Tailwind CSS variables\n monaco.editor.defineTheme('sqlrooms-light', {\n base: 'vs',\n inherit: true,\n rules: [],\n colors: {\n 'editor.background': getCssColor('--background', '#ffffff'),\n 'editor.foreground': getCssColor('--foreground', '#000000'),\n 'editor.lineHighlightBackground': getCssColor('--muted', '#f5f5f5'),\n 'editorCursor.foreground': getCssColor('--primary', '#000000'),\n 'editor.selectionBackground': getCssColor('--accent', '#e3e3e3'),\n 'editorLineNumber.foreground': getCssColor(\n '--muted-foreground',\n '#888888',\n ),\n ...getMenuColors(false),\n },\n });\n\n monaco.editor.defineTheme('sqlrooms-dark', {\n base: 'vs-dark',\n inherit: true,\n rules: [],\n colors: {\n 'editor.background': getCssColor('--background', '#1e1e1e'),\n 'editor.foreground': getCssColor('--foreground', '#d4d4d4'),\n 'editor.lineHighlightBackground': getCssColor('--muted', '#2a2a2a'),\n 'editorCursor.foreground': getCssColor('--primary', '#ffffff'),\n 'editor.selectionBackground': getCssColor('--accent', '#264f78'),\n 'editorLineNumber.foreground': getCssColor(\n '--muted-foreground',\n '#858585',\n ),\n ...getMenuColors(true),\n },\n });\n\n // Define JSON-specific themes with rich token coloring\n monaco.editor.defineTheme('sqlrooms-json-light', getJsonEditorTheme(false));\n monaco.editor.defineTheme('sqlrooms-json-dark', getJsonEditorTheme(true));\n\n // Apply the custom theme based on content type\n const isDark =\n appTheme === 'dark' ||\n (appTheme === 'system' &&\n window.matchMedia('(prefers-color-scheme: dark)').matches);\n\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monaco.editor.setTheme(\n isDark ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monaco.editor.setTheme(isDark ? 'sqlrooms-dark' : 'sqlrooms-light');\n }\n\n if (onMount) {\n onMount(editor, monaco);\n }\n };\n\n // Apply readOnly option\n useEffect(() => {\n if (editorRef.current) {\n editorRef.current.updateOptions({readOnly});\n }\n }, [readOnly]);\n\n // Update the editor theme when app theme changes\n useEffect(() => {\n if (editorRef.current && monacoRef.current && !explicitTheme) {\n const isDark =\n appTheme === 'dark' ||\n (appTheme === 'system' &&\n window.matchMedia('(prefers-color-scheme: dark)').matches);\n\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monacoRef.current.editor.setTheme(\n isDark ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monacoRef.current.editor.setTheme(\n isDark ? 'sqlrooms-dark' : 'sqlrooms-light',\n );\n }\n\n // Force re-render to apply the new theme\n setRenderKey((key) => key + 1);\n }\n }, [appTheme, explicitTheme, language]);\n\n // Listen for system theme changes if using system theme\n useEffect(() => {\n if (appTheme === 'system' && !explicitTheme && monacoRef.current) {\n const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n const handleChange = () => {\n if (monacoRef.current) {\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monacoRef.current.editor.setTheme(\n mediaQuery.matches ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monacoRef.current.editor.setTheme(\n mediaQuery.matches ? 'sqlrooms-dark' : 'sqlrooms-light',\n );\n }\n // Force re-render to apply the new theme\n setRenderKey((key) => key + 1);\n }\n };\n\n // Add listener for theme changes\n mediaQuery.addEventListener('change', handleChange);\n\n // Clean up\n return () => {\n mediaQuery.removeEventListener('change', handleChange);\n };\n }\n }, [appTheme, explicitTheme, language]);\n\n // Get monospace font for code editor\n const fontFamily = getMonospaceFont();\n\n const combinedOptions = useMemo(\n () => ({\n ...DEFAULT_MONACO_OPTIONS,\n readOnly,\n fontFamily,\n ...options,\n }),\n [options, fontFamily, readOnly],\n );\n console.log(combinedOptions);\n\n return (\n <div className={cn('h-[300px] w-full', className)}>\n <Editor\n height=\"100%\"\n width=\"100%\"\n language={language}\n theme={theme}\n value={value}\n options={combinedOptions}\n onMount={handleEditorDidMount}\n onChange={onChange}\n loading={<Spinner />}\n key={renderKey}\n {...props}\n />\n </div>\n );\n};\n"]}
|
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ClB,OAAO,EAAE,KAAC,OAAO,KAAG,KAEhB,KAAK,IADJ,SAAS,CAEd,GACE,CACP,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import {Editor, EditorProps, OnChange, OnMount} from '@monaco-editor/react';\nimport {Spinner, cn, useTheme} from '@sqlrooms/ui';\nimport React, {\n useEffect,\n useMemo,\n useRef,\n useState,\n useSyncExternalStore,\n} from 'react';\nimport {\n getCssColor,\n getJsonEditorTheme,\n getMenuColors,\n getMonospaceFont,\n} from '../utils/color-utils';\nimport type * as Monaco from 'monaco-editor';\n\nexport interface MonacoEditorProps extends Omit<EditorProps, 'onMount'> {\n /**\n * Callback when the editor is mounted\n */\n onMount?: OnMount;\n /**\n * Callback when the editor content changes\n */\n onChange?: OnChange;\n /**\n * CSS class name for the editor container\n * @default ''\n */\n className?: string;\n /**\n * The language of the editor\n * @default 'javascript'\n */\n language?: string;\n /**\n * The theme of the editor\n * Can be explicitly set or will automatically use the app theme if not provided\n * @default 'vs-dark'\n */\n theme?: 'vs-dark' | 'light';\n /**\n * The value of the editor\n */\n value?: string;\n /**\n * Whether the editor is read-only\n * @default false\n */\n readOnly?: boolean;\n /**\n * Additional options for the editor\n */\n options?: Monaco.editor.IStandaloneEditorConstructionOptions;\n}\n\nconst DEFAULT_MONACO_OPTIONS: Monaco.editor.IStandaloneEditorConstructionOptions =\n {\n minimap: {enabled: false},\n scrollBeyondLastLine: false,\n automaticLayout: true,\n fontLigatures: true,\n fixedOverflowWidgets: true,\n };\n/**\n * A wrapper around the Monaco Editor component\n */\nexport const MonacoEditor: React.FC<MonacoEditorProps> = ({\n className,\n language = 'javascript',\n theme: explicitTheme,\n value = '',\n readOnly = false,\n onMount,\n onChange,\n options = {},\n ...props\n}) => {\n const {theme: appTheme} = useTheme();\n\n // Track system dark preference without setState-in-effect\n const systemPrefersDark = useSyncExternalStore(\n (onStoreChange) => {\n if (appTheme !== 'system' || typeof window === 'undefined') {\n return () => {};\n }\n const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n mediaQuery.addEventListener('change', onStoreChange);\n return () => mediaQuery.removeEventListener('change', onStoreChange);\n },\n () => {\n if (appTheme !== 'system' || typeof window === 'undefined') return false;\n return window.matchMedia('(prefers-color-scheme: dark)').matches;\n },\n () => false,\n );\n\n const isDark =\n explicitTheme === 'vs-dark'\n ? true\n : explicitTheme === 'light'\n ? false\n : appTheme === 'dark' || (appTheme === 'system' && systemPrefersDark);\n\n // Determine editor theme name passed to the Editor component\n const theme = isDark ? 'vs-dark' : 'light';\n\n const editorRef = useRef<any>(null);\n const monacoRef = useRef<any>(null);\n\n const handleEditorDidMount: OnMount = (editor, monaco) => {\n editorRef.current = editor;\n monacoRef.current = monaco;\n\n // Special language configuration for JSON\n if (language === 'json') {\n // Define a more robust tokenizer for JSON with improved rules\n monaco.languages.setMonarchTokensProvider('json', {\n tokenizer: {\n root: [\n // Property keys (strings followed by a colon)\n [/\"([^\"]*)\"(?=\\s*:)/, 'string.key.json'],\n\n // Regular string values (any quoted string not followed by a colon)\n [/\"([^\"]*)\"(?!\\s*:)/, 'string.value.json'],\n\n // Numbers (integers, decimals, and scientific notation)\n [/-?\\d+(?:\\.\\d+)?(?:[eE][+-]?\\d+)?/, 'number'],\n\n // Keywords\n [/\\b(?:true|false|null)\\b/, 'keyword'],\n\n // Punctuation and delimiters\n [/[{}[\\],:]/, 'delimiter'],\n ],\n },\n });\n }\n\n // Define editor themes with Tailwind CSS variables\n monaco.editor.defineTheme('sqlrooms-light', {\n base: 'vs',\n inherit: true,\n rules: [],\n colors: {\n 'editor.background': getCssColor('--background', '#ffffff'),\n 'editor.foreground': getCssColor('--foreground', '#000000'),\n 'editor.lineHighlightBackground': getCssColor('--muted', '#f5f5f5'),\n 'editorCursor.foreground': getCssColor('--primary', '#000000'),\n 'editor.selectionBackground': getCssColor('--accent', '#e3e3e3'),\n 'editorLineNumber.foreground': getCssColor(\n '--muted-foreground',\n '#888888',\n ),\n ...getMenuColors(false),\n },\n });\n\n monaco.editor.defineTheme('sqlrooms-dark', {\n base: 'vs-dark',\n inherit: true,\n rules: [],\n colors: {\n 'editor.background': getCssColor('--background', '#1e1e1e'),\n 'editor.foreground': getCssColor('--foreground', '#d4d4d4'),\n 'editor.lineHighlightBackground': getCssColor('--muted', '#2a2a2a'),\n 'editorCursor.foreground': getCssColor('--primary', '#ffffff'),\n 'editor.selectionBackground': getCssColor('--accent', '#264f78'),\n 'editorLineNumber.foreground': getCssColor(\n '--muted-foreground',\n '#858585',\n ),\n ...getMenuColors(true),\n },\n });\n\n // Define JSON-specific themes with rich token coloring\n monaco.editor.defineTheme('sqlrooms-json-light', getJsonEditorTheme(false));\n monaco.editor.defineTheme('sqlrooms-json-dark', getJsonEditorTheme(true));\n\n // Apply the custom theme based on content type\n const isDarkMount =\n explicitTheme === 'vs-dark'\n ? true\n : explicitTheme === 'light'\n ? false\n : appTheme === 'dark' ||\n (appTheme === 'system' &&\n window.matchMedia('(prefers-color-scheme: dark)').matches);\n\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monaco.editor.setTheme(\n isDarkMount ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monaco.editor.setTheme(isDarkMount ? 'sqlrooms-dark' : 'sqlrooms-light');\n }\n\n if (onMount) {\n onMount(editor, monaco);\n }\n };\n\n // Apply readOnly option\n useEffect(() => {\n if (editorRef.current) {\n editorRef.current.updateOptions({readOnly});\n }\n }, [readOnly]);\n\n // Update the editor theme when app theme changes\n useEffect(() => {\n if (editorRef.current && monacoRef.current) {\n const targetDark =\n explicitTheme === 'vs-dark'\n ? true\n : explicitTheme === 'light'\n ? false\n : isDark;\n\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monacoRef.current.editor.setTheme(\n targetDark ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monacoRef.current.editor.setTheme(\n targetDark ? 'sqlrooms-dark' : 'sqlrooms-light',\n );\n }\n }\n }, [appTheme, explicitTheme, isDark, language]);\n\n // Listen for system theme changes if using system theme\n useEffect(() => {\n if (appTheme === 'system' && !explicitTheme && monacoRef.current) {\n const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n const handleChange = () => {\n if (monacoRef.current) {\n // Use JSON-specific theme for JSON files\n if (language === 'json') {\n monacoRef.current.editor.setTheme(\n mediaQuery.matches ? 'sqlrooms-json-dark' : 'sqlrooms-json-light',\n );\n } else {\n monacoRef.current.editor.setTheme(\n mediaQuery.matches ? 'sqlrooms-dark' : 'sqlrooms-light',\n );\n }\n }\n };\n\n // Add listener for theme changes\n mediaQuery.addEventListener('change', handleChange);\n\n // Clean up\n return () => {\n mediaQuery.removeEventListener('change', handleChange);\n };\n }\n }, [appTheme, explicitTheme, language]);\n\n // Get monospace font for code editor\n const fontFamily = getMonospaceFont();\n\n const combinedOptions = useMemo(\n () => ({\n ...DEFAULT_MONACO_OPTIONS,\n readOnly,\n fontFamily,\n ...options,\n }),\n [options, fontFamily, readOnly],\n );\n\n // Force remount when theme or language changes so Monaco applies the scheme.\n const [renderKey, setRenderKey] = useState(`${theme}-${language}`);\n useEffect(() => {\n setRenderKey(`${theme}-${language}`);\n }, [theme, language]);\n return (\n <div className={cn('h-[300px] w-full', className)}>\n <Editor\n height=\"100%\"\n width=\"100%\"\n language={language}\n theme={theme}\n value={value}\n options={combinedOptions}\n onMount={handleEditorDidMount}\n onChange={onChange}\n loading={<Spinner />}\n key={renderKey}\n {...props}\n />\n </div>\n );\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sqlrooms/monaco-editor",
|
|
3
|
-
"version": "0.26.1-rc.
|
|
3
|
+
"version": "0.26.1-rc.12",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"module": "dist/index.js",
|
|
7
7
|
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
8
9
|
"author": "Ilya Boyandin <ilya@boyandin.me>",
|
|
9
10
|
"license": "MIT",
|
|
10
11
|
"repository": {
|
|
@@ -19,8 +20,8 @@
|
|
|
19
20
|
},
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"@monaco-editor/react": "^4.7.0",
|
|
22
|
-
"@sqlrooms/ui": "0.26.1-rc.
|
|
23
|
-
"monaco-editor": "^0.
|
|
23
|
+
"@sqlrooms/ui": "0.26.1-rc.12",
|
|
24
|
+
"monaco-editor": "^0.55.0"
|
|
24
25
|
},
|
|
25
26
|
"peerDependencies": {
|
|
26
27
|
"react": ">=18",
|
|
@@ -33,5 +34,5 @@
|
|
|
33
34
|
"typecheck": "tsc --noEmit",
|
|
34
35
|
"typedoc": "typedoc"
|
|
35
36
|
},
|
|
36
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "2b9d9c03a48aae500c6f725b36bb89d43ef8d2eb"
|
|
37
38
|
}
|