Skip to content

Instantly share code, notes, and snippets.

@redgoose-dev
Last active February 18, 2021 09:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redgoose-dev/148c980770ccbc233baa7020e0ae49c1 to your computer and use it in GitHub Desktop.
Save redgoose-dev/148c980770ccbc233baa7020e0ae49c1 to your computer and use it in GitHub Desktop.
contenteditable with vue2
<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