riku
2025-04-27 f46786f11c5c08ead7501a82e5a71430ad69b782
miniprogram_npm/tdesign-miniprogram/upload/upload.js
@@ -36,6 +36,12 @@
            customFiles: [],
            customLimit: 0,
            column: 4,
            dragBaseData: {},
            rows: 0,
            dragWrapStyle: '',
            dragList: [],
            dragging: true,
            dragLayout: false,
        };
        this.properties = props;
        this.controlledProps = [
@@ -45,7 +51,7 @@
            },
        ];
        this.observers = {
            'files, max'(files, max) {
            'files, max, draggable'(files, max) {
                this.handleLimit(files, max);
            },
            gridConfig() {
@@ -86,7 +92,9 @@
                return Promise.resolve();
            },
            onAddTap() {
                const { mediaType, source } = this.properties;
                const { disabled, mediaType, source } = this.properties;
                if (disabled)
                    return;
                if (source === 'media') {
                    this.chooseMedia(mediaType);
                }
@@ -146,6 +154,51 @@
                this._trigger('add', { files });
                this.startUpload(files);
            },
            dragVibrate(e) {
                var _a;
                const { vibrateType } = e;
                const { draggable } = this.data;
                const dragVibrate = (_a = draggable === null || draggable === void 0 ? void 0 : draggable.vibrate) !== null && _a !== void 0 ? _a : true;
                const dragCollisionVibrate = draggable === null || draggable === void 0 ? void 0 : draggable.collisionVibrate;
                if ((dragVibrate && vibrateType === 'longPress') || (dragCollisionVibrate && vibrateType === 'touchMove')) {
                    wx.vibrateShort({
                        type: 'light',
                    });
                }
            },
            dragStatusChange(e) {
                const { dragging } = e;
                this.setData({ dragging });
            },
            dragEnd(e) {
                const { dragCollisionList } = e;
                let files = [];
                if (dragCollisionList.length === 0) {
                    files = this.data.customFiles;
                }
                else {
                    files = dragCollisionList.reduce((list, item) => {
                        const { realKey, data, fixed } = item;
                        if (!fixed) {
                            list[realKey] = Object.assign({}, data);
                        }
                        return list;
                    }, []);
                }
                this.triggerDropEvent(files);
            },
            triggerDropEvent(files) {
                const { transition } = this.properties;
                if (transition.backTransition) {
                    const timer = setTimeout(() => {
                        this.triggerEvent('drop', { files });
                        clearTimeout(timer);
                    }, transition.duration);
                }
                else {
                    this.triggerEvent('drop', { files });
                }
            },
        };
    }
    onProofTap(e) {
@@ -164,7 +217,9 @@
        this.setData({
            customFiles: customFiles.length > max ? customFiles.slice(0, max) : customFiles,
            customLimit: max - customFiles.length,
            dragging: true,
        });
        this.initDragLayout();
    }
    triggerSuccessEvent(files) {
        this._trigger('success', { files: [...this.data.customFiles, ...files] });
@@ -214,6 +269,82 @@
            column: column,
        });
    }
    initDragLayout() {
        const { draggable, disabled } = this.properties;
        if (!draggable || disabled)
            return;
        this.initDragList();
        this.initDragBaseData();
    }
    initDragList() {
        let i = 0;
        const { column, customFiles, customLimit } = this.data;
        const dragList = [];
        customFiles.forEach((item, index) => {
            dragList.push({
                realKey: i,
                sortKey: index,
                tranX: `${(index % column) * 100}%`,
                tranY: `${Math.floor(index / column) * 100}%`,
                data: Object.assign({}, item),
            });
            i += 1;
        });
        if (customLimit > 0) {
            const listLength = dragList.length;
            dragList.push({
                realKey: listLength,
                sortKey: listLength,
                tranX: `${(listLength % column) * 100}%`,
                tranY: `${Math.floor(listLength / column) * 100}%`,
                fixed: true,
            });
        }
        this.data.rows = Math.ceil(dragList.length / column);
        this.setData({
            dragList,
        });
    }
    initDragBaseData() {
        const { classPrefix, rows, column, customFiles } = this.data;
        if (customFiles.length === 0) {
            this.setData({
                dragBaseData: {},
                dragWrapStyle: '',
                dragLayout: false,
            });
            return;
        }
        const query = this.createSelectorQuery();
        const selectorGridItem = `.${classPrefix} >>> .t-grid-item`;
        const selectorGrid = `.${classPrefix} >>> .t-grid`;
        query.select(selectorGridItem).boundingClientRect();
        query.select(selectorGrid).boundingClientRect();
        query.selectViewport().scrollOffset();
        query.exec((res) => {
            const [{ width, height }, { left, top }, { scrollTop }] = res;
            const dragBaseData = {
                rows,
                classPrefix,
                itemWidth: width,
                itemHeight: height,
                wrapLeft: left,
                wrapTop: top + scrollTop,
                columns: column,
            };
            const dragWrapStyle = `height: ${rows * height}px`;
            this.setData({
                dragBaseData,
                dragWrapStyle,
                dragLayout: true,
            }, () => {
                const timer = setTimeout(() => {
                    this.setData({ dragging: false });
                    clearTimeout(timer);
                }, 0);
            });
        });
    }
};
Upload = __decorate([
    wxComponent()