ink-framework-maps 0.0.0-stage → 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vandam Dinh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,4 @@
1
+ {
2
+ "version": 1,
3
+ "modules": { "*": ["maps"] }
4
+ }
package/package.json CHANGED
@@ -1,6 +1,22 @@
1
1
  {
2
2
  "name": "ink-framework-maps",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-alpha.10",
4
+ "exports": {
5
+ ".": "./src/index.ts"
6
+ },
7
+ "peerDependencies": {
8
+ "ink": "npm:ink-framework@0.1.0-alpha.10",
9
+ "react": "19.2.8"
10
+ },
11
+ "license": "MIT",
12
+ "repository": {
13
+ "type": "git",
14
+ "url": "git+https://github.com/No-Scrolling/ink.git",
15
+ "directory": "packages/maps"
16
+ },
17
+ "files": [
18
+ "src",
19
+ "*.json",
20
+ "*.ts"
21
+ ]
22
+ }
package/src/index.ts ADDED
@@ -0,0 +1,126 @@
1
+ import { createElement, useEffect, useMemo, useRef, useState } from "react";
2
+ import { Field, useColourScheme } from "ink";
3
+ import { NativeError } from "ink/native";
4
+ import { attachNativeController } from "ink/native/controller";
5
+
6
+ export interface Coordinate { latitude: number; longitude: number }
7
+ export interface MapCamera { centre: Coordinate; zoom: number }
8
+ export interface MapMarker extends Coordinate { id: string; label?: string }
9
+ type MapUpdate = { styleURL: string; colourScheme: string; markers: MapMarker[] };
10
+ function sameUpdate(previous: MapUpdate, next: MapUpdate) {
11
+ return previous.styleURL === next.styleURL && previous.colourScheme === next.colourScheme
12
+ && previous.markers.length === next.markers.length && previous.markers.every((marker, index) => {
13
+ const other = next.markers[index];
14
+ return marker.id === other.id && marker.latitude === other.latitude && marker.longitude === other.longitude
15
+ && marker.label === other.label;
16
+ });
17
+ }
18
+ type Attachment = ReturnType<typeof attachNativeController>;
19
+ const attachmentKey = Symbol("map attachment");
20
+ export interface MapController {
21
+ moveTo(camera: MapCamera): Promise<void>;
22
+ readonly [attachmentKey]: { current: Attachment | null };
23
+ }
24
+
25
+ function coordinate(value: Coordinate): Coordinate {
26
+ if (!Number.isFinite(value.latitude) || Math.abs(value.latitude) > 90
27
+ || !Number.isFinite(value.longitude) || Math.abs(value.longitude) > 180) {
28
+ throw new RangeError("Map coordinates require latitude between -90 and 90 and longitude between -180 and 180");
29
+ }
30
+ return { latitude: value.latitude, longitude: value.longitude };
31
+ }
32
+ function zoomLevel(value: number): number {
33
+ if (!Number.isFinite(value) || value < 0 || value > 22) throw new RangeError("Map zoom must be between 0 and 22");
34
+ return value;
35
+ }
36
+ export function useMap(): MapController {
37
+ const attachment = useRef<Attachment | null>(null);
38
+ return useMemo(() => ({
39
+ [attachmentKey]: attachment,
40
+ async moveTo({ centre, zoom }: MapCamera) {
41
+ const camera = { centre: coordinate(centre), zoom: zoomLevel(zoom) };
42
+ if (!attachment.current) throw new NativeError("unavailable", "Map is not attached");
43
+ await attachment.current.call("move-to", camera);
44
+ },
45
+ }), []);
46
+ }
47
+
48
+ export interface MapViewProps {
49
+ styleURL?: string;
50
+ initialCentre: Coordinate;
51
+ initialZoom?: number;
52
+ markers?: readonly MapMarker[];
53
+ controller?: MapController;
54
+ onMarkerPress?: (id: string) => void;
55
+ onCameraIdle?: (camera: MapCamera) => void;
56
+ }
57
+ export function MapView({ styleURL: customStyleURL, initialCentre, initialZoom = 14, markers = [], controller,
58
+ onMarkerPress, onCameraIdle }: MapViewProps) {
59
+ const colourScheme = useColourScheme();
60
+ const styleURL = customStyleURL ?? `https://tiles.openfreemap.org/styles/${colourScheme === "light" ? "positron" : "dark"}`;
61
+ const ownController = useMap();
62
+ const mapController = controller ?? ownController;
63
+ const initial = useRef({ initialCentre: coordinate(initialCentre), initialZoom: zoomLevel(initialZoom) });
64
+ const callbacks = useRef({ onMarkerPress, onCameraIdle });
65
+ callbacks.current = { onMarkerPress, onCameraIdle };
66
+ const [id, setId] = useState<number | null>(null);
67
+ const [error, setError] = useState<Error | null>(null);
68
+ if (!styleURL.startsWith("https://")) throw new TypeError("Map styleURL must use HTTPS");
69
+ const ids = new Set<string>();
70
+ const nextUpdate = { styleURL, colourScheme, markers: markers.map(marker => {
71
+ if (!marker.id || ids.has(marker.id)) throw new TypeError("Map markers require unique non-empty IDs");
72
+ ids.add(marker.id);
73
+ return { id: marker.id, ...coordinate(marker), ...(marker.label === undefined ? {} : { label: marker.label }) };
74
+ }) };
75
+ const latestUpdate = useRef(nextUpdate);
76
+ if (!sameUpdate(latestUpdate.current, nextUpdate)) latestUpdate.current = nextUpdate;
77
+ const update = latestUpdate.current;
78
+ const sent = useRef<MapUpdate | null>(null);
79
+ useEffect(() => {
80
+ if (mapController[attachmentKey].current) throw new Error("A map controller can only belong to one MapView");
81
+ setError(null);
82
+ setId(null);
83
+ let active = true;
84
+ sent.current = latestUpdate.current;
85
+ const attachment = attachNativeController("maps", { ...initial.current, ...latestUpdate.current }, value => {
86
+ if (!active) return;
87
+ if (typeof value !== "object" || value === null || !("type" in value)) {
88
+ setError(new NativeError("protocol", "Invalid map event"));
89
+ } else if (value.type === "ready") {
90
+ setError(null);
91
+ } else if (value.type === "marker-press" && "id" in value && typeof value.id === "string") {
92
+ callbacks.current.onMarkerPress?.(value.id);
93
+ } else if (value.type === "camera-idle" && "centre" in value && typeof value.centre === "object"
94
+ && value.centre !== null && "latitude" in value.centre && typeof value.centre.latitude === "number"
95
+ && "longitude" in value.centre && typeof value.centre.longitude === "number"
96
+ && "zoom" in value && typeof value.zoom === "number") {
97
+ const camera = { centre: { latitude: value.centre.latitude, longitude: value.centre.longitude }, zoom: value.zoom };
98
+ initial.current = { initialCentre: camera.centre, initialZoom: camera.zoom };
99
+ callbacks.current.onCameraIdle?.(camera);
100
+ } else if (value.type === "error" && "error" in value && typeof value.error === "object" && value.error !== null
101
+ && "message" in value.error && typeof value.error.message === "string") {
102
+ setError(new NativeError("unexpected", value.error.message));
103
+ } else setError(new NativeError("protocol", "Invalid map event"));
104
+ });
105
+ mapController[attachmentKey].current = attachment;
106
+ void attachment.ready.then(() => { if (active) setId(attachment.id); }, failure => { if (active) setError(failure); });
107
+ return () => {
108
+ active = false;
109
+ mapController[attachmentKey].current = null;
110
+ void attachment.dispose().catch(failure => console.error("Could not release map", failure));
111
+ };
112
+ }, [mapController]);
113
+ useEffect(() => { setError(null); }, [styleURL]);
114
+ useEffect(() => {
115
+ let active = true;
116
+ const attachment = mapController[attachmentKey].current;
117
+ if (attachment && sent.current !== update) {
118
+ sent.current = update;
119
+ void attachment.call("update", update).catch(failure => { if (active) setError(failure); });
120
+ }
121
+ return () => { active = false; };
122
+ }, [mapController, update]);
123
+ if (error) return createElement(Field, { label: "Map" }, error.message);
124
+ if (id === null) return null;
125
+ return createElement("MapView", { controller: id });
126
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.