Skip to content

Commit

Permalink
Add lang and script parameters to TypeScript style generation (#275)
Browse files Browse the repository at this point in the history
Style 4.0.0-alpha.0

* Add lang and script parameters to TypeScript style generation

* Add comments, support Hindi

* Use multiline names on all labels

* Add generate languages script

* Add special case for Japanese

* Use Mixed-Japanese

* Use pmap:pgf:name* when available

* Generate all themes in all supported languages

* Add lang dropdown to app

* Remove generate_layers.ts

* Update package.json
  • Loading branch information
wipfli authored Aug 25, 2024
1 parent 57cc4b9 commit b6b976d
Show file tree
Hide file tree
Showing 14 changed files with 706 additions and 59 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ jobs:
github_token: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/setup-node@v3
with:
node-version: 18.x
node-version-file: 'app/.nvmrc'
- run: echo "VITE_GIT_SHA=$(git rev-parse --short HEAD)" >> app/.env
- name: Check styles
run: npm install && npx tsc --noEmit && npm run check
run: npm ci && npm run tsc && npm run check
working-directory: styles
- run: npm install && npx tsc --noEmit && npm run check && npx vite build
- run: npm ci && npm run tsc && npm run check && npm run build
working-directory: app
- run: python .github/check_examples.py
- uses: peaceiris/actions-gh-pages@v3
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Styles v4.0.0-alpha.0
------
- Add lang and script parameters to TypeScript style generation [#275]

Tiles v4.0.0-alpha.3
------
- Replace Natural Earth places at low zooms with OSM [#289]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ java -jar target/*-with-deps.jar --download --force --area=monaco

```shell
cd app
npm install
nvm use
npm ci
npm run dev
```

Expand Down
1 change: 1 addition & 0 deletions app/.nvmrc
6 changes: 4 additions & 2 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"build": "vite build",
"tsc": "tsc --noEmit",
"preview": "vite preview",
"test": "vitest",
"check": "biome check src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space"
"check": "biome check src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space",
"format": "biome format --write src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space"
},
"dependencies": {
"maplibre-gl": "^4.1.3",
Expand Down
34 changes: 28 additions & 6 deletions app/src/MapViewComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { renderToString } from "react-dom/server";
import { useDropzone } from "react-dropzone";
import layers from "../../styles/src/index.ts";
import { language_script_pairs } from "../../styles/src/language.ts";

import { LayerSpecification } from "@maplibre/maplibre-gl-style-spec";

Expand Down Expand Up @@ -68,6 +69,7 @@ export const isValidPMTiles = (tiles?: string): boolean => {

function getMaplibreStyle(
theme: string,
lang: string,
localSprites: boolean,
tiles?: string,
npmLayers?: LayerSpecification[],
Expand Down Expand Up @@ -121,14 +123,19 @@ function getMaplibreStyle(
if (npmLayers && npmLayers.length > 0) {
style.layers = style.layers.concat(npmLayers);
} else {
style.layers = style.layers.concat(layers("protomaps", theme));
style.layers = style.layers.concat(layers("protomaps", theme, lang));
}
return style;
}

function StyleJsonPane(props: { theme: string }) {
function StyleJsonPane(props: { theme: string; lang: string }) {
const stringified = JSON.stringify(
getMaplibreStyle(props.theme, false, "https://example.com/tiles.json"),
getMaplibreStyle(
props.theme,
props.lang,
false,
"https://example.com/tiles.json",
),
null,
4,
);
Expand All @@ -150,6 +157,7 @@ function StyleJsonPane(props: { theme: string }) {

function MapLibreView(props: {
theme: string;
lang: string;
localSprites: boolean;
tiles?: string;
npmLayers: LayerSpecification[];
Expand All @@ -173,7 +181,7 @@ function MapLibreView(props: {
const map = new maplibregl.Map({
hash: "map",
container: "map",
style: getMaplibreStyle("", false),
style: getMaplibreStyle("", "en", false),
});

map.addControl(new maplibregl.NavigationControl());
Expand All @@ -196,7 +204,7 @@ function MapLibreView(props: {
maxWidth: "none",
});

map.on("mousedown", (e) => {
map.on("contextmenu", (e) => {
const features = map.queryRenderedFeatures(e.point);
if (features.length) {
const content = renderToString(
Expand Down Expand Up @@ -251,6 +259,7 @@ function MapLibreView(props: {
mapRef.current.setStyle(
getMaplibreStyle(
props.theme,
props.lang,
props.localSprites,
props.tiles,
props.npmLayers,
Expand All @@ -264,6 +273,7 @@ function MapLibreView(props: {
}, [
props.tiles,
props.theme,
props.lang,
props.localSprites,
props.npmLayers,
props.droppedArchive,
Expand All @@ -276,6 +286,7 @@ function MapLibreView(props: {
export default function MapViewComponent() {
const hash = parseHash(location.hash);
const [theme, setTheme] = useState<string>(hash.theme || "light");
const [lang, setLang] = useState<string>(hash.lang || "en");
const [tiles, setTiles] = useState<string | undefined>(hash.tiles);
const [localSprites, setLocalSprites] = useState<boolean>(
hash.local_sprites === "true",
Expand All @@ -291,6 +302,7 @@ export default function MapViewComponent() {
useEffect(() => {
const record = {
theme: theme,
lang: lang,
tiles: tiles,
local_sprites: localSprites ? "true" : undefined,
npm_version: publishedStyleVersion,
Expand Down Expand Up @@ -368,6 +380,8 @@ export default function MapViewComponent() {
})();
}, [publishedStyleVersion, theme]);

language_script_pairs.sort((a, b) => a.full_name.localeCompare(b.full_name));

return (
<div className="map-container">
<nav>
Expand All @@ -388,6 +402,13 @@ export default function MapViewComponent() {
<option value="grayscale">data viz (grayscale)</option>
<option value="black">data viz (black)</option>
</select>
<select onChange={(e) => setLang(e.target.value)} value={lang}>
{language_script_pairs.map((pair) => (
<option key={pair.lang} value={pair.lang}>
{pair.full_name}
</option>
))}
</select>
<input
id="localSprites"
type="checkbox"
Expand Down Expand Up @@ -431,10 +452,11 @@ export default function MapViewComponent() {
tiles={tiles}
localSprites={localSprites}
theme={theme}
lang={lang}
npmLayers={npmLayers}
droppedArchive={droppedArchive}
/>
{showStyleJson && <StyleJsonPane theme={theme} />}
{showStyleJson && <StyleJsonPane theme={theme} lang={lang} />}
</div>
</div>
);
Expand Down
2 changes: 1 addition & 1 deletion app/src/VisualTestsComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export default function VisualTestsComponent() {
const leftLayers = await layersForVersion(leftLayersStr);
const rightLayers = rightLayersStr
? await layersForVersion(rightLayersStr)
: layers("protomaps", "light");
: layers("protomaps", "light", "en", "Latin");

setDisplayInfo([
leftTiles,
Expand Down
24 changes: 20 additions & 4 deletions styles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,28 @@ nvm use
npm ci
```

## Generate JSON layers
## Generate JSON styles in all themes and languages

This only generates the `layers` key of a style; you'll assemble `source`, `glyphs`, `sprite` yourself.
To generate style.json files in all themes and supported languages, run:

`generate-layers SOURCE_NAME THEME`
```
npm run generate-styles https://example.com/your-tilejson-url.json
```

Note that you have to replace the tilejson url with your own.

This will create files in the `dist/styles/` folder like this:

```
npm run generate-layers protomaps light
dist/
styles/
black/
ar.json
bg.json
...
contrast/
ar.json
bg.json
...
...
```
18 changes: 7 additions & 11 deletions styles/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protomaps-themes-base",
"version": "3.1.0",
"version": "4.0.0-alpha.0",
"description": "Protomaps basemap themes for MapLibre GL JS",
"type": "module",
"main": "dist/cjs/index.cjs",
Expand All @@ -24,17 +24,13 @@
"src"
],
"scripts": {
"generate-layers": "tsx src/generate_layers.ts",
"dist-light": "mkdir -p dist/layers && npm run --silent generate-layers protomaps light > dist/layers/light.json",
"dist-dark": "mkdir -p dist/layers && npm run --silent generate-layers protomaps dark > dist/layers/dark.json",
"dist-white": "mkdir -p dist/layers && npm run --silent generate-layers protomaps white > dist/layers/white.json",
"dist-grayscale": "mkdir -p dist/layers && npm run --silent generate-layers protomaps grayscale > dist/layers/grayscale.json",
"dist-black": "mkdir -p dist/layers && npm run --silent generate-layers protomaps black > dist/layers/black.json",
"dist-all": "npm run dist-light && npm run dist-dark && npm run dist-white && npm run dist-grayscale && npm run dist-black",
"build": "tsup && npm run dist-all",
"generate-styles": "tsx src/generate_styles.ts",
"build": "tsup && npm run generate-styles https://example.com/tiles.json",
"test": "tsx test/index.test.ts",
"tsc": "tsc --noEmit --watch",
"check": "biome check src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space"
"tsc-watch": "tsc --noEmit --watch",
"tsc": "tsc --noEmit",
"check": "biome check src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space",
"format": "biome format --write src test --javascript-formatter-indent-style=space --json-formatter-indent-style=space"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit b6b976d

Please sign in to comment.