mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-02 14:04:50 +00:00
fix(webui): exclude file extension when selecting file name during inline rename (#285)
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { describe, expect, it, vi } from 'vitest'
|
||||
import { getFileNameBaseRange, selectFileNameWithoutExtension } from './rename-utils'
|
||||
|
||||
describe('rename-utils', () => {
|
||||
describe('getFileNameBaseRange', () => {
|
||||
it('returns range excluding file extension for standard filenames', () => {
|
||||
expect(getFileNameBaseRange('test.png')).toEqual([0, 4])
|
||||
expect(getFileNameBaseRange('document.pdf')).toEqual([0, 8])
|
||||
expect(getFileNameBaseRange('archive.tar.gz')).toEqual([0, 11])
|
||||
})
|
||||
|
||||
it('returns full length range for filenames without extension', () => {
|
||||
expect(getFileNameBaseRange('README')).toEqual([0, 6])
|
||||
expect(getFileNameBaseRange('my-folder')).toEqual([0, 9])
|
||||
})
|
||||
|
||||
it('returns full length range for dotfiles starting with dot', () => {
|
||||
expect(getFileNameBaseRange('.gitignore')).toEqual([0, 10])
|
||||
expect(getFileNameBaseRange('.env')).toEqual([0, 4])
|
||||
})
|
||||
|
||||
it('returns range excluding trailing dot for filenames ending with dot', () => {
|
||||
expect(getFileNameBaseRange('file.')).toEqual([0, 4])
|
||||
})
|
||||
|
||||
it('returns [0, 0] for empty string', () => {
|
||||
expect(getFileNameBaseRange('')).toEqual([0, 0])
|
||||
})
|
||||
})
|
||||
|
||||
describe('selectFileNameWithoutExtension', () => {
|
||||
it('calls focus and setSelectionRange on input element', () => {
|
||||
const input = {
|
||||
focus: vi.fn(),
|
||||
value: 'image.png',
|
||||
setSelectionRange: vi.fn(),
|
||||
select: vi.fn(),
|
||||
} as unknown as HTMLInputElement
|
||||
|
||||
selectFileNameWithoutExtension(input)
|
||||
|
||||
expect(input.focus).toHaveBeenCalled()
|
||||
expect(input.setSelectionRange).toHaveBeenCalledWith(0, 5)
|
||||
expect(input.select).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('falls back to select() if setSelectionRange is not a function', () => {
|
||||
const input = {
|
||||
focus: vi.fn(),
|
||||
value: 'image.png',
|
||||
setSelectionRange: undefined,
|
||||
select: vi.fn(),
|
||||
} as unknown as HTMLInputElement
|
||||
|
||||
selectFileNameWithoutExtension(input)
|
||||
|
||||
expect(input.focus).toHaveBeenCalled()
|
||||
expect(input.select).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Calculates the start and end selection range for a file name to exclude its extension.
|
||||
* e.g. "test.png" -> [0, 4] ("test")
|
||||
* e.g. "archive.tar.gz" -> [0, 11] ("archive.tar")
|
||||
* e.g. "file_without_ext" -> [0, 16] ("file_without_ext")
|
||||
* e.g. ".gitignore" -> [0, 10] (".gitignore")
|
||||
*/
|
||||
export function getFileNameBaseRange(name: string): [number, number] {
|
||||
const lastDotIndex = name.lastIndexOf('.')
|
||||
if (lastDotIndex > 0) {
|
||||
return [0, lastDotIndex]
|
||||
}
|
||||
return [0, name.length]
|
||||
}
|
||||
|
||||
/**
|
||||
* Focuses the input element and selects the base file name, excluding the extension.
|
||||
*/
|
||||
export function selectFileNameWithoutExtension(input: HTMLInputElement) {
|
||||
input.focus()
|
||||
const [start, end] = getFileNameBaseRange(input.value)
|
||||
if (typeof input.setSelectionRange === 'function') {
|
||||
input.setSelectionRange(start, end)
|
||||
} else {
|
||||
input.select()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user