In Vue, v-model on a text input is the obvious choice. On a rich text editor it is still a good default, but the value is a whole HTML document, not a short string, and that changes a few decisions. This article covers when to bind with v-model, when to read the content only on save, and the mistakes that make the cursor jump.
Two ways to connect the editor
Bound. The component takes the HTML through v-model and emits a new value as the user types. Your ref always holds the current content.
<script setup>
import { ref } from "vue";
import { RichTextEditor } from "@richscripts/richtexteditor/vue";
const html = ref("<p>Draft your update...</p>");
</script>
<template>
<RichTextEditor v-model="html" :config="{ height: 400 }" />
</template>Read on demand. The component gets a starting value once. You keep a template ref and ask for the content when you need it, for example when the user presses Save.
<script setup>
import { ref } from "vue";
import { RichTextEditor } from "@richscripts/richtexteditor/vue";
const editorRef = ref(null);
function save() {
const html = editorRef.value?.getHTMLCode() ?? "";
// send html to your API ...
}
</script>
<template>
<RichTextEditor ref="editorRef" :default-value="'<p></p>'" />
<button @click="save">Save</button>
</template>Both samples are the ones on the Vue rich text editor page.
Which one to use
- Use
v-modelwhen other parts of the page react to the content while the user types: a live preview, a character counter, a Save button that enables when something changed. - Read on demand when the content only matters at save time, or when documents are long. A bound value means the document is turned into an HTML string on each change, and that work grows with the document.
If you are unsure, start with v-model, then type in the longest document your users will realistically write. If typing stays smooth, keep it.
Do not write back while the user types
The most common bug with a bound editor is a cursor that jumps to the start or the end. The cause is nearly always the same: code that changes the bound value in response to the editorâs own update. A watcher that trims the HTML, a formatter that âcleansâ it, or a store that returns a slightly different string all send new content back into the editor, and replacing the content resets the selection.
- Treat the bound value as owned by the editor while it has focus. Read it; do not rewrite it.
- Clean and sanitize on the server when you save, not in a watcher.
- Set the value from outside only for real external changes: loading another record, or a reset.
The same problem in React is described in why the cursor jumps in a React editor.
Autosave from a bound value
Do not send a request on every update. Watch the value, wait until the user has paused, and also set a maximum wait so that someone who types without stopping is still saved. Number each request and ignore a response that is older than the last one you applied. Autosave without losing the last sentence covers the details, including what to do when the page is closed.
An empty editor is not an empty string
A required-field check such as html.length > 0 passes for an editor that holds only an empty paragraph. Check for real content instead: text, or an element such as an image or a table. See required validation for a rich text editor.
Server rendering
A rich text editor needs the browserâs document, so it cannot render on the server. In a Nuxt application, render the editor component only on the client, and render the saved HTML itself on the server for visitors who are reading, not editing.
A short test list
- Type in the middle of a paragraph and confirm the cursor stays where it is.
- Load a second record into the same editor and confirm the content changes.
- Type quickly for ten seconds and count the save requests.
- Submit with only an empty paragraph and confirm the required check fails.
- Leave and re-enter the page and confirm no editor from the earlier visit is left behind.
RichTextEditor is a perpetual-licence JavaScript editor â one purchase, self-hosted, no metered editor loads. Download the evaluation or see how it compares.