Skip to content

Commit

Permalink
feat: init the JsonModal component
Browse files Browse the repository at this point in the history
  • Loading branch information
dnlkoch committed Jul 24, 2023
1 parent 0399cb5 commit 4841157
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 20 deletions.
27 changes: 21 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"not IE 11"
],
"dependencies": {
"@monaco-editor/react": "^4.5.1",
"@reduxjs/toolkit": "^1.9.5",
"@terrestris/base-util": "^1.0.1",
"@terrestris/mapfish-print-manager": "^10.0.0",
Expand Down
10 changes: 10 additions & 0 deletions src/bootstrap.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React from 'react';

import {
loader
} from '@monaco-editor/react';

import {
Alert,
ConfigProvider,
Expand Down Expand Up @@ -564,6 +568,12 @@ const matchRole = (role: string | RegExp, element: string): boolean => {

const renderApp = async () => {
try {
loader.config({
paths: {
vs: './vs'
}
});

const keycloak = await initKeycloak();

if (keycloak) {
Expand Down
25 changes: 25 additions & 0 deletions src/components/DisplayField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ import {
useTranslation
} from 'react-i18next';

import JsonModal from '../JsonModal';

export type ValueType = string | number | boolean | moment.Moment;

export type DisplayFieldProps = {
format?: string;
suffix?: string;
value?: ValueType | ValueType[];
label?: string;
};

export const DisplayField: React.FC<DisplayFieldProps> = ({
format = 'DD.MM.YYYY',
suffix,
value,
label,
...passThroughProps
}): JSX.Element => {

Expand Down Expand Up @@ -53,6 +57,27 @@ export const DisplayField: React.FC<DisplayFieldProps> = ({
displayText = value.join(', ');
}

const isJson = (val: ValueType | ValueType[]): val is string => {
let v = typeof val !== 'string' ? JSON.stringify(val) : val;

try {
v = JSON.parse(v);
} catch (e) {
return false;
}

return typeof v === 'object' && v !== null;
};

if (value && isJson(value)) {
return (
<JsonModal
value={value}
label={label}
/>
);
}

return (
<Typography.Text
className="displayfield"
Expand Down
5 changes: 5 additions & 0 deletions src/components/JsonModal/index.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.json-modal-button {
svg {
padding-right: 10px;
}
}
9 changes: 9 additions & 0 deletions src/components/JsonModal/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import JsonModal from '.';

describe('<JsonModal />', () => {

it('is defined', () => {
expect(JsonModal).toBeDefined();
});

});
109 changes: 109 additions & 0 deletions src/components/JsonModal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import React, {
useState
} from 'react';

import {
faBoxOpen
} from '@fortawesome/free-solid-svg-icons';
import {
FontAwesomeIcon
} from '@fortawesome/react-fontawesome';

import Editor from '@monaco-editor/react';

import {
Button,
Modal,
ModalProps
} from 'antd';

import {
useTranslation
} from 'react-i18next';

import './index.less';

export type ValueType = string | number | boolean | moment.Moment;

export type JsonModalProps = {
value?: string;
label?: string;
} & ModalProps ;

export const JsonModal: React.FC<JsonModalProps> = ({
value,
label,
...passThroughProps
}): JSX.Element => {

const [isOpen, setIsOpen] = useState(false);

const {
t
} = useTranslation();

const onButtonClick = () => {
setIsOpen(true);
};

const onCancel = () => {
setIsOpen(false);
};

if (!value) {
return <></>;
}

return (
<>
<Button
className='json-modal-button'
title={t('JsonModal.buttonTitle', {
propertyName: label
})}
onClick={onButtonClick}
icon={(
<FontAwesomeIcon
icon={faBoxOpen}
/>
)}
>
{t('JsonModal.buttonTitle', {
propertyName: label
})}
</Button>
<Modal
open={isOpen}
onCancel={onCancel}
width={800}
title={label}
footer={false}
{...passThroughProps}
>
<Editor
height="500px"
language="json"
value={JSON.stringify(JSON.parse(value), null, ' ')}
options={{
automaticLayout: true,
readOnly: true,
lineNumbers: 'off',
scrollBeyondLastLine: false,
minimap: {
enabled: false
},
scrollbar: {
useShadows: false
},
showFoldingControls: 'always',
selectionHighlight: false,
renderLineHighlight: 'none',
occurrencesHighlight: false
}}
/>
</Modal>
</>
);
};

export default JsonModal;
19 changes: 5 additions & 14 deletions src/components/ToolMenu/FeatureInfo/FeatureInfoForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,18 @@ export const FeatureInfoForm: React.FC<FeatureInfoFormProps> = ({
form.setFieldsValue(feature.getProperties());
}, [feature, form]);

const createFormItem = (fieldCfg: any) => {
let field: React.ReactNode;

const createReadOnlyComponent = (fieldConfig: any) => {
return (
<DisplayField
{...fieldConfig.fieldProps}
/>
);
};

field = createReadOnlyComponent(fieldCfg);

const createFormItem = (fieldCfg: PropertyFormItemReadConfig) => {
return (
<Form.Item
key={fieldCfg.propertyName}
name={fieldCfg.propertyName}
label={fieldCfg.displayName || fieldCfg.propertyName}
{...fieldCfg.fieldProps}
>
{field}
<DisplayField
label={fieldCfg.displayName || fieldCfg.propertyName}
{...fieldCfg.fieldProps}
/>
</Form.Item>
);
};
Expand Down
6 changes: 6 additions & 0 deletions src/i18n/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ export default {
PaginationToolbar: {
copyAsGeoJson: 'Als GeoJSON kopieren (inkl. Geometrie)',
copyAsObject: 'Als Objekt kopieren (nur angezeigte Werte)'
},
JsonModal: {
buttonTitle: 'Öffne {{propertyName}}'
}
}
},
Expand Down Expand Up @@ -430,6 +433,9 @@ export default {
PaginationToolbar: {
copyAsGeoJson: 'Copy as GeoJSON (incl. geometry)',
copyAsObject: 'Copy as object (displayed values only)'
},
JsonModal: {
buttonTitle: 'Show {{propertyName}}'
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ module.exports = {
patterns: [{
from: path.join(__dirname, 'resources', 'config', 'gis-client-config.js'),
to: '.'
}, {
from: './node_modules/monaco-editor/min/vs',
to: 'vs'
}]
}),
new webpack.DefinePlugin({
Expand Down

0 comments on commit 4841157

Please sign in to comment.