fix: preserve cjk code block copy text

This commit is contained in:
lucaronin
2026-05-14 07:48:04 +02:00
parent 2fcea0624d
commit 95dab219b8
3 changed files with 41 additions and 2 deletions

View File

@@ -796,6 +796,31 @@ describe('SingleEditorView', () => {
expect(clipboardData.setData).toHaveBeenCalledWith('text/plain', json)
})
it('copies CJK fenced code text from the selected DOM fragment when selection text is mojibake', async () => {
const text = 'const label = "中文測試"'
const { container } = renderEditorHarness()
const { codeBlock, code } = createCodeBlockFixture(text)
await act(async () => {
container.appendChild(codeBlock)
await Promise.resolve()
})
const range = document.createRange()
range.selectNodeContents(code)
const getSelection = vi.spyOn(window, 'getSelection').mockReturnValue({
rangeCount: 1,
isCollapsed: false,
getRangeAt: () => range,
toString: () => 'const label = "中文測試"',
} as Selection)
const clipboardData = { setData: vi.fn() }
fireEvent.copy(code, { clipboardData })
getSelection.mockRestore()
expect(clipboardData.setData).toHaveBeenCalledWith('text/plain', text)
})
it('copies fenced code from the code-block action button', async () => {
const json = '{\n "id": "Demo"\n}'
const writeText = vi.fn().mockResolvedValue(undefined)

View File

@@ -421,7 +421,7 @@ function selectedCodeBlockText(options: {
const range = selectedCodeBlockRange(options)
if (!range) return null
return options.selection?.toString() || range.cloneContents().textContent || ''
return range.cloneContents().textContent || options.selection?.toString() || ''
}
function selectedEditorRange(selection: Selection | null, container: HTMLElement): Range | null {