Skip to content

Commit

Permalink
feat: support navigate editor history by mouse 3/4 (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
erha19 authored and bytemain committed Sep 27, 2022
1 parent 1c1c59a commit 7a6f9fe
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
16 changes: 10 additions & 6 deletions packages/editor/src/browser/editor.contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ export class EditorContribution
private editorDocumentModelService: IEditorDocumentModelService;

@Autowired(IDocPersistentCacheProvider)
cacheProvider: IDocPersistentCacheProvider;
private cacheProvider: IDocPersistentCacheProvider;

@Autowired()
historyService: EditorHistoryService;
private historyService: EditorHistoryService;

@Autowired()
monacoService: MonacoService;
private monacoService: MonacoService;

@Autowired(IOpenerService)
private readonly openerService: IOpenerService;
Expand Down Expand Up @@ -431,7 +431,7 @@ export class EditorContribution

initialize() {
this.editorStatusBarService.setListener();
this.historyService.start();
this.historyService.init();
}

registerCommands(commands: CommandRegistry): void {
Expand Down Expand Up @@ -1340,13 +1340,17 @@ export class EditorAutoSaveEditorContribution implements BrowserEditorContributi
});
commands.registerCommand(EDITOR_COMMANDS.COPY_PATH, {
execute: (uri) => {
if (!uri) {return;}
if (!uri) {
return;
}
this.commandService.executeCommand(FILE_COMMANDS.COPY_PATH.id, uri);
},
});
commands.registerCommand(EDITOR_COMMANDS.COPY_RELATIVE_PATH, {
execute: (uri) => {
if (!uri) {return;}
if (!uri) {
return;
}
this.commandService.executeCommand(FILE_COMMANDS.COPY_RELATIVE_PATH.id, uri);
},
});
Expand Down
57 changes: 52 additions & 5 deletions packages/editor/src/browser/history/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Injectable, Autowired } from '@opensumi/di';
import { IPosition, URI, WithEventBus, OnEvent } from '@opensumi/ide-core-browser';
import {
IPosition,
URI,
WithEventBus,
OnEvent,
PreferenceService,
addDisposableListener,
EventType,
DisposableCollection,
} from '@opensumi/ide-core-browser';

import { WorkbenchEditorService } from '../../common';
import { EditorSelectionChangeEvent, EditorGroupChangeEvent, EditorGroupCloseEvent } from '../types';
Expand All @@ -10,18 +19,56 @@ const SoftMaxStateLength = 100;

@Injectable()
export class EditorHistoryService extends WithEventBus {
private static readonly MOUSE_NAVIGATION_SETTING = 'editor.mouseBackForwardToNavigate';

@Autowired(PreferenceService)
private preferenceService: PreferenceService;

@Autowired(WorkbenchEditorService)
private editorService: WorkbenchEditorService;

private currentIndex = -1;

private stack: EditorHistoryState[] = [];

private closedStack: URI[] = [];

start() {
// do nothing
init() {
this.registerMouseNavigationListener();
}

@Autowired(WorkbenchEditorService)
editorService: WorkbenchEditorService;
private registerMouseNavigationListener() {
const disposables = new DisposableCollection();
const handleMouseBackForwardSupport = () => {
disposables.dispose();
if (this.preferenceService.get(EditorHistoryService.MOUSE_NAVIGATION_SETTING)) {
disposables.push(addDisposableListener(window.document, EventType.MOUSE_DOWN, (e) => this.onMouseDown(e)));
}
this.disposables.push(disposables);
};
this.disposables.push(
this.preferenceService.onSpecificPreferenceChange(EditorHistoryService.MOUSE_NAVIGATION_SETTING, () => {
if (this.preferenceService.get(EditorHistoryService.MOUSE_NAVIGATION_SETTING)) {
handleMouseBackForwardSupport();
}
}),
);
handleMouseBackForwardSupport();
}

private onMouseDown(event: MouseEvent) {
// Support to navigate in history when mouse buttons 4/5 are pressed
switch (event.button) {
case 3:
event.stopPropagation();
this.back();
break;
case 4:
event.stopPropagation();
this.forward();
break;
}
}

@OnEvent(EditorSelectionChangeEvent)
onEditorSelectionChangeEvent(e: EditorSelectionChangeEvent) {
Expand Down
7 changes: 6 additions & 1 deletion packages/editor/src/browser/preference/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1861,14 +1861,19 @@ const customEditorSchema: PreferenceSchemaProperties = {
},
'diffEditor.ignoreTrimWhitespace': {
type: 'boolean',
default: false, // 开天修改
default: false,
description: '%diffEditor.configuration.ignoreTrimWhitespace%',
},
'editor.experimental.stickyScroll.enabled': {
type: 'boolean',
default: false,
description: '%editor.experimental.stickyScroll.enabled.description%',
},
'editor.mouseBackForwardToNavigate': {
type: 'boolean',
default: true,
description: '%editor.configuration.mouseBackForwardToNavigate%',
},
};

export const editorPreferenceSchema: PreferenceSchema = {
Expand Down
4 changes: 3 additions & 1 deletion packages/i18n/src/common/en-US.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,18 +116,20 @@ export const localizationBundle = {
'editor.format.chooseFormatter': 'Choose the Formatter',
'editor.formatDocument.label.multiple': 'Format Document With...',
'editor.formatSelection.label.multiple': 'Format Selection With...',
'editor.configuration.preferredFormatter': 'Preferred formatter for files',
'editor.chooseEncoding': 'Reopen with encoding (Unsaved Changes will be lost)',
'editor.guessEncodingFromContent': 'Guess from content',
'editor.changeEol': 'Select End Of Line Sequence',
'editor.failToOpen': 'Failed to open {0}. Error message: {1}',
'editor.changeLanguageId': 'Select Language Mode',
'editor.configuration.preferredFormatter': 'Preferred formatter for files',
'editor.configuration.maxTokenizationLineLength':
'Lines above this length will not be tokenized for performance reasons',
'editor.configuration.quickSuggestionsDelay': 'Quick suggestions show delay (in ms) Defaults to 100 (ms)',
'editor.configuration.bracketPairColorization.enabled':
"Controls whether bracket pair colorization is enabled or not. Use 'workbench.colorCustomizations' to override the bracket highlight colors.",
'editor.configuration.guides.bracketPairs': 'Controls whether bracket pair guides are enabled or not.',
'editor.configuration.mouseBackForwardToNavigate':
"Enables the use of mouse buttons four and five for commands 'Go Back' and 'Go Forward'.",
'editor.lineHeight': 'Line Height',
'editor.lineHeight.description':
'Controls the line height.\r\nUse 0 to automatically compute the line height from the font size.\r\nValues between 0 and 8 will be used as a multiplier with the font size.\r\nValues greater than or equal to 8 will be used as effective values.',
Expand Down
1 change: 1 addition & 0 deletions packages/i18n/src/common/zh-CN.lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ export const localizationBundle = {
'editor.configuration.bracketPairColorization.enabled':
'控制是否启用括号对着色。使用 “workbench.colorCustomizations” 替代括号突出显示颜色。',
'editor.configuration.guides.bracketPairs': '控制是否启用括号对指南。',
'editor.configuration.mouseBackForwardToNavigate': '允许使用鼠标按钮 4 和 5 来执行命令“Go Back”和“Go Forward”。',
'editor.autoSave.enum.off': '不启用',
'editor.files.eol': '文件行尾字符',
'editor.files.eolDesc': '控制文件默认行尾字符。',
Expand Down

0 comments on commit 7a6f9fe

Please sign in to comment.