mirror of
https://github.com/maputnik/editor.git
synced 2026-07-27 16:27:26 +00:00
Remove dead code
This commit is contained in:
@@ -29,7 +29,7 @@ import ModalDebug from "./modals/ModalDebug";
|
||||
import ModalGlobalState from "./modals/ModalGlobalState";
|
||||
|
||||
import {downloadGlyphsMetadata, downloadSpriteMetadata} from "../libs/metadata";
|
||||
import style from "../libs/style";
|
||||
import { emptyStyle, replaceAccessTokens } from "../libs/style";
|
||||
import { undoMessages, redoMessages } from "../libs/diffmessage";
|
||||
import { createStyleStore, type IStyleStore } from "../libs/store/style-store-factory";
|
||||
import { RevisionStore } from "../libs/revisions";
|
||||
@@ -137,7 +137,7 @@ export default class App extends React.Component<any, AppState> {
|
||||
this.state = {
|
||||
errors: [],
|
||||
infos: [],
|
||||
mapStyle: style.emptyStyle,
|
||||
mapStyle: emptyStyle,
|
||||
selectedLayerIndex: 0,
|
||||
sources: {},
|
||||
vectorLayers: {},
|
||||
@@ -698,7 +698,7 @@ export default class App extends React.Component<any, AppState> {
|
||||
mapStyle: (dirtyMapStyle || mapStyle),
|
||||
mapView: this.state.mapView,
|
||||
replaceAccessTokens: (mapStyle: StyleSpecification) => {
|
||||
return style.replaceAccessTokens(mapStyle, {
|
||||
return replaceAccessTokens(mapStyle, {
|
||||
allowFallback: true
|
||||
});
|
||||
},
|
||||
|
||||
@@ -9,7 +9,7 @@ import {type WithTranslation, withTranslation} from "react-i18next";
|
||||
import FieldString from "../FieldString";
|
||||
import InputButton from "../InputButton";
|
||||
import Modal from "./Modal";
|
||||
import style from "../../libs/style";
|
||||
import { replaceAccessTokens, stripAccessTokens } from "../../libs/style";
|
||||
import fieldSpecAdditional from "../../libs/field-spec-additional";
|
||||
import type {OnStyleChangedCallback, StyleSpecificationWithId} from "../../libs/definitions";
|
||||
|
||||
@@ -32,8 +32,8 @@ class ModalExportInternal extends React.Component<ModalExportInternalProps> {
|
||||
|
||||
tokenizedStyle() {
|
||||
return format(
|
||||
style.stripAccessTokens(
|
||||
style.replaceAccessTokens(this.props.mapStyle)
|
||||
stripAccessTokens(
|
||||
replaceAccessTokens(this.props.mapStyle)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import Modal from "./Modal";
|
||||
import InputButton from "../InputButton";
|
||||
import InputUrl from "../InputUrl";
|
||||
|
||||
import style from "../../libs/style";
|
||||
import { ensureStyleValidity } from "../../libs/style";
|
||||
import publicStyles from "../../config/styles.json";
|
||||
|
||||
type PublicStyleProps = {
|
||||
@@ -109,7 +109,7 @@ class ModalOpenInternal extends React.Component<ModalOpenInternalProps, ModalOpe
|
||||
activeRequestUrl: null
|
||||
});
|
||||
|
||||
const mapStyle = style.ensureStyleValidity(body);
|
||||
const mapStyle = ensureStyleValidity(body);
|
||||
console.log("Loaded style ", mapStyle.id);
|
||||
this.props.onStyleOpen(mapStyle);
|
||||
this.onOpenToggle();
|
||||
@@ -165,7 +165,7 @@ class ModalOpenInternal extends React.Component<ModalOpenInternalProps, ModalOpe
|
||||
});
|
||||
return;
|
||||
}
|
||||
mapStyle = style.ensureStyleValidity(mapStyle);
|
||||
mapStyle = ensureStyleValidity(mapStyle);
|
||||
|
||||
this.props.onStyleOpen(mapStyle, fileHandle);
|
||||
this.onOpenToggle();
|
||||
@@ -193,7 +193,7 @@ class ModalOpenInternal extends React.Component<ModalOpenInternalProps, ModalOpe
|
||||
});
|
||||
return;
|
||||
}
|
||||
mapStyle = style.ensureStyleValidity(mapStyle);
|
||||
mapStyle = ensureStyleValidity(mapStyle);
|
||||
this.props.onStyleOpen(mapStyle);
|
||||
this.onOpenToggle();
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ import FieldString from "../FieldString";
|
||||
import FieldSelect from "../FieldSelect";
|
||||
import ModalSourcesTypeEditor, { type EditorMode } from "./ModalSourcesTypeEditor";
|
||||
|
||||
import style from "../../libs/style";
|
||||
import { generateId } from "../../libs/style";
|
||||
import { deleteSource, addSource, changeSource } from "../../libs/source";
|
||||
import publicSources from "../../config/tilesets.json";
|
||||
import { type OnStyleChangedCallback, type StyleSpecificationWithId } from "../../libs/definitions";
|
||||
@@ -121,7 +121,7 @@ class AddSource extends React.Component<AddSourceProps, AddSourceState> {
|
||||
super(props);
|
||||
this.state = {
|
||||
mode: "tilejson_vector",
|
||||
sourceId: style.generateId(),
|
||||
sourceId: generateId(),
|
||||
source: this.defaultSource("tilejson_vector"),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,120 +0,0 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import type { StyleSpecification } from "maplibre-gl";
|
||||
import style from "./style";
|
||||
|
||||
function baseStyle(overrides: Partial<StyleSpecification> = {}): StyleSpecification {
|
||||
return { version: 8, sources: {}, layers: [], ...overrides } as StyleSpecification;
|
||||
}
|
||||
|
||||
describe("ensureStyleValidity", () => {
|
||||
it("adds an id and strips interactive from layers", () => {
|
||||
const result = style.ensureStyleValidity(
|
||||
baseStyle({
|
||||
layers: [{ id: "l", type: "background", interactive: true } as any],
|
||||
})
|
||||
);
|
||||
expect(result.id).toBeTruthy();
|
||||
expect("interactive" in result.layers[0]).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps an existing id", () => {
|
||||
const result = style.ensureStyleValidity(baseStyle({ id: "keep-me" } as any));
|
||||
expect(result.id).toBe("keep-me");
|
||||
});
|
||||
});
|
||||
|
||||
describe("generateId", () => {
|
||||
it("generates a non-empty string", () => {
|
||||
expect(typeof style.generateId()).toBe("string");
|
||||
expect(style.generateId().length).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("indexOfLayer", () => {
|
||||
const layers = [{ id: "a" }, { id: "b" }] as any;
|
||||
it("returns the index of a matching layer", () => {
|
||||
expect(style.indexOfLayer(layers, "b")).toBe(1);
|
||||
});
|
||||
it("returns null when not found", () => {
|
||||
expect(style.indexOfLayer(layers, "missing")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("getAccessToken", () => {
|
||||
it("reads the token from metadata", () => {
|
||||
const s = baseStyle({ metadata: { "maputnik:openmaptiles_access_token": "abc" } } as any);
|
||||
expect(style.getAccessToken("openmaptiles", s, {})).toBe("abc");
|
||||
});
|
||||
it("falls back to the bundled token only when allowed", () => {
|
||||
const s = baseStyle();
|
||||
expect(style.getAccessToken("openmaptiles", s, {})).toBeUndefined();
|
||||
expect(style.getAccessToken("openmaptiles", s, { allowFallback: true })).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe("replaceAccessTokens", () => {
|
||||
it("replaces {key} in a source url and in glyphs", () => {
|
||||
const s = baseStyle({
|
||||
metadata: { "maputnik:openmaptiles_access_token": "TОKEN" } as any,
|
||||
sources: { openmaptiles: { type: "vector", url: "https://api.maptiler.com/x?key={key}" } } as any,
|
||||
glyphs: "https://api.maptiler.com/fonts/{fontstack}/{range}.pbf?key={key}",
|
||||
});
|
||||
const result = style.replaceAccessTokens(s);
|
||||
expect((result.sources.openmaptiles as any).url).toContain("key=TОKEN");
|
||||
expect(result.glyphs).toContain("key=TОKEN");
|
||||
});
|
||||
|
||||
it("maps thunderforest transport/outdoors sources to the thunderforest token", () => {
|
||||
const s = baseStyle({
|
||||
metadata: { "maputnik:thunderforest_access_token": "TF" } as any,
|
||||
sources: { thunderforest_transport: { type: "vector", url: "https://tile.thunderforest.com/x?apikey={key}" } } as any,
|
||||
});
|
||||
const result = style.replaceAccessTokens(s);
|
||||
expect((result.sources.thunderforest_transport as any).url).toContain("TF");
|
||||
});
|
||||
|
||||
it("appends an api_key query param for stadia sources", () => {
|
||||
const s = baseStyle({
|
||||
metadata: { "maputnik:stadia_access_token": "ST" } as any,
|
||||
sources: { basemap: { type: "vector", url: "https://tiles.stadiamaps.com/data/x.json" } } as any,
|
||||
});
|
||||
const result = style.replaceAccessTokens(s);
|
||||
expect((result.sources.basemap as any).url).toContain("api_key=ST");
|
||||
});
|
||||
|
||||
it("uses the locationiq token for locationiq sources", () => {
|
||||
const s = baseStyle({
|
||||
metadata: { "maputnik:locationiq_access_token": "LIQ" } as any,
|
||||
sources: { liq: { type: "vector", url: "https://tiles.locationiq.com/v3/x?key={key}" } } as any,
|
||||
});
|
||||
const result = style.replaceAccessTokens(s);
|
||||
expect((result.sources.liq as any).url).toContain("LIQ");
|
||||
});
|
||||
|
||||
it("leaves sources without a url or token untouched", () => {
|
||||
const s = baseStyle({
|
||||
sources: {
|
||||
noUrl: { type: "geojson", data: {} } as any,
|
||||
noToken: { type: "vector", url: "https://api.maptiler.com/x?key={key}" } as any,
|
||||
},
|
||||
});
|
||||
const result = style.replaceAccessTokens(s);
|
||||
expect((result.sources.noToken as any).url).toContain("{key}");
|
||||
});
|
||||
});
|
||||
|
||||
describe("stripAccessTokens", () => {
|
||||
it("removes provider access tokens from metadata", () => {
|
||||
const s = baseStyle({
|
||||
metadata: {
|
||||
"maputnik:openmaptiles_access_token": "a",
|
||||
"maputnik:thunderforest_access_token": "b",
|
||||
"maputnik:renderer": "mlgljs",
|
||||
} as any,
|
||||
});
|
||||
const result = style.stripAccessTokens(s);
|
||||
expect(result.metadata).not.toHaveProperty("maputnik:openmaptiles_access_token");
|
||||
expect(result.metadata).not.toHaveProperty("maputnik:thunderforest_access_token");
|
||||
expect((result.metadata as any)["maputnik:renderer"]).toBe("mlgljs");
|
||||
});
|
||||
});
|
||||
+1
-11
@@ -45,15 +45,6 @@ function ensureStyleValidity(style: StyleSpecification): StyleSpecificationWithI
|
||||
return ensureHasNoInteractive(ensureHasNoRefs(ensureHasId(style)));
|
||||
}
|
||||
|
||||
function indexOfLayer(layers: LayerSpecification[], layerId: string) {
|
||||
for (let i = 0; i < layers.length; i++) {
|
||||
if(layers[i].id === layerId) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function getAccessToken(sourceName: string, mapStyle: StyleSpecification, opts: {allowFallback?: boolean}) {
|
||||
const metadata = mapStyle.metadata || {} as any;
|
||||
let accessToken = metadata[`maputnik:${sourceName}_access_token`];
|
||||
@@ -148,10 +139,9 @@ function stripAccessTokens(mapStyle: StyleSpecification) {
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
export {
|
||||
ensureStyleValidity,
|
||||
emptyStyle,
|
||||
indexOfLayer,
|
||||
generateId,
|
||||
getAccessToken,
|
||||
replaceAccessTokens,
|
||||
|
||||
Reference in New Issue
Block a user