fix(editor): 图片超链接弹窗带出图片地址(docs/31 更正)
上一笔把图片「超链接」做成预填包裹链接,用户指出「没有带出来 这张图片的 url」——要的是图片自己的地址出现在弹窗里。改版 ImageLinkModal:主字段「图片地址」打开即带出该图 src(本地相对 路径或网络地址),可查看可改;包裹链接降为第二字段「点击时打开 的链接(可空)」。提交时一条事务同时处理 src 与 link 标记, 没改动则不发事务。新增弹窗预填与 src patch 测试。
This commit is contained in:
45
tests/image-link-modal.test.tsx
Normal file
45
tests/image-link-modal.test.tsx
Normal 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()
|
||||
})
|
||||
})
|
||||
@@ -99,6 +99,16 @@ describe('图片链接标记(docs/31)', () => {
|
||||
await editor.destroy(); root.remove()
|
||||
})
|
||||
|
||||
it('改图片地址(src):info 带出原地址,patch 后序列化与回读都是新地址', async () => {
|
||||
const { root, editor } = await makeEditor('\n')
|
||||
const img = root.querySelector('img')!
|
||||
expect(imageInfoAtElement(editor, img)?.src).toBe('doc.assets/a.png')
|
||||
expect(updateImageAtElement(editor, img, { src: 'https://cdn.example.com/a.png' })).toBe(true)
|
||||
expect(getMarkdown(editor)).toBe('\n')
|
||||
expect(imageInfoAtElement(editor, root.querySelector('img')!)?.src).toBe('https://cdn.example.com/a.png')
|
||||
await editor.destroy(); root.remove()
|
||||
})
|
||||
|
||||
it('已有包裹链接的文档解析后能读出 href', async () => {
|
||||
const { root, editor } = await makeEditor('[](https://x.dev)\n')
|
||||
const img = root.querySelector('img')!
|
||||
|
||||
Reference in New Issue
Block a user