@octostar/map-component 0.3.4 → 0.3.6

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/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { jsxs, jsx, Fragment } from "react/jsx-runtime";
2
- import { useState, useEffect, useSyncExternalStore, useCallback, useMemo, createContext, useContext, useRef, forwardRef, useImperativeHandle } from "react";
1
+ import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
+ import { useRef, useLayoutEffect, useState, useEffect, useSyncExternalStore, useCallback, useMemo, createContext, useContext, forwardRef, useImperativeHandle } from "react";
3
3
  import maplibregl from "maplibre-gl";
4
4
  import MapboxDraw from "@mapbox/mapbox-gl-draw";
5
5
  import * as turf from "@turf/turf";
@@ -2175,6 +2175,68 @@ function frameYield(scheduler = defaultScheduler, fallbackMs = 50) {
2175
2175
  }, fallbackMs);
2176
2176
  });
2177
2177
  }
2178
+ function placeContextMenu(point2, parent, menu) {
2179
+ const scaleX = parent.offsetWidth > 0 ? parent.rect.width / parent.offsetWidth : 1;
2180
+ const scaleY = parent.offsetHeight > 0 ? parent.rect.height / parent.offsetHeight : 1;
2181
+ const left = (point2.x - parent.rect.left) / scaleX - parent.clientLeft;
2182
+ const top = (point2.y - parent.rect.top) / scaleY - parent.clientTop;
2183
+ return {
2184
+ left: fit(left, menu.width, parent.clientWidth),
2185
+ top: fit(top, menu.height, parent.clientHeight)
2186
+ };
2187
+ }
2188
+ function fit(start, size, available) {
2189
+ const opened = start + size > available ? start - size : start;
2190
+ return Math.max(0, Math.min(opened, available - size));
2191
+ }
2192
+ function MapContextMenu({ x, y, items, onClose }) {
2193
+ const menuRef = useRef(null);
2194
+ useLayoutEffect(() => {
2195
+ const menu = menuRef.current;
2196
+ const parent = menu == null ? void 0 : menu.offsetParent;
2197
+ if (!menu || !(parent instanceof HTMLElement)) return;
2198
+ const { left, top } = placeContextMenu(
2199
+ { x, y },
2200
+ {
2201
+ rect: parent.getBoundingClientRect(),
2202
+ offsetWidth: parent.offsetWidth,
2203
+ offsetHeight: parent.offsetHeight,
2204
+ clientLeft: parent.clientLeft,
2205
+ clientTop: parent.clientTop,
2206
+ clientWidth: parent.clientWidth,
2207
+ clientHeight: parent.clientHeight
2208
+ },
2209
+ { width: menu.offsetWidth, height: menu.offsetHeight }
2210
+ );
2211
+ menu.style.left = `${left}px`;
2212
+ menu.style.top = `${top}px`;
2213
+ }, [x, y, items]);
2214
+ return /* @__PURE__ */ jsx(
2215
+ "div",
2216
+ {
2217
+ ref: menuRef,
2218
+ className: "z-[9999] min-w-[160px] max-h-full overflow-y-auto rounded-md border bg-popover p-1 shadow-md",
2219
+ style: { position: "absolute", left: 0, top: 0 },
2220
+ "data-testid": "context-menu",
2221
+ children: items.map((item) => /* @__PURE__ */ jsx(
2222
+ "button",
2223
+ {
2224
+ className: `flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover-elevate active-elevate-2 ${item.disabled ? "opacity-50 cursor-not-allowed" : "cursor-pointer"} ${item.destructive ? "text-destructive" : ""}`,
2225
+ disabled: item.disabled,
2226
+ onClick: () => {
2227
+ if (!item.disabled) {
2228
+ item.onSelect();
2229
+ onClose();
2230
+ }
2231
+ },
2232
+ "data-testid": `context-menu-item-${item.id}`,
2233
+ children: item.label
2234
+ },
2235
+ item.id
2236
+ ))
2237
+ }
2238
+ );
2239
+ }
2178
2240
  function CursorCoordinatesOverlay({ map }) {
2179
2241
  const [coords, setCoords] = useState(null);
2180
2242
  useEffect(() => {
@@ -7382,7 +7444,7 @@ function formatDateTime(date) {
7382
7444
  minute: "2-digit"
7383
7445
  });
7384
7446
  }
7385
- function Histogram({ bins, maxCount, colorGroupedBins }) {
7447
+ function Histogram({ bins, maxCount, colorGroupedBins, selectedBins }) {
7386
7448
  const [hoveredBin, setHoveredBin] = useState(null);
7387
7449
  const containerRef = useRef(null);
7388
7450
  if (bins.length === 0 || maxCount === 0) return null;
@@ -7441,7 +7503,8 @@ function Histogram({ bins, maxCount, colorGroupedBins }) {
7441
7503
  /* @__PURE__ */ jsxs("div", { className: "text-muted-foreground", children: [
7442
7504
  bins[hoveredBin].count,
7443
7505
  " event",
7444
- bins[hoveredBin].count !== 1 ? "s" : ""
7506
+ bins[hoveredBin].count !== 1 ? "s" : "",
7507
+ !!(selectedBins == null ? void 0 : selectedBins[hoveredBin]) && ` (${selectedBins[hoveredBin]} selected)`
7445
7508
  ] })
7446
7509
  ]
7447
7510
  }
@@ -7512,7 +7575,9 @@ function Timebar({
7512
7575
  return { ...bin, count };
7513
7576
  });
7514
7577
  }, [hasSelection, selectionAsLine, storeHistogramBins, selectedTemporalFeatures]);
