Skip to content

Commit

Permalink
[KOGITO-2343] Shortcuts are always active for nodes (conflict) (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ederign authored Jun 5, 2020
1 parent 492e7ba commit 9d264be
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ export class EditorEnvelopeController {
specialDomElements: SpecialDomElements,
stateControl: StateControl,
renderer: Renderer,
resourceContentEditorCoordinator: ResourceContentEditorCoordinator
resourceContentEditorCoordinator: ResourceContentEditorCoordinator,
keyboardShortcutsApi: KeyboardShortcutsApi
) {
this.renderer = renderer;
this.editorFactory = editorFactory;
Expand All @@ -59,7 +60,10 @@ export class EditorEnvelopeController {
this.editorEnvelopeView!.setLoading();
editor
.setContent(contentPath, editorContent.content)
.finally(() => this.editorEnvelopeView!.setLoadingFinished())
.finally(() => {
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
this.editorEnvelopeView!.setLoadingFinished()
})
.then(() => self.notify_ready());
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ beforeEach(() => {
callback();
}
},
new ResourceContentEditorCoordinator()
new ResourceContentEditorCoordinator(),
new DefaultKeyboardShortcutsService( { channel: ChannelType.VSCODE, operatingSystem: OperatingSystem.WINDOWS })
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`EditorEnvelopeController after received content 1`] = `
exposing={[Function]}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down Expand Up @@ -94,6 +95,7 @@ exports[`EditorEnvelopeController after received content 1`] = `
}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down Expand Up @@ -393,6 +395,7 @@ exports[`EditorEnvelopeController opens 1`] = `
exposing={[Function]}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down Expand Up @@ -590,6 +593,7 @@ exports[`EditorEnvelopeController receives init request 1`] = `
exposing={[Function]}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`EditorEnvelopeView after loading stops 1`] = `
}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down Expand Up @@ -41,6 +42,7 @@ exports[`EditorEnvelopeView after loading stops and editor is set 1`] = `
}
keyboardShortcuts={
DefaultKeyboardShortcutsService {
"delayedKeyBindings": Array [],
"editorContext": Object {
"channel": "VSCODE",
"operatingSystem": "WINDOWS",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("DefaultKeyboardShortcutsService", () => {
});

const [wasFired] = resolveWhenKeyPressed("ctrl+a", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(1);

fire("keydown", { ctrlKey: true, code: "KeyA" });
Expand All @@ -40,6 +41,7 @@ describe("DefaultKeyboardShortcutsService", () => {
});

const [wasFired] = resolveWhenKeyPressed("ctrl+a", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(1);
fire("keydown", { metaKey: true, code: "KeyA" });
await wasFired;
Expand All @@ -54,6 +56,7 @@ describe("DefaultKeyboardShortcutsService", () => {
});

const [wasFiredDownA, wasFiredUpA] = resolveWhenKeyDownThenUp("ctrl+a", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(1);
fire("keydown", { ctrlKey: true, code: "KeyA" });
await wasFiredDownA;
Expand All @@ -66,6 +69,7 @@ describe("DefaultKeyboardShortcutsService", () => {
//

const [wasFiredDownB, wasFiredUpB] = resolveWhenKeyDownThenUp("ctrl+b", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(2);
fire("keydown", { ctrlKey: true, code: "KeyB" });
await wasFiredDownB;
Expand All @@ -83,6 +87,7 @@ describe("DefaultKeyboardShortcutsService", () => {
});

const [wasFired] = resolveWhenKeyPressedOnce("ctrl+c", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(1);

fire("keydown", { ctrlKey: true, code: "KeyC" });
Expand All @@ -98,6 +103,7 @@ describe("DefaultKeyboardShortcutsService", () => {
});

const [wasFired, id] = resolveWhenKeyPressed("ctrl+c", keyboardShortcutsApi);
keyboardShortcutsApi.executeDelayedShortcutsRegistration();
expect(keyboardShortcutsApi.registered().length).toStrictEqual(1);

fire("keydown", { ctrlKey: true, code: "KeyC" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,22 @@ const KEY_CODES = new Map<string, string>([
["z", "KeyZ"]
]);

enum ShortCutsType {
KeyDownThenUp,
KeyDown
}

export interface DelayedRegisterKeyBinding{
type: ShortCutsType;
binding: KeyBinding;
}

export class DefaultKeyboardShortcutsService implements KeyboardShortcutsApi {
private readonly editorContext: EditorContext;

private eventIdentifiers = 1;
private readonly keyBindings = new Map<number, KeyBinding>();
private delayedKeyBindings = new Array<DelayedRegisterKeyBinding>();

constructor(editorContext: EditorContext) {
this.editorContext = editorContext;
Expand Down Expand Up @@ -130,12 +141,25 @@ export class DefaultKeyboardShortcutsService implements KeyboardShortcutsApi {

this.keyBindings.set(this.eventIdentifiers, keyBinding);

this.keyBindingElement(keyBinding).addEventListener("keydown", keyBinding.listener);
this.keyBindingElement(keyBinding).addEventListener("keyup", keyBinding.listener);

if (keyBinding?.opts?.element || document.querySelector(".session-container")) {
this.keyBindingElement(keyBinding).addEventListener("keydown", keyBinding.listener);
this.keyBindingElement(keyBinding).addEventListener("keyup", keyBinding.listener);
}
else {
const delayedShorcut = {
type: ShortCutsType.KeyDownThenUp,
binding: keyBinding,
};
this.delayedKeyBindings.push(delayedShorcut);
}
return this.eventIdentifiers++;
}

private keyBindingElement(keyBinding?: KeyBinding) {
return keyBinding?.opts?.element ?? document.querySelector(".session-container") ?? window;
}


public registerKeyPress(
combination: string,
label: string,
Expand All @@ -156,17 +180,41 @@ export class DefaultKeyboardShortcutsService implements KeyboardShortcutsApi {
console.debug(`Fired (press) [${combination}]!`);
onKeyPress(e.target);
}

return true;
},
opts
};

this.keyBindings.set(this.eventIdentifiers, keyBinding);
this.keyBindingElement(keyBinding).addEventListener("keydown", keyBinding.listener);

if (keyBinding?.opts?.element || document.querySelector(".session-container")) {
this.keyBindingElement(keyBinding).addEventListener("keydown", keyBinding.listener);
}
else {
const delayedShorcut = {
type: ShortCutsType.KeyDown,
binding: keyBinding,
};
this.delayedKeyBindings.push(delayedShorcut);
}

return this.eventIdentifiers++;
}

public executeDelayedShortcutsRegistration() {
this.delayedKeyBindings.forEach(delayedKeyBindings => {

if (delayedKeyBindings.type === ShortCutsType.KeyDown) {
this.keyBindingElement(delayedKeyBindings.binding).addEventListener("keydown", delayedKeyBindings.binding.listener);
}
else {
this.keyBindingElement(delayedKeyBindings.binding).addEventListener("keydown", delayedKeyBindings.binding.listener);
this.keyBindingElement(delayedKeyBindings.binding).addEventListener("keyup", delayedKeyBindings.binding.listener);
}
});
this.delayedKeyBindings = [];
}

public registerKeyPressOnce(
combination: string,
onKeyPress: (target: EventTarget | null) => Thenable<void>,
Expand All @@ -185,10 +233,6 @@ export class DefaultKeyboardShortcutsService implements KeyboardShortcutsApi {
return id;
}

private keyBindingElement(keyBinding?: KeyBinding) {
return keyBinding?.opts?.element ?? document.querySelector(".session-container") ?? window;
}

public deregister(id: number): void {
const keyBinding = this.keyBindings.get(id);
this.keyBindingElement(keyBinding).removeEventListener("keypress", keyBinding?.listener!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ export interface KeyboardShortcutsApi {
*/
registerKeyPressOnce(combination: string, action: () => Thenable<void>, opts?: KeyBindingServiceOpts): number;


/**
* Returns a list of all the registered Keyboard Shortcuts.
*/
registered(): KeyBinding[];

/**
* Execute delayed shortcuts after envelope load.
*/
executeDelayedShortcutsRegistration(): void;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/microeditor-envelope/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export function init(args: {
specialDomElements,
stateControl,
renderer,
resourceContentEditorCoordinator
resourceContentEditorCoordinator,
keyboardShortcutsService
);

return editorEnvelopeController
Expand Down

0 comments on commit 9d264be

Please sign in to comment.