@1771technologies/lytenyte-pro
Version:
Blazingly fast headless React data grid with 100s of features.
113 lines (112 loc) • 5.55 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { Fallback, LnInternalShareProvider, Root as RootCore, usePiece, } from "@1771technologies/lytenyte-core/internal";
import { forwardRef, memo, useEffect, useMemo, useRef, useState, } from "react";
import { useProAPI } from "./hooks/use-pro-api.js";
import { ProRootProvider } from "./context.js";
import { hasAValidLicense, licenseState } from "../license.js";
import { useControlled, useEvent, useRoot } from "@1771technologies/lytenyte-core/internal";
import { CellSelectionDriver } from "../cell-selection/cell-selection-driver/cell-selection-driver.js";
import { CellSelectionBottom, CellSelectionCenter, CellSelectionTop, } from "../cell-selection/cell-selection-containers.js";
import { splitCellSelectionRect } from "../cell-selection/split-cell-selection-rect.js";
const RootWrapper = ({ children, ...p }, forwarded) => {
useEffect(() => {
if (hasAValidLicense)
return;
const existing = document.getElementById("lng1771-watermark");
if (existing)
return;
const invalidLicenseWatermark = document.createElement("div");
invalidLicenseWatermark.style.position = "fixed";
invalidLicenseWatermark.style.bottom = "0px";
invalidLicenseWatermark.style.insetInlineEnd = "0px";
invalidLicenseWatermark.style.background = "rgb(255, 167, 167)";
invalidLicenseWatermark.style.color = "black";
invalidLicenseWatermark.style.fontSize = "1.2rem";
invalidLicenseWatermark.style.fontWeight = "bold";
invalidLicenseWatermark.style.border = "1px solid black";
invalidLicenseWatermark.style.padding = "16px";
if (licenseState === "expired")
invalidLicenseWatermark.innerHTML = `LyteNyte Grid: License key expired. Your license covers earlier versions only.`;
else if (licenseState === "invalid")
invalidLicenseWatermark.innerHTML = `LyteNyte Grid: Invalid license key. Please verify the key and try again.`;
else
invalidLicenseWatermark.innerHTML = `LyteNyte Grid PRO is being used for evaluation.
<a href="https://1771Technologies.com/pricing">Click here</a> to secure your license.`;
document.body.appendChild(invalidLicenseWatermark);
return () => invalidLicenseWatermark.remove();
}, []);
const components = p.cellSelectionMode !== "none"
? {
ln_topComponent: CellSelectionTop,
ln_bottomComponent: CellSelectionBottom,
ln_centerComponent: CellSelectionCenter,
}
: {};
return (_jsx(RootCore, { ref: forwarded, ...p, ...components, __noFallback: true, children: _jsx(RootImpl, { ...p, children: children }) }));
};
const RootImpl = ({ children, ...p }) => {
const { view, source } = useRoot();
const cellSelectionMode = p.cellSelectionMode ?? "none";
const [cellSelections, setCellSelections] = useControlled({
controlled: p.cellSelections,
default: [],
});
const onCellSelectionChange = useEvent((rects) => {
p.onCellSelectionChange?.(rects);
setCellSelections(rects);
});
const api = useProAPI(cellSelections);
const [cellSelectionPivot, setSelectionPivot] = useState(null);
const [cellSelectionAdditiveRects, setCellSelectionAdditiveRects] = useState(null);
const cellSelectionIsDeselect = useRef(false);
const topCount = source.useTopCount();
const rowCount = source.useRowCount();
const botCount = source.useBottomCount();
const cellSelectionSplits = useMemo(() => {
const centerCount = rowCount - topCount - botCount;
const selections = cellSelections;
const splits = selections.flatMap((rect) => {
return splitCellSelectionRect({
rect,
rowTopCount: topCount,
rowCenterCount: centerCount,
colStartCount: view.startCount,
colCenterCount: view.centerCount,
});
});
return splits;
}, [botCount, cellSelections, rowCount, topCount, view.centerCount, view.startCount]);
const excludeMarker = Boolean(p.columnMarker?.on && p.cellSelectionExcludeMarker);
const value = useMemo(() => {
return {
api,
excludeMarker,
keepSelection: p.cellSelectionMaintainOnNonCellPosition ?? false,
cellSelectionMode,
cellSelections,
cellSelectionPivot,
setSelectionPivot: setSelectionPivot,
onCellSelectionChange,
cellSelectionAdditiveRects,
setCellSelectionAdditiveRects,
cellSelectionIsDeselect,
cellSelectionSplits,
};
}, [
api,
excludeMarker,
p.cellSelectionMaintainOnNonCellPosition,
cellSelectionMode,
cellSelections,
cellSelectionPivot,
onCellSelectionChange,
cellSelectionAdditiveRects,
cellSelectionSplits,
]);
const cellSelectionsPiece = usePiece(cellSelections);
return (_jsx(LnInternalShareProvider, { value: useMemo(() => ({
cellSelections: cellSelectionsPiece,
hasCellSelection: (p.cellSelectionMode ?? "none") !== "none",
}), [cellSelectionsPiece, p.cellSelectionMode]), children: _jsxs(ProRootProvider, { value: value, children: [cellSelectionMode !== "none" && _jsx(CellSelectionDriver, {}), children ?? _jsx(Fallback, {})] }) }));
};
export const Root = memo(forwardRef(RootWrapper));