refactor(react/collections): reintroduce scale

This commit is contained in:
Elian Doran 2025-09-04 22:05:44 +03:00
parent 9adf9a841c
commit ec378a8fc5
No known key found for this signature in database
3 changed files with 14 additions and 11 deletions

View File

@ -35,6 +35,7 @@ enum State {
export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewModeProps<MapData>) { export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewModeProps<MapData>) {
const [ state, setState ] = useState(State.Normal); const [ state, setState ] = useState(State.Normal);
const [ layerName ] = useNoteLabel(note, "map:style"); const [ layerName ] = useNoteLabel(note, "map:style");
const [ hasScale ] = useNoteLabelBoolean(note, "map:scale");
const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly"); const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly");
const [ notes, setNotes ] = useState<FNote[]>([]); const [ notes, setNotes ] = useState<FNote[]>([]);
const spacedUpdate = useSpacedUpdate(() => { const spacedUpdate = useSpacedUpdate(() => {
@ -96,6 +97,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
}} }}
onClick={onClick} onClick={onClick}
onContextMenu={onContextMenu} onContextMenu={onContextMenu}
scale={hasScale}
> >
{notes.map(note => <NoteMarker note={note} editable={!isReadOnly} />)} {notes.map(note => <NoteMarker note={note} editable={!isReadOnly} />)}
</Map> </Map>

View File

@ -1,9 +1,8 @@
import { useEffect, useRef, useState } from "preact/hooks"; import { useEffect, useRef, useState } from "preact/hooks";
import L, { LatLng, Layer, LeafletMouseEvent } from "leaflet"; import L, { control, LatLng, Layer, LeafletMouseEvent } from "leaflet";
import "leaflet/dist/leaflet.css"; import "leaflet/dist/leaflet.css";
import { MAP_LAYERS } from "./map_layer"; import { MAP_LAYERS } from "./map_layer";
import { ComponentChildren, createContext } from "preact"; import { ComponentChildren, createContext } from "preact";
import { map } from "jquery";
export const ParentMap = createContext<L.Map | null>(null); export const ParentMap = createContext<L.Map | null>(null);
@ -15,9 +14,10 @@ interface MapProps {
children: ComponentChildren; children: ComponentChildren;
onClick?: (e: LeafletMouseEvent) => void; onClick?: (e: LeafletMouseEvent) => void;
onContextMenu?: (e: LeafletMouseEvent) => void; onContextMenu?: (e: LeafletMouseEvent) => void;
scale: boolean;
} }
export default function Map({ coordinates, zoom, layerName, viewportChanged, children, onClick, onContextMenu }: MapProps) { export default function Map({ coordinates, zoom, layerName, viewportChanged, children, onClick, onContextMenu, scale }: MapProps) {
const mapRef = useRef<L.Map>(null); const mapRef = useRef<L.Map>(null);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
@ -103,6 +103,15 @@ export default function Map({ coordinates, zoom, layerName, viewportChanged, chi
} }
}, [ mapRef, onContextMenu ]); }, [ mapRef, onContextMenu ]);
// Scale
useEffect(() => {
const map = mapRef.current;
if (!scale || !map) return;
const scaleControl = control.scale();
scaleControl.addTo(map);
return () => scaleControl.remove();
}, [ mapRef, scale ]);
return ( return (
<div <div
ref={containerRef} ref={containerRef}

View File

@ -39,14 +39,6 @@ export default class GeoView extends ViewMode<MapData> {
args.$parent.append(this.$root); args.$parent.append(this.$root);
} }
async renderMap() {
const layerName = this.parentNote.getLabelValue("map:style") ?? ;
if (this.parentNote.hasLabel("map:scale")) {
L.control.scale().addTo(map);
}
}
async #onMapInitialized() { async #onMapInitialized() {
const map = this.map; const map = this.map;
if (!map) { if (!map) {