add the character count extension

This commit is contained in:
Hans Pagel
2021-01-25 23:40:15 +01:00
parent 2f192700b2
commit 26fb84fe27
10 changed files with 95 additions and 27 deletions

View File

@@ -0,0 +1,80 @@
<template>
<div>
<editor-content :editor="editor" />
<div :class="{'character-limit': true, 'character-limit--warning': characters === limit}">
{{ characters }}/{{ limit }} characters
</div>
</div>
</template>
<script>
import { Editor, EditorContent } from '@tiptap/vue-starter-kit'
import Document from '@tiptap/extension-document'
import Paragraph from '@tiptap/extension-paragraph'
import Text from '@tiptap/extension-text'
import CharacterCount from '@tiptap/extension-character-count'
export default {
components: {
EditorContent,
},
data() {
return {
editor: null,
limit: 280,
}
},
mounted() {
this.editor = new Editor({
extensions: [
Document,
Paragraph,
Text,
CharacterCount.configure({
limit: this.limit,
}),
],
content: `
<p>
Lets make sure people cant write more than 280 characters. I bet you could build one of the biggest social networks on that idea.
</p>
`,
})
},
computed: {
characters() {
if (this.editor) {
return this.editor.state.doc.content.size - 2
}
return null
},
},
beforeDestroy() {
this.editor.destroy()
},
}
</script>
<style lang="scss">
/* Basic editor styles */
.ProseMirror {
> * + * {
margin-top: 0.75em;
}
}
.character-limit {
margin-top: 1rem;
color: #868e96;
&--warning {
color: #f03e3e;
}
}
</style>