@h4wldev/react-naver-maps
Version:
React Navermaps API integration for modern development.
130 lines (128 loc) • 3.26 kB
JavaScript
import {
getUncontrolledKey,
makeUncontrolledKeyMap
} from "./chunk-YNLQN2GP.mjs";
import {
omitUndefined
} from "./chunk-SOBCLIXD.mjs";
import {
getKeys
} from "./chunk-4MXCHP7N.mjs";
import {
useNavermaps
} from "./chunk-VVUNPVHD.mjs";
import {
HandleEvents
} from "./chunk-D77WM3Z3.mjs";
import {
Overlay
} from "./chunk-P2HFB547.mjs";
// src/overlays/marker.tsx
import mapKeys from "lodash.mapkeys";
import pick from "lodash.pick";
import { forwardRef, useLayoutEffect, useImperativeHandle, useRef, useState } from "react";
import { useFirstMountState } from "react-use";
import { jsx } from "react/jsx-runtime";
var primitiveKeys = [
"animation",
"icon",
"shape",
"title",
"cursor",
"clickable",
"draggable",
"visible",
"zIndex"
];
var locationalKeys = ["position"];
var uncontrolledKeyMap = makeUncontrolledKeyMap(locationalKeys);
var kvoKeys = [
...primitiveKeys,
...locationalKeys
];
var kvoEvents = kvoKeys.map((key) => `${key}_changed`);
var uiEvents = [
"mousedown",
"mouseup",
"click",
"dblclick",
"rightclick",
"mouseover",
"mouseout",
"dragstart",
"drag",
"dragend"
];
var events = [...uiEvents, ...kvoEvents];
function makeInitialOption(props) {
const uncontrolledProps = pick(props, getKeys(uncontrolledKeyMap));
const prefixCleared = mapKeys(uncontrolledProps, (_, key) => uncontrolledKeyMap[key]);
const kvoProps = pick(props, kvoKeys);
return omitUndefined({ ...kvoProps, ...prefixCleared });
}
function isLocationalKey(key) {
return locationalKeys.includes(key);
}
function isEqualKvo(kvo, target) {
if (kvo === void 0) {
return false;
}
if (kvo === target) {
return true;
}
try {
return kvo.equals(target);
} catch {
return kvo === target;
}
}
var Marker = forwardRef(function Marker2(props, ref) {
const navermaps = useNavermaps();
const [marker] = useState(() => new navermaps.Marker(makeInitialOption(props)));
useImperativeHandle(ref, () => marker);
const isFirst = useFirstMountState();
const dirtiesRef = useRef({});
dirtiesRef.current = getDirties();
function getDirties() {
if (isFirst) {
return {};
}
return kvoKeys.reduce((acc, key) => {
if (props[key] === void 0) {
return acc;
}
if (isLocationalKey(key) && props[getUncontrolledKey(key)] !== void 0) {
return acc;
}
const kvos = marker.getOptions(key);
if (isEqualKvo(kvos[key], props[key])) {
return acc;
}
return {
...acc,
[key]: props[key]
};
}, {});
}
function pickDirties(keys) {
return pick(dirtiesRef.current, keys);
}
useLayoutEffect(() => {
const { position } = pickDirties(["position"]);
if (position) {
marker.setPosition(position);
}
}, [dirtiesRef.current["position"]]);
useLayoutEffect(() => {
const dirties = pickDirties(primitiveKeys);
if (Object.values(dirties).length < 1) {
return;
}
marker.setOptions(dirties);
}, primitiveKeys.map((key) => dirtiesRef.current[key]));
return /* @__PURE__ */ jsx(Overlay, { element: marker, children: /* @__PURE__ */ jsx(HandleEvents, { events, listeners: props }) });
});
export {
Marker
};
//# sourceMappingURL=chunk-3SRLO46M.mjs.map