@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.cjs CHANGED
@@ -2195,6 +2195,68 @@ function frameYield(scheduler = defaultScheduler, fallbackMs = 50) {
2195
2195
  }, fallbackMs);
2196
2196
  });
2197
2197
  }
2198
+ function placeContextMenu(point, parent, menu) {
2199
+ const scaleX = parent.offsetWidth > 0 ? parent.rect.width / parent.offsetWidth : 1;
2200
+ const scaleY = parent.offsetHeight > 0 ? parent.rect.height / parent.offsetHeight : 1;
2201
+ const left = (point.x - parent.rect.left) / scaleX - parent.clientLeft;
2202
+ const top = (point.y - parent.rect.top) / scaleY - parent.clientTop;
2203
+ return {
2204
+ left: fit(left, menu.width, parent.clientWidth),
2205
+ top: fit(top, menu.height, parent.clientHeight)
2206
+ };
2207
+ }
2208
+ function fit(start, size, available) {
2209
+ const opened = start + size > available ? start - size : start;
2210
+ return Math.max(0, Math.min(opened, available - size));
2211
+ }
2212
+ function MapContextMenu({ x, y, items, onClose }) {
2213
+ const menuRef = react.useRef(null);
2214
+ react.useLayoutEffect(() => {
2215
+ const menu = menuRef.current;
2216
+ const parent = menu == null ? void 0 : menu.offsetParent;
2217
+ if (!menu || !(parent instanceof HTMLElement)) return;
2218
+ const { left, top } = placeContextMenu(
2219
+ { x, y },
2220
+ {
2221
+ rect: parent.getBoundingClientRect(),
2222
+ offsetWidth: parent.offsetWidth,
2223
+ offsetHeight: parent.offsetHeight,
2224
+ clientLeft: parent.clientLeft,
2225
+ clientTop: parent.clientTop,
2226
+ clientWidth: parent.clientWidth,
2227
+ clientHeight: parent.clientHeight
2228
+ },
2229
+ { width: menu.offsetWidth, height: menu.offsetHeight }
2230
+ );
2231
+ menu.style.left = `${left}px`;
2232
+ menu.style.top = `${top}px`;
2233
+ }, [x, y, items]);
2234
+ return /* @__PURE__ */ jsxRuntime.jsx(
2235
+ "div",
2236
+ {
2237
+ ref: menuRef,
2238
+ className: "z-[9999] min-w-[160px] max-h-full overflow-y-auto rounded-md border bg-popover p-1 shadow-md",
2239
+ style: { position: "absolute", left: 0, top: 0 },
2240
+ "data-testid": "context-menu",
2241
+ children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
2242
+ "button",
2243
+ {
2244
+ 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" : ""}`,
2245
+ disabled: item.disabled,
2246
+ onClick: () => {
2247
+ if (!item.disabled) {
2248
+ item.onSelect();
2249
+ onClose();
2250
+ }
2251
+ },
2252
+ "data-testid": `context-menu-item-${item.id}`,
2253
+ children: item.label
2254
+ },
2255
+ item.id
2256
+ ))
2257
+ }
2258
+ );
2259
+ }
2198
2260
  function CursorCoordinatesOverlay({ map }) {
2199
2261
  const [coords, setCoords] = react.useState(null);
2200
2262
  react.useEffect(() => {
@@ -16849,27 +16911,12 @@ const MapEditorCanvas = react.forwardRef(function MapEditorCanvas2({
16849
16911
  }
16850
16912
  ),
16851
16913
  contextMenu && /* @__PURE__ */ jsxRuntime.jsx(
16852
- "div",
16914
+ MapContextMenu,
16853
16915
  {
16854
- className: "fixed z-[9999] min-w-[160px] rounded-md border bg-popover p-1 shadow-md",
16855
- style: { left: contextMenu.x, top: contextMenu.y },
16856
- "data-testid": "context-menu",
16857
- children: contextMenu.items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
16858
- "button",
16859
- {
16860
- 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" : ""}`,
16861
- disabled: item.disabled,
16862
- onClick: () => {
16863
- if (!item.disabled) {
16864
- item.onSelect();
16865
- setContextMenu(null);
16866
- }
16867
- },
16868
- "data-testid": `context-menu-item-${item.id}`,
16869
- children: item.label
16870
- },
16871
- item.id
16872
- ))
16916
+ x: contextMenu.x,
16917
+ y: contextMenu.y,
16918
+ items: contextMenu.items,
16919
+ onClose: () => setContextMenu(null)
16873
16920
  }
16874
16921
  ),
16875
16922
  /* @__PURE__ */ jsxRuntime.jsx(CursorCoordinatesOverlay, { map: isMapLoaded ? map.current : null }),