纯js实现编辑器撤消/重做(回退)功能 undo/redo

Core:基础类-后期所有的编辑修改类都继承于它

setStep:保存方法,所有的修改操作都会出触发,用于保存到后端及保存用户操作
_selectionNavUndo:撤消方法
_selectionNavRedo:回退方法

EditorText:继承于Core的修改文字的操作类(实例)

setText:EditorText的修改文字方法

编辑器修改流程

初始化基础类--->继承基础类--->建立监听--->修改内容--->保存操作--->上传后端(本地可忽略)

核心JS

//基类
			var Core = function() {}
			Core.prototype = {
				arrayPrevStep: [], //存放撤消操作方法列表
				arrayNextStep: [], //存放恢复操作方法列表
				triggerUndo: false, //撤消操作标记
				triggerRedo: false, //恢复操作标记
				_undo: null, //撤消按钮
				_redo: null, //恢复按钮
				setStep: function(func) {   //操作之后触发保存的方法(调用后台保存方法)
					if (this.triggerUndo) {
						this.arrayNextStep.push(func);

						if (this._redo.classList.contains('unactive')) {
							this._redo.classList.remove('unactive');
						}
					} else {
						if (!this.triggerRedo) Core.prototype.arrayNextStep = [];
						this.arrayPrevStep.push(func);

						if (this.arrayPrevStep.length > 20) {
							this.arrayPrevStep.shift();
						}

						if (this._undo.classList.contains('unactive')) {
							this._undo.classList.remove('unactive');
						}
						if (this.arrayNextStep.length < 1) {
							this._redo.classList.add('unactive');
						}
					}

					Core.prototype.triggerUndo = false;
					Core.prototype.triggerRedo = false;
				},
				_selectionNavUndo: function() {
					var _this = this;

					_this._undo.addEventListener('click', function() {

						var head = _this.arrayPrevStep.length - 1;
						if (head !== -1) {
							Core.prototype.triggerUndo = true;

							try {
								_this.arrayPrevStep[head]();
							} catch (e) {
								_this.arrayPrevStep = [];
							}

							Core.prototype.arrayPrevStep = _this.arrayPrevStep.slice(0, head);
							if (_this.arrayPrevStep.length === 0 &&
								!_this._undo.classList.contains('unactive')) {
								_this._undo.classList.add('unactive');
							}
						}
					});
				},
				_selectionNavRedo: function() {
					var _this = this;

					_this._redo.addEventListener('click', function() {

						var head = _this.arrayNextStep.length - 1;
						if (head !== -1) {
							Core.prototype.triggerRedo = true;
							try {
								_this.arrayNextStep[head]();
								Core.prototype.arrayNextStep = _this.arrayNextStep.slice(0, head);
							} catch (e) {
								Core.prototype.arrayPrevStep = [];
								Core.prototype.arrayNextStep = [];
								Core.prototype.triggerUndo = false;
								Core.prototype.triggerRedo = false;
								console.error(e);
							}
							if (_this.arrayNextStep.length === 0 &&
								!_this._redo.classList.contains('unactive')) {
								_this._redo.classList.add('unactive');
							}
						}
					});
				}
			}
			Core.prototype.constructor = Core;

			Core.prototype._undo = document.querySelector('.undo');
			Core.prototype._redo = document.querySelector('.redo');
			
			// 初始化撤消/恢复按钮
			Core.prototype._selectionNavUndo();
			Core.prototype._selectionNavRedo();


			// 操作场景(输入框改变)
			var EditorText = function(el) {
				var _this = this;
				this._targetObject = el;
				this._targetObject.addEventListener("change", function(e) {
					var saveVal = _this._text;
					_this._text = this.value;
					_this.setText(_this._text, saveVal);
				})
			}
			
			// 继承Core基础类,并新增EditorText特有方法
			EditorText.prototype = Object.assign(Object.create(Core.prototype), {
				_targetObject: null,
				_text: "",
				setText: function(newValue, oldValue) {
					var _this = this;
					_this._targetObject.value = newValue;
					_this.setStep(function() {
						_this.setText(oldValue, newValue)
					})
				}
			})
			EditorText.prototype.constructor = EditorText;
			
			document.querySelectorAll("input").forEach(item => {
				// 建立监听
				new EditorText(item);
			});

HTML

