Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support yaml to config plugin in plugin config page #1490

Merged
merged 10 commits into from
Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
"@mrblenny/react-flow-chart": "^0.0.14",
"@rjsf/antd": "2.2.0",
"@rjsf/core": "2.2.0",
"@uiw/react-codemirror": "^3.0.5",
"@types/js-yaml": "^4.0.0",
"@uiw/react-codemirror": "^3.0.5",
"ajv": "^7.0.3",
"ajv-formats": "^1.5.1",
"antd": "^4.4.0",
Expand All @@ -82,7 +82,8 @@
"umi-request": "^1.0.8",
"url-regex-safe": "^1.0.2",
"use-merge-value": "^1.0.1",
"uuid": "7.0.3"
"uuid": "7.0.3",
"yaml": "^1.10.0"
},
"devDependencies": {
"@ant-design/pro-cli": "^2.0.2",
Expand Down
38 changes: 33 additions & 5 deletions web/src/components/Plugin/PluginDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React, { useEffect, useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import {
Button,
notification,
Expand All @@ -37,6 +37,7 @@ import type { DefinedError } from 'ajv';
import addFormats from 'ajv-formats';

import { fetchSchema } from './service';
import { json2yaml, yaml2json } from '../../helpers';

type Props = {
name: string;
Expand Down Expand Up @@ -92,6 +93,11 @@ const PluginDetail: React.FC<Props> = ({
const ref = useRef<any>(null);
const data = initialData[name] || {};
const pluginType = pluginList.find((item) => item.name === name)?.type;
const [codeMirrorMode, setCodeMirrorMode] = useState<PluginComponent.CodeMirrorMode>('json');
const modeOptions = [
{ label: 'Json', value: 'json' },
{ label: 'Yaml', value: 'yaml' },
];

useEffect(() => {
form.setFieldsValue({
Expand Down Expand Up @@ -144,7 +150,24 @@ const PluginDetail: React.FC<Props> = ({
});
});
};

const handleModeChange = (value: PluginComponent.CodeMirrorMode) => {
switch (value){
case 'javascript':
ref.current.editor.setValue(
js_beautify(yaml2json(ref.current.editor.getValue(), true), {
indent_size: 2,
}),
);
break;
case 'yaml':
ref.current.editor.setValue(
json2yaml(ref.current.editor.getValue())
);
break;
default: break;
}
setCodeMirrorMode(value)
}
const formatCodes = () => {
try {
if (ref.current) {
Expand Down Expand Up @@ -200,7 +223,9 @@ const PluginDetail: React.FC<Props> = ({
type="primary"
onClick={() => {
try {
const editorData = JSON.parse(ref.current?.editor.getValue());
const editorData = codeMirrorMode === 'javascript' ?
JSON.parse(ref.current?.editor.getValue()) :
yaml2json(ref.current?.editor.getValue(), false);
validateData(name, editorData).then((value) => {
onChange({ formData: form.getFieldsValue(), codemirrorData: value });
});
Expand Down Expand Up @@ -271,7 +296,10 @@ const PluginDetail: React.FC<Props> = ({
>
Document
</Button>,
<Button type="primary" onClick={formatCodes} key={2}>
<Select defaultValue="javascript" options={modeOptions} onChange={(value: PluginComponent.CodeMirrorMode) => {
handleModeChange(value)
}}></Select>,
<Button type="primary" onClick={formatCodes} key={3}>
Format
</Button>,
]}
Expand All @@ -286,7 +314,7 @@ const PluginDetail: React.FC<Props> = ({
}}
value={JSON.stringify(data, null, 2)}
options={{
mode: 'json-ld',
mode: codeMirrorMode,
readOnly: readonly ? 'nocursor' : '',
lineWrapping: true,
lineNumbers: true,
Expand Down
2 changes: 2 additions & 0 deletions web/src/components/Plugin/typing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ declare namespace PluginComponent {
};

type ReferPage = '' | 'route' | 'consumer' | 'service' | 'plugin';

type CodeMirrorMode = 'json' | 'yaml';
}
4 changes: 4 additions & 0 deletions web/src/global.less
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@ ol {
vertical-align: -0.15em;
fill: currentColor;
}

.ant-drawer-body > .ant-page-header {
padding: 16px 12px;
}
19 changes: 19 additions & 0 deletions web/src/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { notification } from 'antd';
import type { MenuDataItem } from '@ant-design/pro-layout';
import { history } from 'umi';
import moment from 'moment';
import YAML from 'yaml';
import yaml from 'js-yaml';

import { codeMessage } from './constants';
import IconFont from './components/IconFont';
Expand Down Expand Up @@ -130,3 +132,20 @@ export const timestampToLocaleString = (timestamp: number) => {

return moment.unix(timestamp).format('YYYY-MM-DD HH:mm:ss');
};

/**
* Transform json string to yaml string
* @param jsonStr
*/
export const json2yaml = (jsonStr: string): string => {
return yaml.dump(JSON.parse(jsonStr))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if jsonStr's value is '', an error will occur

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot, already fixed.

use try...catch to handle the exception

}

/**
* Transform yaml string to json
* @param yamlStr
* @param returnString true for json string , false for json object
*/
export const yaml2json = (yamlStr: string, returnString: boolean): string | Object => {
return returnString? JSON.stringify(YAML.parse(yamlStr)): YAML.parse(yamlStr);
}