DEV: Add keybindings (#157)

- Ability to Esc to close context menu
- Ability to Ctrl/Cmd + Z to undo results
This commit is contained in:
Keegan George 2023-08-23 15:35:53 -07:00 committed by GitHub
parent 78558b9cf5
commit 65c6b5e16c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View File

@ -60,6 +60,7 @@ export default class AiHelperContextMenu extends Component {
willDestroy() { willDestroy() {
super.willDestroy(...arguments); super.willDestroy(...arguments);
document.removeEventListener("selectionchange", this.selectionChanged); document.removeEventListener("selectionchange", this.selectionChanged);
document.removeEventListener("keydown", this.onKeyDown);
this._popper?.destroy(); this._popper?.destroy();
} }
@ -124,6 +125,19 @@ export default class AiHelperContextMenu extends Component {
this.positionContextMenu(); this.positionContextMenu();
} }
@bind
onKeyDown(event) {
const cmdOrCtrl = event.ctrlKey || event.metaKey;
if (cmdOrCtrl && event.key === "z" && this.oldEditorValue) {
return this.undoAIAction();
}
if (event.key === "Escape") {
return this.closeContextMenu();
}
}
@debounce(INPUT_DELAY) @debounce(INPUT_DELAY)
_onSelectionChanged() { _onSelectionChanged() {
this.positionContextMenu(); this.positionContextMenu();
@ -189,6 +203,7 @@ export default class AiHelperContextMenu extends Component {
@action @action
setupContextMenu() { setupContextMenu() {
document.addEventListener("selectionchange", this.selectionChanged); document.addEventListener("selectionchange", this.selectionChanged);
document.addEventListener("keydown", this.onKeyDown);
this._dEditorInput = document.querySelector(".d-editor-input"); this._dEditorInput = document.querySelector(".d-editor-input");

View File

@ -157,6 +157,28 @@ RSpec.describe "AI Composer helper", type: :system, js: true do
ai_helper_context_menu.click_undo_button ai_helper_context_menu.click_undo_button
expect(composer.composer_input.value).to eq(OpenAiCompletionsInferenceStubs.spanish_text) expect(composer.composer_input.value).to eq(OpenAiCompletionsInferenceStubs.spanish_text)
end end
it "reverts results when Ctrl/Cmd + Z is pressed on the keyboard" do
trigger_context_menu(OpenAiCompletionsInferenceStubs.spanish_text)
ai_helper_context_menu.click_ai_button
ai_helper_context_menu.select_helper_model(
OpenAiCompletionsInferenceStubs.text_mode_to_id(mode),
)
wait_for do
composer.composer_input.value == OpenAiCompletionsInferenceStubs.translated_response.strip
end
ai_helper_context_menu.press_undo_keys
expect(composer.composer_input.value).to eq(OpenAiCompletionsInferenceStubs.spanish_text)
end
it "hides the context menu when pressing Escape on the keyboard" do
trigger_context_menu(OpenAiCompletionsInferenceStubs.spanish_text)
ai_helper_context_menu.click_ai_button
ai_helper_context_menu.press_escape_key
expect(ai_helper_context_menu).to have_no_context_menu
end
end end
context "when using the proofreading mode" do context "when using the proofreading mode" do

View File

@ -3,6 +3,7 @@
module PageObjects module PageObjects
module Components module Components
class AIHelperContextMenu < PageObjects::Components::Base class AIHelperContextMenu < PageObjects::Components::Base
COMPOSER_EDITOR_SELECTOR = ".d-editor-input"
CONTEXT_MENU_SELECTOR = ".ai-helper-context-menu" CONTEXT_MENU_SELECTOR = ".ai-helper-context-menu"
TRIGGER_STATE_SELECTOR = "#{CONTEXT_MENU_SELECTOR}__trigger" TRIGGER_STATE_SELECTOR = "#{CONTEXT_MENU_SELECTOR}__trigger"
OPTIONS_STATE_SELECTOR = "#{CONTEXT_MENU_SELECTOR}__options" OPTIONS_STATE_SELECTOR = "#{CONTEXT_MENU_SELECTOR}__options"
@ -26,10 +27,22 @@ module PageObjects
find("#{RESETS_STATE_SELECTOR} .undo").click find("#{RESETS_STATE_SELECTOR} .undo").click
end end
def press_undo_keys
find(COMPOSER_EDITOR_SELECTOR).send_keys([:control, "z"])
end
def press_escape_key
find("body").send_keys(:escape)
end
def has_context_menu? def has_context_menu?
page.has_css?(CONTEXT_MENU_SELECTOR) page.has_css?(CONTEXT_MENU_SELECTOR)
end end
def has_no_context_menu?
page.has_no_css?(CONTEXT_MENU_SELECTOR)
end
def showing_triggers? def showing_triggers?
page.has_css?(TRIGGER_STATE_SELECTOR) page.has_css?(TRIGGER_STATE_SELECTOR)
end end