<html lang="zh">
	<body>
		<style>
			.main {
				margin: 400px auto;
				width: 500px;
			}

			.btn-box {
				margin-top: 20px;
			}
			
			button{
				background-color: #07c160;
				color: #ffffff;
				border: none;
				padding: 6px 18px;
				border-radius: 3px;
			}
			
			button.unactive{
				cursor: no-drop;
				background-color: #f6f6f6;
				color: #999999;
			}
		</style>
		<div class="main">
			<div>
				<input type="text" name="name" placeholder="请输入姓名">
				<input type="text" name="age" placeholder="请输入年龄">
			</div>

			<div class="btn-box">
				<button class="undo unactive">撤销</button>
				<button class="redo unactive">恢复</button>
			</div>
		</div>

		<script>
			//核心js
		</script>
	</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Eclipse 中,可以使用 `org.eclipse.ui.actions.ActionFactory.UNDO` 和 `org.eclipse.ui.actions.ActionFactory.REDO` 来创建 UndoRedo 动作。然后,将这些动作关联到菜单栏 Edit 中的 UndoRedo 菜单项。 要在 `ViewPart` 中实现 Undo/Redo,您需要遵循以下步骤: 1. 在 `ViewPart` 类中添加以下字段: ```java private IUndoContext undoContext; private IUndoableOperation undoOperation; private IAction undoAction; private IAction redoAction; ``` 2. 在 `createPartControl` 方法中初始化这些字段: ```java public void createPartControl(Composite parent) { // 创建 Undo/Redo 上下文 undoContext = new ObjectUndoContext(new Object()); // 创建 Undo/Redo 动作 undoAction = ActionFactory.UNDO.create(getSite().getWorkbenchWindow()); undoAction.setText("Undo"); undoAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_TOOL_UNDO)); undoAction.setEnabled(false); redoAction = ActionFactory.REDO.create(getSite().getWorkbenchWindow()); redoAction.setText("Redo"); redoAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages() .getImageDescriptor(ISharedImages.IMG_TOOL_REDO)); redoAction.setEnabled(false); // 将 Undo/Redo 动作添加到菜单栏 Edit 中 IMenuManager menuMgr = getViewSite().getActionBars().getMenuManager(); menuMgr.add(new Separator()); menuMgr.add(undoAction); menuMgr.add(redoAction); } ``` 3. 在 `createPartControl` 方法中为 Undo/Redo 动作添加处理程序: ```java public void createPartControl(Composite parent) { // ... // 为 Undo/Redo 动作添加处理程序 undoAction.setHandler(new UndoActionHandler(getViewSite(), undoContext)); redoAction.setHandler(new RedoActionHandler(getViewSite(), undoContext)); } ``` 4. 在 `createPartControl` 方法中为 `parent` 控件创建上下文菜单,并将 Undo/Redo 动作添加到上下文菜单中: ```java public void createPartControl(Composite parent) { // ... // 为 parent 控件创建上下文菜单,并将 Undo/Redo 动作添加到上下文菜单中 MenuManager menuMgr = new MenuManager(); menuMgr.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); getSite().registerContextMenu(menuMgr, getSite().getSelectionProvider()); parent.setMenu(menuMgr.createContextMenu(parent)); menuMgr.addMenuListener(new IMenuListener() { public void menuAboutToShow(IMenuManager manager) { manager.add(new Separator()); manager.add(undoAction); manager.add(redoAction); } }); } ``` 5. 在 `ViewPart` 类中添加以下方法: ```java public void executeCommand(final IUndoableOperation operation) { // 如果操作不可撤销,则返回 if (!operation.canUndo()) { return; } // 如果当前有未完成的撤消操作,则合并操作 if (undoOperation != null) { undoOperation.addContext(operation.getContext()); undoOperation.add(operation); } else { // 否则,开始新的撤消操作 undoOperation = operation; } // 注册撤消操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.add(undoOperation); // 更新 Undo/Redo 动作的状态 undoAction.setEnabled(undoOperation.canUndo()); redoAction.setEnabled(undoOperation.canRedo()); // 将操作添加到 Undo/Redo 上下文中 undoContext.addMatch(undoOperation); } public void undo() { // 撤消上一个操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.undo(undoContext, null, null); } public void redo() { // 重做上一个操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.redo(undoContext, null, null); } ``` 6. 在 `ViewPart` 类中添加以下代码,以便在视图关闭时清除 Undo/Redo 上下文中的操作: ```java public void dispose() { // 清除 Undo/Redo 上下文中的操作 IOperationHistory operationHistory = OperationHistoryFactory.getOperationHistory(); operationHistory.dispose(undoContext, true, true, true); super.dispose(); } ``` 现在,您可以在 `executeCommand` 方法中执行所有需要撤消/恢复支持的操作,并且在菜单栏 Edit 中的 Undo/Redo 菜单项中使用 Undo/Redo 动作来撤消/恢复操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值