Skip to content

Commit 871bd1c

Browse files
committed
feat: undo redo interceptor and change helper
1 parent 52dbb25 commit 871bd1c

2 files changed

Lines changed: 61 additions & 9 deletions

File tree

src/editor/EditorCommandHandlers.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,11 +1190,45 @@ define(function (require, exports, module) {
11901190
return result.promise();
11911191
}
11921192

1193+
let _undoInterceptor = null;
1194+
let _redoInterceptor = null;
1195+
1196+
/**
1197+
* Use require("editor/EditorHelper/ChangeHelper") to intercept. this for internal use only
1198+
* @private
1199+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
1200+
*/
1201+
function _setUndoInterceptor(interceptor) {
1202+
_undoInterceptor = interceptor;
1203+
}
1204+
1205+
/**
1206+
* Use require("editor/EditorHelper/ChangeHelper") to intercept. this for internal use only
1207+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
1208+
*/
1209+
function _setRedoInterceptor(interceptor) {
1210+
_redoInterceptor = interceptor;
1211+
}
1212+
11931213
function handleUndo() {
1214+
if(_undoInterceptor){
1215+
const focusedEditor = EditorManager.getFocusedEditor();
1216+
const codeMirror = focusedEditor && focusedEditor._codeMirror;
1217+
if(_undoInterceptor(focusedEditor, codeMirror, null)){
1218+
return;
1219+
}
1220+
}
11941221
return handleUndoRedo("undo");
11951222
}
11961223

11971224
function handleRedo() {
1225+
if(_redoInterceptor){
1226+
const focusedEditor = EditorManager.getFocusedEditor();
1227+
const codeMirror = focusedEditor && focusedEditor._codeMirror;
1228+
if(_redoInterceptor(focusedEditor, codeMirror, null)){
1229+
return;
1230+
}
1231+
}
11981232
return handleUndoRedo("redo");
11991233
}
12001234

@@ -1273,4 +1307,7 @@ define(function (require, exports, module) {
12731307
CommandManager.register(Strings.CMD_COPY, Commands.EDIT_COPY, _execCommandCopy);
12741308
CommandManager.register(Strings.CMD_PASTE, Commands.EDIT_PASTE, _execCommandPaste);
12751309
CommandManager.register(Strings.CMD_SELECT_ALL, Commands.EDIT_SELECT_ALL, _handleSelectAll);
1310+
1311+
exports._setUndoInterceptor = _setUndoInterceptor;
1312+
exports._setRedoInterceptor = _setRedoInterceptor;
12761313
});

src/editor/EditorHelper/ChangeHelper.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ define(function (require, exports, module) {
3131
let _keyEventInterceptor = null;
3232

3333
const CodeMirror = require("thirdparty/CodeMirror/lib/codemirror"),
34-
Menus = require("command/Menus");
34+
Menus = require("command/Menus"),
35+
EditorCommandHandlers = require("editor/EditorCommandHandlers");
3536

3637
function _applyChanges(changeList) {
3738
// eslint-disable-next-line no-invalid-this
@@ -314,43 +315,57 @@ define(function (require, exports, module) {
314315
Editor.prototype._dontDismissPopupOnScroll = _dontDismissPopupOnScroll;
315316
}
316317

318+
/**
319+
* Sets the undo interceptor function in before it goes to codemirror
320+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
321+
*/
322+
function setUndoInterceptor(interceptor) {
323+
EditorCommandHandlers._setUndoInterceptor(interceptor);
324+
}
325+
326+
/**
327+
* Sets the redo interceptor function in before it goes to codemirror
328+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
329+
*/
330+
function setRedoInterceptor(interceptor) {
331+
EditorCommandHandlers._setRedoInterceptor(interceptor);
332+
}
333+
317334
/**
318335
* Sets the cut interceptor function in codemirror
319-
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
320-
preventDefault
336+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
321337
*/
322338
function setCutInterceptor(interceptor) {
323339
_cutInterceptor = interceptor;
324340
}
325341

326342
/**
327343
* Sets the copy interceptor function in codemirror
328-
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
329-
preventDefault
344+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
330345
*/
331346
function setCopyInterceptor(interceptor) {
332347
_copyInterceptor = interceptor;
333348
}
334349

335350
/**
336351
* Sets the paste interceptor function in codemirror
337-
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
338-
preventDefault
352+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
339353
*/
340354
function setPasteInterceptor(interceptor) {
341355
_pasteInterceptor = interceptor;
342356
}
343357

344358
/**
345359
* Sets the key down/up/press interceptor function in codemirror
346-
* @param {Function} interceptor - Function(editor, cm, event) that returns true to
347-
preventDefault
360+
* @param {Function} interceptor - Function(editor, cm, event) that returns true to preventDefault
348361
*/
349362
function setKeyEventInterceptor(interceptor) {
350363
_keyEventInterceptor = interceptor;
351364
}
352365

353366
exports.addHelpers =addHelpers;
367+
exports.setUndoInterceptor = setUndoInterceptor;
368+
exports.setRedoInterceptor = setRedoInterceptor;
354369
exports.setCutInterceptor = setCutInterceptor;
355370
exports.setCopyInterceptor = setCopyInterceptor;
356371
exports.setPasteInterceptor = setPasteInterceptor;

0 commit comments

Comments
 (0)