UNPKG

@eccenca/gui-elements

Version:

GUI elements based on other libraries, usable in React application, written in Typescript.

116 lines 6.32 kB
var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { memo, useEffect } from "react"; import { MiniMap as ReactFlowMiniMapV9 } from "react-flow-renderer"; import { miniMapUtils } from "./utils.js"; import { ReactFlowVersions, useReactFlowVersion } from "../versionsupport.js"; import { MiniMapV12 } from "./MiniMapV12.js"; var minimapCalcConf = { navigationOn: false, minimapElement: null, flowElement: null, }; /** An improved mini-map for react-flow that supports navigation via the mini-map. */ export var MiniMap = memo(function (_a) { var flowVersion = _a.flowVersion, otherProps = __rest(_a, ["flowVersion"]); var flowVersionCheck = flowVersion || useReactFlowVersion(); switch (flowVersionCheck) { case ReactFlowVersions.V9: return React.createElement(MiniMapV9, __assign({}, otherProps)); case ReactFlowVersions.V12: return React.createElement(MiniMapV12, __assign({}, otherProps)); default: return React.createElement(React.Fragment, null); // cannot exit on its own } }); /** * Mini-map support for for React Flow v9. * @deprecated (v26) will be removed, use when `MiniMap` directly */ export var MiniMapV9 = memo(function (_a) { var _b; var flowInstance = _a.flowInstance, _c = _a.enableNavigation, enableNavigation = _c === void 0 ? false : _c, _d = _a.maskColor, maskColor = _d === void 0 ? "#ddddddbb" : _d, _e = _a.nodeClassName, nodeClassName = _e === void 0 ? miniMapUtils.nodeClassName : _e, _f = _a.nodeColor, nodeColor = _f === void 0 ? miniMapUtils.nodeColor : _f, _g = _a.nodeStrokeColor, nodeStrokeColor = _g === void 0 ? miniMapUtils.borderColor : _g, wrapperProps = _a.wrapperProps, minimapProps = __rest(_a, ["flowInstance", "enableNavigation", "maskColor", "nodeClassName", "nodeColor", "nodeStrokeColor", "wrapperProps"]); var minimapWrapper = React.useRef(null); useEffect(function () { var minimapDiv = minimapWrapper.current; if (enableNavigation && flowInstance && minimapDiv) { minimapCalcConf = { navigationOn: false, minimapElement: minimapDiv.querySelector(".react-flow__minimap"), flowElement: minimapDiv.closest(".react-flow"), }; } }, [flowInstance, enableNavigation]); // sets the visible area of the canvas based on mouse movement on the mini-map var handleMiniMapMouseMove = function (event) { var _a, _b; var minimapConfig = (_b = (_a = minimapCalcConf.minimapElement) === null || _a === void 0 ? void 0 : _a.getAttribute("viewBox")) === null || _b === void 0 ? void 0 : _b.split(" "); if (minimapCalcConf.navigationOn && minimapCalcConf.minimapElement && minimapCalcConf.flowElement && flowInstance && minimapConfig) { var minimapBounds = minimapCalcConf.minimapElement.getBoundingClientRect(); var canvasBounds = minimapCalcConf.flowElement.getBoundingClientRect(); var instanceState = flowInstance.toObject(); var minimapCoordinates = { x0: parseInt(minimapConfig[0]), y0: parseInt(minimapConfig[1]), x1: parseInt(minimapConfig[2]) + parseInt(minimapConfig[0]), y1: parseInt(minimapConfig[3]) + parseInt(minimapConfig[1]), }; var minimapClick = { x: event.clientX - minimapBounds.left, y: event.clientY - minimapBounds.top, }; var canvasPosition = { x: ((minimapCoordinates.x1 - minimapCoordinates.x0) / minimapBounds.width) * minimapClick.x * -1 - minimapCoordinates.x0, y: ((minimapCoordinates.y1 - minimapCoordinates.y0) / minimapBounds.height) * minimapClick.y * -1 - minimapCoordinates.y0, }; var canvasNewState = { zoom: instanceState.zoom, x: canvasPosition.x * instanceState.zoom + canvasBounds.width / 2, y: canvasPosition.y * instanceState.zoom + canvasBounds.height / 2, }; flowInstance.setTransform(canvasNewState); } }; // sets the view for the user when clicked and finish navigation var handleMiniMapMouseUp = function (event) { handleMiniMapMouseMove(event); minimapCalcConf.navigationOn = false; }; // enables the mini-map fake drag effect see "handleMiniMapMouseMove" above. var handleMiniMapMouseDown = function () { if (enableNavigation && flowInstance) { minimapCalcConf.navigationOn = true; } }; return (React.createElement("div", __assign({ ref: minimapWrapper, onMouseDown: handleMiniMapMouseDown, onMouseUp: handleMiniMapMouseUp, onMouseMove: handleMiniMapMouseMove, onMouseLeave: handleMiniMapMouseUp }, wrapperProps, { style: flowInstance && enableNavigation ? __assign(__assign({}, ((_b = wrapperProps === null || wrapperProps === void 0 ? void 0 : wrapperProps.style) !== null && _b !== void 0 ? _b : {})), { cursor: "grab" }) : wrapperProps === null || wrapperProps === void 0 ? void 0 : wrapperProps.style }), React.createElement(ReactFlowMiniMapV9, __assign({ maskColor: maskColor, nodeClassName: nodeClassName, nodeColor: nodeColor, nodeStrokeColor: nodeStrokeColor }, minimapProps)))); }); export default MiniMap; //# sourceMappingURL=MiniMap.js.map