@fc-components/monaco-editor 0.1.3 → 0.1.5
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.
- package/README.md +53 -30
- package/dist/monaco-editor.cjs.development.js +43 -26
- package/dist/monaco-editor.cjs.development.js.map +1 -1
- package/dist/monaco-editor.cjs.production.min.js +1 -1
- package/dist/monaco-editor.cjs.production.min.js.map +1 -1
- package/dist/monaco-editor.esm.js +44 -27
- package/dist/monaco-editor.esm.js.map +1 -1
- package/dist/promql/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/promql/index.tsx +44 -28
package/README.md
CHANGED
|
@@ -4,37 +4,60 @@
|
|
|
4
4
|
import { PromQLMonacoEditor } from '@fc-components/monaco-editor';
|
|
5
5
|
|
|
6
6
|
const App = () => {
|
|
7
|
+
const [value, setValue] = React.useState<string>();
|
|
8
|
+
const promQLMonacoEditorRef = React.useRef<typeof PromQLMonacoEditor | null>(null);
|
|
9
|
+
|
|
7
10
|
return (
|
|
8
|
-
<
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
11
|
+
<div style={{ padding: 16 }}>
|
|
12
|
+
<PromQLMonacoEditor
|
|
13
|
+
ref={promQLMonacoEditorRef}
|
|
14
|
+
theme='light' // or 'dark'
|
|
15
|
+
size='middle' // 'small', 'middle', or 'large'
|
|
16
|
+
placeholder='Enter your PromQL query here...'
|
|
17
|
+
variablesNames={['$job', '$instance']} // Example variable names
|
|
18
|
+
apiPrefix='/api/n9e/proxy/1/api/v1'
|
|
19
|
+
request={(resource, options) => {
|
|
20
|
+
const params = options?.body?.toString();
|
|
21
|
+
const search = params ? `?${params}` : '';
|
|
22
|
+
return fetch(resource + search, {
|
|
23
|
+
method: 'Get',
|
|
24
|
+
headers: new Headers({
|
|
25
|
+
Authorization: `Bearer ${localStorage.getItem('access_token') || ''}`,
|
|
26
|
+
}),
|
|
27
|
+
});
|
|
28
|
+
}}
|
|
29
|
+
value={value}
|
|
30
|
+
enableAutocomplete={true} // Enable completion
|
|
31
|
+
durationVariablesCompletion={false} // Disable duration variables completion. eg. $__interval, $__range, $__rate_interval
|
|
32
|
+
interpolateString={(query) => {
|
|
33
|
+
// Interpolate variables in the query string
|
|
34
|
+
return query.replace(/\$__interval/g, '10s');
|
|
35
|
+
}}
|
|
36
|
+
onShiftEnter={(value) => {
|
|
37
|
+
console.log('Shift+Enter pressed, current value:', value);
|
|
38
|
+
}}
|
|
39
|
+
onBlur={(value) => {
|
|
40
|
+
console.log('Editor lost focus, current value:', value);
|
|
41
|
+
}}
|
|
42
|
+
onChange={(value) => {
|
|
43
|
+
console.log('Value changed:', value);
|
|
44
|
+
setValue(value);
|
|
45
|
+
}}
|
|
46
|
+
/>
|
|
47
|
+
<div>
|
|
48
|
+
<a
|
|
49
|
+
onClick={() => {
|
|
50
|
+
if (promQLMonacoEditorRef.current) {
|
|
51
|
+
const editor = promQLMonacoEditorRef.current.getEditor();
|
|
52
|
+
editor.trigger('keyboard', 'type', { text: 'Insert Text' });
|
|
53
|
+
editor.focus();
|
|
54
|
+
}
|
|
55
|
+
}}
|
|
56
|
+
>
|
|
57
|
+
Click to insert text after the cursor
|
|
58
|
+
</a>
|
|
59
|
+
</div>
|
|
60
|
+
</div>
|
|
38
61
|
);
|
|
39
62
|
};
|
|
40
63
|
|
|
@@ -1959,7 +1959,6 @@ function isErrorBoundary(boundary) {
|
|
|
1959
1959
|
|
|
1960
1960
|
var _excluded = ["error"];
|
|
1961
1961
|
var PROMQL_LANG_ID = monacoPromql.promLanguageDefinition.id;
|
|
1962
|
-
var EDITOR_HEIGHT_OFFSET = 2;
|
|
1963
1962
|
var SIZE_MAP = {
|
|
1964
1963
|
small: {
|
|
1965
1964
|
className: 'ant-input-sm',
|
|
@@ -1978,8 +1977,8 @@ var SIZE_MAP = {
|
|
|
1978
1977
|
}
|
|
1979
1978
|
};
|
|
1980
1979
|
var themeMap = {
|
|
1981
|
-
light: '
|
|
1982
|
-
dark: '
|
|
1980
|
+
light: 'light',
|
|
1981
|
+
dark: 'n9e-dark'
|
|
1983
1982
|
};
|
|
1984
1983
|
var getStyles = function getStyles(placeholder) {
|
|
1985
1984
|
return {
|
|
@@ -1991,7 +1990,7 @@ var getStyles = function getStyles(placeholder) {
|
|
|
1991
1990
|
})
|
|
1992
1991
|
};
|
|
1993
1992
|
};
|
|
1994
|
-
function PromQLEditor(props) {
|
|
1993
|
+
var index = /*#__PURE__*/React.forwardRef(function PromQLEditor(props, ref) {
|
|
1995
1994
|
var id = uuid.v4();
|
|
1996
1995
|
var _props$size = props.size,
|
|
1997
1996
|
size = _props$size === void 0 ? 'middle' : _props$size,
|
|
@@ -2009,27 +2008,19 @@ function PromQLEditor(props) {
|
|
|
2009
2008
|
var autocompleteDisposeFun = React.useRef(null);
|
|
2010
2009
|
var containerRef = React.useRef(null);
|
|
2011
2010
|
var dataProviderRef = React.useRef(null);
|
|
2011
|
+
var editorRef = React.useRef(null);
|
|
2012
2012
|
var styles = getStyles(placeholder);
|
|
2013
|
-
React.useEffect(function () {
|
|
2014
|
-
// 注册 PromQL 语言
|
|
2015
|
-
var aliases = monacoPromql.promLanguageDefinition.aliases,
|
|
2016
|
-
extensions = monacoPromql.promLanguageDefinition.extensions,
|
|
2017
|
-
mimetypes = monacoPromql.promLanguageDefinition.mimetypes;
|
|
2018
|
-
monaco.languages.register({
|
|
2019
|
-
id: PROMQL_LANG_ID,
|
|
2020
|
-
aliases: aliases,
|
|
2021
|
-
extensions: extensions,
|
|
2022
|
-
mimetypes: mimetypes
|
|
2023
|
-
});
|
|
2024
|
-
// 设置语法高亮
|
|
2025
|
-
monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language);
|
|
2026
|
-
// 设置语言配置
|
|
2027
|
-
monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration);
|
|
2028
|
-
return function () {
|
|
2029
|
-
autocompleteDisposeFun.current == null || autocompleteDisposeFun.current();
|
|
2030
|
-
};
|
|
2031
|
-
}, []);
|
|
2032
2013
|
var handleEditorDidMount = function handleEditorDidMount(editor) {
|
|
2014
|
+
editorRef.current = editor;
|
|
2015
|
+
monaco.editor.defineTheme('n9e-dark', {
|
|
2016
|
+
base: 'vs-dark',
|
|
2017
|
+
inherit: true,
|
|
2018
|
+
rules: [],
|
|
2019
|
+
colors: {
|
|
2020
|
+
'editor.background': '#00000000',
|
|
2021
|
+
focusBorder: '#00000000'
|
|
2022
|
+
}
|
|
2023
|
+
});
|
|
2033
2024
|
var isEditorFocused = editor.createContextKey('isEditorFocused' + id, false);
|
|
2034
2025
|
// we setup on-blur
|
|
2035
2026
|
editor.onDidBlurEditorWidget(function () {
|
|
@@ -2072,7 +2063,7 @@ function PromQLEditor(props) {
|
|
|
2072
2063
|
var containerDiv = containerRef.current;
|
|
2073
2064
|
if (containerDiv !== null) {
|
|
2074
2065
|
var pixelHeight = editor.getContentHeight();
|
|
2075
|
-
containerDiv.style.height = pixelHeight +
|
|
2066
|
+
containerDiv.style.height = pixelHeight + "px";
|
|
2076
2067
|
containerDiv.style.width = '100%';
|
|
2077
2068
|
var pixelWidth = containerDiv.clientWidth;
|
|
2078
2069
|
editor.layout({
|
|
@@ -2135,6 +2126,32 @@ function PromQLEditor(props) {
|
|
|
2135
2126
|
dataProvider.setVariablesNames(variablesNames || []);
|
|
2136
2127
|
}
|
|
2137
2128
|
}, [JSON.stringify(variablesNames)]);
|
|
2129
|
+
React.useEffect(function () {
|
|
2130
|
+
// 注册 PromQL 语言
|
|
2131
|
+
var aliases = monacoPromql.promLanguageDefinition.aliases,
|
|
2132
|
+
extensions = monacoPromql.promLanguageDefinition.extensions,
|
|
2133
|
+
mimetypes = monacoPromql.promLanguageDefinition.mimetypes;
|
|
2134
|
+
monaco.languages.register({
|
|
2135
|
+
id: PROMQL_LANG_ID,
|
|
2136
|
+
aliases: aliases,
|
|
2137
|
+
extensions: extensions,
|
|
2138
|
+
mimetypes: mimetypes
|
|
2139
|
+
});
|
|
2140
|
+
// 设置语法高亮
|
|
2141
|
+
monaco.languages.setMonarchTokensProvider(PROMQL_LANG_ID, language);
|
|
2142
|
+
// 设置语言配置
|
|
2143
|
+
monaco.languages.setLanguageConfiguration(PROMQL_LANG_ID, languageConfiguration);
|
|
2144
|
+
return function () {
|
|
2145
|
+
autocompleteDisposeFun.current == null || autocompleteDisposeFun.current();
|
|
2146
|
+
};
|
|
2147
|
+
}, []);
|
|
2148
|
+
React.useImperativeHandle(ref, function () {
|
|
2149
|
+
return {
|
|
2150
|
+
getEditor: function getEditor() {
|
|
2151
|
+
return editorRef.current;
|
|
2152
|
+
}
|
|
2153
|
+
};
|
|
2154
|
+
});
|
|
2138
2155
|
return React__default.createElement("div", {
|
|
2139
2156
|
className: 'ant-input' + (size ? " " + SIZE_MAP[size].className : '')
|
|
2140
2157
|
}, React__default.createElement("div", {
|
|
@@ -2180,7 +2197,7 @@ function PromQLEditor(props) {
|
|
|
2180
2197
|
wordWrap: 'on'
|
|
2181
2198
|
}
|
|
2182
2199
|
})));
|
|
2183
|
-
}
|
|
2200
|
+
});
|
|
2184
2201
|
|
|
2185
|
-
exports.PromQLMonacoEditor =
|
|
2202
|
+
exports.PromQLMonacoEditor = index;
|
|
2186
2203
|
//# sourceMappingURL=monaco-editor.cjs.development.js.map
|