From 9a8cc79e1772930ce886f08e9ea0a3f7147ed177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Zbytovsk=C3=BD?= Date: Tue, 6 Aug 2024 15:26:37 +0200 Subject: [PATCH] wip --- .../EditDialog/EditContent/ComboSearchBox.tsx | 9 +++-- .../EditDialog/EditContent/EditContent.tsx | 2 +- .../EditContent/FeatureTypeSelect.tsx | 4 +- .../EditDialog/EditContent/FieldsEditor.tsx | 21 +++++++--- .../EditContent/MajorKeysEditor.tsx | 2 +- src/services/tagging/data.ts | 2 +- src/services/tagging/fields.ts | 38 ++++++++++++------- src/services/tagging/idTaggingScheme.ts | 6 --- src/services/tagging/ourPresets.ts | 3 +- src/services/tagging/types/Fields.ts | 11 +++--- src/services/tagging/types/Presets.ts | 8 ++-- 11 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/components/FeaturePanel/EditDialog/EditContent/ComboSearchBox.tsx b/src/components/FeaturePanel/EditDialog/EditContent/ComboSearchBox.tsx index e2e1937af..877c70fdc 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/ComboSearchBox.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/ComboSearchBox.tsx @@ -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'; @@ -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(''); diff --git a/src/components/FeaturePanel/EditDialog/EditContent/EditContent.tsx b/src/components/FeaturePanel/EditDialog/EditContent/EditContent.tsx index fb46c821a..61c9f071b 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/EditContent.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/EditContent.tsx @@ -17,7 +17,7 @@ export const EditContent = () => (
- + {/**/} diff --git a/src/components/FeaturePanel/EditDialog/EditContent/FeatureTypeSelect.tsx b/src/components/FeaturePanel/EditDialog/EditContent/FeatureTypeSelect.tsx index 1035abe85..638a605e0 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/FeatureTypeSelect.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/FeatureTypeSelect.tsx @@ -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 { @@ -90,7 +90,7 @@ export const FeatureTypeSelect = () => { ), ); })(); - }, []); + }, [feature.schema?.preset.presetKey, setPreset]); if (options.length === 0) { return null; diff --git a/src/components/FeaturePanel/EditDialog/EditContent/FieldsEditor.tsx b/src/components/FeaturePanel/EditDialog/EditContent/FieldsEditor.tsx index 18082ced2..727115b71 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/FieldsEditor.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/FieldsEditor.tsx @@ -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'; @@ -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 (
@@ -93,7 +103,7 @@ export const FieldsEditor = () => { return (
{ {t('editdialog.add_major_tag')}: {inactiveFields.map((field) => { - const fieldTranslation = getFieldTranslation(field); const label = fieldTranslation?.label ?? `[${key}]`; diff --git a/src/components/FeaturePanel/EditDialog/EditContent/MajorKeysEditor.tsx b/src/components/FeaturePanel/EditDialog/EditContent/MajorKeysEditor.tsx index a8fadbac9..0f471c37f 100644 --- a/src/components/FeaturePanel/EditDialog/EditContent/MajorKeysEditor.tsx +++ b/src/components/FeaturePanel/EditDialog/EditContent/MajorKeysEditor.tsx @@ -33,7 +33,7 @@ const getData = (numberOfWikimediaItems) => { }; }; -export const MajorKeysEditor = () => { +export const MajorKeysEditor = () => { const { focusTag } = useEditDialogContext(); const { tags: { tags, setTag }, diff --git a/src/services/tagging/data.ts b/src/services/tagging/data.ts index fc9347149..228f4f00b 100644 --- a/src/services/tagging/data.ts +++ b/src/services/tagging/data.ts @@ -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'; diff --git a/src/services/tagging/fields.ts b/src/services/tagging/fields.ts index b84c11ff2..91aa92175 100644 --- a/src/services/tagging/fields.ts +++ b/src/services/tagging/fields.ts @@ -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) => @@ -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), }; }; diff --git a/src/services/tagging/idTaggingScheme.ts b/src/services/tagging/idTaggingScheme.ts index 37f1f3dff..111d4e4ce 100644 --- a/src/services/tagging/idTaggingScheme.ts +++ b/src/services/tagging/idTaggingScheme.ts @@ -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([ diff --git a/src/services/tagging/ourPresets.ts b/src/services/tagging/ourPresets.ts index 86029d79f..5d5345ec1 100644 --- a/src/services/tagging/ourPresets.ts +++ b/src/services/tagging/ourPresets.ts @@ -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', diff --git a/src/services/tagging/types/Fields.ts b/src/services/tagging/types/Fields.ts index cdeaa877e..7cc4adeb0 100644 --- a/src/services/tagging/types/Fields.ts +++ b/src/services/tagging/types/Fields.ts @@ -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 @@ -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 */ @@ -226,3 +223,7 @@ export type Field = { export type Fields = { [fieldKey: string]: Field; }; + +export type RawFields = { + [fieldKey: string]: Omit; +}; diff --git a/src/services/tagging/types/Presets.ts b/src/services/tagging/types/Presets.ts index 4d2b488a4..52992cd68 100644 --- a/src/services/tagging/types/Presets.ts +++ b/src/services/tagging/types/Presets.ts @@ -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; }; };