From 5888183350669c3ff33ee1db05c2c5b2a0ace476 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=87=AF?= Date: Tue, 17 Nov 2026 14:23:15 +0800 Subject: [PATCH 1/2] fix(math): restore formula textarea cursor and focus --- .../formula-editor/__tests__/index.test.ts | 66 +++++++++++++++++-- .../device/math/formula-editor/index.vue | 16 +++-- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts b/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts index ceeb4ef3..4b081692 100644 --- a/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts +++ b/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts @@ -2,9 +2,9 @@ import { fileURLToPath as migrationNodePath, pathToFileURL as migrationSourceUrl import { resolve as migrationResolve, dirname as migrationDirname } from 'node:path' import { fileURLToPath as migrationFilePath } from 'node:url' import { readFileSync } from 'node:fs' -import { resolve } from 'node:path' import { mount } from '@vue/test-utils' -import { defineComponent, h, nextTick, ref, type PropType } from 'vue' +import { ElInput } from 'element-plus' +import { defineComponent, h, nextTick, ref, type Component, type PropType } from 'vue' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { renderFormulaLatex } from '../latex-renderer' import DcDeviceMathFormulaEditor from '../index.vue' @@ -129,8 +129,9 @@ const ElTooltipStub = defineComponent({ } }) -function mountComponent(props = {}) { +function mountComponent(props = {}, inputComponent: Component = ElInputStub) { const wrapper = mount(DcDeviceMathFormulaEditor, { + attachTo: document.body, props: { modelValue: '', label: 'Formula', @@ -144,7 +145,7 @@ function mountComponent(props = {}) { props: ['label'], template: '' }, - ElInput: ElInputStub, + ElInput: inputComponent, ElButton: { emits: ['click'], template: '' @@ -317,4 +318,61 @@ describe('DcDeviceMathFormulaEditor', () => { expect(wrapper.get('[data-testid="math-token-option"]').text()).toBe('Mic 1') expect((wrapper.get('[data-testid="math-formula-input"]').element as HTMLInputElement).value).toBe("'Mic 1'") }) + it('focuses the real textarea inside an inserted function', async () => { + const wrapper = mountComponent({}, ElInput) + await wrapper + .findAll('[data-testid="math-formula-type-option"]') + .find(option => option.text() === 'Localized Functions')! + .trigger('click') + await wrapper + .findAll('[data-testid="math-token-option"]') + .find(option => option.text() === 'Square Root')! + .trigger('click') + await nextTick() + + const textarea = wrapper.get('textarea').element + expect(textarea.value).toBe('sqrt()') + expect(document.activeElement).toBe(textarea) + expect([textarea.selectionStart, textarea.selectionEnd]).toEqual([5, 5]) + }) + + it('inserts a channel at the clicked position in the real textarea', async () => { + const wrapper = mountComponent( + { + modelValue: '12+34', + channels: [{ id: 'mic', customName: 'Mic 1' }] + }, + ElInput + ) + const input = wrapper.get('textarea') + input.element.focus() + input.element.setSelectionRange(3, 3) + await input.trigger('click') + + await wrapper.get('[data-testid="math-token-option"]').trigger('click') + await nextTick() + + expect(input.element.value).toBe("12+'Mic 1'34") + expect([input.element.selectionStart, input.element.selectionEnd]).toEqual([10, 10]) + }) + + it('replaces the selected text in the real textarea', async () => { + const wrapper = mountComponent( + { + modelValue: '12+34', + channels: [{ id: 'mic', customName: 'Mic 1' }] + }, + ElInput + ) + const input = wrapper.get('textarea') + input.element.focus() + input.element.setSelectionRange(3, 5) + await input.trigger('select') + + await wrapper.get('[data-testid="math-token-option"]').trigger('click') + await nextTick() + + expect(input.element.value).toBe("12+'Mic 1'") + expect([input.element.selectionStart, input.element.selectionEnd]).toEqual([10, 10]) + }) }) diff --git a/apps/frontend/src/modules/device/math/formula-editor/index.vue b/apps/frontend/src/modules/device/math/formula-editor/index.vue index 8198898c..edd7d5b2 100644 --- a/apps/frontend/src/modules/device/math/formula-editor/index.vue +++ b/apps/frontend/src/modules/device/math/formula-editor/index.vue @@ -157,12 +157,13 @@ interface FormulaGroupConfig { } type FormulaInputCandidate = Pick< - HTMLInputElement, + HTMLInputElement | HTMLTextAreaElement, 'value' | 'selectionStart' | 'selectionEnd' | 'scrollLeft' | 'focus' | 'setSelectionRange' > interface FormulaInputExpose { input?: FormulaInputCandidate | { value?: FormulaInputCandidate } + textarea?: FormulaInputCandidate | { value?: FormulaInputCandidate } $el?: Element } @@ -305,7 +306,10 @@ function translateExisting(key: string, fallback = ''): string { } function syncFormulaCursor(event?: Event) { - const inputElement = event?.target instanceof HTMLInputElement ? event.target : getFormulaInputElement() + const inputElement = + event?.target instanceof HTMLInputElement || event?.target instanceof HTMLTextAreaElement + ? event.target + : getFormulaInputElement() if (!inputElement || typeof inputElement.selectionStart !== 'number') return formulaCursorStart.value = inputElement.selectionStart @@ -401,19 +405,19 @@ function parsePixelValue(value: string): number { return Number.isFinite(parsedValue) ? parsedValue : 0 } -function getFormulaInputElement(): HTMLInputElement | null { +function getFormulaInputElement(): HTMLInputElement | HTMLTextAreaElement | null { const inputComponent = formulaInputRef.value - const exposedInput = inputComponent?.input + const exposedInput = inputComponent?.textarea ?? inputComponent?.input if (isInputElement(exposedInput)) return exposedInput if (isInputElement(exposedInput?.value)) return exposedInput.value - return inputComponent?.$el?.querySelector?.('input') || null + return inputComponent?.$el?.querySelector('textarea, input') || null } function isInputElement( value: FormulaInputCandidate | { value?: FormulaInputCandidate } | string | null | undefined -): value is HTMLInputElement { +): value is HTMLInputElement | HTMLTextAreaElement { return Boolean( value && typeof (value as FormulaInputCandidate).focus === 'function' && From f3b235a0b5632faa536b73831bb5deb9d5854c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=87=AF?= Date: Tue, 17 Nov 2026 14:31:45 +0800 Subject: [PATCH 2/2] test(math): attach only real input focus regressions to document --- .../modules/device/math/formula-editor/__tests__/index.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts b/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts index 4b081692..2973b6f9 100644 --- a/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts +++ b/apps/frontend/src/modules/device/math/formula-editor/__tests__/index.test.ts @@ -131,7 +131,7 @@ const ElTooltipStub = defineComponent({ function mountComponent(props = {}, inputComponent: Component = ElInputStub) { const wrapper = mount(DcDeviceMathFormulaEditor, { - attachTo: document.body, + attachTo: inputComponent === ElInput ? document.body : undefined, props: { modelValue: '', label: 'Formula',