-
-
Notifications
You must be signed in to change notification settings - Fork 199
fix(mobile): keep QuickAdd dialogs above the on-screen keyboard #2133
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
Merged
+154
−3
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,20 @@ | ||
| import type { Plugin } from "obsidian"; | ||
|
|
||
| /** | ||
| * On a phone a QuickAdd dialog shrinks to the space above the keyboard | ||
| * (styles.css), which can leave the field being typed in below its scroll | ||
| * area, under a pinned footer. Obsidian's own keyboard scroll measures the | ||
| * document selection, which for an <input> is not the field. Once the keyboard | ||
| * is up, scroll the focused field back into view. | ||
| */ | ||
| export function keepFocusedFieldInView(plugin: Plugin): void { | ||
| const onKeyboardShown = () => { | ||
| const field = activeDocument.activeElement; | ||
| if (field?.instanceOf(HTMLElement) && field.closest(".quickAddModal")) { | ||
| field.scrollIntoView({ block: "nearest" }); | ||
| } | ||
| }; | ||
| // Capacitor's Keyboard plugin dispatches these on window in the mobile app. | ||
| window.addEventListener("keyboardDidShow", onKeyboardShown); | ||
| plugin.register(() => window.removeEventListener("keyboardDidShow", onKeyboardShown)); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
| import { afterEach, expect, it } from "vitest"; | ||
| import { MacroChoice } from "../../src/types/choices/MacroChoice"; | ||
| import type IChoice from "../../src/types/choices/IChoice"; | ||
| import { createQuickAddE2EHarness } from "./e2eVault"; | ||
| import { jsLiteral, POLL_OPTS, waitForElement } from "./uiHelpers"; | ||
|
|
||
| const getContext = createQuickAddE2EHarness("phone-keyboard"); | ||
|
|
||
| // #2132: on a phone the keyboard covered the lower part of QuickAdd dialogs: | ||
| // the date prompt's Ok, the multi-select's Done, and the macro builder's field | ||
| // under its pinned footer. Obsidian sets --keyboard-height while the keyboard | ||
| // is up; the dialog now sits in the space above it. | ||
| const KEYBOARD = 345; | ||
|
|
||
| // Settings shows its tab list instead of a page on a phone, so the macro test | ||
| // emulates the phone once the builder is open. | ||
| const emulatePhoneWithKeyboard = () => getContext().obsidian.dev.evalJson(`(() => { | ||
| window.__qaPhoneClasses = document.body.className; | ||
| document.body.classList.remove("is-tablet"); | ||
| document.body.classList.add("is-mobile", "is-phone"); | ||
| document.documentElement.style.setProperty("--keyboard-height", "${KEYBOARD}px"); | ||
| return true; | ||
| })()`); | ||
|
|
||
| afterEach(async () => { | ||
| const { obsidian } = getContext(); | ||
| await obsidian.dev.evalJson(`(() => { | ||
| if (window.__qaPhoneClasses !== undefined) document.body.className = window.__qaPhoneClasses; | ||
| delete window.__qaPhoneClasses; | ||
| document.documentElement.style.removeProperty("--keyboard-height"); | ||
| for (const container of [...document.querySelectorAll(".modal-container")].reverse()) { | ||
| const close = [...container.querySelectorAll("button")].find((b) => /^(Cancel|Done)$/.test(b.textContent.trim())); | ||
| (close ?? container.querySelector(".modal-close-button"))?.click(); | ||
| } | ||
| app.setting.close(); | ||
| return true; | ||
| })()`); | ||
| await expect.poll(() => obsidian.dev.evalJson<number>( | ||
| 'document.querySelectorAll(".modal-container").length', | ||
| ), POLL_OPTS).toBe(0); | ||
| }); | ||
|
|
||
| const aboveKeyboard = (selector: string) => `(() => { | ||
| const el = [...document.querySelectorAll(".modal-container")].pop().querySelector(${jsLiteral(selector)}); | ||
| const rect = el.getBoundingClientRect(); | ||
| return rect.top >= 0 && rect.bottom <= innerHeight - ${KEYBOARD}; | ||
| })()`; | ||
|
|
||
| it("keeps the date prompt's field and Ok above the keyboard", async () => { | ||
| const { obsidian } = getContext(); | ||
| await emulatePhoneWithKeyboard(); | ||
| await obsidian.dev.evalJson(`(() => { | ||
| app.plugins.plugins.quickadd.api.format("{{VDATE:due,YYYY-MM-DD}}").catch(() => {}); | ||
| return true; | ||
| })()`); | ||
| await waitForElement(obsidian, ".qaDatePrompt .qa-date-picker"); | ||
| expect(await obsidian.dev.evalJson<boolean>(aboveKeyboard(".qa-vdate-input"))).toBe(true); | ||
| expect(await obsidian.dev.evalJson<boolean>(aboveKeyboard(".qa-prompt-actions-primary button.mod-cta"))).toBe(true); | ||
| }); | ||
|
|
||
| // Enough options that the dialog is taller than the space above the keyboard. | ||
| // The desktop window is wider than 540px, as a phone is in landscape, so the | ||
| // multi-select's wide-screen height cap applies. | ||
| const OPTIONS = Array.from({ length: 20 }, (_, i) => `Option ${i + 1}`).join(","); | ||
|
|
||
| it("keeps the multi-select's Done above the keyboard", async () => { | ||
| const { obsidian } = getContext(); | ||
| await emulatePhoneWithKeyboard(); | ||
| await obsidian.dev.evalJson(`(() => { | ||
| app.plugins.plugins.quickadd.api.format("{{VALUE:${OPTIONS}|multi}}").catch(() => {}); | ||
| return true; | ||
| })()`); | ||
| await waitForElement(obsidian, ".qaMultiSuggester .qa-multi-actions"); | ||
| expect(await obsidian.dev.evalJson<boolean>(aboveKeyboard(".qa-multi-actions button.mod-cta"))).toBe(true); | ||
| }); | ||
|
|
||
| it("brings the macro builder's focused field out from under its footer once the keyboard is up", async () => { | ||
| const { obsidian, plugin } = getContext(); | ||
| const macro = new MacroChoice("Keyboard macro"); | ||
| await plugin.data<{ choices: IChoice[] }>().patch((data) => { | ||
| data.choices = [macro]; | ||
| }); | ||
| await plugin.reload({ waitUntilReady: true }); | ||
| await obsidian.dev.evalJson("app.setting.open(); app.setting.openTabById('quickadd'); true"); | ||
| await waitForElement(obsidian, '[aria-label="Configure Keyboard macro"]'); | ||
| await obsidian.dev.evalJson(`document.querySelector('[aria-label="Configure Keyboard macro"]').click(), true`); | ||
| await waitForElement(obsidian, ".macroBuilder .qa-modal-footer"); | ||
| await emulatePhoneWithKeyboard(); | ||
|
|
||
| const coveredByFooter = `(() => { | ||
| const modal = document.querySelector(".macroBuilder"); | ||
| const field = [...modal.querySelectorAll("input")].find((input) => input.placeholder.startsWith("Start typing script")); | ||
| return field.getBoundingClientRect().bottom > modal.querySelector(".qa-modal-footer").getBoundingClientRect().top; | ||
| })()`; | ||
| // The field sits low in the builder: with the keyboard taking the bottom of | ||
| // the screen it starts under the footer, as on a phone. | ||
| expect(await obsidian.dev.evalJson<boolean>(`(() => { | ||
| const modal = document.querySelector(".macroBuilder"); | ||
| modal.querySelector(".modal-content").scrollTop = 0; | ||
| [...modal.querySelectorAll("input")].find((input) => input.placeholder.startsWith("Start typing script")).focus({ preventScroll: true }); | ||
| return ${coveredByFooter}; | ||
| })()`)).toBe(true); | ||
|
|
||
| await obsidian.dev.evalJson('window.dispatchEvent(new Event("keyboardDidShow")), true'); | ||
| expect(await obsidian.dev.evalJson<boolean>(coveredByFooter)).toBe(false); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.