FormattingToolbar

Offers font, bold, italic, underline, colour and alignment controls for Word and spreadsheet selections.

import { FormattingToolbar } from '@tumblerjs/svelte';

Example

Preview

Loading project-budget.xlsx…

Select some cells, then format them. A control shows as mixed when the cells differ, for example when only some are bold.

ToolbarPreview.svelte
<script lang="ts">
  import { untrack } from 'svelte';
  import {
    createGridSelection,
    type FormattingPatch,
  } from '@tumblerjs/core';
  import { openSpreadsheetArtifact } from '@tumblerjs/sheets';
  import {
    FormattingToolbar,
    SpreadsheetGrid,
  } from '@tumblerjs/svelte';

  let { bytes }: { bytes: Uint8Array } = $props();
  let artifact = $state(
    untrack(() => openSpreadsheetArtifact(bytes)),
  );
  let selection = $state(
    createGridSelection({ row: 1, column: 1 }),
  );
  let error = $state('');
  const range = $derived(selection.range);

  function format(patch: FormattingPatch) {
    try {
      artifact = artifact.applyFormatting(range, patch);
      error = '';
    } catch {
      error = 'Could not format these cells.';
    }
  }
</script>

<div class="toolbar">
  <FormattingToolbar
    state={artifact.formattingState(range)}
    capabilities={artifact.formattingCapabilities(range)}
    onformat={format}
  />
</div>
<div class="sheet">
  <SpreadsheetGrid
    worksheet={artifact.worksheet}
    calculation={artifact.calculation}
    {selection}
    onselectionchange={(next) => (selection = next)}
    readonly
  />
</div>
{#if error}<p role="alert">{error}</p>{/if}

<style>
  .toolbar { overflow-x: auto; }
  .toolbar :global(.formatting-toolbar) {
    min-width: 480px;
  }
  .sheet { height: 240px; min-width: 0; }
  .sheet :global(.tumbler-grid) { height: 100%; }
</style>

Usage

The toolbar holds no formatting of its own, so your page supplies it and applies the changes. For a spreadsheet range, pass artifact.formattingState(range) as state and artifact.formattingCapabilities(range) as capabilities. In onformat, call artifact.applyFormatting(range, patch) and assign the returned artifact. Word artifacts have the same three methods. For slides, use PresentationFormattingToolbar from @tumblerjs/svelte/slides.

The example takes the file contents as a bytes prop. To run it in your app, save the example source as ToolbarPreview.svelte, put project-budget.xlsx in static/, and load it from +page.svelte:

+page.svelte
<script lang="ts">
  import { onMount } from 'svelte';
  import ToolbarPreview from './ToolbarPreview.svelte';

  let bytes = $state<Uint8Array>();
  onMount(async () => {
    const response = await fetch('/project-budget.xlsx');
    bytes = new Uint8Array(await response.arrayBuffer());
  });
</script>

{#if bytes}<ToolbarPreview {bytes} />{/if}

Guide: Word text and styles →

Props

PropDescription
staterequired FormattingStateThe formatting of the current selection. When the selection mixes formatting, for example only some of it is bold, the control shows as mixed.
capabilitiesrequired FormattingCapabilitiesWhich controls to show for the current selection. The rest are hidden.
onformat (patch: FormattingPatch) => voidCalled with the change when the user uses a control.
disabled booleanDisables every control.