UNPKG

react-mosaic-component2

Version:
101 lines (99 loc) 4.04 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/MosaicRoot.tsx var MosaicRoot_exports = {}; __export(MosaicRoot_exports, { MosaicRoot: () => MosaicRoot }); module.exports = __toCommonJS(MosaicRoot_exports); var import_lodash_es = require("lodash-es"); var import_react = __toESM(require("react"), 1); var import_contextTypes = require("./contextTypes.cjs"); var import_Split = require("./Split.cjs"); var import_BoundingBox = require("./util/BoundingBox.cjs"); var import_mosaicUtilities = require("./util/mosaicUtilities.cjs"); var MosaicRoot = class extends import_react.default.PureComponent { static contextType = import_contextTypes.MosaicContext; render() { const { root } = this.props; return /* @__PURE__ */ import_react.default.createElement("div", { className: "mosaic-root" }, this.renderRecursively(root, import_BoundingBox.BoundingBox.empty(), [])); } renderRecursively(node, boundingBox, path) { if ((0, import_mosaicUtilities.isParent)(node)) { const splitPercentage = node.splitPercentage == null ? 50 : node.splitPercentage; const { first, second } = import_BoundingBox.BoundingBox.split(boundingBox, splitPercentage, node.direction); return (0, import_lodash_es.flatten)( [ this.renderRecursively(node.first, first, path.concat("first")), this.renderSplit(node.direction, boundingBox, splitPercentage, path), this.renderRecursively(node.second, second, path.concat("second")) ].filter(nonNullElement) ); } else { return /* @__PURE__ */ import_react.default.createElement("div", { key: node, className: "mosaic-tile", style: { ...import_BoundingBox.BoundingBox.asStyles(boundingBox) } }, this.props.renderTile(node, path)); } } renderSplit(direction, boundingBox, splitPercentage, path) { const { resize } = this.props; if (resize !== "DISABLED") { return /* @__PURE__ */ import_react.default.createElement( import_Split.Split, { key: path.join(",") + "splitter", ...resize, boundingBox, splitPercentage, direction, onChange: (percentage) => this.onResize(percentage, path, true), onRelease: (percentage) => this.onResize(percentage, path, false) } ); } else { return null; } } onResize = (percentage, path, suppressOnRelease) => { this.context.mosaicActions.updateTree( [ { path, spec: { splitPercentage: { $set: percentage } } } ], suppressOnRelease ); }; }; function nonNullElement(x) { return x !== null; }