UNPKG

react-mosaic-component2

Version:
146 lines (144 loc) 5.37 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/util/mosaicUpdates.ts var mosaicUpdates_exports = {}; __export(mosaicUpdates_exports, { buildSpecFromUpdate: () => buildSpecFromUpdate, createDragToUpdates: () => createDragToUpdates, createExpandUpdate: () => createExpandUpdate, createHideUpdate: () => createHideUpdate, createRemoveUpdate: () => createRemoveUpdate, updateTree: () => updateTree }); module.exports = __toCommonJS(mosaicUpdates_exports); var import_immutability_helper = __toESM(require("immutability-helper"), 1); var import_lodash_es = require("lodash-es"); var import_internalTypes = require("../internalTypes.cjs"); var import_mosaicUtilities = require("./mosaicUtilities.cjs"); function buildSpecFromUpdate(mosaicUpdate) { if (mosaicUpdate.path.length > 0) { return (0, import_lodash_es.set)({}, mosaicUpdate.path, mosaicUpdate.spec); } else { return mosaicUpdate.spec; } } function updateTree(root, updates) { let currentNode = root; updates.forEach((mUpdate) => { currentNode = (0, import_immutability_helper.default)(currentNode, buildSpecFromUpdate(mUpdate)); }); return currentNode; } function createRemoveUpdate(root, path) { const parentPath = (0, import_lodash_es.dropRight)(path); const nodeToRemove = (0, import_lodash_es.last)(path); const siblingPath = parentPath.concat((0, import_mosaicUtilities.getOtherBranch)(nodeToRemove)); const sibling = (0, import_mosaicUtilities.getAndAssertNodeAtPathExists)(root, siblingPath); return { path: parentPath, spec: { $set: sibling } }; } function isPathPrefixEqual(a, b, length) { return (0, import_lodash_es.isEqual)((0, import_lodash_es.take)(a, length), (0, import_lodash_es.take)(b, length)); } function createDragToUpdates(root, sourcePath, destinationPath, position) { let destinationNode = (0, import_mosaicUtilities.getAndAssertNodeAtPathExists)(root, destinationPath); const updates = []; const destinationIsParentOfSource = isPathPrefixEqual(sourcePath, destinationPath, destinationPath.length); if (destinationIsParentOfSource) { destinationNode = updateTree(destinationNode, [ createRemoveUpdate(destinationNode, (0, import_lodash_es.drop)(sourcePath, destinationPath.length)) ]); } else { updates.push(createRemoveUpdate(root, sourcePath)); const removedNodeParentIsInPath = isPathPrefixEqual(sourcePath, destinationPath, sourcePath.length - 1); if (removedNodeParentIsInPath) { destinationPath.splice(sourcePath.length - 1, 1); } } const sourceNode = (0, import_mosaicUtilities.getAndAssertNodeAtPathExists)(root, sourcePath); let first; let second; if (position === import_internalTypes.MosaicDropTargetPosition.LEFT || position === import_internalTypes.MosaicDropTargetPosition.TOP) { first = sourceNode; second = destinationNode; } else { first = destinationNode; second = sourceNode; } let direction = "column"; if (position === import_internalTypes.MosaicDropTargetPosition.LEFT || position === import_internalTypes.MosaicDropTargetPosition.RIGHT) { direction = "row"; } updates.push({ path: destinationPath, spec: { $set: { first, second, direction } } }); return updates; } function createHideUpdate(path) { const targetPath = (0, import_lodash_es.dropRight)(path); const thisBranch = (0, import_lodash_es.last)(path); let splitPercentage; if (thisBranch === "first") { splitPercentage = 0; } else { splitPercentage = 100; } return { path: targetPath, spec: { splitPercentage: { $set: splitPercentage } } }; } function createExpandUpdate(path, percentage) { let spec = {}; for (let i = path.length - 1; i >= 0; i--) { const branch = path[i]; const splitPercentage = branch === "first" ? percentage : 100 - percentage; spec = { splitPercentage: { $set: splitPercentage }, [branch]: spec }; } return { spec, path: [] }; }