Files
edentext/src/lib/components/ribbon/tabs/ReferencesTab.svelte
T
Steffen Becker eaaec541af feat: an alphabetical index, marked word by word
References ▸ Index entry marks a place with the term the index should
list — under a key of its own where it belongs elsewhere — and the Index
in the same menu lists each term once, sorted case-insensitively, with
every page it was marked on.

Both formats write a place, not a range, so the entry is an inline atom:
ODF text:alphabetical-index-mark plus text:alphabetical-index, Word XE
plus INDEX. LibreOffice reads our file back unchanged. The bibliography,
the other half of that gap, is still open.
2026-08-14 08:44:00 +02:00

142 lines
5.1 KiB
Svelte

<script lang="ts">
import type { Editor } from '@tiptap/core';
import RibbonGroup from '../RibbonGroup.svelte';
import RibbonButton from '../RibbonButton.svelte';
import CaptionDialog from '../../CaptionDialog.svelte';
import type { IndexKind } from '../../../editor/extensions/tableOfContents';
import { anchored, clickOutside, isMenuOpen, toggleMenu, closeMenu } from '../menu.svelte';
import type { HfZone } from '../../../storage/headerFooter';
import { t } from '../../../i18n/i18n.svelte';
import { shortcutHint } from '../../../editor/shortcuts';
import { HEADING_LEVELS } from '../../../export/odt';
let { editor, tick, hfActive = null, onNoteOptions }: {
editor: Editor | null;
tick: number;
hfActive?: HfZone | null;
onNoteOptions?: () => void;
} = $props();
// The levels button drives a table of contents only — a caption index has one level.
// The document holds at most one, so the first hit is it.
let toc = $derived.by<{ pos: number; maxLevel: number } | null>(() => {
if (tick < 0 || !editor) return null;
let found: { pos: number; maxLevel: number } | null = null;
editor.state.doc.descendants((node, pos) => {
if (found || node.type.name !== 'tableOfContents' || node.attrs.index !== 'toc') return;
found = { pos, maxLevel: (node.attrs.maxLevel ?? 5) as number };
});
return found;
});
const LEVELS = HEADING_LEVELS;
const INDEX_KINDS: IndexKind[] = ['toc', 'figures', 'tables', 'alphabetical'];
let captionOpen = $state(false);
let hasSelection = $derived(tick >= 0 && !!editor && !editor.state.selection.empty);
// The selected text is the term unless the reader gives another — the same prompt
// both word processors open, which fills itself from the selection.
function markIndexEntry() {
if (!editor) return;
const { from, to } = editor.state.selection;
const term = prompt(t().ribbon.indexEntryPrompt, editor.state.doc.textBetween(from, to, ' '));
if (term?.trim()) editor.chain().focus().setTextSelection(to).insertIndexEntry(term).run();
}
function setMaxLevel(level: number) {
closeMenu();
if (!editor || !toc) return;
editor.chain().focus().setNodeSelection(toc.pos).updateAttributes('tableOfContents', { maxLevel: level }).run();
}
</script>
<RibbonGroup label={t().ribbon.groups.toc}>
<div class="rb-menu-wrap" use:clickOutside={'indexKind'}>
<RibbonButton
variant="big"
icon="toc"
label={t().ribbon.toc}
title={hfActive ? t().toolbarExpanded.tocNotInHf : t().toolbarExpanded.insertToc}
disabled={!editor || !!hfActive}
caret
active={isMenuOpen('indexKind')}
onclick={() => toggleMenu('indexKind')}
/>
{#if isMenuOpen('indexKind')}
<div class="ribbon-menu" use:anchored role="menu">
{#each INDEX_KINDS as k}
<button onclick={() => { closeMenu(); editor?.chain().focus().setTableOfContents(k).run(); }}>
{t().ribbon.indexes[k]}
</button>
{/each}
</div>
{/if}
</div>
<div class="rb-menu-wrap" use:clickOutside={'tocLevels'}>
<RibbonButton
variant="big"
icon="tocLevels"
label={t().ribbon.tocOptions}
title={t().ribbon.tocOptions}
disabled={!toc}
caret
active={isMenuOpen('tocLevels')}
onclick={() => toggleMenu('tocLevels')}
/>
{#if isMenuOpen('tocLevels')}
<div class="ribbon-menu" use:anchored role="menu">
<div class="rb-menu-label">{t().ribbon.tocMaxLevel}</div>
{#each LEVELS as l}
<button class:selected={toc?.maxLevel === l} onclick={() => setMaxLevel(l)}>{l}</button>
{/each}
</div>
{/if}
</div>
<RibbonButton
variant="big"
icon="caption"
label={t().ribbon.insertCaption}
title={t().caption.title}
disabled={!editor || !!hfActive}
onclick={() => (captionOpen = true)}
/>
<RibbonButton
variant="big"
icon="bookmark"
label={t().ribbon.indexEntry}
title={hasSelection ? t().ribbon.indexEntry : t().ribbon.indexEntryNeedsSelection}
disabled={!editor || !!hfActive || !hasSelection}
onclick={markIndexEntry}
/>
</RibbonGroup>
<CaptionDialog bind:open={captionOpen} {editor} />
<RibbonGroup label={t().ribbon.groups.notes}>
<RibbonButton
variant="big"
icon="footnote"
label={t().ribbon.footnote}
title={hfActive ? t().toolbarExpanded.noteNotInHf : `${t().toolbarExpanded.insertFootnote} (${shortcutHint('footnote')})`}
disabled={!editor || !!hfActive}
onclick={() => editor?.chain().focus().insertNote('footnote').run()}
/>
<RibbonButton
icon="endnote"
label={t().ribbon.endnote}
title={hfActive ? t().toolbarExpanded.noteNotInHf : `${t().toolbarExpanded.insertEndnote} (${shortcutHint('endnote')})`}
disabled={!editor || !!hfActive}
onclick={() => editor?.chain().focus().insertNote('endnote').run()}
/>
<RibbonButton
icon="tocLevels"
label={t().ribbon.noteOptions}
title={t().notesDialog.title}
disabled={!onNoteOptions}
onclick={() => onNoteOptions?.()}
/>
</RibbonGroup>
<style>
.rb-menu-wrap { position: relative; }
</style>