Skip to content

AI-native design editor. Open-source Figma alternative.

refactor(variables): reuse useInlineRename for dialog renaming

Danila Poyarkov506b5f81e1aa
diff --git a/packages/vue/src/editor/inline-rename/use.ts b/packages/vue/src/editor/inline-rename/use.ts
index 5283cdf93..50a23afe4 100644
--- a/packages/vue/src/editor/inline-rename/use.ts
+++ b/packages/vue/src/editor/inline-rename/use.ts
@@ -5,7 +5,7 @@ export interface InlineRenameState<T extends string> {
editingId: Ref<T | null>
start: (id: T, currentName: string) => void
focusInput: (input: HTMLInputElement | null) => Promise<void>
- commit: (id: T, input: HTMLInputElement) => void
+ commit: (id: T, eventOrInput: Event | HTMLInputElement) => void
cancel: () => void
onKeydown: (e: KeyboardEvent) => void
}
@@ -35,8 +35,13 @@ export function useInlineRename<T extends string>(
input?.select()
}
- function commit(id: T, input: HTMLInputElement) {
+ function commit(id: T, eventOrInput: Event | HTMLInputElement) {
if (editingId.value !== id) return
+ const input =
+ eventOrInput instanceof HTMLInputElement
+ ? eventOrInput
+ : (eventOrInput.target as HTMLInputElement | null)
+ if (!input) return
const value = input.value.trim()
if (value && value !== originalName) {
onCommit(id, value)
diff --git a/packages/vue/src/variables/dialog/use.ts b/packages/vue/src/variables/dialog/use.ts
index a33a9e43f..ea99ab224 100644
--- a/packages/vue/src/variables/dialog/use.ts
+++ b/packages/vue/src/variables/dialog/use.ts
@@ -1,67 +1,32 @@
-import { nextTick, ref } from 'vue'
-
+import { useInlineRename } from '#vue/editor/inline-rename/use'
import { useVariables } from '#vue/variables/use'
export function useVariablesDialogState() {
const variables = useVariables()
- const editingCollectionId = ref<string | null>(null)
- const editingModeId = ref<string | null>(null)
- async function focusCollectionInput(input: HTMLInputElement | null) {
- if (!input) return
- await nextTick()
- input.focus()
- input.select()
- }
+ const collectionRename = useInlineRename((id, newName) => {
+ variables.renameCollection(id, newName)
+ })
- function startRenameCollection(id: string) {
- editingCollectionId.value = id
- }
+ const modeRename = useInlineRename((modeId, newName) => {
+ variables.renameMode(modeId, newName)
+ })
- function commitRenameCollection(id: string, event: Event) {
- if (editingCollectionId.value !== id) return
- const input = event.target
- if (!(input instanceof HTMLInputElement)) return
- const value = input.value.trim()
- const col = variables.collections.value.find((collection) => collection.id === id)
- if (col && value && value !== col.name) {
- variables.renameCollection(id, value)
- }
- editingCollectionId.value = null
+ function startRenameCollection(id: string) {
+ const col = variables.collections.value.find((c) => c.id === id)
+ if (col) collectionRename.start(id, col.name)
}
function startRenameMode(modeId: string) {
- editingModeId.value = modeId
- }
-
- async function focusModeInput(input: HTMLInputElement | null) {
- if (!input) return
- await nextTick()
- input.focus()
- input.select()
- }
-
- function commitRenameMode(modeId: string, event: Event) {
- if (editingModeId.value !== modeId) return
- const input = event.target
- if (!(input instanceof HTMLInputElement)) return
- const value = input.value.trim()
const mode = variables.activeModes.value.find((m) => m.modeId === modeId)
- if (mode && value && value !== mode.name) {
- variables.renameMode(modeId, value)
- }
- editingModeId.value = null
+ if (mode) modeRename.start(modeId, mode.name)
}
return {
...variables,
- editingCollectionId,
- editingModeId,
- focusCollectionInput,
- focusModeInput,
+ collectionRename,
+ modeRename,
startRenameCollection,
- commitRenameCollection,
- startRenameMode,
- commitRenameMode
+ startRenameMode
}
}
diff --git a/src/components/LayerTree.vue b/src/components/LayerTree.vue
index 4778eba6a..051e82816 100644
--- a/src/components/LayerTree.vue
+++ b/src/components/LayerTree.vue
@@ -104,7 +104,7 @@ function onTreeSelect(e: CustomEvent, select: (additive: boolean) => void) {
data-test-id="layers-item-input"
class="min-w-0 flex-1 rounded border border-accent bg-input px-1 py-0 text-xs text-surface outline-none"
:value="node.name"
- @blur="rename.commit(node.id, $event.target as HTMLInputElement)"
+ @blur="rename.commit(node.id, $event)"
@keydown.stop="rename.onKeydown"
/>
</div>
diff --git a/src/components/PagesPanel.vue b/src/components/PagesPanel.vue
index 6973d67b4..f7b6d3be2 100644
--- a/src/components/PagesPanel.vue
+++ b/src/components/PagesPanel.vue
@@ -64,7 +64,7 @@ function handlePageDblClick(
data-test-id="pages-item-input"
class="min-w-0 flex-1 rounded border border-accent bg-input px-1 py-0 text-xs text-surface outline-none"
:value="pg.name"
- @blur="rename.commit(pg.id, $event.target as HTMLInputElement)"
+ @blur="rename.commit(pg.id, $event)"
@keydown.stop="rename.onKeydown"
/>
</div>
diff --git a/src/components/VariablesDialog.vue b/src/components/VariablesDialog.vue
index 62b407c3f..fdaddff34 100644
--- a/src/components/VariablesDialog.vue
+++ b/src/components/VariablesDialog.vue
@@ -91,16 +91,12 @@ const collectionInput = templateRef<HTMLInputElement>('collectionInput')
const modeInput = templateRef<HTMLInputElement>('modeInput')
watch(collectionInput, (input) => {
- void ctx.focusCollectionInput(input)
+ void ctx.collectionRename.focusInput(input)
})
watch(modeInput, (input) => {
- void ctx.focusModeInput(input)
+ void ctx.modeRename.focusInput(input)
})
-function blurOnEnter(e: KeyboardEvent) {
- if (e.target instanceof HTMLInputElement) e.target.blur()
-}
-
function getModeId(columnId: string): string | undefined {
return columnId.startsWith('mode-') ? columnId.slice(5) : undefined
}
@@ -149,13 +145,12 @@ function modeId(columnId: string): string {
<TabsList class="flex flex-1 gap-0.5 overflow-x-auto px-3 py-1">
<template v-for="col in ctx.collections.value" :key="col.id">
<input
- v-if="ctx.editingCollectionId.value === col.id"
+ v-if="ctx.collectionRename.editingId.value === col.id"
ref="collectionInput"
class="w-24 rounded border border-accent bg-input px-2 py-0.5 text-xs text-surface outline-none"
:value="col.name"
- @blur="ctx.commitRenameCollection(col.id, $event)"
- @keydown.enter="blurOnEnter"
- @keydown.escape="ctx.editingCollectionId.value = null"
+ @blur="ctx.collectionRename.commit(col.id, $event)"
+ @keydown="ctx.collectionRename.onKeydown"
/>
<TabsTrigger
v-else
@@ -257,13 +252,12 @@ function modeId(columnId: string): string {
>
<template v-if="getModeId(header.column.id)">
<input
- v-if="ctx.editingModeId.value === getModeId(header.column.id)"
+ v-if="ctx.modeRename.editingId.value === getModeId(header.column.id)"
ref="modeInput"
class="-mx-1 w-full rounded border border-accent bg-input px-1 py-0 text-[11px] font-medium text-surface outline-none"
:value="header.column.columnDef.header"
- @blur="ctx.commitRenameMode(modeId(header.column.id), $event)"
- @keydown.enter="blurOnEnter"
- @keydown.escape="ctx.editingModeId.value = null"
+ @blur="ctx.modeRename.commit(modeId(header.column.id), $event)"
+ @keydown="ctx.modeRename.onKeydown"
/>
<ContextMenuRoot v-else>
<ContextMenuTrigger as-child>