DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.
fix(mobile): stop Backspace from dropping the composer behind the iPad keyboard (#1197)
Zuyang Liu573cbfdaab4a
diff --git a/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.test.ts b/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.test.tsindex 11e81445..f95f4ff6 100644--- a/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.test.ts+++ b/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.test.ts@@ -1,4 +1,5 @@// @vitest-environment jsdom+import type { Mock } from 'vitest'import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'import { createLifecycleController } from '../../../../dsh-tauri/src/client/controller'import { registerMobileKeyboard } from './keyboard'@@ -12,10 +13,14 @@ vi.mock('dsh-tauri/client', () => ({let controller: ReturnType<typeof createLifecycleController>let editor: HTMLElementlet viewport: FakeViewport+let scrollBy: Mock<typeof window.scrollBy>+const nativeScrollBy = window.scrollByconst labels: Record<string, string> = { 'input.send': '发送消息', 'input.send.queue': '排队发送', 'input.send.steer': '插话发送' }class FakeViewport extends EventTarget {+ width = window.innerWidthheight = window.innerHeight+ offsetTop = 0scale = 1}@@ -33,6 +38,10 @@ beforeEach(() => {editor.focus()viewport = new FakeViewport()Object.defineProperty(window, 'visualViewport', { configurable: true, value: viewport })+ Object.defineProperty(document.documentElement, 'clientWidth', { configurable: true, value: window.innerWidth })+ Object.defineProperty(document.documentElement, 'clientHeight', { configurable: true, value: window.innerHeight })+ scrollBy = vi.fn<typeof window.scrollBy>()+ Object.defineProperty(window, 'scrollBy', { configurable: true, writable: true, value: scrollBy })controller = createLifecycleController()const register = registerMobileKeyboard as unknown as (controller: unknown, ctx: unknown) => voidregister(controller, { locale: { bind: () => (key: string) => labels[key] ?? key } })@@ -41,6 +50,10 @@ beforeEach(() => {afterEach(() => {controller.dispose()Reflect.deleteProperty(window, 'visualViewport')+ for (const key of ['clientWidth', 'clientHeight', 'scrollHeight'])+ Reflect.deleteProperty(document.documentElement, key)+ Object.defineProperty(window, 'scrollBy', { configurable: true, writable: true, value: nativeScrollBy })+ document.documentElement.removeAttribute('data-dsh-mobile-sidebar')document.documentElement.removeAttribute('style')document.body.innerHTML = ''vi.restoreAllMocks()@@ -130,14 +143,89 @@ describe('mobile keyboard viewport fit', () => {it('follows the keyboard height the iOS app reports', () => {const report = (height: number) => window.dispatchEvent(new CustomEvent('dsh-mobile-keyboard', { detail: { height } }))report(300)- expect(reserved()).toBe(`${window.innerHeight - 300}px`)+ expect(reserved()).toBe(`${document.documentElement.clientHeight - 300}px`)report(0)expect(reserved()).toBe('')})- it('drops the reservation when the plugin unloads', () => {+ it('keeps fitting when Safari zooms the page out without any pinch zoom', () => {+ viewport.scale = 0.85+ viewport.height = 322.3+ viewport.dispatchEvent(new Event('resize'))+ expect(reserved()).toBe('322px')+ })++ it('leaves a pinch-zoomed page to the browser', () => {+ const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => {})+ viewport.scale = 1.6+ viewport.width = window.innerWidth / 1.6+ viewport.height = 300+ viewport.offsetTop = 200+ viewport.dispatchEvent(new Event('resize'))+ expect(reserved()).toBe('')+ expect(scrollTo).not.toHaveBeenCalled()+ })++ it('measures the keyboard against the layout height while iOS shrinks innerHeight during its pan', () => {+ vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(322)+ viewport.height = 322.3+ viewport.dispatchEvent(new Event('resize'))+ expect(reserved()).toBe('322px')+ window.dispatchEvent(new CustomEvent('dsh-mobile-keyboard', { detail: { height: 300 } }))+ expect(reserved()).toBe(`${document.documentElement.clientHeight - 300}px`)+ })++ it('pulls a composer pan back to the top once the page fits, but leaves other fields to iOS', () => {+ const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => {})+ viewport.height = 322.3+ viewport.offsetTop = 503.5+ viewport.dispatchEvent(new Event('resize'))+ expect(scrollTo).toHaveBeenCalledExactlyOnceWith(0, 0)+ viewport.offsetTop = 0+ vi.spyOn(window, 'scrollY', 'get').mockReturnValue(120)+ viewport.dispatchEvent(new Event('scroll'))+ expect(scrollTo).toHaveBeenCalledTimes(2)+ document.querySelector<HTMLInputElement>('#setting')!.focus()+ viewport.dispatchEvent(new Event('scroll'))+ expect(scrollTo).toHaveBeenCalledTimes(2)+ })++ it('drops the reservation and viewport listeners when the plugin unloads', () => {+ const scrollTo = vi.spyOn(window, 'scrollTo').mockImplementation(() => {})window.dispatchEvent(new CustomEvent('dsh-mobile-keyboard', { detail: { height: 300 } }))controller.dispose()expect(reserved()).toBe('')+ viewport.height = 322.3+ viewport.offsetTop = 503.5+ viewport.dispatchEvent(new Event('scroll'))+ expect(reserved()).toBe('')+ expect(scrollTo).not.toHaveBeenCalled()+ })+})++describe('composer caret reveal', () => {+ it('drops the window scroll Lexical makes after a JS edit while the page cannot scroll', () => {+ vi.spyOn(window, 'innerHeight', 'get').mockReturnValue(322)+ Object.defineProperty(document.documentElement, 'scrollHeight', { configurable: true, value: document.documentElement.clientHeight + 1 })+ window.scrollBy(0, -294.5)+ expect(scrollBy).not.toHaveBeenCalled()+ document.documentElement.setAttribute('data-dsh-mobile-sidebar', '')+ Object.defineProperty(document.documentElement, 'scrollHeight', { configurable: true, value: document.documentElement.clientHeight + 900 })+ window.scrollBy({ top: -294.5 })+ expect(scrollBy).not.toHaveBeenCalled()+ })++ it('still scrolls pages that can scroll and other fields, and restores the native method on unload', () => {+ Object.defineProperty(document.documentElement, 'scrollHeight', { configurable: true, value: document.documentElement.clientHeight + 2 })+ window.scrollBy(0, 48)+ expect(scrollBy).toHaveBeenLastCalledWith(0, 48)+ expect(scrollBy.mock.contexts.at(-1)).toBe(window)+ Reflect.deleteProperty(document.documentElement, 'scrollHeight')+ document.querySelector<HTMLInputElement>('#setting')!.focus()+ window.scrollBy(0, 24)+ expect(scrollBy).toHaveBeenLastCalledWith(0, 24)+ editor.focus()+ controller.dispose()+ expect(window.scrollBy).toBe(scrollBy)})})diff --git a/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.ts b/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.tsindex 4953733c..d8c24ea3 100644--- a/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.ts+++ b/packages/dsh-tauri-mobile-ui/src/client/register/keyboard.ts@@ -9,36 +9,60 @@ const NATIVE_KEYBOARD = 'dsh-mobile-keyboard'export const registerMobileKeyboard = defineRegister<ClientContext>((controller, ctx) => {const t = ctx.locale.bind('conversation')+ const root = document.documentElementconst viewport = window.visualViewportlet touch: { id: number, x: number, y: number } | undefinedlet nativeHeight = 0+ let fitted = 0- function dismissKeyboard() {+ function composerEditor() {const editor = document.activeElement- if (detectMobileDevice() && editor instanceof HTMLElement && editor.closest(COMPOSER)- && editor.matches(EDITABLE)) {- editor.blur()- }+ return editor instanceof HTMLElement && editor.matches(EDITABLE) && editor.closest(COMPOSER) ? editor : null+ }++ function dismissKeyboard() {+ if (detectMobileDevice())+ composerEditor()?.blur()}+ // iOS shrinks innerHeight to the part of the page below its keyboard pan, and Safari's page zoom reports a+ // visualViewport scale other than 1 without any pinch zoom, so the keyboard is measured against the layout viewport.function shrinkHeight() {if (nativeHeight > 0)- return Math.max(0, window.innerHeight - nativeHeight)+ return Math.max(0, root.clientHeight - nativeHeight)const editor = document.activeElement- if (!(editor instanceof HTMLElement) || !editor.matches(EDITABLE) || !viewport || Math.abs(viewport.scale - 1) > 0.01)+ if (!(editor instanceof HTMLElement) || !editor.matches(EDITABLE) || !viewport || Math.abs(viewport.width - root.clientWidth) >= 1)return 0- return window.innerHeight - viewport.height >= 1 ? Math.floor(viewport.height) : 0+ return root.clientHeight - viewport.height >= 1 ? Math.floor(viewport.height) : 0}- // keep:effect The keyboard overlays the WebView instead of shrinking it, so the page itself has to reserve the covered height or the composer stays behind it.+ // keep:effect The keyboard overlays the WebView instead of shrinking it, so the page itself has to reserve the covered height or the composer stays behind it;+ // once the page fits, the pan iOS used to reveal the composer would only show the empty space below it.function fitKeyboard() {const height = shrinkHeight()- const style = document.documentElement.style- if (height > 0)- style.setProperty(VIEWPORT_HEIGHT, `${height}px`)- else- style.removeProperty(VIEWPORT_HEIGHT)+ if (height !== fitted) {+ fitted = height+ if (height > 0)+ root.style.setProperty(VIEWPORT_HEIGHT, `${height}px`)+ else+ root.style.removeProperty(VIEWPORT_HEIGHT)+ }+ if (height > 0 && composerEditor() && ((viewport?.offsetTop ?? 0) >= 1 || window.scrollY >= 1))+ window.scrollTo(0, 0)+ }++ const scrollBy = window.scrollBy+ function keepComposerInView(this: Window, ...args: unknown[]) {+ if (!composerEditor() || (!root.hasAttribute('data-dsh-mobile-sidebar') && root.scrollHeight - root.clientHeight > 1))+ Reflect.apply(scrollBy, this, args)}+ // keep:effect iOS reports caret rects relative to the keyboard-panned viewport while Lexical adds visualViewport.offsetTop on top,+ // so its caret reveal after a JS edit scrolls the composer behind the keyboard; a page that cannot scroll never needs that scroll.+ window.scrollBy = keepComposerInView+ controller.add(() => {+ if (window.scrollBy === keepComposerInView)+ window.scrollBy = scrollBy+ })function onNativeKeyboard(event: Event) {const detail = (event as CustomEvent<{ height?: number }>).detail@@ -48,10 +72,12 @@ export const registerMobileKeyboard = defineRegister<ClientContext>((controller,window.addEventListener(NATIVE_KEYBOARD, onNativeKeyboard)viewport?.addEventListener('resize', fitKeyboard)+ viewport?.addEventListener('scroll', fitKeyboard)controller.add(() => {window.removeEventListener(NATIVE_KEYBOARD, onNativeKeyboard)viewport?.removeEventListener('resize', fitKeyboard)- document.documentElement.style.removeProperty(VIEWPORT_HEIGHT)+ viewport?.removeEventListener('scroll', fitKeyboard)+ root.style.removeProperty(VIEWPORT_HEIGHT)})controller.listen('click', (event) => {