mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-05-23 22:25:05 +00:00
## Summary - Stop contextmenu event propagation in EditableText component - Allows browser's native context menu (copy/paste) when renaming nodes ## Test plan - [ ] Double-click a node title to enter edit mode - [ ] Select text and right-click - [ ] Verify browser's native context menu appears (not the node context menu) ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-7633-fix-prevent-custom-context-menu-when-editing-text-2ce6d73d365081e38461d080abe12b32) by [Unito](https://www.unito.io)
105 lines
2.5 KiB
Vue
105 lines
2.5 KiB
Vue
<template>
|
|
<div class="editable-text">
|
|
<span v-if="!isEditing">
|
|
{{ modelValue }}
|
|
</span>
|
|
<!-- Avoid double triggering finishEditing event when keyup.enter is triggered -->
|
|
<InputText
|
|
v-else
|
|
ref="inputRef"
|
|
v-model:model-value="inputValue"
|
|
v-focus
|
|
type="text"
|
|
size="small"
|
|
fluid
|
|
:pt="{
|
|
root: {
|
|
onBlur: finishEditing,
|
|
...inputAttrs
|
|
}
|
|
}"
|
|
@keyup.enter.capture.stop="blurInputElement"
|
|
@keyup.escape.stop="cancelEditing"
|
|
@click.stop
|
|
@contextmenu.stop
|
|
@pointerdown.stop.capture
|
|
@pointermove.stop.capture
|
|
/>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import InputText from 'primevue/inputtext'
|
|
import { nextTick, ref, watch } from 'vue'
|
|
|
|
const {
|
|
modelValue,
|
|
isEditing = false,
|
|
inputAttrs = {}
|
|
} = defineProps<{
|
|
modelValue: string
|
|
isEditing?: boolean
|
|
inputAttrs?: Record<string, string>
|
|
}>()
|
|
|
|
const emit = defineEmits(['edit', 'cancel'])
|
|
const inputValue = ref<string>(modelValue)
|
|
const inputRef = ref<InstanceType<typeof InputText> | undefined>()
|
|
const isCanceling = ref(false)
|
|
|
|
const blurInputElement = () => {
|
|
// @ts-expect-error - $el is an internal property of the InputText component
|
|
inputRef.value?.$el.blur()
|
|
}
|
|
const finishEditing = () => {
|
|
// Don't save if we're canceling
|
|
if (!isCanceling.value) {
|
|
emit('edit', inputValue.value)
|
|
}
|
|
isCanceling.value = false
|
|
}
|
|
const cancelEditing = () => {
|
|
// Set canceling flag to prevent blur from saving
|
|
isCanceling.value = true
|
|
// Reset to original value
|
|
inputValue.value = modelValue
|
|
// Emit cancel event
|
|
emit('cancel')
|
|
// Blur the input to exit edit mode
|
|
blurInputElement()
|
|
}
|
|
watch(
|
|
() => isEditing,
|
|
async (newVal) => {
|
|
if (newVal) {
|
|
inputValue.value = modelValue
|
|
await nextTick(() => {
|
|
if (!inputRef.value) return
|
|
const fileName = inputValue.value.includes('.')
|
|
? inputValue.value.split('.').slice(0, -1).join('.')
|
|
: inputValue.value
|
|
const start = 0
|
|
const end = fileName.length
|
|
// @ts-expect-error - $el is an internal property of the InputText component
|
|
const inputElement = inputRef.value.$el
|
|
inputElement.setSelectionRange?.(start, end)
|
|
})
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
)
|
|
const vFocus = {
|
|
mounted: (el: HTMLElement) => el.focus()
|
|
}
|
|
</script>
|
|
|
|
<style scoped>
|
|
.editable-text {
|
|
display: inline;
|
|
}
|
|
.editable-text input {
|
|
width: 100%;
|
|
box-sizing: border-box;
|
|
}
|
|
</style>
|