mirror of
https://github.com/maputnik/editor.git
synced 2026-12-06 00:34:22 +00:00
Closes #846. `react-aria-menubutton` was only used for the "Layer options" menu in the layer editor header. This PR builds that menu with `downshift`'s `useSelect`, which the project already uses for the autocomplete and sprite inputs, and removes `react-aria-menubutton` and its types from `package.json`. The menu is a new `LayerEditorMenu` component: - Same items, same `data-wd-key`s, and the button keeps the `skip-target-layer-editor` id, so the skip link still focuses it. - The menu closes after an action. The old menu stayed open (`closeOnSelection={false}`); it now uses `useSelect`'s default. - Arrow keys, Enter, Space and Escape work from the button. - In right-to-left languages the menu opens toward the inside of the layer editor. Its position now uses `inset-inline-end`, which replaces the separate `[dir="rtl"]` rule. One more change: the items already had a `disabled` flag for "Move layer up" on the first layer and "Move layer down" on the last one, but `react-aria-menubutton` never used it. `useSelect` supports it (`isItemDisabled`), so those two items are now dimmed and skipped. Screen readers now hear the button as a combobox with a listbox, the pattern `useSelect` follows, instead of a menu button. | Before | After | | --- | --- | |  |  | Both show the menu opened with the keyboard and the second item highlighted. The after image is on the first layer, so "Move layer up" is dimmed. **Performance:** no measurable impact. It's one small menu that only renders its items while open, and the bundle loses one dependency. ## Launch Checklist - [x] Briefly describe the changes in this PR. - [x] Link to related issues: #846. - [x] Include before/after visuals or gifs if this PR includes visual changes. - [x] Write tests for all new functionality: 5 e2e tests in `layer-editor.spec.ts` ("layer options menu"): keyboard selection, closing after a click, Escape, the disabled item (new `shouldBeDisabled` helper), and the menu staying inside the layer editor in Hebrew. The existing menu tests (delete and move via the menu, skip link) still pass. - [x] Add an entry to `CHANGELOG.md` under the `## main` section. Tested locally: `npm run lint` and `npm run lint-css` clean, `npx vitest run` (59 passed), full e2e with `npx playwright test` (231 passed). I also checked the menu by hand in English and Hebrew. Assisted-By: Claude Opus 5.5 (Claude Code). It helped write the component, the tests and this description. I've reviewed and tested the change and can answer questions about it.
390 lines
12 KiB
TypeScript
390 lines
12 KiB
TypeScript
import React, { type JSX } from "react";
|
|
import { Accordion } from "react-accessible-accordion";
|
|
import { IconContext } from "react-icons";
|
|
import { type BackgroundLayerSpecification, type LayerSpecification, type SourceSpecification } from "maplibre-gl";
|
|
import { v8 } from "@maplibre/maplibre-gl-style-spec";
|
|
|
|
import { FieldJson } from "./FieldJson";
|
|
import { FilterEditor } from "./FilterEditor";
|
|
import { PropertyGroup } from "./PropertyGroup";
|
|
import { LayerEditorGroup } from "./LayerEditorGroup";
|
|
import { LayerEditorMenu, type LayerEditorMenuItem } from "./LayerEditorMenu";
|
|
import { FieldType } from "./FieldType";
|
|
import { FieldId } from "./FieldId";
|
|
import { FieldMinZoom } from "./FieldMinZoom";
|
|
import { FieldMaxZoom } from "./FieldMaxZoom";
|
|
import { FieldComment } from "./FieldComment";
|
|
import { FieldSource } from "./FieldSource";
|
|
import { FieldSourceLayer } from "./FieldSourceLayer";
|
|
import { changeType, changeProperty } from "../libs/layer";
|
|
import { formatLayerId } from "../libs/format";
|
|
import { type WithTranslation, withTranslation } from "react-i18next";
|
|
import { type TFunction } from "i18next";
|
|
import { NON_SOURCE_LAYERS } from "../libs/non-source-layers";
|
|
import { type MappedError, type MappedLayerErrors, type OnMoveLayerCallback } from "../libs/definitions";
|
|
|
|
type MaputnikLayoutGroup = {
|
|
id: string;
|
|
title: string;
|
|
type: string;
|
|
fields: string[];
|
|
};
|
|
|
|
function getLayoutForSymbolType(t: TFunction): MaputnikLayoutGroup[] {
|
|
const groups: MaputnikLayoutGroup[] = [];
|
|
groups.push({
|
|
title: t("General layout properties"),
|
|
id: "General_layout_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["layout_symbol"]).filter(f => f.startsWith("symbol-"))
|
|
});
|
|
groups.push({
|
|
title: t("Text layout properties"),
|
|
id: "Text_layout_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["layout_symbol"]).filter(f => f.startsWith("text-"))
|
|
});
|
|
groups.push({
|
|
title: t("Icon layout properties"),
|
|
id: "Icon_layout_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["layout_symbol"]).filter(f => f.startsWith("icon-"))
|
|
});
|
|
groups.push({
|
|
title: t("Text paint properties"),
|
|
id: "Text_paint_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["paint_symbol"]).filter(f => f.startsWith("text-"))
|
|
});
|
|
groups.push({
|
|
title: t("Icon paint properties"),
|
|
id: "Icon_paint_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["paint_symbol"]).filter(f => f.startsWith("icon-"))
|
|
});
|
|
return groups;
|
|
}
|
|
|
|
function getLayoutForType(type: LayerSpecification["type"], t: TFunction): MaputnikLayoutGroup[] {
|
|
if (Object.keys(v8.layer.type.values).indexOf(type) < 0) {
|
|
return [];
|
|
}
|
|
if (type === "symbol") {
|
|
return getLayoutForSymbolType(t);
|
|
}
|
|
const groups: MaputnikLayoutGroup[] = [];
|
|
if (Object.keys(v8["paint_" + type as keyof typeof v8]).length > 0) {
|
|
groups.push({
|
|
title: t("Paint properties"),
|
|
id: "Paint_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["paint_" + type as keyof typeof v8]),
|
|
});
|
|
}
|
|
if (Object.keys(v8["layout_" + type as keyof typeof v8]).length > 0) {
|
|
groups.push({
|
|
title: t("Layout properties"),
|
|
id: "Layout_properties",
|
|
type: "properties",
|
|
fields: Object.keys(v8["layout_" + type as keyof typeof v8])
|
|
});
|
|
}
|
|
return groups;
|
|
}
|
|
|
|
function layoutGroups(layerType: LayerSpecification["type"], t: TFunction): { id: string, title: string, type: string, fields?: string[] }[] {
|
|
const layerGroup = {
|
|
id: "layer",
|
|
title: t("Layer"),
|
|
type: "layer"
|
|
};
|
|
const filterGroup = {
|
|
id: "filter",
|
|
title: t("Filter"),
|
|
type: "filter"
|
|
};
|
|
const editorGroup = {
|
|
id: "jsoneditor",
|
|
title: t("JSON Editor"),
|
|
type: "jsoneditor"
|
|
};
|
|
return [layerGroup, filterGroup]
|
|
.concat(getLayoutForType(layerType, t))
|
|
.concat([editorGroup]);
|
|
}
|
|
|
|
type LayerEditorInternalProps = {
|
|
layer: LayerSpecification
|
|
sources: { [key: string]: SourceSpecification & { layers: string[] } }
|
|
vectorLayers: { [key: string]: any }
|
|
spec: any
|
|
onLayerChanged(index: number, layer: LayerSpecification): void
|
|
onLayerIdChange(...args: unknown[]): unknown
|
|
onMoveLayer: OnMoveLayerCallback
|
|
onLayerDestroy(...args: unknown[]): unknown
|
|
onLayerCopy(...args: unknown[]): unknown
|
|
onLayerVisibilityToggle(...args: unknown[]): unknown
|
|
isFirstLayer?: boolean
|
|
isLastLayer?: boolean
|
|
layerIndex: number
|
|
errors?: MappedError[]
|
|
} & WithTranslation;
|
|
|
|
type LayerEditorState = {
|
|
editorGroups: { [keys: string]: boolean }
|
|
};
|
|
|
|
/** Layer editor supporting multiple types of layers. */
|
|
class LayerEditorInternal extends React.Component<LayerEditorInternalProps, LayerEditorState> {
|
|
static defaultProps = {
|
|
onLayerChanged: () => { },
|
|
onLayerIdChange: () => { },
|
|
onLayerDestroyed: () => { },
|
|
};
|
|
|
|
constructor(props: LayerEditorInternalProps) {
|
|
super(props);
|
|
|
|
const editorGroups: { [keys: string]: boolean } = {};
|
|
for (const group of layoutGroups(this.props.layer.type, props.t)) {
|
|
editorGroups[group.title] = true;
|
|
}
|
|
|
|
this.state = { editorGroups };
|
|
}
|
|
|
|
static getDerivedStateFromProps(props: Readonly<LayerEditorInternalProps>, state: LayerEditorState) {
|
|
const additionalGroups = { ...state.editorGroups };
|
|
|
|
for (const group of getLayoutForType(props.layer.type, props.t)) {
|
|
if (!(group.title in additionalGroups)) {
|
|
additionalGroups[group.title] = true;
|
|
}
|
|
}
|
|
|
|
return {
|
|
editorGroups: additionalGroups
|
|
};
|
|
}
|
|
|
|
|
|
changeProperty(group: keyof LayerSpecification | null, property: string, newValue: any) {
|
|
this.props.onLayerChanged(
|
|
this.props.layerIndex,
|
|
changeProperty(this.props.layer, group, property, newValue)
|
|
);
|
|
}
|
|
|
|
onGroupToggle(groupTitle: string, active: boolean) {
|
|
const changedActiveGroups = {
|
|
...this.state.editorGroups,
|
|
[groupTitle]: active,
|
|
};
|
|
this.setState({
|
|
editorGroups: changedActiveGroups
|
|
});
|
|
}
|
|
|
|
renderGroupType(type: string, fields?: string[]): JSX.Element {
|
|
let comment = "";
|
|
if (this.props.layer.metadata) {
|
|
comment = (this.props.layer.metadata as any)["maputnik:comment"];
|
|
}
|
|
const { errors, layerIndex } = this.props;
|
|
|
|
const errorData: MappedLayerErrors = {};
|
|
errors!.forEach(error => {
|
|
if (
|
|
error.parsed &&
|
|
error.parsed.type === "layer" &&
|
|
error.parsed.data.index == layerIndex
|
|
) {
|
|
errorData[error.parsed.data.key] = {
|
|
message: error.parsed.data.message
|
|
};
|
|
}
|
|
});
|
|
|
|
let sourceLayerIds;
|
|
const layer = this.props.layer as Exclude<LayerSpecification, BackgroundLayerSpecification>;
|
|
if (Object.prototype.hasOwnProperty.call(this.props.sources, layer.source)) {
|
|
sourceLayerIds = this.props.sources[layer.source].layers;
|
|
}
|
|
|
|
switch (type) {
|
|
case "layer": return <div>
|
|
<FieldId
|
|
value={this.props.layer.id}
|
|
wdKey="layer-editor.layer-id"
|
|
error={errorData.id}
|
|
commitOnBlur
|
|
onChange={newId => this.props.onLayerIdChange(this.props.layerIndex, this.props.layer.id, newId)}
|
|
/>
|
|
<FieldType
|
|
wdKey="layer-editor.layer-type"
|
|
error={errorData.type}
|
|
value={this.props.layer.type}
|
|
onChange={newType => this.props.onLayerChanged(
|
|
this.props.layerIndex,
|
|
changeType(this.props.layer, newType)
|
|
)}
|
|
/>
|
|
{this.props.layer.type !== "background" && <FieldSource
|
|
wdKey="layer-editor.layer-source"
|
|
error={errorData.source}
|
|
sourceIds={Object.keys(this.props.sources!)}
|
|
value={this.props.layer.source}
|
|
onChange={v => this.changeProperty(null, "source", v)}
|
|
/>
|
|
}
|
|
{!NON_SOURCE_LAYERS.includes(this.props.layer.type) &&
|
|
<FieldSourceLayer
|
|
error={errorData["source-layer"]}
|
|
sourceLayerIds={sourceLayerIds}
|
|
value={(this.props.layer as any)["source-layer"]}
|
|
onChange={v => this.changeProperty(null, "source-layer", v)}
|
|
/>
|
|
}
|
|
<FieldMinZoom
|
|
error={errorData.minzoom}
|
|
value={this.props.layer.minzoom}
|
|
onChange={v => this.changeProperty(null, "minzoom", v)}
|
|
/>
|
|
<FieldMaxZoom
|
|
error={errorData.maxzoom}
|
|
value={this.props.layer.maxzoom}
|
|
onChange={v => this.changeProperty(null, "maxzoom", v)}
|
|
/>
|
|
<FieldComment
|
|
error={errorData.comment}
|
|
value={comment}
|
|
onChange={v => this.changeProperty("metadata", "maputnik:comment", v == "" ? undefined : v)}
|
|
/>
|
|
</div>;
|
|
case "filter": return <div>
|
|
<div className="maputnik-filter-editor-wrapper">
|
|
<FilterEditor
|
|
errors={errorData}
|
|
filter={(this.props.layer as any).filter}
|
|
properties={this.props.vectorLayers[(this.props.layer as any)["source-layer"]]}
|
|
onChange={f => this.changeProperty(null, "filter", f)}
|
|
/>
|
|
</div>
|
|
</div>;
|
|
case "properties":
|
|
return <PropertyGroup
|
|
errors={errorData}
|
|
layer={this.props.layer}
|
|
groupFields={fields!}
|
|
spec={this.props.spec}
|
|
onChange={this.changeProperty.bind(this)}
|
|
/>;
|
|
case "jsoneditor":
|
|
return <FieldJson
|
|
lintType="layer"
|
|
value={this.props.layer}
|
|
onChange={(layer: LayerSpecification) => {
|
|
this.props.onLayerChanged(
|
|
this.props.layerIndex,
|
|
layer
|
|
);
|
|
}}
|
|
/>;
|
|
default: return <></>;
|
|
}
|
|
}
|
|
|
|
moveLayer(offset: number) {
|
|
this.props.onMoveLayer({
|
|
oldIndex: this.props.layerIndex,
|
|
newIndex: this.props.layerIndex + offset
|
|
});
|
|
}
|
|
|
|
render() {
|
|
const t = this.props.t;
|
|
|
|
const groupIds: string[] = [];
|
|
const layerType = this.props.layer.type;
|
|
const groups = layoutGroups(layerType, t).filter(group => {
|
|
return !(layerType === "background" && group.type === "source");
|
|
}).map(group => {
|
|
const groupId = group.id;
|
|
groupIds.push(groupId);
|
|
return <LayerEditorGroup
|
|
data-wd-key={group.title}
|
|
id={groupId}
|
|
key={groupId}
|
|
title={group.title}
|
|
isActive={this.state.editorGroups[group.title]}
|
|
onActiveToggle={this.onGroupToggle.bind(this, group.title)}
|
|
>
|
|
{this.renderGroupType(group.type, group.fields)}
|
|
</LayerEditorGroup>;
|
|
});
|
|
|
|
const layout = this.props.layer.layout || {};
|
|
|
|
const items: {
|
|
[key: string]: LayerEditorMenuItem
|
|
} = {
|
|
delete: {
|
|
text: t("Delete"),
|
|
handler: () => this.props.onLayerDestroy(this.props.layerIndex),
|
|
wdKey: "menu-delete-layer"
|
|
},
|
|
duplicate: {
|
|
text: t("Duplicate"),
|
|
handler: () => this.props.onLayerCopy(this.props.layerIndex),
|
|
wdKey: "menu-duplicate-layer"
|
|
},
|
|
hide: {
|
|
text: (layout.visibility === "none") ? t("Show") : t("Hide"),
|
|
handler: () => this.props.onLayerVisibilityToggle(this.props.layerIndex),
|
|
wdKey: "menu-hide-layer"
|
|
},
|
|
moveLayerUp: {
|
|
text: t("Move layer up"),
|
|
disabled: this.props.isFirstLayer,
|
|
handler: () => this.moveLayer(-1),
|
|
wdKey: "menu-move-layer-up"
|
|
},
|
|
moveLayerDown: {
|
|
text: t("Move layer down"),
|
|
disabled: this.props.isLastLayer,
|
|
handler: () => this.moveLayer(+1),
|
|
wdKey: "menu-move-layer-down"
|
|
}
|
|
};
|
|
|
|
return <IconContext.Provider value={{ size: "14px", color: "#8e8e8e" }}>
|
|
<section className="maputnik-layer-editor"
|
|
role="main"
|
|
aria-label={t("Layer editor")}
|
|
data-wd-key="layer-editor"
|
|
>
|
|
<header data-wd-key="layer-editor.header">
|
|
<div className="layer-header">
|
|
<h2 className="layer-header__title">
|
|
{t("Layer")}: {formatLayerId(this.props.layer.id)}
|
|
</h2>
|
|
<div className="layer-header__info">
|
|
<LayerEditorMenu items={Object.values(items)} />
|
|
</div>
|
|
</div>
|
|
|
|
</header>
|
|
<Accordion
|
|
allowMultipleExpanded={true}
|
|
allowZeroExpanded={true}
|
|
preExpanded={groupIds}
|
|
>
|
|
{groups}
|
|
</Accordion>
|
|
</section>
|
|
</IconContext.Provider>;
|
|
}
|
|
}
|
|
|
|
export const LayerEditor = withTranslation()(LayerEditorInternal);
|