fix(editor): 图片超链接弹窗带出图片地址(docs/31 更正)

上一笔把图片「超链接」做成预填包裹链接,用户指出「没有带出来
这张图片的 url」——要的是图片自己的地址出现在弹窗里。改版
ImageLinkModal:主字段「图片地址」打开即带出该图 src(本地相对
路径或网络地址),可查看可改;包裹链接降为第二字段「点击时打开
的链接(可空)」。提交时一条事务同时处理 src 与 link 标记,
没改动则不发事务。新增弹窗预填与 src patch 测试。
This commit is contained in:
Muse
2026-11-15 10:06:47 +08:00
parent 0c09d94501
commit 1c0dd00088
6 changed files with 120 additions and 14 deletions

View File

@@ -0,0 +1,45 @@
// 图片链接弹窗(docs/31 更正):打开就带出图片地址,包裹链接在第二字段。
import { describe, it, expect, vi } from 'vitest'
import { createRoot } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { ImageLinkModal } from '../src/components/Modals'
function setInput(input: HTMLInputElement, value: string) {
const setter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set
setter?.call(input, value)
input.dispatchEvent(new Event('input', { bubbles: true }))
}
describe('ImageLinkModal', () => {
it('预填图片地址与已有包裹链接;改地址后提交回传两者', async () => {
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
const onSubmit = vi.fn()
await act(async () => {
root.render(<ImageLinkModal src="doc.assets/a.png" linkHref="https://x.dev" onSubmit={onSubmit} onCancel={() => undefined} />)
})
const inputs = container.querySelectorAll<HTMLInputElement>('.modal-input')
expect(inputs.length).toBe(2)
expect(inputs[0].value).toBe('doc.assets/a.png')
expect(inputs[1].value).toBe('https://x.dev')
await act(async () => { setInput(inputs[0], 'https://cdn.example.com/a.png') })
const ok = [...container.querySelectorAll('button')].find((b) => b.textContent === '确定')!
await act(async () => { ok.dispatchEvent(new MouseEvent('click', { bubbles: true })) })
expect(onSubmit).toHaveBeenCalledWith({ src: 'https://cdn.example.com/a.png', linkHref: 'https://x.dev' })
root.unmount(); container.remove()
})
it('无包裹链接时第二字段为空', async () => {
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
await act(async () => {
root.render(<ImageLinkModal src="https://img.example.com/b.png" linkHref="" onSubmit={() => undefined} onCancel={() => undefined} />)
})
const inputs = container.querySelectorAll<HTMLInputElement>('.modal-input')
expect(inputs[0].value).toBe('https://img.example.com/b.png')
expect(inputs[1].value).toBe('')
root.unmount(); container.remove()
})
})