Last active
February 18, 2021 09:59
-
-
Save redgoose-dev/148c980770ccbc233baa7020e0ae49c1 to your computer and use it in GitHub Desktop.
contenteditable with vue2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<component | |
:is="tag" | |
:contenteditable="true" | |
@input="update" | |
@blur="update" | |
@paste="paste"/> | |
</template> | |
<script> | |
export default { | |
name: 'content-editable', | |
props: { | |
tag: { type: String, default: 'div' }, | |
value: { type: String }, | |
}, | |
mounted() { | |
this.$el.innerText = this.value; | |
}, | |
watch: { | |
'value': function (value) | |
{ | |
if (value !== this.$el.innerText) | |
{ | |
this.$el.innerText = this.value; | |
} | |
} | |
}, | |
methods: { | |
update(e) | |
{ | |
this.$emit('input', e.target.innerText); | |
}, | |
paste(e) | |
{ | |
e.preventDefault(); | |
if (!window) return; | |
let text = (e.originalEvent || e).clipboardData.getData('text/plain'); | |
window.document.execCommand('insertText', false, text); | |
}, | |
}, | |
} | |
</script> | |
<style scoped> | |
div { | |
display: inline-block; | |
width: 100%; | |
box-sizing: border-box; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment