Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -129,8 +129,9 @@ const ElTooltipStub = defineComponent({
}
})

function mountComponent(props = {}) {
function mountComponent(props = {}, inputComponent: Component = ElInputStub) {
const wrapper = mount(DcDeviceMathFormulaEditor, {
attachTo: inputComponent === ElInput ? document.body : undefined,
Comment thread
KyleHuangFromHZ marked this conversation as resolved.
Resolved
props: {
modelValue: '',
label: 'Formula',
Expand All @@ -144,7 +145,7 @@ function mountComponent(props = {}) {
props: ['label'],
template: '<label><span>{{ label }}</span><slot /></label>'
},
ElInput: ElInputStub,
ElInput: inputComponent,
ElButton: {
emits: ['click'],
template: '<button type="button" @click="$emit(\'click\', $event)"><slot /></button>'
Expand Down Expand Up @@ -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])
})
})
16 changes: 10 additions & 6 deletions apps/frontend/src/modules/device/math/formula-editor/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<HTMLInputElement | HTMLTextAreaElement>('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' &&
Expand Down
Loading