UNPKG

@antv/s2

Version:

effective spreadsheet render core lib

69 lines 3.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createResizeObserver = exports.analyzeAdaptive = void 0; const tslib_1 = require("tslib"); const lodash_1 = require("lodash"); const utils_1 = require("../../utils"); const resize_1 = require("../constant/resize"); const analyzeAdaptive = (defaultContainer, adaptive) => { var _a, _b, _c; if ((0, lodash_1.isBoolean)(adaptive)) { return { container: defaultContainer, adaptiveWidth: true, adaptiveHeight: false, }; } return { container: ((_a = adaptive === null || adaptive === void 0 ? void 0 : adaptive.getContainer) === null || _a === void 0 ? void 0 : _a.call(adaptive)) || defaultContainer, adaptiveWidth: (_b = adaptive === null || adaptive === void 0 ? void 0 : adaptive.width) !== null && _b !== void 0 ? _b : true, adaptiveHeight: (_c = adaptive === null || adaptive === void 0 ? void 0 : adaptive.height) !== null && _c !== void 0 ? _c : true, }; }; exports.analyzeAdaptive = analyzeAdaptive; const createResizeObserver = (params) => { let isFirstRender = true; const { s2, adaptive, container, wrapper } = params; const { container: actualWrapper, adaptiveWidth, adaptiveHeight, } = (0, exports.analyzeAdaptive)(wrapper, adaptive); if (!actualWrapper || !container || !adaptive || !s2) { return; } const render = (width, height) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { s2 === null || s2 === void 0 ? void 0 : s2.changeSheetSize(width, height); yield (s2 === null || s2 === void 0 ? void 0 : s2.render(false)); }); const debounceRender = (0, lodash_1.debounce)(render, resize_1.RESIZE_RENDER_DELAY); const onResize = () => tslib_1.__awaiter(void 0, void 0, void 0, function* () { // 解决父容器有缩放, 获取宽高不对的问题: https://github.com/antvis/S2/pull/1425 const { clientWidth: containerWidth, clientHeight: containerHeight } = container; const width = adaptiveWidth ? (0, utils_1.floor)(containerWidth !== null && containerWidth !== void 0 ? containerWidth : s2.options.width) : s2.options.width; // 去除 header 和 page 后才是 sheet 真正的高度 const height = adaptiveHeight ? (0, utils_1.floor)(containerHeight !== null && containerHeight !== void 0 ? containerHeight : s2.options.height) : s2.options.height; if (!adaptiveWidth && !adaptiveHeight) { return; } if (isFirstRender) { yield render(width, height); isFirstRender = false; return; } debounceRender(width, height); }); const resizeObserver = new ResizeObserver(([entry] = []) => { if (entry) { onResize(); } }); resizeObserver.observe(actualWrapper, { box: 'border-box', }); return () => { resizeObserver.unobserve(actualWrapper); }; }; exports.createResizeObserver = createResizeObserver; //# sourceMappingURL=resize.js.map