@octostar/map-component 0.3.5 → 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(() => {
@@ -16829,27 +16891,12 @@ const MapEditorCanvas = forwardRef(function MapEditorCanvas2({
16829
16891
  }
16830
16892
  ),
16831
16893
  contextMenu && /* @__PURE__ */ jsx(
16832
- "div",
16894
+ MapContextMenu,
16833
16895
  {
16834
- className: "fixed z-[9999] min-w-[160px] rounded-md border bg-popover p-1 shadow-md",
16835
- style: { left: contextMenu.x, top: contextMenu.y },
16836
- "data-testid": "context-menu",
16837
- children: contextMenu.items.map((item) => /* @__PURE__ */ jsx(
16838
- "button",
16839
- {
16840
- 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" : ""}`,
16841
- disabled: item.disabled,
16842
- onClick: () => {
16843
- if (!item.disabled) {
16844
- item.onSelect();
16845
- setContextMenu(null);
16846
- }
16847
- },
16848
- "data-testid": `context-menu-item-${item.id}`,
16849
- children: item.label
16850
- },
16851
- item.id
16852
- ))
16896
+ x: contextMenu.x,
16897
+ y: contextMenu.y,
16898
+ items: contextMenu.items,
16899
+ onClose: () => setContextMenu(null)
16853
16900
  }
16854
16901
  ),
16855
16902
  /* @__PURE__ */ jsx(CursorCoordinatesOverlay, { map: isMapLoaded ? map.current : null }),