Engineering

v-model on a rich text editor in Vue

When to bind an editor with v-model and when to read the content only on save, why writing back to the bound value moves the cursor, and how to autosave.

5 min read

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.

Code example
<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.

Code example
<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

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.

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


RichTextEditor is a perpetual-licence JavaScript editor — one purchase, self-hosted, no metered editor loads. Download the evaluation or see how it compares.