Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz committed Aug 16, 2024
1 parent ba0e5dd commit 9a8cc79
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useMemo, useState } from 'react';
import { InputBase, ListSubheader, MenuItem, Select } from '@mui/material';
import SearchIcon from '@mui/icons-material/Search';
import styled from 'styled-components';
import styled from '@emotion/styled';
import Maki from '../../../utils/Maki';
import { TranslatedPreset } from './FeatureTypeSelect';

Expand Down Expand Up @@ -43,8 +43,11 @@ const renderOption = (option) =>
</>
);

export const ComboSearchBox = ({ value, setValue, options }:{

export const ComboSearchBox = ({
value,
setValue,
options,
}: {
options: TranslatedPreset[];
}) => {
const [searchText, setSearchText] = useState('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const EditContent = () => (
<FeatureTypeSelect />
<FieldsEditor />
<hr />
<MajorKeysEditor />
{/*<MajorKeysEditor />*/}
<OptionsEditor />
<ContributionInfoBox />
<CommentField />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useEffect, useState } from 'react';
import { Box, Typography } from '@mui/material';
import styled from 'styled-components';
import styled from '@emotion/styled';
import { getPoiClass } from '../../../../services/getPoiClass';
import { allPresets } from '../../../../services/tagging/data';
import {
Expand Down Expand Up @@ -90,7 +90,7 @@ export const FeatureTypeSelect = () => {
),
);
})();
}, []);
}, [feature.schema?.preset.presetKey, setPreset]);

