-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor undo-redo action for editors (#13963)
- Loading branch information
Showing
22 changed files
with
367 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { inject, injectable, named, postConstruct } from 'inversify'; | ||
import { ContributionProvider } from '../common'; | ||
|
||
export const UndoRedoHandler = Symbol('UndoRedoHandler'); | ||
|
||
export interface UndoRedoHandler<T> { | ||
priority: number; | ||
select(): T | undefined; | ||
undo(item: T): void; | ||
redo(item: T): void; | ||
} | ||
|
||
@injectable() | ||
export class UndoRedoHandlerService { | ||
|
||
@inject(ContributionProvider) @named(UndoRedoHandler) | ||
protected readonly provider: ContributionProvider<UndoRedoHandler<unknown>>; | ||
|
||
protected handlers: UndoRedoHandler<unknown>[]; | ||
|
||
@postConstruct() | ||
protected init(): void { | ||
this.handlers = this.provider.getContributions().sort((a, b) => b.priority - a.priority); | ||
} | ||
|
||
undo(): void { | ||
for (const handler of this.handlers) { | ||
const selection = handler.select(); | ||
if (selection) { | ||
handler.undo(selection); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
redo(): void { | ||
for (const handler of this.handlers) { | ||
const selection = handler.select(); | ||
if (selection) { | ||
handler.redo(selection); | ||
return; | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
@injectable() | ||
export class DomInputUndoRedoHandler implements UndoRedoHandler<Element> { | ||
|
||
priority = 1000; | ||
|
||
select(): Element | undefined { | ||
const element = document.activeElement; | ||
if (element && ['input', 'textarea'].includes(element.tagName.toLowerCase())) { | ||
return element; | ||
} | ||
return undefined; | ||
} | ||
|
||
undo(item: Element): void { | ||
document.execCommand('undo'); | ||
} | ||
|
||
redo(item: Element): void { | ||
document.execCommand('redo'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { UndoRedoHandler } from '@theia/core/lib/browser'; | ||
import { injectable } from '@theia/core/shared/inversify'; | ||
import { ICodeEditor } from '@theia/monaco-editor-core/esm/vs/editor/browser/editorBrowser'; | ||
import { ICodeEditorService } from '@theia/monaco-editor-core/esm/vs/editor/browser/services/codeEditorService'; | ||
import { StandaloneServices } from '@theia/monaco-editor-core/esm/vs/editor/standalone/browser/standaloneServices'; | ||
|
||
@injectable() | ||
export abstract class AbstractMonacoUndoRedoHandler implements UndoRedoHandler<ICodeEditor> { | ||
priority: number; | ||
abstract select(): ICodeEditor | undefined; | ||
undo(item: ICodeEditor): void { | ||
item.trigger('MonacoUndoRedoHandler', 'undo', undefined); | ||
} | ||
redo(item: ICodeEditor): void { | ||
item.trigger('MonacoUndoRedoHandler', 'redo', undefined); | ||
} | ||
} | ||
|
||
@injectable() | ||
export class FocusedMonacoUndoRedoHandler extends AbstractMonacoUndoRedoHandler { | ||
override priority = 10000; | ||
|
||
protected codeEditorService = StandaloneServices.get(ICodeEditorService); | ||
|
||
override select(): ICodeEditor | undefined { | ||
const focusedEditor = this.codeEditorService.getFocusedCodeEditor(); | ||
if (focusedEditor && focusedEditor.hasTextFocus()) { | ||
return focusedEditor; | ||
} | ||
return undefined; | ||
} | ||
} | ||
|
||
@injectable() | ||
export class ActiveMonacoUndoRedoHandler extends AbstractMonacoUndoRedoHandler { | ||
override priority = 0; | ||
|
||
protected codeEditorService = StandaloneServices.get(ICodeEditorService); | ||
|
||
override select(): ICodeEditor | undefined { | ||
const focusedEditor = this.codeEditorService.getActiveCodeEditor(); | ||
if (focusedEditor) { | ||
focusedEditor.focus(); | ||
return focusedEditor; | ||
} | ||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/notebook/src/browser/contributions/notebook-undo-redo-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// ***************************************************************************** | ||
// Copyright (C) 2024 TypeFox and others. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Eclipse Public License v. 2.0 which is available at | ||
// http://www.eclipse.org/legal/epl-2.0. | ||
// | ||
// This Source Code may also be made available under the following Secondary | ||
// Licenses when the conditions for such availability set forth in the Eclipse | ||
// Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
// with the GNU Classpath Exception which is available at | ||
// https://www.gnu.org/software/classpath/license.html. | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 | ||
// ***************************************************************************** | ||
|
||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { ApplicationShell, UndoRedoHandler } from '@theia/core/lib/browser'; | ||
import { NotebookEditorWidget } from '../notebook-editor-widget'; | ||
|
||
@injectable() | ||
export class NotebookUndoRedoHandler implements UndoRedoHandler<NotebookEditorWidget> { | ||
|
||
@inject(ApplicationShell) | ||
protected readonly applicationShell: ApplicationShell; | ||
|
||
priority = 200; | ||
select(): NotebookEditorWidget | undefined { | ||
const current = this.applicationShell.currentWidget; | ||
if (current instanceof NotebookEditorWidget) { | ||
return current; | ||
} | ||
return undefined; | ||
} | ||
undo(item: NotebookEditorWidget): void { | ||
item.undo(); | ||
} | ||
redo(item: NotebookEditorWidget): void { | ||
item.redo(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.