Skip to Content

SDK

Vue

Reference for @obelism/content-vue: live preview composables and rich text for Vue 3 and Nuxt.

2 min read
npm i @obelism/content @obelism/content-vue

@obelism/content-vue is for Vue 3 and Nuxt, and re-exports editable and the types of @obelism/content. The composables return computed refs; call them in setup. The bridge starts after mount, so server rendering and hydration always see "not editing".

Live preview

app/app.vue
<script setup lang="ts">
import { ContentBridge } from '@obelism/content-vue'
</script>

<template>
	<!-- Starts the bridge, so blocks are selectable on every page. -->
	<ContentBridge />
	<NuxtPage />
</template>
app/components/ContentPage.vue
<script setup lang="ts">
import { editable, useLiveEntry } from '@obelism/content-vue'

const props = defineProps<{ entry: PageEntry }>()
// The editor's unsaved version while editing; the entry otherwise.
const live = useLiveEntry(() => props.entry)
</script>

<template>
	<main v-bind="editable(live)">
		<section
			v-for="block in live.fields.body"
			:key="block._uid"
			v-bind="editable(block)"
		>
			…
		</section>
	</main>
</template>

Reference

  • <ContentBridge url? prompt? page-path? />: starts the bridge. Pass url only when the CMS isn't content.obelism.studio; :prompt="false" leaves the prompt box out; pagePath(path, locale) maps a CMS path in the page's locale to the site's URL for pages made with New page (default / + the path).
  • useLiveEntry(entry, { url? }): a computed ref of the entry (a value, ref or getter), or of the editor's live version of it.
  • useContentBridge(): a computed ref of { isEditing, mode, entryId, environment }, after mount.
  • useContentBridgeEvent('saved' | 'published' | 'update', listener): e.g. refreshNuxtData() on publish.
  • useContentPrompt(): a computed ref of { available, mode, prompting, canUndo, selectedUid, prompt, createPage, undo, select, save, publish, signOut, setSelecting }, for your own prompt UI with <ContentBridge :prompt="false" />.

Rich text

<script setup lang="ts">
import {
	RichText,
	type RichTextMarkProps,
	type RichTextNodeProps,
} from '@obelism/content-vue'
import { type FunctionalComponent, h } from 'vue'

import { NuxtLink } from '#components'
import Card from './Card.vue'

defineProps<{ entry: ArticleEntry }>()

const Heading: FunctionalComponent<RichTextNodeProps> = (_, { slots }) =>
	h('h2', slots.default?.())
const Link: FunctionalComponent<RichTextMarkProps> = ({ mark }, { slots }) =>
	h(NuxtLink, { to: String(mark.attrs?.href ?? '') }, slots.default)
</script>

<template>
	<RichText
		:document="entry.fields.body"
		:embedded-entry="Card"
		:nodes="{ heading: Heading }"
		:marks="{ link: Link }"
	/>
</template>

Overrides are components: a node or mark override gets node or mark as a prop and its rendered children in the default slot, embeddedEntry gets entry (a stub, { id, contentType }, past the requested depth) and node, embeddedAsset gets asset and node. It renders on the server too. Links are only rendered for safe URLs (http(s):, mailto:, tel:, # and paths). See rich text for the nodes and marks.

On this page

Marks in the margin

  • Note
  • Watch out
  • Updated

Scribes marked doubtful lines in the margin. That practice is called obelism.