if (options.length === 0) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
InputLabel,
MenuItem,
Select,
TextField, Typography,
TextField,
Typography,
} from '@mui/material';
import React, { useState } from 'react';
import { FeatureTags } from '../../../../services/types';
Expand All @@ -25,15 +26,24 @@ const getActiveFields = (fields: Field[], tags: FeatureTags) => {
return activeFields;
};
export const FieldsEditor = () => {
const { preset, tags: {tags, setTag} } = useEditContext();
const {
preset,
tags: { tags, setTag },
} = useEditContext();

// TODO remove condition
if (!preset) {
return null;
}


const { fields, moreFields, universalFields } = getFields(preset);
const [activeFields, setActiveFields] = useState(getActiveFields(fields, tags))
const inactiveFields = fields.filter((field) => !activeFields.includes(field));
const [activeFields, setActiveFields] = useState(
getActiveFields(fields, tags),
);
const inactiveFields = fields.filter(
(field) => !activeFields.includes(field),
);

return (
<div>
Expand Down Expand Up @@ -93,7 +103,7 @@ export const FieldsEditor = () => {
return (
<div key={key}>
<TextField
label={label}
label={label + ' – ' + field.fieldKey}
value={tags[k]}
InputLabelProps={{ shrink: true }}
variant="outlined"
Expand All @@ -119,7 +129,6 @@ export const FieldsEditor = () => {
{t('editdialog.add_major_tag')}:
</Typography>
{inactiveFields.map((field) => {

const fieldTranslation = getFieldTranslation(field);
const label = fieldTranslation?.label ?? `[${key}]`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const getData = (numberOfWikimediaItems) => {
};
};

export const MajorKeysEditor = () => {
export const MajorKeysEditor = () => {
const { focusTag } = useEditDialogContext();
const {
tags: { tags, setTag },
Expand Down
2 changes: 1 addition & 1 deletion src/services/tagging/data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fieldsJson from '@openstreetmap/id-tagging-schema/dist/fields.json';
import presetsJson from '@openstreetmap/id-tagging-schema/dist/presets.json';
import { Fields } from './types/Fields';
import { Fields, RawFields } from './types/Fields';
import { Presets } from './types/Presets';
import { publishDbgObject } from '../../utils';
import { ourFields, ourPresets } from './ourPresets';
Expand Down
38 changes: 25 additions & 13 deletions src/services/tagging/fields.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
// links like {shop}, are recursively resolved to their fields
import { Preset } from './types/Presets';
import { allFields, allPresets } from './data';
import { deduplicate } from './utils';
import { Field } from './types/Fields';
import { getFieldTranslation } from './translations';
import { not } from '../../utils';

type FieldType = 'fields' | 'moreFields';

const resolveLinks = (fieldKeys: string[], fieldType: FieldType): string[] =>
// links like {shop}, are recursively resolved to their fields
const resolveLinks = (fieldKeys: string[], type: FieldType): string[] =>
fieldKeys.flatMap((key) => {
if (key.match(/^{.*}$/)) {
const presetKey = key.substr(1, key.length - 2);
const linkedFields = allPresets[presetKey][fieldType];
return resolveLinks(linkedFields, fieldType);
const linkedFields = allPresets[presetKey][type];
return resolveLinks(linkedFields, type);
}
return key;
});

const resolveParents = (preset: Preset, fieldType: FieldType): string[] => {
const resolveParents = (preset: Preset, type: FieldType): string[] => {
const parts = preset.presetKey.split('/');

if (parts.length > 1) {
const parentKey = parts.slice(0, parts.length - 1).join('/');
const parentPreset = allPresets[parentKey];
if (parentPreset) {
return [
...resolveParents(parentPreset, fieldType),
...(preset[fieldType] ?? []),
];
return [...resolveParents(parentPreset, type), ...(preset[type] ?? [])];
}
}

return preset[fieldType] ?? [];
return preset[type] ?? [];
};

const resolveFieldKeys = (preset: Preset, fieldType: FieldType) =>
Expand Down Expand Up @@ -58,15 +57,28 @@ export const getFieldKeys = (preset: Preset): string[] => {
return deduplicate(allFieldKeys);
};

const translateFields = (fields: Field[]): Field[] =>
fields.map((field) => {
const fieldTranslation = getFieldTranslation(field);
return {
...field,
fieldTranslation: { label: `[${field.fieldKey}]`, ...fieldTranslation },
};
});

const eatPreset = (preset: Preset, fields: Field[]) => {
return fields.filter((field) => !preset.tags[field.key]);
};

export const getFields = (preset: Preset) => {
const fields = resolveFields(preset, 'fields');
const moreFields = resolveFields(preset, 'moreFields');
const universalFields = getUniversalFields();

return {
fields,
moreFields,
universalFields,
fields: eatPreset(preset, translateFields(fields)),
moreFields: translateFields(moreFields),
universalFields: translateFields(universalFields),
};
};

Expand Down
6 changes: 0 additions & 6 deletions src/services/tagging/idTaggingScheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ const getUiField = (
feature: Feature,
key: string,
): UiField => {
// TODO this should be removed now the parsing works ok (+run tests)
if (field.type === 'typeCombo') {
keysTodo.remove(field.key); // ignores eg. railway=tram_stop on public_transport=stop_position
return undefined;
}

const value = feature.tags[key];

const keysInField = deduplicate([
Expand Down
3 changes: 2 additions & 1 deletion src/services/tagging/ourPresets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RawPresets } from './types/Presets';
import type { RawFields } from './types/Fields';

export const ourFields = {
export const ourFields: RawFields = {
'climbing/summit_log': {
key: 'climbing:summit_log',
type: 'check',
Expand Down
11 changes: 6 additions & 5 deletions src/services/tagging/types/Fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type FieldType =
export type Field = {
// added by osmapp (not in schema)
fieldKey: string;
//TODO ?? fieldTranslation?: FieldTranslation;
fieldTranslation?: FieldTranslation;

/**
* Tag key whose value is to be displayed
Expand Down Expand Up @@ -83,10 +83,7 @@ export type Field = {
/**
* If specified, only show the field for these kinds of geometry
*/
geometry?: [
'point' | 'vertex' | 'line' | 'area' | 'relation', // minimal one entry
...('point' | 'vertex' | 'line' | 'area' | 'relation')[],
];
geometry?: ('point' | 'vertex' | 'line' | 'area' | 'relation')[]; // minimal one entry
/**
* The default value for this field
*/
Expand Down Expand Up @@ -226,3 +223,7 @@ export type Field = {
export type Fields = {
[fieldKey: string]: Field;
};

export type RawFields = {
[fieldKey: string]: Omit<Field, 'fieldKey'>;
};
8 changes: 4 additions & 4 deletions src/services/tagging/types/Presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ export type RawPresets = {

export type FieldTranslation = {
label: string;
placeholder: string;
terms: string;
options: {
placeholder?: string;
terms?: string;
options?: {
[key: string]: { title: string; description: string };
};
types: {
types?: {
[key: string]: string;
};
};
Expand Down

0 comments on commit 9a8cc79

Please sign in to comment.