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
118 changes: 118 additions & 0 deletions src/renderer/src/components/InlineNameEdit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
import { flushPromises, mount } from '@vue/test-utils'
import InlineNameEdit from './InlineNameEdit.vue'

function mountName(name: string, onRename?: (newName: string) => Promise<boolean>) {
const wrapper = mount(InlineNameEdit, {
props: { name, prefix: 'test', onRename },
attachTo: document.body
})
const field = () => wrapper.get<HTMLElement>('.test-name').element
return { wrapper, field }
}

async function typeName(field: HTMLElement, text: string): Promise<void> {
field.focus()
field.textContent = text
await flushPromises()
}

afterEach(() => {
vi.restoreAllMocks()
})

describe('InlineNameEdit', () => {
it('renames on Enter', async () => {
const onRename = vi.fn().mockResolvedValue(true)
const { field } = mountName('Old', onRename)

await typeName(field(), ' New Name ')
field().dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true }))
await flushPromises()

expect(onRename).toHaveBeenCalledWith('New Name')
expect(document.activeElement).not.toBe(field())
})

it('keeps editing when Enter only confirms an input method candidate', async () => {
const onRename = vi.fn().mockResolvedValue(true)
const { field } = mountName('Old', onRename)

await typeName(field(), 'ni')
field().dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', isComposing: true, bubbles: true })
)
await flushPromises()

expect(onRename).not.toHaveBeenCalled()
expect(document.activeElement).toBe(field())
})

it('throws the draft away on Escape', async () => {
const onRename = vi.fn().mockResolvedValue(true)
const { field } = mountName('Old', onRename)

await typeName(field(), 'Draft')
field().dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
await flushPromises()

expect(onRename).not.toHaveBeenCalled()
expect(field().textContent).toBe('Old')
})

it('keeps the draft unsaved when the user switches to another app mid-edit', async () => {
const onRename = vi.fn().mockResolvedValue(true)
const { field } = mountName('Old', onRename)

await typeName(field(), 'Half typ')
vi.spyOn(document, 'hasFocus').mockReturnValue(false)
field().dispatchEvent(new FocusEvent('blur'))
await flushPromises()

expect(onRename).not.toHaveBeenCalled()
expect(field().textContent).toBe('Half typ')
})

it('puts the saved name back when the rename is refused', async () => {
const onRename = vi.fn().mockResolvedValue(false)
const { field } = mountName('Original', onRename)

await typeName(field(), 'Duplicate')
field().blur()
await flushPromises()

expect(onRename).toHaveBeenCalledWith('Duplicate')
expect(field().textContent).toBe('Original')
})

it('pastes multi-line text as a single line of plain text', async () => {
const onRename = vi.fn().mockResolvedValue(true)
const { field } = mountName('', onRename)
field().focus()

const clipboardData = new DataTransfer()
clipboardData.setData('text/plain', 'My\nBuild')
clipboardData.setData('text/html', '<b>Bold</b>')
field().dispatchEvent(new ClipboardEvent('paste', { clipboardData, bubbles: true }))
await flushPromises()

expect(field().textContent).toBe('My Build')
})

it('shows a read-only name when renaming is not offered', async () => {
const { wrapper, field } = mountName('Comfy Cloud')

expect(field().textContent).toBe('Comfy Cloud')
expect(field().getAttribute('contenteditable')).toBeNull()
expect(wrapper.find('.test-edit-btn').exists()).toBe(false)
})

it('shows the new name when it changes outside the editor', async () => {
const { wrapper, field } = mountName('First', vi.fn().mockResolvedValue(true))

await wrapper.setProps({ name: 'Second' })
await flushPromises()

expect(field().textContent).toBe('Second')
})
})
83 changes: 83 additions & 0 deletions src/renderer/src/components/InlineNameEdit.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<script setup lang="ts">
import { computed, useTemplateRef } from 'vue'
import { useI18n } from 'vue-i18n'
import Button from '@comfyorg/design-system/button/Button'
import { Pencil } from 'lucide-vue-next'
import { useInlineTextEdit } from '../composables/useInlineTextEdit'

