mirror of
https://github.com/labring/FastGPT.git
synced 2025-07-22 12:20:34 +00:00
fix monaco editor default value (#4793)
* fix monaco editor default value * fix
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useCallback, useRef, useState } from 'react';
|
import React, { useEffect, useCallback, useRef, useState, useMemo } from 'react';
|
||||||
import Editor, { type Monaco, loader, useMonaco } from '@monaco-editor/react';
|
import Editor, { type Monaco, loader, useMonaco } from '@monaco-editor/react';
|
||||||
import { Box, type BoxProps } from '@chakra-ui/react';
|
import { Box, type BoxProps } from '@chakra-ui/react';
|
||||||
import MyIcon from '../../Icon';
|
import MyIcon from '../../Icon';
|
||||||
@@ -169,10 +169,27 @@ const JSONEditor = ({
|
|||||||
document.addEventListener('mouseup', handleMouseUp);
|
document.addEventListener('mouseup', handleMouseUp);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const formatedValue = useMemo(() => {
|
||||||
|
if (typeof value === 'string') {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (value === undefined || value === null) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.stringify(value, null, 2);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('JSON stringify error:', error);
|
||||||
|
return String(value) || '';
|
||||||
|
}
|
||||||
|
}, [value]);
|
||||||
|
|
||||||
const onBlur = useCallback(() => {
|
const onBlur = useCallback(() => {
|
||||||
if (!value) return;
|
if (!formatedValue) return;
|
||||||
// replace {{xx}} to true
|
// replace {{xx}} to true
|
||||||
const replaceValue = value?.replace(/{{(.*?)}}/g, 'true');
|
const replaceValue = formatedValue?.replace(/{{(.*?)}}/g, 'true');
|
||||||
try {
|
try {
|
||||||
JSON.parse(replaceValue);
|
JSON.parse(replaceValue);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -181,7 +198,8 @@ const JSONEditor = ({
|
|||||||
title: t('common:json_parse_error')
|
title: t('common:json_parse_error')
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [value]);
|
}, [formatedValue, toast, t]);
|
||||||
|
|
||||||
const beforeMount = useCallback((monaco: Monaco) => {
|
const beforeMount = useCallback((monaco: Monaco) => {
|
||||||
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
|
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
|
||||||
validate: false,
|
validate: false,
|
||||||
@@ -241,7 +259,7 @@ const JSONEditor = ({
|
|||||||
theme="JSONEditorTheme"
|
theme="JSONEditorTheme"
|
||||||
beforeMount={beforeMount}
|
beforeMount={beforeMount}
|
||||||
defaultValue={defaultValue}
|
defaultValue={defaultValue}
|
||||||
value={value}
|
value={formatedValue}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
onChange?.(e || '');
|
onChange?.(e || '');
|
||||||
if (!e) {
|
if (!e) {
|
||||||
|
@@ -151,7 +151,9 @@ const ChatItemContextProvider = ({
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
variablesForm.setValue('variables', newVariableValue);
|
Object.entries(newVariableValue).forEach(([key, value]) => {
|
||||||
|
variablesForm.setValue(`variables.${key}`, value);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
[variablesForm]
|
[variablesForm]
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user