@cccsaurora/howler-ui 3.2.0-dev.1786.1 → 3.2.0-dev.1791.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.
|
@@ -1,93 +1,123 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { Editor, useMonaco } from '@monaco-editor/react';
|
|
3
3
|
import { useTheme } from '@mui/material';
|
|
4
|
-
import { useAppTheme, useAppThemeBuilder } from '@tui/core';
|
|
5
4
|
import { memo, useCallback, useEffect, useMemo } from 'react';
|
|
5
|
+
const toMonacoColor = (color) => {
|
|
6
|
+
const trimmedColor = color.trim();
|
|
7
|
+
const hexMatch = trimmedColor.match(/^#?([\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/i);
|
|
8
|
+
if (hexMatch) {
|
|
9
|
+
let hex = hexMatch[1] ?? '';
|
|
10
|
+
if (hex.length === 3 || hex.length === 4) {
|
|
11
|
+
hex = hex
|
|
12
|
+
.split('')
|
|
13
|
+
.map(character => character.repeat(2))
|
|
14
|
+
.join('');
|
|
15
|
+
}
|
|
16
|
+
return `#${hex.toUpperCase()}`;
|
|
17
|
+
}
|
|
18
|
+
const rgbMatch = trimmedColor.match(/^rgba?\((.*)\)$/i);
|
|
19
|
+
if (!rgbMatch) {
|
|
20
|
+
return trimmedColor;
|
|
21
|
+
}
|
|
22
|
+
const [channelValues = '', slashAlpha] = (rgbMatch[1] ?? '').split('/');
|
|
23
|
+
const channels = channelValues
|
|
24
|
+
.split(/[\s,]+/)
|
|
25
|
+
.filter(Boolean)
|
|
26
|
+
.map(channel => channel.trim());
|
|
27
|
+
const alpha = slashAlpha?.trim() ?? (channels.length > 3 ? channels.pop() : undefined);
|
|
28
|
+
if (channels.length !== 3 || !channels.every(channel => /^-?(?:\d+\.?\d*|\.\d+)%?$/.test(channel))) {
|
|
29
|
+
return trimmedColor;
|
|
30
|
+
}
|
|
31
|
+
const hexChannels = channels
|
|
32
|
+
.map(channel => {
|
|
33
|
+
const amount = Number.parseFloat(channel);
|
|
34
|
+
const value = channel.endsWith('%') ? (amount * 255) / 100 : amount;
|
|
35
|
+
return Math.round(Math.min(255, Math.max(0, value)))
|
|
36
|
+
.toString(16)
|
|
37
|
+
.padStart(2, '0');
|
|
38
|
+
})
|
|
39
|
+
.join('');
|
|
40
|
+
if (!alpha) {
|
|
41
|
+
return `#${hexChannels.toUpperCase()}`;
|
|
42
|
+
}
|
|
43
|
+
const alphaValue = Number.parseFloat(alpha);
|
|
44
|
+
if (/^-?(?:\d+\.?\d*|\.\d+)%?$/.test(alpha)) {
|
|
45
|
+
const normalizedAlpha = alpha.endsWith('%') ? alphaValue / 100 : alphaValue;
|
|
46
|
+
const alphaHex = Math.round(Math.min(1, Math.max(0, normalizedAlpha)) * 255)
|
|
47
|
+
.toString(16)
|
|
48
|
+
.padStart(2, '0');
|
|
49
|
+
return `#${hexChannels}${alphaHex}`.toUpperCase();
|
|
50
|
+
}
|
|
51
|
+
return trimmedColor;
|
|
52
|
+
};
|
|
53
|
+
const toMonacoTokenColor = (color) => toMonacoColor(color).replace(/^#/, '');
|
|
6
54
|
const ThemedEditor = ({ beforeMount, options = {}, id, ...otherProps }) => {
|
|
7
|
-
const { current: currentTheme, optionsOverride } = useAppTheme();
|
|
8
|
-
const themeBuilder = useAppThemeBuilder();
|
|
9
|
-
const { lightTheme, darkTheme } = useMemo(() => themeBuilder(currentTheme, optionsOverride), [themeBuilder, currentTheme, optionsOverride]);
|
|
10
55
|
const theme = useTheme();
|
|
11
56
|
const monaco = useMonaco();
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
token: 'invalid',
|
|
36
|
-
foreground: lightTheme.palette.error.main.toUpperCase().replaceAll('#', '')
|
|
37
|
-
},
|
|
38
|
-
{
|
|
39
|
-
token: 'boolean',
|
|
40
|
-
foreground: lightTheme.palette.success.main.toUpperCase().replaceAll('#', '')
|
|
41
|
-
}
|
|
42
|
-
],
|
|
43
|
-
colors: {
|
|
44
|
-
'editor.background': lightBackground
|
|
57
|
+
const monacoTheme = useMemo(() => ({
|
|
58
|
+
base: theme.palette.mode === 'light' ? 'vs' : 'vs-dark',
|
|
59
|
+
inherit: true,
|
|
60
|
+
rules: [
|
|
61
|
+
{
|
|
62
|
+
token: 'handlebars',
|
|
63
|
+
foreground: toMonacoTokenColor(theme.palette.warning.dark)
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
token: 'operator',
|
|
67
|
+
foreground: toMonacoTokenColor(theme.palette.warning.light)
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
token: 'string.invalid',
|
|
71
|
+
foreground: toMonacoTokenColor(theme.palette.error.main)
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
token: 'invalid',
|
|
75
|
+
foreground: toMonacoTokenColor(theme.palette.error.main)
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
token: 'boolean',
|
|
79
|
+
foreground: toMonacoTokenColor(theme.palette.success.main)
|
|
45
80
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
81
|
+
],
|
|
82
|
+
colors: {
|
|
83
|
+
'editor.background': toMonacoColor(theme.palette.background.paper),
|
|
84
|
+
'editor.foreground': toMonacoColor(theme.palette.text.primary),
|
|
85
|
+
'editorGutter.background': toMonacoColor(theme.palette.background.paper),
|
|
86
|
+
'editorLineNumber.foreground': toMonacoColor(theme.palette.text.disabled),
|
|
87
|
+
'editorLineNumber.activeForeground': toMonacoColor(theme.palette.text.secondary),
|
|
88
|
+
'editorCursor.foreground': toMonacoColor(theme.palette.primary.main),
|
|
89
|
+
'editor.selectionBackground': toMonacoColor(theme.palette.action.selected),
|
|
90
|
+
'editor.inactiveSelectionBackground': toMonacoColor(theme.palette.action.hover),
|
|
91
|
+
'editor.lineHighlightBackground': toMonacoColor(theme.palette.action.hover),
|
|
92
|
+
'editorWhitespace.foreground': toMonacoColor(theme.palette.divider),
|
|
93
|
+
'editorIndentGuide.background': toMonacoColor(theme.palette.divider),
|
|
94
|
+
'editorIndentGuide.activeBackground': toMonacoColor(theme.palette.text.disabled),
|
|
95
|
+
'editorWidget.background': toMonacoColor(theme.palette.background.paper),
|
|
96
|
+
'editorWidget.border': toMonacoColor(theme.palette.divider),
|
|
97
|
+
'editorSuggestWidget.background': toMonacoColor(theme.palette.background.paper),
|
|
98
|
+
'editorSuggestWidget.border': toMonacoColor(theme.palette.divider),
|
|
99
|
+
'editorSuggestWidget.selectedBackground': toMonacoColor(theme.palette.action.selected),
|
|
100
|
+
'editorHoverWidget.background': toMonacoColor(theme.palette.background.paper),
|
|
101
|
+
'editorHoverWidget.border': toMonacoColor(theme.palette.divider),
|
|
102
|
+
'editorOverviewRuler.border': toMonacoColor(theme.palette.divider),
|
|
103
|
+
'scrollbarSlider.background': toMonacoColor(theme.palette.action.hover),
|
|
104
|
+
'scrollbarSlider.hoverBackground': toMonacoColor(theme.palette.action.selected),
|
|
105
|
+
'scrollbarSlider.activeBackground': toMonacoColor(theme.palette.action.active)
|
|
51
106
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
rules: [
|
|
56
|
-
{
|
|
57
|
-
token: 'handlebars',
|
|
58
|
-
foreground: darkTheme.palette.warning.dark.toUpperCase().replaceAll('#', '')
|
|
59
|
-
},
|
|
60
|
-
{
|
|
61
|
-
token: 'operator',
|
|
62
|
-
foreground: darkTheme.palette.warning.light.toUpperCase().replaceAll('#', '')
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
token: 'string.invalid',
|
|
66
|
-
foreground: darkTheme.palette.error.main.toUpperCase().replaceAll('#', '')
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
token: 'invalid',
|
|
70
|
-
foreground: darkTheme.palette.error.main.toUpperCase().replaceAll('#', '')
|
|
71
|
-
},
|
|
72
|
-
{
|
|
73
|
-
token: 'boolean',
|
|
74
|
-
foreground: darkTheme.palette.success.main.toUpperCase().replaceAll('#', '')
|
|
75
|
-
}
|
|
76
|
-
],
|
|
77
|
-
colors: {
|
|
78
|
-
'editor.background': darkBackground
|
|
79
|
-
}
|
|
80
|
-
});
|
|
107
|
+
}), [theme]);
|
|
108
|
+
const _beforeMount = useCallback((_monaco) => {
|
|
109
|
+
_monaco.editor.defineTheme('howler', monacoTheme);
|
|
81
110
|
_monaco.languages.register({ id: 'lucene' });
|
|
82
111
|
_monaco.languages.register({ id: 'eql' });
|
|
83
112
|
beforeMount?.(_monaco);
|
|
84
|
-
}, [beforeMount,
|
|
113
|
+
}, [beforeMount, monacoTheme]);
|
|
85
114
|
useEffect(() => {
|
|
86
115
|
if (!monaco) {
|
|
87
116
|
return;
|
|
88
117
|
}
|
|
89
|
-
monaco.editor.
|
|
90
|
-
|
|
118
|
+
monaco.editor.defineTheme('howler', monacoTheme);
|
|
119
|
+
monaco.editor.setTheme('howler');
|
|
120
|
+
}, [monaco, monacoTheme]);
|
|
91
121
|
const _options = useMemo(() => ({
|
|
92
122
|
automaticLayout: true,
|
|
93
123
|
minimap: { enabled: false },
|
|
@@ -99,6 +129,6 @@ const ThemedEditor = ({ beforeMount, options = {}, id, ...otherProps }) => {
|
|
|
99
129
|
},
|
|
100
130
|
...options
|
|
101
131
|
}), [options]);
|
|
102
|
-
return
|
|
132
|
+
return _jsx(Editor, { ...otherProps, wrapperProps: { id }, theme: "howler", beforeMount: _beforeMount, options: _options });
|
|
103
133
|
};
|
|
104
134
|
export default memo(ThemedEditor);
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
/// <reference types="vitest" />
|
|
3
3
|
import { render } from '@testing-library/react';
|
|
4
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
4
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
5
|
const mockDefineTheme = vi.hoisted(() => vi.fn());
|
|
6
6
|
const mockRegisterLanguage = vi.hoisted(() => vi.fn());
|
|
7
7
|
const mockSetTheme = vi.hoisted(() => vi.fn());
|
|
8
8
|
const capturedProps = vi.hoisted(() => ({ current: null }));
|
|
9
|
-
|
|
9
|
+
const createPalette = (mode = 'light', paper = mode === 'light' ? '#fff' : '#111', primary = mode === 'light' ? '#0055aa' : '#268bd2', foreground = mode === 'light' ? '#202020' : '#eee8d5', warningDark = '#abc') => ({
|
|
10
|
+
mode,
|
|
11
|
+
background: { paper },
|
|
12
|
+
primary: { main: primary },
|
|
13
|
+
text: { primary: foreground, secondary: '#777777', disabled: '#aaaaaa' },
|
|
14
|
+
divider: '#dddddd',
|
|
15
|
+
action: {
|
|
16
|
+
selected: 'rgba(10, 20, 30, 0.2)',
|
|
17
|
+
hover: 'rgba(10, 20, 30, 0.1)',
|
|
18
|
+
active: 'rgba(10, 20, 30, 0.3)'
|
|
19
|
+
},
|
|
20
|
+
warning: { dark: warningDark, light: '#def' },
|
|
21
|
+
error: { main: '#123456' },
|
|
22
|
+
success: { main: '#654321' }
|
|
23
|
+
});
|
|
10
24
|
let monacoValue = null;
|
|
25
|
+
let themePalette = createPalette();
|
|
11
26
|
vi.mock('@monaco-editor/react', () => ({
|
|
12
27
|
Editor: (props) => {
|
|
13
28
|
capturedProps.current = props;
|
|
@@ -16,51 +31,67 @@ vi.mock('@monaco-editor/react', () => ({
|
|
|
16
31
|
useMonaco: () => monacoValue
|
|
17
32
|
}));
|
|
18
33
|
vi.mock('@mui/material', () => ({
|
|
19
|
-
useTheme: () => ({ palette:
|
|
20
|
-
}));
|
|
21
|
-
vi.mock('@tui/core', () => ({
|
|
22
|
-
useAppTheme: () => ({ current: 'default', optionsOverride: { dense: true } }),
|
|
23
|
-
useAppThemeBuilder: () => () => ({
|
|
24
|
-
lightTheme: {
|
|
25
|
-
palette: {
|
|
26
|
-
background: { paper: '#fff' },
|
|
27
|
-
warning: { dark: '#abc', light: '#def' },
|
|
28
|
-
error: { main: '#123456' },
|
|
29
|
-
success: { main: '#654321' }
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
darkTheme: {
|
|
33
|
-
palette: {
|
|
34
|
-
background: { paper: '#111' },
|
|
35
|
-
warning: { dark: '#aaa', light: '#bbb' },
|
|
36
|
-
error: { main: '#222222' },
|
|
37
|
-
success: { main: '#333333' }
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
})
|
|
34
|
+
useTheme: () => ({ palette: themePalette })
|
|
41
35
|
}));
|
|
42
36
|
import ThemedEditor from './ThemedEditor';
|
|
43
37
|
describe('ThemedEditor', () => {
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
themePalette = createPalette();
|
|
40
|
+
monacoValue = null;
|
|
41
|
+
vi.clearAllMocks();
|
|
42
|
+
});
|
|
44
43
|
it('defines custom monaco themes and merges editor options', () => {
|
|
45
|
-
monacoValue = { editor: { setTheme: mockSetTheme } };
|
|
44
|
+
monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
|
|
46
45
|
render(_jsx(ThemedEditor, { id: "the-editor", options: { readOnly: true }, beforeMount: vi.fn(), value: "x" }));
|
|
47
46
|
const fakeMonaco = {
|
|
48
47
|
editor: { defineTheme: mockDefineTheme },
|
|
49
48
|
languages: { register: mockRegisterLanguage }
|
|
50
49
|
};
|
|
51
50
|
capturedProps.current.beforeMount(fakeMonaco);
|
|
52
|
-
expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({
|
|
53
|
-
|
|
51
|
+
expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({
|
|
52
|
+
base: 'vs',
|
|
53
|
+
colors: expect.objectContaining({ 'editor.background': '#FFFFFF', 'editor.foreground': '#202020' })
|
|
54
|
+
}));
|
|
54
55
|
expect(mockRegisterLanguage).toHaveBeenCalledWith({ id: 'lucene' });
|
|
55
56
|
expect(capturedProps.current.wrapperProps).toEqual({ id: 'the-editor' });
|
|
56
57
|
expect(capturedProps.current.options).toMatchObject({ readOnly: true, automaticLayout: true });
|
|
57
58
|
expect(mockSetTheme).toHaveBeenCalledWith('howler');
|
|
58
59
|
});
|
|
59
|
-
it('
|
|
60
|
-
|
|
61
|
-
monacoValue = { editor: { setTheme: mockSetTheme } };
|
|
60
|
+
it('converts RGB palette colors to Monaco token colors', () => {
|
|
61
|
+
themePalette = createPalette('light', '#fff', '#0055aa', '#202020', 'RGB(126, 95, 0)');
|
|
62
|
+
monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
|
|
62
63
|
render(_jsx(ThemedEditor, { value: "x" }));
|
|
63
|
-
|
|
64
|
-
|
|
64
|
+
capturedProps.current.beforeMount({
|
|
65
|
+
editor: { defineTheme: mockDefineTheme },
|
|
66
|
+
languages: { register: mockRegisterLanguage }
|
|
67
|
+
});
|
|
68
|
+
expect(mockDefineTheme).toHaveBeenCalledWith('howler', expect.objectContaining({
|
|
69
|
+
rules: expect.arrayContaining([expect.objectContaining({ token: 'handlebars', foreground: '7E5F00' })])
|
|
70
|
+
}));
|
|
71
|
+
});
|
|
72
|
+
it('uses and updates colors from the active app theme', () => {
|
|
73
|
+
themePalette = createPalette('dark', '#002B36');
|
|
74
|
+
monacoValue = { editor: { defineTheme: mockDefineTheme, setTheme: mockSetTheme } };
|
|
75
|
+
const { rerender } = render(_jsx(ThemedEditor, { value: "x" }));
|
|
76
|
+
expect(capturedProps.current.theme).toBe('howler');
|
|
77
|
+
expect(mockDefineTheme).toHaveBeenLastCalledWith('howler', expect.objectContaining({
|
|
78
|
+
base: 'vs-dark',
|
|
79
|
+
colors: expect.objectContaining({
|
|
80
|
+
'editor.background': '#002B36',
|
|
81
|
+
'editor.foreground': '#EEE8D5',
|
|
82
|
+
'editor.selectionBackground': '#0A141E33'
|
|
83
|
+
})
|
|
84
|
+
}));
|
|
85
|
+
expect(mockSetTheme).toHaveBeenCalledWith('howler');
|
|
86
|
+
themePalette = createPalette('dark', '#101010', '#cc5500', '#f0f0f0');
|
|
87
|
+
rerender(_jsx(ThemedEditor, { value: "y" }));
|
|
88
|
+
expect(mockDefineTheme).toHaveBeenLastCalledWith('howler', expect.objectContaining({
|
|
89
|
+
base: 'vs-dark',
|
|
90
|
+
colors: expect.objectContaining({
|
|
91
|
+
'editor.background': '#101010',
|
|
92
|
+
'editor.foreground': '#F0F0F0',
|
|
93
|
+
'editorCursor.foreground': '#CC5500'
|
|
94
|
+
})
|
|
95
|
+
}));
|
|
65
96
|
});
|
|
66
97
|
});
|
package/package.json
CHANGED