const props = withDefaults(
defineProps<{
name: string
prefix: string
onRename?: (newName: string) => Promise<boolean>
textClass?: string
iconSize?: number
pencilWidth?: string
}>(),
{
onRename: undefined,
textClass: 'text-[16px] leading-6 font-semibold text-text',
iconSize: 13,
pencilWidth: '17px'
}
)

const { t } = useI18n()

const editable = computed(() => !!props.onRename)
const nameEl = useTemplateRef<HTMLElement>('nameEl')

const { focus, onKeydown, onPaste, onBlur } = useInlineTextEdit(nameEl, {
value: () => props.name,
commit: async (next) => (await props.onRename?.(next)) === true
})

defineExpose({ focus })
</script>

<template>
<span
:class="`${prefix}-name-wrap`"
class="group/name -ml-1.5 inline-flex max-w-[calc(100%+0.375rem)] min-w-0 items-center gap-1.5"
:style="{ '--inline-name-pencil': pencilWidth }"
>
<span
v-if="editable"
:id="`${prefix}-name`"
ref="nameEl"
:class="[`${prefix}-name`, textClass]"
class="peer/name min-w-0 cursor-text truncate rounded-md px-1.5 py-0.5 outline-none transition-[background-color] duration-[120ms] ease-[ease] hover:bg-[var(--brand-surface-bg-hover)] focus-visible:bg-[var(--brand-surface-bg-hover)] focus-visible:shadow-[0_0_0_2px_var(--focus-ring,var(--neutral-50))]"
role="textbox"
:aria-label="t('statusFactPanel.editName')"
contenteditable="plaintext-only"
spellcheck="false"
@blur="onBlur"
@keydown="onKeydown"
@paste="onPaste"
/>
<span
v-else
:class="[`${prefix}-name`, `${prefix}-name-static`, textClass]"
class="min-w-0 cursor-default truncate rounded-md px-1.5 py-0.5 outline-none"
>{{ name }}</span
>
<Button
v-if="editable"
variant="textonly"
size="unset"
type="button"
:class="`${prefix}-edit-btn`"
class="static -ml-1.5 inline-flex w-0 shrink-0 appearance-auto items-center justify-center overflow-hidden rounded-md border-none bg-transparent p-0 transition-[width,margin-left] duration-[120ms] ease-[ease] group-hover/name:ml-0 group-hover/name:w-[var(--inline-name-pencil)] peer-focus-visible/name:ml-0 peer-focus-visible/name:w-[var(--inline-name-pencil)] hover:bg-transparent focus-visible:ml-0 focus-visible:w-[var(--inline-name-pencil)] focus-visible:ring-0 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-[var(--focus-ring,var(--neutral-50))] focus-visible:[outline-style:solid] [&>svg]:opacity-0 group-hover/name:[&>svg]:opacity-60 peer-focus-visible/name:[&>svg]:opacity-60 focus-visible:[&>svg]:opacity-60"
:aria-label="t('statusFactPanel.editName')"
:aria-controls="`${prefix}-name`"
@click="focus"
>
<Pencil
:size="iconSize"
:class="`${prefix}-edit-hint`"
class="shrink-0 text-text-muted transition-opacity duration-[120ms] ease-[ease] select-none"
aria-hidden="true"
/>
</Button>
</span>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ vi.mock('../../composables/useComfyUISettings', () => ({
useComfyUISettings: () => ({
...useComfyUISettingsState,
updateField: vi.fn(),
renameInstallation: vi.fn().mockResolvedValue(true),
runAction: useComfyUISettingsState.runActionStub,
sectionsForTab: (tab: string) =>
computed(() => {
Expand All @@ -51,7 +52,7 @@ vi.mock('../../views/comfyUISettings/SettingsSectionList.vue', () => ({
default: { template: '<div data-testid="settings-section-list-stub"></div>' }
}))
vi.mock('../../views/comfyUISettings/StatusFactPanel.vue', () => ({
default: { template: '<div />' }
default: { name: 'StatusFactPanel', props: ['hideHero', 'onRename'], template: '<div />' }
}))
vi.mock('../../views/comfyUISettings/StoragePane.vue', () => ({
default: { template: '<div />' }
Expand All @@ -70,6 +71,7 @@ vi.mock('../../views/comfyUISettings/MoreMenu.vue', () => ({
default: {
name: 'MoreMenu',
props: ['open', 'actions'],
emits: ['pick', 'close'],
template: '<div v-if="open" data-testid="more-menu">menu</div>'
}
}))
Expand Down Expand Up @@ -810,4 +812,91 @@ describe('ComfyUISettingsContent', () => {
expect(w.emitted('op-dismiss')).toHaveLength(1)
})
})

describe('rename from the page title', () => {
let titleTarget: HTMLElement | null = null

function mountTitleTarget(): HTMLElement {
titleTarget = document.createElement('div')
titleTarget.innerHTML = '<h1 id="page-title"></h1><div id="page-actions-slot"></div>'
document.body.append(titleTarget)
return titleTarget
}

afterEach(() => {
titleTarget?.remove()
titleTarget = null
})

function moreMenu(w: VueWrapper) {
return w
.findAllComponents({ name: 'MoreMenu' })
.find((m) => (m.props('actions') as { id: string }[]).some((a) => a.id === 'rename'))!
}

function mountOnPage() {
return mountContent({
pageActionsTarget: '#page-actions-slot',
pageTitleTarget: '#page-title',
initialTab: 'status'
})
}

it('edits the name in the page title and points the Rename item at it', async () => {
useComfyUISettingsState.pinBottomActions.value = [
{ id: 'rename', label: 'Rename' },
{ id: 'untrack', label: 'Forget' }
]
useComfyUISettingsState.runActionStub.mockClear()
const target = mountTitleTarget()
const w = await mountOnPage()
await flushPromises()

const name = target.querySelector<HTMLElement>('.build-details-title-name')!
expect(name.textContent).toBe('My Install')
expect(name.getAttribute('contenteditable')).toBe('plaintext-only')
expect(w.findComponent({ name: 'StatusFactPanel' }).props('hideHero')).toBe(true)

moreMenu(w).vm.$emit('pick', { id: 'rename', label: 'Rename' })
await flushPromises()
expect(document.activeElement).toBe(name)
expect(useComfyUISettingsState.runActionStub).not.toHaveBeenCalled()

moreMenu(w).vm.$emit('pick', { id: 'untrack', label: 'Forget' })
expect(useComfyUISettingsState.runActionStub).toHaveBeenCalledWith({
id: 'untrack',
label: 'Forget'
})
})

it('shows a read-only title when the build offers no Rename action', async () => {
useComfyUISettingsState.pinBottomActions.value = [{ id: 'untrack', label: 'Forget' }]
const target = mountTitleTarget()
const w = await mountOnPage()
await flushPromises()

const name = target.querySelector<HTMLElement>('.build-details-title-name')!
expect(name.textContent).toBe('My Install')
expect(name.getAttribute('contenteditable')).toBeNull()
expect(target.querySelector('.build-details-title-edit-btn')).toBeNull()
expect(w.findComponent({ name: 'StatusFactPanel' }).props('onRename')).toBeUndefined()
})

it('keeps the About name and the Rename prompt outside the page', async () => {
useComfyUISettingsState.pinBottomActions.value = [{ id: 'rename', label: 'Rename' }]
useComfyUISettingsState.runActionStub.mockClear()
const w = await mountContent({ initialTab: 'status' })

const panel = w.findComponent({ name: 'StatusFactPanel' })
expect(panel.props('hideHero')).toBe(false)
expect(panel.props('onRename')).toBeTypeOf('function')
expect(document.querySelector('.build-details-title-name')).toBeNull()

moreMenu(w).vm.$emit('pick', { id: 'rename', label: 'Rename' })
expect(useComfyUISettingsState.runActionStub).toHaveBeenCalledWith({
id: 'rename',
label: 'Rename'
})
})
})
})
Loading
Loading