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 all 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
74 changes: 64 additions & 10 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 @@ -88,10 +89,21 @@ const PluginDetail: React.FC<Props> = ({
onChange = () => {},
}) => {
const { formatMessage } = useIntl();
enum codeMirrorModeList {
Json = 'Json',
Yaml = 'Yaml',
}
const [form] = Form.useForm();
const ref = useRef<any>(null);
const data = initialData[name] || {};
const pluginType = pluginList.find((item) => item.name === name)?.type;
const [codeMirrorMode, setCodeMirrorMode] = useState<PluginComponent.CodeMirrorMode>(
codeMirrorModeList.Json,
);
const modeOptions = [
{ label: codeMirrorModeList.Json, value: codeMirrorModeList.Json },
{ label: codeMirrorModeList.Yaml, value: codeMirrorModeList.Yaml },
];

useEffect(() => {
form.setFieldsValue({
Expand Down Expand Up @@ -144,7 +156,41 @@ const PluginDetail: React.FC<Props> = ({
});
});
};
const handleModeChange = (value: PluginComponent.CodeMirrorMode) => {
switch (value) {
case codeMirrorModeList.Json: {
const { data: yamlData , error } = yaml2json(ref.current.editor.getValue(), true);

if (error) {
notification.error({
message: 'Invalid Yaml data',
});
return;
}
ref.current.editor.setValue(
js_beautify(yamlData, {
indent_size: 2,
}),
);
break;
}
case codeMirrorModeList.Yaml: {
const { data: jsonData, error } = json2yaml(ref.current.editor.getValue());

if (error) {
notification.error({
message: 'Invalid Json data',
});
return;
}
ref.current.editor.setValue(jsonData);
break;
}
default:
break;
}
setCodeMirrorMode(value);
};
const formatCodes = () => {
try {
if (ref.current) {
Expand Down Expand Up @@ -200,7 +246,10 @@ const PluginDetail: React.FC<Props> = ({
type="primary"
onClick={() => {
try {
const editorData = JSON.parse(ref.current?.editor.getValue());
const editorData =
codeMirrorMode === codeMirrorModeList.Json
? JSON.parse(ref.current?.editor.getValue())
: yaml2json(ref.current?.editor.getValue(), false).data;
validateData(name, editorData).then((value) => {
onChange({ formData: form.getFieldsValue(), codemirrorData: value });
});
Expand Down Expand Up @@ -258,20 +307,25 @@ const PluginDetail: React.FC<Props> = ({
icon={<LinkOutlined />}
onClick={() => {
if (name.startsWith('serverless')) {
window.open(
'https://apisix.apache.org/docs/apisix/plugins/serverless',
);
window.open('https://apisix.apache.org/docs/apisix/plugins/serverless');
} else {
window.open(
`https://apisix.apache.org/docs/apisix/plugins/${name}`,
);
window.open(`https://apisix.apache.org/docs/apisix/plugins/${name}`);
}
}}
key={1}
>
Document
</Button>,
<Button type="primary" onClick={formatCodes} key={2}>
<Select
defaultValue={codeMirrorModeList.Json}
value={codeMirrorMode}
options={modeOptions}
onChange={(value: PluginComponent.CodeMirrorMode) => {
handleModeChange(value);
}}
data-cy='code-mirror-mode'
></Select>,
<Button type="primary" onClick={formatCodes} key={3}>
Format
</Button>,
]}
Expand All @@ -286,7 +340,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;
}
42 changes: 42 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 @@ -131,6 +133,46 @@ 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): { data: string; error: boolean } => {
try {
return {
data: yaml.dump(JSON.parse(jsonStr)),
error: false,
};
} catch (err) {
return {
data: '',
error: true,
};
}
};

/**
* Transform yaml string to json
* @param yamlStr
* @param returnString true for json string , false for json object
*/
export const yaml2json = (
yamlStr: string,
returnString: boolean,
): { data: any; error: boolean } => {
try {
return {
data: returnString ? JSON.stringify(YAML.parse(yamlStr)) : YAML.parse(yamlStr),
error: false,
};
} catch (err) {
return {
data: '',
error: true,
};
}
};

export const transformLableValueToKeyValue = (data: string[]) => {
return (data || []).map((item) => {
const index = item.indexOf(':');
Expand Down