@glideapps/glide-data-grid
Version:
React data grid for beautifully displaying and editing large amounts of data with amazing performance.
79 lines • 3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RenderStateProvider = exports.WindowingTrackerBase = exports.unpackNumberToColRow = exports.unpackRow = exports.unpackCol = exports.packColRowToNumber = void 0;
const support_js_1 = require("./support.js");
// max safe int 2^53 - 1 (minus 1 omitted from here on)
// max safe columns is 2^21 or 2,097,151
// max safe rows is 2^32 or 4,294,967,295
// If 3 rows render as an inch, then the max safe height is 1,431,655,765 inches or 22,426,868 miles
// the distance to the moon is 238,900 miles, so this would give you a data grid that goes to the moon and back 94 times
// seems fine
const rowShift = 1 << 21;
function packColRowToNumber(col, row) {
return (row + 2) * rowShift + col;
}
exports.packColRowToNumber = packColRowToNumber;
function unpackCol(packed) {
return packed % rowShift;
}
exports.unpackCol = unpackCol;
function unpackRow(packed) {
return Math.floor(packed / rowShift) - 2;
}
exports.unpackRow = unpackRow;
function unpackNumberToColRow(packed) {
const col = unpackCol(packed);
const row = unpackRow(packed);
return [col, row];
}
exports.unpackNumberToColRow = unpackNumberToColRow;
class WindowingTrackerBase {
visibleWindow = {
x: 0,
y: 0,
width: 0,
height: 0,
};
freezeCols = 0;
freezeRows = [];
isInWindow = (packed) => {
const col = unpackCol(packed);
const row = unpackRow(packed);
const w = this.visibleWindow;
const colInWindow = (col >= w.x && col <= w.x + w.width) || col < this.freezeCols;
const rowInWindow = (row >= w.y && row <= w.y + w.height) || this.freezeRows.includes(row);
return colInWindow && rowInWindow;
};
setWindow(newWindow, freezeCols, freezeRows) {
if (this.visibleWindow.x === newWindow.x &&
this.visibleWindow.y === newWindow.y &&
this.visibleWindow.width === newWindow.width &&
this.visibleWindow.height === newWindow.height &&
this.freezeCols === freezeCols &&
(0, support_js_1.deepEqual)(this.freezeRows, freezeRows))
return;
this.visibleWindow = newWindow;
this.freezeCols = freezeCols;
this.freezeRows = freezeRows;
this.clearOutOfWindow();
}
}
exports.WindowingTrackerBase = WindowingTrackerBase;
class RenderStateProvider extends WindowingTrackerBase {
cache = new Map();
setValue = (location, state) => {
this.cache.set(packColRowToNumber(location[0], location[1]), state);
};
getValue = (location) => {
return this.cache.get(packColRowToNumber(location[0], location[1]));
};
clearOutOfWindow = () => {
for (const [key] of this.cache.entries()) {
if (!this.isInWindow(key)) {
this.cache.delete(key);
}
}
};
}
exports.RenderStateProvider = RenderStateProvider;
//# sourceMappingURL=render-state-provider.js.map