Skip to content
json-render-office
Esc
↑↓navigate↵open⌘Jpreview
On this page

Examples

Live examples from the demo app.

The previews on this page run the example files of the demo app, so the docs and the demo cannot drift apart. The same files are in apps/example/src/examples. Open the Code tab of a preview to read it.

Preview

The web renderer of json-render-report draws a fixed report spec. The same spec is what the other two examples use.

<script setup lang="ts">
import { JSONUIProvider, Renderer } from '@json-render/vue'
import { themeStyle, webRegistry, webStyles } from 'json-render-report/web'

import { quarterly } from './quarterly'

// Vue templates reject a literal <style>, so the tag goes through a binding.
const styleTag = 'style'
const style = themeStyle()
</script>

<template>
  <div class="frame" role="region" aria-label="Report preview" tabindex="0">
    <component :is="styleTag">{{ webStyles }}</component>
    <JSONUIProvider :registry="webRegistry">
      <div :style="style">
        <Renderer :spec="quarterly" :registry="webRegistry" />
      </div>
    </JSONUIProvider>
  </div>
</template>

<style scoped>
.frame {
  max-height: 420px;
  overflow: auto;
  border: 1px solid #d4d4d4;
  border-radius: 8px;
  background: #ffffff;
}
</style>

Download

The same spec as a PowerPoint deck or a Word document, written in the browser with renderToPptxBlob and renderToDocxBlob.

<script setup lang="ts">
import { ref } from 'vue'

import { quarterly } from './quarterly'

const busy = ref<'pptx' | 'docx' | null>(null)

async function download(format: 'pptx' | 'docx'): Promise<void> {
  busy.value = format
  try {
    const { renderToDocxBlob, renderToPptxBlob } = await import('json-render-report')
    const blob = format === 'pptx' ? await renderToPptxBlob(quarterly) : await renderToDocxBlob(quarterly)
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = `quarterly-review.${format}`
    link.click()
    URL.revokeObjectURL(link.href)
  } finally {
    busy.value = null
  }
}
</script>

<template>
  <div class="buttons">
    <button type="button" :disabled="busy !== null" @click="download('pptx')">
      {{ busy === 'pptx' ? 'Preparing…' : 'Download PPTX' }}
    </button>
    <button type="button" :disabled="busy !== null" @click="download('docx')">
      {{ busy === 'docx' ? 'Preparing…' : 'Download DOCX' }}
    </button>
  </div>
</template>

<style scoped>
.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
button {
  min-height: 44px;
  padding: 12px 24px;
  border: 1px solid #d4d4d4;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
</style>

Streaming

The spec arrives as JSONL patches, two lines at a time, and the page fills in as createSpecStreamCompiler applies them. See Streaming.

<script setup lang="ts">
import { createSpecStreamCompiler } from '@json-render/core'
import type { Spec } from '@json-render/core'
import { JSONUIProvider, Renderer } from '@json-render/vue'
import { themeStyle, webRegistry, webStyles } from 'json-render-report/web'
import { onBeforeUnmount, onMounted, ref, shallowRef } from 'vue'

import { quarterly } from './quarterly'

const LINES_PER_TICK = 2
const TICK_MS = 250

// The patch lines a model would stream for the fixed spec, parents before children.
const lines: string[] = [JSON.stringify({ op: 'add', path: '/root', value: quarterly.root })]
const visit = (key: string): void => {
  const element = quarterly.elements[key]
  if (!element) return
  lines.push(JSON.stringify({ op: 'add', path: `/elements/${key}`, value: element }))
  element.children?.forEach(visit)
}
visit(quarterly.root)

// Vue templates reject a literal <style>, so the tag goes through a binding.
const styleTag = 'style'
const style = themeStyle()
const spec = shallowRef<Spec | null>(null)
const playing = ref(false)
let timer: ReturnType<typeof setInterval> | undefined

const stop = (): void => {
  clearInterval(timer)
  playing.value = false
}

function replay(): void {
  stop()
  const compiler = createSpecStreamCompiler<Spec>()
  let next = 0
  spec.value = null
  playing.value = true
  timer = setInterval(() => {
    const chunk = lines.slice(next, next + LINES_PER_TICK).join('\n') + '\n'
    next += LINES_PER_TICK
    spec.value = compiler.push(chunk).result
    if (next >= lines.length) stop()
  }, TICK_MS)
}

onMounted(replay)
onBeforeUnmount(stop)
</script>

<template>
  <div class="stream">
    <button type="button" :disabled="playing" @click="replay">{{ playing ? 'Streaming…' : 'Replay' }}</button>
    <div class="frame" role="region" aria-label="Streaming report preview" tabindex="0">
      <component :is="styleTag">{{ webStyles }}</component>
      <JSONUIProvider v-if="spec" :registry="webRegistry">
        <div :style="style">
          <Renderer :spec="spec" :registry="webRegistry" />
        </div>
      </JSONUIProvider>
    </div>
  </div>
</template>

<style scoped>
.stream {
  display: grid;
  gap: 8px;
}
button {
  justify-self: start;
  min-height: 44px;
  padding: 12px 24px;
  border: 1px solid #d4d4d4;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}
.frame {
  height: 420px;
  overflow: auto;
  border: 1px solid #d4d4d4;
  border-radius: 8px;
  background: #ffffff;
}
</style>

Was this page helpful?