7515
- const colorGroupedBins = histogramBins.length > 0 && hasSelection && (selectionAsLine || selectedFeatureColors && selectedFeatureColors.size > 0) && globalMinDate && globalMaxDate ? (() => {
7578
+ const colorGroupedBins = useMemo(() => {
7579
+ if (histogramBins.length === 0 || !hasSelection || !globalMinDate || !globalMaxDate) return [];
7580
+ if (!selectionAsLine && !(selectedFeatureColors && selectedFeatureColors.size > 0)) return [];
7516
7581
  if (selectedTemporalFeatures.length === 0) return [];
7517
7582
  const colorGroups = /* @__PURE__ */ new Map();
7518
7583
  selectedTemporalFeatures.forEach((f) => {
@@ -7536,7 +7601,11 @@ function Timebar({
7536
7601
  result.push({ color, bins });
7537
7602
  });
7538
7603
  return result;
7539
- })() : [];
7604
+ }, [hasSelection, selectionAsLine, histogramBins, selectedTemporalFeatures, selectedFeatureColors, globalMinDate, globalMaxDate]);
7605
+ const selectedBins = useMemo(
7606
+ () => selectionAsLine ? histogramBins.map((_, i) => colorGroupedBins.reduce((sum, group) => sum + group.bins[i], 0)) : void 0,
7607
+ [selectionAsLine, histogramBins, colorGroupedBins]
7608
+ );
7540
7609
  const animationFrameRef = useRef(null);
7541
7610
  const lastFrameTimeRef = useRef(0);
7542
7611
  const playbackDuration = 1e4;
@@ -7654,7 +7723,8 @@ function Timebar({
7654
7723
  {
7655
7724
  bins: histogramBins,
7656
7725
  maxCount: maxBinCount,
7657
- colorGroupedBins
7726
+ colorGroupedBins,
7727
+ selectedBins
7658
7728
  }
7659
7729
  ),
7660
7730
  /* @__PURE__ */ jsx("div", { className: "mt-1", children: /* @__PURE__ */ jsx(
@@ -16816,31 +16886,17 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
16816
16886
  {
16817
16887
  selectedFeatureIds,
16818
16888
  selectedFeatureColors,
16889
+ selectionDisplay: "line",
16819
16890
  timebarExtra
16820
16891
  }
16821
16892
  ),
16822
16893
  contextMenu && /* @__PURE__ */ jsx(
16823
- "div",
16894
+ MapContextMenu,
16824
16895
  {
16825
- className: "fixed z-[9999] min-w-[160px] rounded-md border bg-popover p-1 shadow-md",
16826
- style: { left: contextMenu.x, top: contextMenu.y },
16827
- "data-testid": "context-menu",
16828
- children: contextMenu.items.map((item) => /* @__PURE__ */ jsx(
16829
- "button",
16830
- {
16831
- className: `flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-sm hover-elevate active-elevate-2 ${item.disabled ? "opacity-50 cursor-not-allowed" : "cursor-pointer"} ${item.destructive ? "text-destructive" : ""}`,
16832
- disabled: item.disabled,
16833
- onClick: () => {
16834
- if (!item.disabled) {
16835
- item.onSelect();
16836
- setContextMenu(null);
16837
- }
16838
- },
16839
- "data-testid": `context-menu-item-${item.id}`,
16840
- children: item.label
16841
- },
16842
- item.id
16843
- ))
16896
+ x: contextMenu.x,
16897
+ y: contextMenu.y,
16898
+ items: contextMenu.items,
16899
+ onClose: () => setContextMenu(null)
16844
16900
  }
16845
16901
  ),
16846
16902
  /* @__PURE__ */ jsx(CursorCoordinatesOverlay, { map: isMapLoaded ? map.current : null }),