Skip to content

Commit

Permalink
feat(editor): add a preference prevent scroll after get focused
Browse files Browse the repository at this point in the history
  • Loading branch information
bytemain committed Jul 12, 2024
1 parent 5ece2fb commit e25feac
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 16 deletions.
11 changes: 11 additions & 0 deletions packages/core-browser/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,14 @@ export class FocusTracker extends Disposable {
export function trackFocus(element: HTMLElement | Window): IFocusTracker {
return new FocusTracker(element);
}

/**
* https://developer.mozilla.org/zh-CN/docs/Web/API/MouseEvent/button
*/
export const MouseEventButton = {
Left: 0,
Middle: 1,
Right: 2,
Back: 3,
Forward: 4,
};
8 changes: 6 additions & 2 deletions packages/core-browser/src/services/clipboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,17 @@ export class BrowserClipboardService implements IClipboardService {
textArea.style.position = 'absolute';

textArea.value = text;
textArea.focus();
textArea.focus({
preventScroll: true,
});
textArea.select();

document.execCommand('copy');

if (activeElement instanceof HTMLElement) {
activeElement.focus();
activeElement.focus({
preventScroll: true,
});
}

document.body.removeChild(textArea);
Expand Down
2 changes: 1 addition & 1 deletion packages/core-common/src/settings/general.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const enum GeneralSettingsId {
export enum GeneralSettingsId {
Icon = 'general.icon',
Theme = 'general.theme',
Language = 'general.language',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ export class MonacoCommandRegistry implements IMonacoCommandsRegistry {
protected execute(monacoHandler: MonacoEditorCommandHandler, ...args: any[]): any {
const editor = this.getActiveCodeEditor();
if (editor) {
// editor.focus();
return Promise.resolve(monacoHandler.execute(editor, ...args));
}
return Promise.resolve();
Expand Down
10 changes: 7 additions & 3 deletions packages/editor/src/browser/tab.view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
Event,
IEventBus,
MaybeNull,
MouseEventButton,
PreferenceService,
ResizeEvent,
URI,
Expand Down Expand Up @@ -172,7 +173,10 @@ export const Tabs = ({ group }: ITabsProps) => {
'.' + styles.kt_editor_tab + "[data-uri='" + group.currentResource.uri.toString() + "']",
);
if (currentTab) {
currentTab.scrollIntoView();
currentTab.scrollIntoView({
block: 'nearest',
inline: 'nearest',
});
}
} catch (e) {
// noop
Expand Down Expand Up @@ -425,14 +429,14 @@ export const Tabs = ({ group }: ITabsProps) => {
}}
key={resource.uri.toString()}
onMouseUp={(e) => {
if (e.nativeEvent.which === 2) {
if (e.nativeEvent.button === MouseEventButton.Middle) {
e.preventDefault();
e.stopPropagation();
group.close(resource.uri);
}
}}
onMouseDown={(e) => {
if (e.nativeEvent.which === 1) {
if (e.nativeEvent.button === MouseEventButton.Left) {
group.open(resource.uri, { focus: true });
}
}}
Expand Down
20 changes: 17 additions & 3 deletions packages/editor/src/browser/workbench-editor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,11 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
return this.pinPreviewed(uri);
}

protected getPreventScrollOption(options: IResourceOpenOptions = {}) {
const preventScroll = options.preventScroll ?? this.preferenceService.get('editor.preventScrollAfterFocused');
return preventScroll;
}

async doOpen(
uri: URI,
options: IResourceOpenOptions = {},
Expand All @@ -1357,6 +1362,7 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
const previewMode =
this.preferenceService.get('editor.previewMode') &&
(isUndefinedOrNull(options.preview) ? true : options.preview);

if (
this.currentResource &&
this.currentResource.uri.isEqual(uri) &&
Expand All @@ -1365,7 +1371,10 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
) {
// 就是当前打开的 Resource
if (options.focus && this.currentEditor) {
this._domNode?.focus();
const preventScroll = this.getPreventScrollOption(options);
this._domNode?.focus({
preventScroll,
});
this.currentEditor.monacoEditor.focus();
}
if (options.range && this.currentEditor) {
Expand Down Expand Up @@ -1611,7 +1620,8 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
});

if (options.focus) {
this._domNode?.focus();
const preventScroll = this.getPreventScrollOption(options);
this._domNode?.focus({ preventScroll });
// monaco 编辑器的 focus 多了一步检查,由于此时其实对应编辑器的 dom 的 display 为 none (需要等 React 下一次渲染才会改变为 block),
// 会引起 document.activeElement !== editor.textArea.domNode,进而会导致focus失败
// 需要等待真正 append 之后再
Expand Down Expand Up @@ -1663,7 +1673,10 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {
}
await this.diffEditor.compare(original, modified, options, resource.uri);
if (options.focus) {
this._domNode?.focus();
const preventScroll = this.getPreventScrollOption(options);
this._domNode?.focus({
preventScroll,
});

const disposer = this.eventBus.on(CodeEditorDidVisibleEvent, (e) => {
if (e.payload.groupName === this.name && e.payload.type === EditorOpenType.diff) {
Expand Down Expand Up @@ -2192,6 +2205,7 @@ export class EditorGroup extends WithEventBus implements IGridEditorGroup {

focus() {
this.gainFocus();

if (this.currentOpenType && this.currentOpenType.type === EditorOpenType.code) {
this.codeEditor.focus();
}
Expand Down
5 changes: 5 additions & 0 deletions packages/editor/src/common/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,11 @@ export interface IResourceOpenOptions {
*/
focus?: boolean;

/**
* If set `focus`, the editor's dom will be focused, This option prevents the element from being scrolled after getting the focus.
*/
preventScroll?: boolean;

/**
* 强制使用指定的打开方式
*/
Expand Down
1 change: 0 additions & 1 deletion packages/terminal-next/src/browser/component/tab.item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const renderInfoItem = observer((props: ItemProps) => {
const iconService = useInjectable<IIconService>(IconService);
const handleSelect = debounce(() => props.onClick && props.onClick(), 20);
const handleClose = debounce(() => props.onClose && props.onClose(), 20);
const appConfig = useInjectable<AppConfig>(AppConfig);
const styles_item_container = useDesignStyles(styles.item_container, 'item_container');
const styles_tab_item_selected = useDesignStyles(styles.tab_item_selected, 'tab_item_selected');
const styles_item_info_name = useDesignStyles(styles.item_info_name, 'item_info_name');
Expand Down
12 changes: 7 additions & 5 deletions packages/theme/src/browser/icon.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ export class IconService extends WithEventBus implements IIconService {
}

private listen() {
this.preferenceService.onPreferenceChanged(async (e) => {
if (e.preferenceName === GeneralSettingsId.Icon && this.iconContributionRegistry.has(e.newValue)) {
await this.applyTheme(this.preferenceService.get<string>(GeneralSettingsId.Icon)!);
}
});
this.addDispose(
this.preferenceService.onSpecificPreferenceChange(GeneralSettingsId.Icon, async (e) => {
if (this.iconContributionRegistry.has(e.newValue)) {
await this.applyTheme(e.newValue);
}
}),
);
}

private styleSheetCollection = '';
Expand Down

0 comments on commit e25feac

Please sign in to comment.