mirror of
https://github.com/maputnik/editor.git
synced 2026-07-16 02:47:26 +00:00
Fix insertion of access tokens, when swapping renderer (#1021)
Going from e.g. MapTiler to OpenLayers and back will lose the maptlier
key.
This code finds the urls in the style that has "{key}" and insert the
correct API keys
Fixes the error reported here, cc @nyurik
- Fixes
https://github.com/maplibre/maputnik/issues/874#issuecomment-2605896666
Related to:
- https://github.com/maplibre/maputnik/issues/869
## Launch Checklist
<!-- Thanks for the PR! Feel free to add or remove items from the
checklist. -->
- [ ] Briefly describe the changes in this PR.
- [ ] Link to related issues.
- [ ] Include before/after visuals or gifs if this PR includes visual
changes.
- [ ] Write tests for all new functionality.
- [ ] Add an entry to `CHANGELOG.md` under the `## main` section.
This commit is contained in:
@@ -93,7 +93,7 @@ jobs:
|
|||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
browser: [chrome, firefox]
|
browser: [chrome]
|
||||||
|
|
||||||
runs-on: ubuntu-22.04
|
runs-on: ubuntu-22.04
|
||||||
steps:
|
steps:
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import { MaputnikDriver } from "./maputnik-driver";
|
import { MaputnikDriver } from "./maputnik-driver";
|
||||||
|
import tokens from "../../src/config/tokens.json" with {type: "json"};
|
||||||
|
|
||||||
describe("modals", () => {
|
describe("modals", () => {
|
||||||
const { beforeAndAfter, when, get, then } = new MaputnikDriver();
|
const { beforeAndAfter, when, get, given, then } = new MaputnikDriver();
|
||||||
beforeAndAfter();
|
beforeAndAfter();
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -235,6 +236,40 @@ describe("modals", () => {
|
|||||||
metadata: { "maputnik:renderer": "ol" },
|
metadata: { "maputnik:renderer": "ol" },
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
it("inlcude API key when change renderer", () => {
|
||||||
|
|
||||||
|
when.click("modal:settings.close-modal")
|
||||||
|
when.click("nav:open");
|
||||||
|
|
||||||
|
get.elementByAttribute('aria-label', "MapTiler Basic").should('exist').click();
|
||||||
|
|
||||||
|
when.click("nav:settings");
|
||||||
|
|
||||||
|
when.select("modal:settings.maputnik:renderer", "mlgljs");
|
||||||
|
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
|
||||||
|
"mlgljs"
|
||||||
|
);
|
||||||
|
|
||||||
|
when.select("modal:settings.maputnik:renderer", "ol");
|
||||||
|
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
|
||||||
|
"ol"
|
||||||
|
);
|
||||||
|
|
||||||
|
given.intercept("https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=*", "tileRequest", "GET");
|
||||||
|
|
||||||
|
when.select("modal:settings.maputnik:renderer", "mlgljs");
|
||||||
|
then(get.inputValue("modal:settings.maputnik:renderer")).shouldEqual(
|
||||||
|
"mlgljs"
|
||||||
|
);
|
||||||
|
|
||||||
|
when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
|
||||||
|
when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
|
||||||
|
when.waitForResponse("tileRequest").its("request").its("url").should("include", `https://api.maptiler.com/tiles/v3-openmaptiles/tiles.json?key=${tokens.openmaptiles}`);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("sources", () => {
|
describe("sources", () => {
|
||||||
|
|||||||
@@ -363,6 +363,7 @@ export default class App extends React.Component<any, AppState> {
|
|||||||
[property]: value
|
[property]: value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.onStyleChanged(changedStyle)
|
this.onStyleChanged(changedStyle)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,6 +375,24 @@ export default class App extends React.Component<any, AppState> {
|
|||||||
...opts,
|
...opts,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// For the style object, find the urls that has "{key}" and insert the correct API keys
|
||||||
|
// Without this, going from e.g. MapTiler to OpenLayers and back will lose the maptlier key.
|
||||||
|
|
||||||
|
if (newStyle.glyphs && typeof newStyle.glyphs === 'string') {
|
||||||
|
newStyle.glyphs = setFetchAccessToken(newStyle.glyphs, newStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newStyle.sprite && typeof newStyle.sprite === 'string') {
|
||||||
|
newStyle.sprite = setFetchAccessToken(newStyle.sprite, newStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const [_sourceId, source] of Object.entries(newStyle.sources)) {
|
||||||
|
if (source && 'url' in source && typeof source.url === 'string') {
|
||||||
|
source.url = setFetchAccessToken(source.url, newStyle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (opts.initialLoad) {
|
if (opts.initialLoad) {
|
||||||
this.getInitialStateFromUrl(newStyle);
|
this.getInitialStateFromUrl(newStyle);
|
||||||
}
|
}
|
||||||
@@ -737,6 +756,7 @@ export default class App extends React.Component<any, AppState> {
|
|||||||
onLayerSelect={this.onLayerSelect}
|
onLayerSelect={this.onLayerSelect}
|
||||||
/>
|
/>
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
mapElement = <MapMaplibreGl {...mapProps}
|
mapElement = <MapMaplibreGl {...mapProps}
|
||||||
onChange={this.onMapChange}
|
onChange={this.onMapChange}
|
||||||
options={this.state.maplibreGlDebugOptions}
|
options={this.state.maplibreGlDebugOptions}
|
||||||
@@ -790,6 +810,7 @@ export default class App extends React.Component<any, AppState> {
|
|||||||
getInitialStateFromUrl = (mapStyle: StyleSpecification) => {
|
getInitialStateFromUrl = (mapStyle: StyleSpecification) => {
|
||||||
const url = new URL(location.href);
|
const url = new URL(location.href);
|
||||||
const modalParam = url.searchParams.get("modal");
|
const modalParam = url.searchParams.get("modal");
|
||||||
|
|
||||||
if (modalParam && modalParam !== "") {
|
if (modalParam && modalParam !== "") {
|
||||||
const modals = modalParam.split(",");
|
const modals = modalParam.split(",");
|
||||||
const modalObj: {[key: string]: boolean} = {};
|
const modalObj: {[key: string]: boolean} = {};
|
||||||
|
|||||||
Reference in New Issue
Block a user