Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restored notebook have invalid keybinding #4174

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 17 additions & 24 deletions packages/notebook/src/browser/libro-keybind-contribution.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import { LibroSearchToggleCommand, LibroService, LibroView, NotebookCommands } from '@difizen/libro-jupyter/noeditor';
import { LibroSearchToggleCommand, LibroService, NotebookCommands } from '@difizen/libro-jupyter/noeditor';
import { Container, CommandRegistry as LibroCommandRegistry } from '@difizen/mana-app';

import { Autowired } from '@opensumi/di';
import {
ClientAppContribution,
CommandContribution,
CommandRegistry,
Domain,
IContextKeyService,
KeybindingContribution,
KeybindingRegistry,
KeybindingScope,
MaybePromise,
} from '@opensumi/ide-core-browser';
import { WorkbenchEditorService } from '@opensumi/ide-editor';

import { LIBRO_COMPONENTS_SCHEME_ID } from './libro.protocol';
import { ManaContainer } from './mana';

@Domain(ClientAppContribution)
export class LibroKeybindContribution implements ClientAppContribution {
@Domain(ClientAppContribution, KeybindingContribution, CommandContribution)
export class LibroKeybindContribution implements ClientAppContribution, KeybindingContribution, CommandContribution {
@Autowired(IContextKeyService) contextKeyService: IContextKeyService;
@Autowired(KeybindingRegistry) keybindingRegistry: KeybindingRegistry;
@Autowired(WorkbenchEditorService)
workbenchEditorService: WorkbenchEditorService;
@Autowired(CommandRegistry)
protected readonly commandRegistry: CommandRegistry;
@Autowired(ManaContainer)
private readonly manaContainer: Container;

onStart() {
initialize(): MaybePromise<void> {
this.registerContextKey();
this.registerCommand();
this.registerKeybind();
}

registerContextKey() {
const notebookFocusContext = this.contextKeyService.createKey<boolean>(
'libroNotebookFocused',
this.hasActiveNotebook(),
);
const notebookFocusContext = this.contextKeyService.createKey<boolean>('libroNotebookFocused', false);

this.workbenchEditorService.onActiveResourceChange((e) => {
if (e?.uri?.path.ext === `.${LIBRO_COMPONENTS_SCHEME_ID}`) {
Expand All @@ -55,45 +52,41 @@ export class LibroKeybindContribution implements ClientAppContribution {
return this.manaContainer.get(LibroCommandRegistry);
}

hasActiveNotebook() {
return this.libroService.active instanceof LibroView;
}

registerCommand() {
this.commandRegistry.registerCommand(NotebookCommands['EnterCommandMode'], {
registerCommands(commands: CommandRegistry) {
commands.registerCommand(NotebookCommands['EnterCommandMode'], {
execute: () => {
this.libroCommandRegistry.executeCommand(NotebookCommands['EnterCommandMode'].id);
},
});
this.commandRegistry.registerCommand(NotebookCommands['RunCell'], {
commands.registerCommand(NotebookCommands['RunCell'], {
execute: () => {
this.libroCommandRegistry.executeCommand(NotebookCommands['RunCell'].id);
},
});
this.commandRegistry.registerCommand(NotebookCommands['RunCellAndSelectNext'], {
commands.registerCommand(NotebookCommands['RunCellAndSelectNext'], {
execute: () => {
this.libroCommandRegistry.executeCommand(NotebookCommands['RunCellAndSelectNext'].id);
},
});
this.commandRegistry.registerCommand(NotebookCommands['RunCellAndInsertBelow'], {
commands.registerCommand(NotebookCommands['RunCellAndInsertBelow'], {
execute: () => {
this.libroCommandRegistry.executeCommand(NotebookCommands['RunCellAndInsertBelow'].id);
},
});
this.commandRegistry.registerCommand(NotebookCommands['SplitCellAntCursor'], {
commands.registerCommand(NotebookCommands['SplitCellAntCursor'], {
execute: () => {
this.libroCommandRegistry.executeCommand(NotebookCommands['SplitCellAntCursor'].id);
},
});
this.commandRegistry.registerCommand(LibroSearchToggleCommand.ShowLibroSearch, {
commands.registerCommand(LibroSearchToggleCommand.ShowLibroSearch, {
execute: () => {
Comment on lines +55 to +81
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๐Ÿ› ๏ธ Refactor suggestion

ๅปบ่ฎฎไผ˜ๅŒ–ๅ‘ฝไปคๆณจๅ†Œไปฅๅ‡ๅฐ‘้‡ๅคไปฃ็ 

registerCommands ๆ–นๆณ•ไธญๅคšๆฌก้‡ๅคๆณจๅ†Œๅ‘ฝไปค๏ผŒๅปบ่ฎฎๆๅ–ๅ…ฌๅ…ฑ้€ป่พ‘๏ผŒไฝฟ็”จๅพช็Žฏๆˆ–ๆ˜ ๅฐ„ๆฅ็ฎ€ๅŒ–ไปฃ็ ๏ผŒๆๅ‡ๅฏ่ฏปๆ€งๅ’Œๅฏ็ปดๆŠคๆ€งใ€‚

ๅฏไปฅ่€ƒ่™‘ๅฆ‚ไธ‹ไฟฎๆ”น๏ผš

registerCommands(commands: CommandRegistry) {
  const notebookCommands = [
    NotebookCommands['EnterCommandMode'],
    NotebookCommands['RunCell'],
    NotebookCommands['RunCellAndSelectNext'],
    NotebookCommands['RunCellAndInsertBelow'],
    NotebookCommands['SplitCellAntCursor'],
    LibroSearchToggleCommand.ShowLibroSearch,
  ];

  notebookCommands.forEach((command) => {
    commands.registerCommand(command, {
      execute: () => {
        this.libroCommandRegistry.executeCommand(command.id);
      },
    });
  });
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ๅ…ถๅฎž่ฟ™ไธชๅปบ่ฎฎๆŒบๅฅฝ็š„ cc @zhanba

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

this.libroCommandRegistry.executeCommand(LibroSearchToggleCommand.ShowLibroSearch.id);
},
});
}

registerKeybind() {
this.keybindingRegistry.registerKeybindings(
registerKeybindings(keybindings: KeybindingRegistry): void {
keybindings.registerKeybindings(
[
{
keybinding: 'f1',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ContentLoaderType, OpensumiInjector } from '../mana';
export class LibroOpensumiContentContribution implements ContentContribution {
@inject(OpensumiInjector) injector: Injector;

canHandle = (options) => options.loadType === ContentLoaderType ? 100 : 1;
canHandle = (options) => (options.loadType === ContentLoaderType ? 100 : 1);
async loadContent(options: NotebookOption, model: LibroJupyterModel) {
const fileServiceClient: IFileServiceClient = this.injector.get(IFileServiceClient);
const messageService = this.injector.get(IMessageService);
Expand All @@ -33,7 +33,8 @@ export class LibroOpensumiContentContribution implements ContentContribution {
}
const uri = new URI(options.resource.toString());
const currentFileContents: IContentsModel = {
name: uri.path.name,
name: uri.path.base,
// TODO: should be relative path to notebook root, notebook root may not be same as ide root, and jupyter provide no api to get root dir
path: uri.path.toString(),
last_modified: stat?.lastModification.toString() || new Date().toJSON(),
created: stat?.createTime?.toString() || new Date().toJSON(),
Expand Down
Loading