mirror of
https://github.com/zadam/trilium.git
synced 2025-11-01 19:05:59 +01:00
chore(react/collections/geomap): bring back adding new items
This commit is contained in:
@@ -18,7 +18,7 @@
|
|||||||
z-index: 997;
|
z-index: 997;
|
||||||
}
|
}
|
||||||
|
|
||||||
.geo-map-container.placing-note {
|
.geo-view.placing-note .geo-map-container {
|
||||||
cursor: crosshair;
|
cursor: crosshair;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Map from "./map";
|
import Map from "./map";
|
||||||
import "./index.css";
|
import "./index.css";
|
||||||
import { ViewModeProps } from "../interface";
|
import { ViewModeProps } from "../interface";
|
||||||
import { useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate } from "../../react/hooks";
|
import { useNoteLabel, useNoteLabelBoolean, useNoteProperty, useSpacedUpdate, useTriliumEvent } from "../../react/hooks";
|
||||||
import { DEFAULT_MAP_LAYER_NAME } from "./map_layer";
|
import { DEFAULT_MAP_LAYER_NAME } from "./map_layer";
|
||||||
import { divIcon, LatLng, LeafletMouseEvent } from "leaflet";
|
import { divIcon, LatLng, LeafletMouseEvent } from "leaflet";
|
||||||
import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
|
import { useCallback, useEffect, useMemo, useState } from "preact/hooks";
|
||||||
@@ -11,8 +11,10 @@ import FNote from "../../../entities/fnote";
|
|||||||
import markerIcon from "leaflet/dist/images/marker-icon.png";
|
import markerIcon from "leaflet/dist/images/marker-icon.png";
|
||||||
import markerIconShadow from "leaflet/dist/images/marker-shadow.png";
|
import markerIconShadow from "leaflet/dist/images/marker-shadow.png";
|
||||||
import appContext from "../../../components/app_context";
|
import appContext from "../../../components/app_context";
|
||||||
import { moveMarker } from "./api";
|
import { createNewNote, moveMarker } from "./api";
|
||||||
import openContextMenu from "./context_menu";
|
import openContextMenu from "./context_menu";
|
||||||
|
import toast from "../../../services/toast";
|
||||||
|
import { t } from "../../../services/i18n";
|
||||||
|
|
||||||
const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];
|
const DEFAULT_COORDINATES: [number, number] = [3.878638227135724, 446.6630455551659];
|
||||||
const DEFAULT_ZOOM = 2;
|
const DEFAULT_ZOOM = 2;
|
||||||
@@ -25,7 +27,13 @@ interface MapData {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum State {
|
||||||
|
Normal,
|
||||||
|
NewNote
|
||||||
|
}
|
||||||
|
|
||||||
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 [ layerName ] = useNoteLabel(note, "map:style");
|
const [ layerName ] = useNoteLabel(note, "map:style");
|
||||||
const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly");
|
const [ isReadOnly ] = useNoteLabelBoolean(note, "readOnly");
|
||||||
const [ notes, setNotes ] = useState<FNote[]>([]);
|
const [ notes, setNotes ] = useState<FNote[]>([]);
|
||||||
@@ -37,8 +45,37 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
|
|||||||
|
|
||||||
useEffect(() => { froca.getNotes(noteIds).then(setNotes) }, [ noteIds ]);
|
useEffect(() => { froca.getNotes(noteIds).then(setNotes) }, [ noteIds ]);
|
||||||
|
|
||||||
|
// Note creation.
|
||||||
|
useTriliumEvent("geoMapCreateChildNote", () => {
|
||||||
|
toast.showPersistent({
|
||||||
|
icon: "plus",
|
||||||
|
id: "geo-new-note",
|
||||||
|
title: "New note",
|
||||||
|
message: t("geo-map.create-child-note-instruction")
|
||||||
|
});
|
||||||
|
|
||||||
|
setState(State.NewNote);
|
||||||
|
|
||||||
|
const globalKeyListener: (this: Window, ev: KeyboardEvent) => any = (e) => {
|
||||||
|
if (e.key === "Escape") {
|
||||||
|
setState(State.Normal);
|
||||||
|
|
||||||
|
window.removeEventListener("keydown", globalKeyListener);
|
||||||
|
toast.closePersistent("geo-new-note");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener("keydown", globalKeyListener);
|
||||||
|
});
|
||||||
|
const onClick = useCallback(async (e: LeafletMouseEvent) => {
|
||||||
|
if (state === State.NewNote) {
|
||||||
|
toast.closePersistent("geo-new-note");
|
||||||
|
await createNewNote(note.noteId, e);
|
||||||
|
setState(State.Normal);
|
||||||
|
}
|
||||||
|
}, [ state ]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="geo-view">
|
<div className={`geo-view ${state === State.NewNote ? "placing-note" : ""}`}>
|
||||||
<Map
|
<Map
|
||||||
coordinates={viewConfig?.view?.center ?? DEFAULT_COORDINATES}
|
coordinates={viewConfig?.view?.center ?? DEFAULT_COORDINATES}
|
||||||
zoom={viewConfig?.view?.zoom ?? DEFAULT_ZOOM}
|
zoom={viewConfig?.view?.zoom ?? DEFAULT_ZOOM}
|
||||||
@@ -48,6 +85,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
|
|||||||
viewConfig.view = { center: coordinates, zoom };
|
viewConfig.view = { center: coordinates, zoom };
|
||||||
spacedUpdate.scheduleUpdate();
|
spacedUpdate.scheduleUpdate();
|
||||||
}}
|
}}
|
||||||
|
onClick={onClick}
|
||||||
>
|
>
|
||||||
{notes.map(note => <NoteMarker note={note} editable={!isReadOnly} />)}
|
{notes.map(note => <NoteMarker note={note} editable={!isReadOnly} />)}
|
||||||
</Map>
|
</Map>
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { useEffect, useRef, useState } from "preact/hooks";
|
import { useEffect, useRef, useState } from "preact/hooks";
|
||||||
import L, { LatLng, Layer } from "leaflet";
|
import L, { 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);
|
||||||
|
|
||||||
@@ -12,9 +13,10 @@ interface MapProps {
|
|||||||
layerName: string;
|
layerName: string;
|
||||||
viewportChanged: (coordinates: LatLng, zoom: number) => void;
|
viewportChanged: (coordinates: LatLng, zoom: number) => void;
|
||||||
children: ComponentChildren;
|
children: ComponentChildren;
|
||||||
|
onClick: (e: LeafletMouseEvent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Map({ coordinates, zoom, layerName, viewportChanged, children }: MapProps) {
|
export default function Map({ coordinates, zoom, layerName, viewportChanged, children, onClick }: MapProps) {
|
||||||
const mapRef = useRef<L.Map>(null);
|
const mapRef = useRef<L.Map>(null);
|
||||||
const containerRef = useRef<HTMLDivElement>(null);
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
@@ -80,6 +82,8 @@ export default function Map({ coordinates, zoom, layerName, viewportChanged, chi
|
|||||||
};
|
};
|
||||||
}, [ mapRef, viewportChanged ]);
|
}, [ mapRef, viewportChanged ]);
|
||||||
|
|
||||||
|
useEffect(() => { mapRef.current && mapRef.current.on("click", onClick) }, [ mapRef, onClick ]);
|
||||||
|
|
||||||
return <div ref={containerRef} className="geo-map-container">
|
return <div ref={containerRef} className="geo-map-container">
|
||||||
<ParentMap.Provider value={mapRef.current}>
|
<ParentMap.Provider value={mapRef.current}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -13,10 +13,7 @@ import attributes from "../../../services/attributes.js";
|
|||||||
import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS } from "./map_layer.js";
|
import { DEFAULT_MAP_LAYER_NAME, MAP_LAYERS } from "./map_layer.js";
|
||||||
|
|
||||||
|
|
||||||
enum State {
|
|
||||||
Normal,
|
|
||||||
NewNote
|
|
||||||
}
|
|
||||||
|
|
||||||
export default class GeoView extends ViewMode<MapData> {
|
export default class GeoView extends ViewMode<MapData> {
|
||||||
|
|
||||||
@@ -38,7 +35,6 @@ export default class GeoView extends ViewMode<MapData> {
|
|||||||
|
|
||||||
this.currentMarkerData = {};
|
this.currentMarkerData = {};
|
||||||
this.currentTrackData = {};
|
this.currentTrackData = {};
|
||||||
this._state = State.Normal;
|
|
||||||
|
|
||||||
args.$parent.append(this.$root);
|
args.$parent.append(this.$root);
|
||||||
}
|
}
|
||||||
@@ -127,7 +123,6 @@ export default class GeoView extends ViewMode<MapData> {
|
|||||||
|
|
||||||
#changeState(newState: State) {
|
#changeState(newState: State) {
|
||||||
this._state = newState;
|
this._state = newState;
|
||||||
this.$container.toggleClass("placing-note", newState === State.NewNote);
|
|
||||||
if (hasTouchBar) {
|
if (hasTouchBar) {
|
||||||
this.triggerCommand("refreshTouchBar");
|
this.triggerCommand("refreshTouchBar");
|
||||||
}
|
}
|
||||||
@@ -153,39 +148,6 @@ export default class GeoView extends ViewMode<MapData> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async geoMapCreateChildNoteEvent({ ntxId }: EventData<"geoMapCreateChildNote">) {
|
|
||||||
toast.showPersistent({
|
|
||||||
icon: "plus",
|
|
||||||
id: "geo-new-note",
|
|
||||||
title: "New note",
|
|
||||||
message: t("geo-map.create-child-note-instruction")
|
|
||||||
});
|
|
||||||
|
|
||||||
this.#changeState(State.NewNote);
|
|
||||||
|
|
||||||
const globalKeyListener: (this: Window, ev: KeyboardEvent) => any = (e) => {
|
|
||||||
if (e.key !== "Escape") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.#changeState(State.Normal);
|
|
||||||
|
|
||||||
window.removeEventListener("keydown", globalKeyListener);
|
|
||||||
toast.closePersistent("geo-new-note");
|
|
||||||
};
|
|
||||||
window.addEventListener("keydown", globalKeyListener);
|
|
||||||
}
|
|
||||||
|
|
||||||
async #onMapClicked(e: LeafletMouseEvent) {
|
|
||||||
if (this._state !== State.NewNote) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
toast.closePersistent("geo-new-note");
|
|
||||||
await createNewNote(this.parentNote.noteId, e);
|
|
||||||
this.#changeState(State.Normal);
|
|
||||||
}
|
|
||||||
|
|
||||||
deleteFromMapEvent({ noteId }: EventData<"deleteFromMap">) {
|
deleteFromMapEvent({ noteId }: EventData<"deleteFromMap">) {
|
||||||
moveMarker(noteId, null);
|
moveMarker(noteId, null);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user