fix style dropdown label reactivity on selection change
This commit is contained in:
parent
180c26a244
commit
d25265fd6a
1 changed files with 28 additions and 19 deletions
|
|
@ -31,6 +31,22 @@
|
||||||
let editor = $state<Editor>()
|
let editor = $state<Editor>()
|
||||||
let initialContent = $state<Content | undefined>()
|
let initialContent = $state<Content | undefined>()
|
||||||
|
|
||||||
|
// Version counter to trigger reactivity when editor state changes
|
||||||
|
let editorVersion = $state(0)
|
||||||
|
function onEditorUpdate() {
|
||||||
|
editorVersion++
|
||||||
|
}
|
||||||
|
|
||||||
|
// Derived label - must use $derived.by() for reactivity in Svelte 5
|
||||||
|
// Function calls in templates don't create reactive tracking
|
||||||
|
const styleLabel = $derived.by(() => {
|
||||||
|
void editorVersion // Force re-evaluation when version changes
|
||||||
|
if (editor?.isActive('heading', { level: 1 })) return 'Heading 1'
|
||||||
|
if (editor?.isActive('heading', { level: 2 })) return 'Heading 2'
|
||||||
|
if (editor?.isActive('heading', { level: 3 })) return 'Heading 3'
|
||||||
|
return 'Paragraph'
|
||||||
|
})
|
||||||
|
|
||||||
// Parse description JSON on mount
|
// Parse description JSON on mount
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
if (description) {
|
if (description) {
|
||||||
|
|
@ -55,19 +71,19 @@
|
||||||
onSave(content)
|
onSave(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStyleLabel(): string {
|
|
||||||
if (editor?.isActive('heading', { level: 1 })) return 'Heading 1'
|
|
||||||
if (editor?.isActive('heading', { level: 2 })) return 'Heading 2'
|
|
||||||
if (editor?.isActive('heading', { level: 3 })) return 'Heading 3'
|
|
||||||
return 'Paragraph'
|
|
||||||
}
|
|
||||||
|
|
||||||
function setHeading(level: 1 | 2 | 3) {
|
function setHeading(level: 1 | 2 | 3) {
|
||||||
|
// Defer to let dropdown close and focus settle
|
||||||
|
requestAnimationFrame(() => {
|
||||||
editor?.chain().focus().toggleHeading({ level }).run()
|
editor?.chain().focus().toggleHeading({ level }).run()
|
||||||
|
editorVersion++
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function setParagraph() {
|
function setParagraph() {
|
||||||
|
requestAnimationFrame(() => {
|
||||||
editor?.chain().focus().setParagraph().run()
|
editor?.chain().focus().setParagraph().run()
|
||||||
|
editorVersion++
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleLink() {
|
function toggleLink() {
|
||||||
|
|
@ -90,16 +106,7 @@
|
||||||
<DropdownMenu>
|
<DropdownMenu>
|
||||||
{#snippet trigger({ props })}
|
{#snippet trigger({ props })}
|
||||||
<button class="toolbar-button style-trigger" disabled={!editor} {...props}>
|
<button class="toolbar-button style-trigger" disabled={!editor} {...props}>
|
||||||
{#if editor?.isActive('heading', { level: 1 })}
|
<span>{styleLabel}</span>
|
||||||
<Heading1 size={16} />
|
|
||||||
{:else if editor?.isActive('heading', { level: 2 })}
|
|
||||||
<Heading2 size={16} />
|
|
||||||
{:else if editor?.isActive('heading', { level: 3 })}
|
|
||||||
<Heading3 size={16} />
|
|
||||||
{:else}
|
|
||||||
<Pilcrow size={16} />
|
|
||||||
{/if}
|
|
||||||
<span>{getStyleLabel()}</span>
|
|
||||||
<ChevronDown size={12} />
|
<ChevronDown size={12} />
|
||||||
</button>
|
</button>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
|
|
@ -221,6 +228,8 @@
|
||||||
bind:editor
|
bind:editor
|
||||||
content={initialContent}
|
content={initialContent}
|
||||||
editable={true}
|
editable={true}
|
||||||
|
onUpdate={onEditorUpdate}
|
||||||
|
onSelectionUpdate={onEditorUpdate}
|
||||||
class="description-editor"
|
class="description-editor"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue