You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Monaco Editor是否支持查询重做(redo)栈状态?

如何判断重做栈是否有内容以禁用重做按钮

手动维护撤销/重做栈的场景

如果你是自己实现的撤销/重做逻辑,核心就是直接检查重做栈的长度:

  • 你肯定已经用数组(比如redoStack)存储了待重做的操作记录,只需要判断redoStack.length === 0,就能确定是否没有可重做内容。
  • 每次执行撤销或重做操作后,同步调用更新按钮状态的函数即可。

示例代码:

// 定义存储操作的栈
let undoStack = [];
let redoStack = [];

// 更新重做按钮状态
function updateRedoButton() {
  const redoBtn = document.getElementById('redo-btn');
  redoBtn.disabled = redoStack.length === 0;
}

// 撤销操作逻辑
function undo() {
  if (undoStack.length > 0) {
    const lastState = undoStack.pop();
    redoStack.push(lastState);
    // 应用撤销后的状态逻辑...
    updateRedoButton();
  }
}

// 重做操作逻辑
function redo() {
  if (redoStack.length > 0) {
    const nextState = redoStack.pop();
    undoStack.push(nextState);
    // 应用重做后的状态逻辑...
    updateRedoButton();
  }
}

使用第三方状态管理库的场景

如果是基于Redux、Vuex这类库实现的撤销/重做,它们的配套插件通常会暴露判断栈状态的属性:

  • 比如Redux的redux-undo插件,会在状态中维护past(撤销栈)和future(重做栈)数组,通过state.future.length === 0就能判断是否无可重做内容。
  • Vuex的vuex-undo-redo等插件,也会提供对应的getter方法获取栈长度,根据返回值控制按钮状态。

示例(redux-undo):

import { createStore } from 'redux';
import undoable from 'redux-undo';

// 基础状态reducer
const baseReducer = (state = {}, action) => {
  // 你的状态更新逻辑
};

// 包装为支持撤销/重做的reducer
const undoableReducer = undoable(baseReducer);
const store = createStore(undoableReducer);

// 获取重做栈长度
const getRedoCount = () => store.getState().future.length;

// 订阅状态变化,实时更新按钮
store.subscribe(() => {
  const redoBtn = document.getElementById('redo-btn');
  redoBtn.disabled = getRedoCount() === 0;
});

内容的提问来源于stack exchange,提问作者Justin Greywolf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 11:57:16