UNPKG

react-mosaic-component2

Version:
139 lines (137 loc) 4.26 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/mosaicUtilities.ts var mosaicUtilities_exports = {}; __export(mosaicUtilities_exports, { Corner: () => Corner, createBalancedTreeFromLeaves: () => createBalancedTreeFromLeaves, getAndAssertNodeAtPathExists: () => getAndAssertNodeAtPathExists, getLeaves: () => getLeaves, getNodeAtPath: () => getNodeAtPath, getOtherBranch: () => getOtherBranch, getOtherDirection: () => getOtherDirection, getPathToCorner: () => getPathToCorner, isParent: () => isParent }); module.exports = __toCommonJS(mosaicUtilities_exports); var import_lodash_es = require("lodash-es"); function alternateDirection(node, direction = "row") { if (isParent(node)) { const nextDirection = getOtherDirection(direction); return { direction, first: alternateDirection(node.first, nextDirection), second: alternateDirection(node.second, nextDirection) }; } else { return node; } } var Corner = /* @__PURE__ */ ((Corner2) => { Corner2[Corner2["TOP_LEFT"] = 1] = "TOP_LEFT"; Corner2[Corner2["TOP_RIGHT"] = 2] = "TOP_RIGHT"; Corner2[Corner2["BOTTOM_LEFT"] = 3] = "BOTTOM_LEFT"; Corner2[Corner2["BOTTOM_RIGHT"] = 4] = "BOTTOM_RIGHT"; return Corner2; })(Corner || {}); function isParent(node) { return node.direction != null; } function createBalancedTreeFromLeaves(leaves, startDirection = "row") { if (leaves.length === 0) { return null; } let current = (0, import_lodash_es.clone)(leaves); let next = []; while (current.length > 1) { while (current.length > 0) { if (current.length > 1) { next.push({ direction: "row", first: current.shift(), second: current.shift() }); } else { next.unshift(current.shift()); } } current = next; next = []; } return alternateDirection(current[0], startDirection); } function getOtherBranch(branch) { if (branch === "first") { return "second"; } else if (branch === "second") { return "first"; } else { throw new Error(`Branch '${branch}' not a valid branch`); } } function getOtherDirection(direction) { if (direction === "row") { return "column"; } else { return "row"; } } function getPathToCorner(tree, corner) { let currentNode = tree; const currentPath = []; while (isParent(currentNode)) { if (currentNode.direction === "row" && (corner === 1 /* TOP_LEFT */ || corner === 3 /* BOTTOM_LEFT */)) { currentPath.push("first"); currentNode = currentNode.first; } else if (currentNode.direction === "column" && (corner === 1 /* TOP_LEFT */ || corner === 2 /* TOP_RIGHT */)) { currentPath.push("first"); currentNode = currentNode.first; } else { currentPath.push("second"); currentNode = currentNode.second; } } return currentPath; } function getLeaves(tree) { if (tree == null) { return []; } else if (isParent(tree)) { return getLeaves(tree.first).concat(getLeaves(tree.second)); } else { return [tree]; } } function getNodeAtPath(tree, path) { if (path.length > 0) { return (0, import_lodash_es.get)(tree, path, null); } else { return tree; } } function getAndAssertNodeAtPathExists(tree, path) { if (tree == null) { throw new Error("Root is empty, cannot fetch path"); } const node = getNodeAtPath(tree, path); if (node == null) { throw new Error(`Path [${path.join(", ")}] did not resolve to a node`); } return node; }