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

Dynamically bubble defined hotkeys out of editor #565

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
48 changes: 13 additions & 35 deletions src/app/editor/component/editor/editor.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
fontIcon="mdi-chevron-double-right"
></mat-icon>
</button>
<ng-container [ngSwitch]="sidebar?.value">
<ng-container [ngSwitch]="sidebarView">
<div *ngSwitchCase="'modules'">
<cas-module-list
*ngIf="filename.endsWith('.tf')"
Expand Down Expand Up @@ -119,6 +119,7 @@
</button>
<button
mat-icon-button
hotkeyAction="DISCARD_CHANGES"
color="primary"
fxFlexAlign="center"
[disabled]="file && file.editorContent === file.content"
Expand Down Expand Up @@ -169,40 +170,16 @@
</button>
<mat-divider vertical style="height: 100%"></mat-divider>
<div *ngIf="filename.endsWith('.tf')">
<mat-button-toggle-group
#sidebar="matButtonToggleGroup"
[value]="sidebarView"
hotkeyAction="FILE_VERSIONS"
(hotkeyFn)="sidebarChangedFn($event, sidebarOpen)"
(valueChange)="sidebarChangedFn($event, sidebarOpen)"
>
<mat-button-toggle
value="modules"
aria-label="modules"
hotkeyAction="FILE_MODULES"
(change)="toggleSidebar($event, true)"
>
<mat-icon
color="primary"
fontSet="fas"
fontIcon="fa-code"
matTooltip="Open Modules"
></mat-icon>
</mat-button-toggle>
<mat-button-toggle
value="versions"
aria-label="versions"
hotkeyAction="FILE_VERSIONS"
(change)="toggleSidebar($event, true)"
>
<mat-icon
color="primary"
fontSet="fas"
fontIcon="fa-history"
matTooltip="Open File Versions"
></mat-icon>
</mat-button-toggle>
</mat-button-toggle-group>
<button mat-icon-button hotkeyAction="FILE_MODULES" fxFlexAlign="center" color="primary"
(click)="sidebarView = 'modules'; sidebarChangedFn('modules', true)" [disabled]="sidebarView === 'modules'"
matTooltip="Show the module list">
<mat-icon fontSet="fas" class="fa-lg" fontIcon="fa-code"></mat-icon>
</button>
<button mat-icon-button hotkeyAction="FILE_VERSIONS" fxFlexAlign="center" color="primary"
(click)="sidebarView = 'versions'; sidebarChangedFn('versions', true)" [disabled]="sidebarView === 'versions'"
matTooltip="Show the file versions">
<mat-icon fontSet="fas" class="fa-lg" fontIcon="fa-history"></mat-icon>
</button>
</div>
</mat-toolbar-row>
</mat-toolbar>
Expand All @@ -213,6 +190,7 @@
id="editor"
class="code-editor"
[options]="editorOptions"
(init)="editorInit($event)"
[(ngModel)]="code"
(ngModelChange)="codeChangedEventHandler($event)"
>
Expand Down
33 changes: 32 additions & 1 deletion src/app/editor/component/editor/editor.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { ConfirmDialogService } from 'src/app/sei-cwd-common/confirm-dialog/serv
import { CurrentUserQuery } from 'src/app/users/state';
import { FileQuery, FileService } from '../../../files/state';
import { ModuleQuery, ModuleService } from '../../../modules/state';
import { ComnSettingsService } from '@cmusei/crucible-common';

const SIDEBAR_MIN_WIDTH = 300;

Expand Down Expand Up @@ -82,7 +83,8 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
private fileVersionQuery: FileVersionQuery,
private currentUserQuery: CurrentUserQuery,
private changeDetectorRef: ChangeDetectorRef,
private confirmDialog: ConfirmDialogService
private confirmDialog: ConfirmDialogService,
private settingsService: ComnSettingsService
) {}

ngOnInit(): void {
Expand Down Expand Up @@ -152,6 +154,35 @@ export class EditorComponent implements OnInit, OnChanges, OnDestroy {
this.isSaved$ = this.fileQuery.selectIsSaved(this.fileId);
}

editorInit(editor: any) {
// get the defined hot keys
const hotKeys = this.settingsService.settings.Hotkeys;
const bubbleKeys = [];
// create search strings for the defined hot keys that need to bubble out of the editor
// transforms control.x, alt.x, etc. to ctrl+X, alt+X, etc.
for (const [key, hk] of Object.entries(hotKeys)) {
const parts = (hk as any).keys.split('.');
if (parts.length === 2) {
if (parts[0] === 'control') parts[0] = 'ctrl';
bubbleKeys.push(parts[0] + '+' + parts[1].toUpperCase());
}
}
// get the editor default key bindings
const bindings =
editor._standaloneKeybindingService._getResolver()._defaultKeybindings;
// set the editor key bindings to bubble the defined hot keys that would be intercepted
// by clearing the command and setting bubble to true
bindings.forEach((binding) => {
if (
binding.keypressParts.length === 1 &&
bubbleKeys.some((bk) => bk === binding.keypressParts[0])
) {
binding.command = '';
binding.bubble = true;
}
});
}

updateEditorOptions(file: ModelFile): void {
let writable = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"> </mat-row>
</mat-table>
<!-- This non-displayed button is required, because hotKeys do not work with the button inside the mat-table -->
<button style="display: none;" hotkeyAction="PROJECT_NEW" (click)="createRequest()"></button>
</div>

<!-- Loading Spinner -->
Expand Down
29 changes: 28 additions & 1 deletion src/assets/config/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"AppTopBarText": "Caster",
"Hotkeys": {
"PROJECT_NEW": {
"keys": "meta.p",
"keys": "control.p",
"group": "",
"description": "New Project"
},
Expand All @@ -43,6 +43,33 @@
"group": "Editor",
"description": "Save a file",
"allowIn": ["INPUT", "TEXTAREA"]
},
"DISCARD_CHANGES": {
"keys": "control.q",
"group": "Editor",
"description": "Discard changes",
"allowIn": [
"INPUT",
"TEXTAREA"
]
},
"FILE_MODULES": {
"keys": "alt.m",
"group": "Editor",
"description": "Show file modules",
"allowIn": [
"INPUT",
"TEXTAREA"
]
},
"FILE_VERSIONS": {
"keys": "alt.v",
"group": "Editor",
"description": "Show file versions",
"allowIn": [
"INPUT",
"TEXTAREA"
]
}
}
}
Loading