diff --git a/src/renderer/src/components/InlineNameEdit.test.ts b/src/renderer/src/components/InlineNameEdit.test.ts new file mode 100644 index 000000000..58c689b17 --- /dev/null +++ b/src/renderer/src/components/InlineNameEdit.test.ts @@ -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) { + const wrapper = mount(InlineNameEdit, { + props: { name, prefix: 'test', onRename }, + attachTo: document.body + }) + const field = () => wrapper.get('.test-name').element + return { wrapper, field } +} + +async function typeName(field: HTMLElement, text: string): Promise { + 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', 'Bold') + 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') + }) +}) diff --git a/src/renderer/src/components/InlineNameEdit.vue b/src/renderer/src/components/InlineNameEdit.vue new file mode 100644 index 000000000..d6e5accee --- /dev/null +++ b/src/renderer/src/components/InlineNameEdit.vue @@ -0,0 +1,83 @@ + + + diff --git a/src/renderer/src/components/settings/ComfyUISettingsContent.test.ts b/src/renderer/src/components/settings/ComfyUISettingsContent.test.ts index d66082a1e..ec24100ff 100644 --- a/src/renderer/src/components/settings/ComfyUISettingsContent.test.ts +++ b/src/renderer/src/components/settings/ComfyUISettingsContent.test.ts @@ -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(() => { @@ -51,7 +52,7 @@ vi.mock('../../views/comfyUISettings/SettingsSectionList.vue', () => ({ default: { template: '
' } })) vi.mock('../../views/comfyUISettings/StatusFactPanel.vue', () => ({ - default: { template: '
' } + default: { name: 'StatusFactPanel', props: ['hideHero', 'onRename'], template: '
' } })) vi.mock('../../views/comfyUISettings/StoragePane.vue', () => ({ default: { template: '
' } @@ -70,6 +71,7 @@ vi.mock('../../views/comfyUISettings/MoreMenu.vue', () => ({ default: { name: 'MoreMenu', props: ['open', 'actions'], + emits: ['pick', 'close'], template: '
menu
' } })) @@ -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 = '

' + 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('.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('.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' + }) + }) + }) }) diff --git a/src/renderer/src/components/settings/ComfyUISettingsContent.vue b/src/renderer/src/components/settings/ComfyUISettingsContent.vue index 0719859d2..b84b27ad9 100644 --- a/src/renderer/src/components/settings/ComfyUISettingsContent.vue +++ b/src/renderer/src/components/settings/ComfyUISettingsContent.vue @@ -46,6 +46,7 @@ import MoreMenu, { type MenuAction } from '../../views/comfyUISettings/MoreMenu. import ArgsBuilderPage from '../../views/comfyUISettings/ArgsBuilderPage.vue' import SnapshotsView from '../../views/comfyUISettings/SnapshotsView.vue' import StatusFactPanel from '../../views/comfyUISettings/StatusFactPanel.vue' +import InlineNameEdit from '../InlineNameEdit.vue' import SettingsSectionList from '../../views/comfyUISettings/SettingsSectionList.vue' import StoragePane, { type StorageSnapshot } from '../../views/comfyUISettings/StoragePane.vue' import ConsoleTerminalPane from '../../views/comfyUISettings/ConsoleTerminalPane.vue' @@ -87,6 +88,7 @@ interface Props { currentView?: ViewKind currentCategory?: Category | null pageActionsTarget?: string + pageTitleTarget?: string } const props = withDefaults(defineProps(), { @@ -99,6 +101,7 @@ const props = withDefaults(defineProps(), { currentView: 'dashboard', currentCategory: null, pageActionsTarget: undefined, + pageTitleTarget: undefined, globalSettingsSnapshot: () => ({ sharedDirectoriesFields: [], modelsDirs: [], @@ -366,6 +369,19 @@ function closeMoreMenu(): void { moreMenuOpen.value = false } +const canRename = computed(() => + pinBottomActions.value.some((action) => action.id === 'rename' && action.enabled !== false) +) +const renameHandler = computed(() => (canRename.value ? renameInstallation : undefined)) +const titleEdit = useTemplateRef>('titleEdit') +function handleMorePick(action: ActionDef): void { + if (action.id === 'rename' && titleEdit.value) { + titleEdit.value.focus() + return + } + void runAction(action) +} + type SubPage = 'args' | null const subPage = ref(null) const subPageTransition = ref<'subpage-push' | 'subpage-pop'>('subpage-push') @@ -891,9 +907,10 @@ defineExpose({ :installation="installation" :sections="statusSections" :disk-usage="diskUsageItem" - :on-rename="renameInstallation" + :on-rename="renameHandler" :on-update-url="handleUrlUpdate" :url-restart-pending="pendingRestartFieldIds.has('remoteUrl')" + :hide-hero="!!pageTitleTarget" />
+ + + +
diff --git a/src/renderer/src/composables/useInlineTextEdit.ts b/src/renderer/src/composables/useInlineTextEdit.ts new file mode 100644 index 000000000..530e359b1 --- /dev/null +++ b/src/renderer/src/composables/useInlineTextEdit.ts @@ -0,0 +1,96 @@ +import { watch, type Ref } from 'vue' + +export interface InlineTextEditOptions { + value: () => string + commit: (next: string) => Promise + accepts?: (next: string) => boolean +} + +export interface InlineTextEdit { + focus: () => void + onKeydown: (event: KeyboardEvent) => void + onPaste: (event: ClipboardEvent) => void + onBlur: () => Promise +} + +function selectContents(el: HTMLElement, collapseToEnd: boolean): void { + const range = document.createRange() + range.selectNodeContents(el) + if (collapseToEnd) range.collapse(false) + const selection = window.getSelection() + selection?.removeAllRanges() + selection?.addRange(range) +} + +function isSelectAll(event: KeyboardEvent): boolean { + return (event.metaKey || event.ctrlKey) && !event.altKey && event.key.toLowerCase() === 'a' +} + +export function useInlineTextEdit( + el: Readonly>, + options: InlineTextEditOptions +): InlineTextEdit { + function isEditing(): boolean { + return !!el.value && document.activeElement === el.value + } + + function sync(): void { + const node = el.value + if (!node) return + const value = options.value() + if (node.textContent !== value) node.textContent = value + } + + watch( + [options.value, el], + () => { + if (!isEditing()) sync() + }, + { immediate: true, flush: 'post' } + ) + + function focus(): void { + const node = el.value + if (!node) return + node.focus() + selectContents(node, true) + } + + function onKeydown(event: KeyboardEvent): void { + if (event.isComposing) return + const node = el.value + if (!node) return + if (event.key === 'Enter') { + event.preventDefault() + node.blur() + } else if (event.key === 'Escape') { + event.preventDefault() + sync() + node.blur() + } else if (isSelectAll(event)) { + event.preventDefault() + selectContents(node, false) + } + } + + function onPaste(event: ClipboardEvent): void { + event.preventDefault() + const text = event.clipboardData?.getData('text/plain') ?? '' + document.execCommand('insertText', false, text.replace(/\s*\n\s*/g, ' ')) + } + + async function onBlur(): Promise { + const node = el.value + if (!node || !document.hasFocus()) return + const next = node.textContent?.trim() ?? '' + if (!next || next === options.value() || options.accepts?.(next) === false) { + sync() + return + } + if (node.textContent !== next) node.textContent = next + const committed = await options.commit(next) + if (!committed && !isEditing()) sync() + } + + return { focus, onKeydown, onPaste, onBlur } +} diff --git a/src/renderer/src/panel/BuildDetailsView.test.ts b/src/renderer/src/panel/BuildDetailsView.test.ts index 4a04c6c47..f5b8c7243 100644 --- a/src/renderer/src/panel/BuildDetailsView.test.ts +++ b/src/renderer/src/panel/BuildDetailsView.test.ts @@ -9,7 +9,7 @@ import type { Installation } from '../types/ipc' vi.mock('../components/settings/ComfyUISettingsContent.vue', () => ({ default: { name: 'ComfyUISettingsContent', - props: ['installation', 'globalSettingsSnapshot', 'pageActionsTarget'], + props: ['installation', 'globalSettingsSnapshot', 'pageActionsTarget', 'pageTitleTarget'], emits: [ 'show-progress', 'navigate-list', @@ -18,7 +18,8 @@ vi.mock('../components/settings/ComfyUISettingsContent.vue', () => ({ 'primary-action', 'open-new-window' ], - template: '
' + template: + '
{{ installation.name }}
' } })) @@ -51,7 +52,8 @@ beforeEach(() => { async function mountDetails(installation: Installation = INSTALL) { const wrapper = mount(BuildDetailsView, { - props: { installation } + props: { installation }, + attachTo: document.body }) await flushPromises() return { wrapper, content: wrapper.findComponent({ name: 'ComfyUISettingsContent' }) } @@ -79,12 +81,13 @@ describe('BuildDetailsView', () => { expect(wrapper.get('.build-details-status').text()).toBe('Running') }) - it('hands the settings body its build, shared storage and action slot', async () => { + it('hands the settings body its build, shared storage, title and action slots', async () => { const { content } = await mountDetails() expect(content.props('installation')).toEqual(INSTALL) expect(content.props('globalSettingsSnapshot')).toEqual(SNAPSHOT) expect(content.props('pageActionsTarget')).toBe('#build-details-actions') + expect(content.props('pageTitleTarget')).toBe('#build-details-title') }) it('launches the build from the primary action', async () => { diff --git a/src/renderer/src/panel/BuildDetailsView.vue b/src/renderer/src/panel/BuildDetailsView.vue index 05cc4efcd..1bb38b674 100644 --- a/src/renderer/src/panel/BuildDetailsView.vue +++ b/src/renderer/src/panel/BuildDetailsView.vue @@ -11,6 +11,7 @@ import { useSessionStore } from '../stores/sessionStore' import type { Installation, ShowProgressOpts } from '../types/ipc' const ACTIONS_ID = 'build-details-actions' +const TITLE_ID = 'build-details-title' const { installation } = defineProps<{ installation: Installation @@ -82,19 +83,12 @@ onUnmounted(() => unsubscribeSettings?.())
- -

- {{ installation.name }} -

+ :id="TITLE_ID" + class="m-0 flex min-w-0 font-display text-display font-semibold text-foreground" + />