SOURCE

class Cell {
    constructor(value) {
        this.value = value;
        this.isMerged = false;
        this.mergeOrigin = null;
    }
}

class Table {
    constructor(data) {
        this.data = data.map(row => row.map(value => new Cell(value)));
        this.history = []; // 保存操作历史记录
        this.redoStack = []; // 保存被撤销的操作
    }

    getCell(row, col) {
        return this.data[row][col];
    }
}


Table.prototype.mergeCells = function(startRow, startCol, endRow, endCol) {
    const action = {
        type: 'merge',
        startRow,
        startCol,
        endRow,
        endCol,
        originalValues: []
    };

    // 记录原始值
    for (let i = startRow; i <= endRow; i++) {
        const row = [];
        for (let j = startCol; j <= endCol; j++) {
            row.push({ ...this.getCell(i, j) });
        }
        action.originalValues.push(row);
    }

    // 执行合并操作
    const mergedValue = this.getCell(startRow, startCol).value;
    for (let i = startRow; i <= endRow; i++) {
        for (let j = startCol; j <= endCol; j++) {
            if (i === startRow && j === startCol) {
                this.getCell(i, j).value = mergedValue;
                this.getCell(i, j).isMerged = true;
                this.getCell(i, j).mergeOrigin = { row: startRow, col: startCol };
            } else {
                this.getCell(i, j).value = "MERGED";
                this.getCell(i, j).isMerged = true;
                this.getCell(i, j).mergeOrigin = { row: startRow, col: startCol };
            }
        }
    }

    // 保存历史记录并清空重做堆栈
    this.history.push(action);
    this.redoStack = [];
};


Table.prototype.undo = function() {
    if (this.history.length === 0) return;

    const action = this.history.pop();
    this.redoStack.push(action);

    // 撤销合并操作
    const { startRow, startCol, endRow, endCol, originalValues } = action;
    for (let i = startRow; i <= endRow; i++) {
        for (let j = startCol; j <= endCol; j++) {
            this.data[i][j] = originalValues[i - startRow][j - startCol];
        }
    }
};

Table.prototype.redo = function() {
    if (this.redoStack.length === 0) return;

    const action = this.redoStack.pop();
    this.history.push(action);

    // 重新执行合并操作
    this.mergeCells(action.startRow, action.startCol, action.endRow, action.endCol);
};



// 示例表格数据
let tableData = [
    ["A1", "B1", "C1", "D1"],
    ["A2", "B2", "C2", "D2"],
    ["A3", "B3", "C3", "D3"],
    ["A4", "B4", "C4", "D4"]
];

// 创建表格对象
let table = new Table(tableData);

// 执行合并操作
table.mergeCells(1, 1, 2, 2);
console.log("After merge:", JSON.parse(JSON.stringify(table.data)));

// 撤销合并操作
table.undo();
console.log("After undo:", JSON.parse(JSON.stringify(table.data)));

// 重做合并操作
table.redo();
console.log("After redo:", JSON.parse(JSON.stringify(table.data)));

console 命令行工具 X clear

                    
>
console