为所有文本输入框添加右键快速粘贴的功能

This commit is contained in:
lyswhut
2023-02-05 15:32:11 +08:00
parent 83000b75ca
commit dd5bffe071
11 changed files with 82 additions and 35 deletions
+42 -2
View File
@@ -10,10 +10,13 @@
@input="handleInput"
@change="$emit('change', $event.target.value.trim())"
@keyup.enter="$emit('submit', $event.target.value.trim())"
@contextmenu="handleContextMenu"
>
</template>
<script>
import { clipboardReadText } from '@common/utils/electron'
export default {
props: {
placeholder: {
@@ -32,17 +35,54 @@ export default {
type: String,
default: 'text',
},
trim: {
type: Boolean,
default: true,
},
stopContentEventPropagation: {
type: Boolean,
default: true,
},
autoPaste: {
type: Boolean,
default: true,
},
// bindValue: {
// type: Boolean,
// default: true,
// },
},
emits: ['update:modelValue', 'submit', 'change'],
methods: {
handleInput(event) {
let value = event.target.value.trim()
event.target.value = value
let value = event.target.value
if (this.trim) {
value = value.trim()
event.target.value = value
}
this.$emit('update:modelValue', value)
},
focus() {
this.$refs.dom_input.focus()
},
handleContextMenu(event) {
if (this.stopContentEventPropagation) event.stopPropagation()
if (!this.autoPaste) return
let dom_input = this.$refs.dom_input
if (dom_input.selectionStart === null) return
let str = clipboardReadText()
str = str.trim()
str = str.replace(/\t|\r\n|\n|\r/g, ' ')
str = str.replace(/\s+/g, ' ')
const text = dom_input.value
// if (dom_input.selectionStart == dom_input.selectionEnd) {
const value = text.substring(0, dom_input.selectionStart) + str + text.substring(dom_input.selectionEnd, text.length)
event.target.value = value
this.$emit('update:modelValue', value)
// } else {
// clipboardWriteText(text.substring(dom_input.selectionStart, dom_input.selectionEnd))
// }
},
},
}
</script>