Skip to content

Commit be99b3a

Browse files
committed
refactor: clean up names, remove unused util, align with tiptap conventions
- Merged latest changes from main branch and refactored code - Renamed `showBubbleMenus` to `showBubbleMenu` for consistency with tiptap terminology - Renamed `showSlashCommand` to `showSlashCommands` to accurately reflect its behavior - Corrected typo in `SearcnAndReplace`, changed to `SearchAndReplace` - Removed `getOrderedToolbarItems` utility as a new child-component approach is now used
1 parent 001d1cb commit be99b3a

File tree

10 files changed

+46
-88
lines changed

10 files changed

+46
-88
lines changed

src/lib/components/custom/demo-editor-settings.svelte

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
88
interface Props {
99
showToolBar: boolean;
10-
showBubbleMenus: boolean;
10+
showBubbleMenu: boolean;
1111
editable: boolean;
1212
}
1313
1414
let {
1515
showToolBar = $bindable(true),
16-
showBubbleMenus = $bindable(true),
16+
showBubbleMenu = $bindable(true),
1717
editable = $bindable(true)
1818
}: Props = $props();
1919
</script>
2020

2121
<Dialog.Root>
2222
<Dialog.Trigger class={buttonVariants({ variant: 'outline' })}>
2323
<Settings />
24-
<span>Demo Settings</span>
24+
<span>Editor settings</span>
2525
</Dialog.Trigger>
2626
<Dialog.Content class="h-fit max-h-[95vh] w-80 max-w-[95vw] p-4">
2727
<Dialog.Header class="mb-4">
@@ -31,13 +31,13 @@
3131
<div class="flex w-full items-center gap-2">
3232
<Checkbox id="toolbar" bind:checked={showToolBar} />
3333
<Label for="toolbar" class="w-full cursor-pointer text-sm font-medium leading-none"
34-
>Show Editor Toolbar</Label
34+
>Show editor toolbar</Label
3535
>
3636
</div>
3737
<div class="flex w-full items-center gap-2">
38-
<Checkbox id="menus" bind:checked={showBubbleMenus} />
38+
<Checkbox id="menus" bind:checked={showBubbleMenu} />
3939
<Label for="menus" class="w-full cursor-pointer text-sm font-medium leading-none"
40-
>Show Editor Menus</Label
40+
>Show editor bubble menu</Label
4141
>
4242
</div>
4343
<div class="flex w-full items-center gap-2">

src/lib/components/custom/editor-output.svelte

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
<Dialog.Root>
2525
<Dialog.Trigger class={buttonVariants({ variant: 'outline' })}>
26-
<FileJson /> Show Output
26+
<FileJson /> Show output
2727
</Dialog.Trigger>
2828
<Dialog.Content
2929
class={cn(
@@ -47,7 +47,9 @@
4747
<Dialog.Title>JSON Output</Dialog.Title>
4848
<Dialog.Description>Observe the JSON output of the editor content</Dialog.Description>
4949
</Dialog.Header>
50+
5051
<ShikiCode class="size-full overflow-auto" {code} lang="json" />
52+
5153
<Button
5254
variant="outline"
5355
class="ml-auto w-fit"

src/lib/edra/headless/editor.svelte

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@
4545
class: className = '',
4646
content = undefined,
4747
editable = true,
48-
showBubbleMenus = true,
48+
showBubbleMenu = true,
4949
limit = undefined,
50-
editable = true,
5150
editor = $bindable<Editor | undefined>(),
52-
showSlashCommand = true,
51+
showSlashCommands = true,
5352
onUpdate,
5453
children
5554
}: EdraProps = $props();
@@ -77,7 +76,7 @@
7776
AudioExtended(AudioExtendedComponent),
7877
ImageExtended(ImageExtendedComponent),
7978
VideoExtended(VideoExtendedComponent),
80-
...(showSlashCommand ? [slashcommand(SlashCommandList)] : [])
79+
...(showSlashCommands ? [slashcommand(SlashCommandList)] : [])
8180
],
8281
{
8382
editable,
@@ -90,7 +89,6 @@
9089
);
9190
return () => editor?.destroy();
9291
});
93-
9492
</script>
9593

9694
{#if editor}
@@ -99,7 +97,7 @@
9997

10098
<div class={`edra ${className}`}>
10199
{@render children?.()}
102-
{#if editor && showBubbleMenus}
100+
{#if editor && showBubbleMenu}
103101
<LinkMenu {editor} />
104102
<TableRowMenu {editor} />
105103
<TableColMenu {editor} />

src/lib/edra/headless/toolbar.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import type { Editor } from '@tiptap/core';
33
import { commands } from '../commands/commands.js';
44
import EdraToolBarIcon from './components/EdraToolBarIcon.svelte';
5-
import SearcnAndReplace from './components/SearcnAndReplace.svelte';
5+
import SearchAndReplace from './components/SearchAndReplace.svelte';
66
import type { Snippet } from 'svelte';
77
88
interface Props {
@@ -15,7 +15,6 @@
1515
1616
// Special components that are handled separately
1717
const specialComponents = ['fontSize', 'quickColor', 'searchAndReplace'];
18-
const toolbarItems = getOrderedToolbarItems(allowedCommands, commands, specialComponents) as Array<{ type: string; command?: any }>;
1918
let showSearchAndReplace = $state(false);
2019
const colorCommands = commands.colors.commands;
2120
const fontCommands = commands.fonts.commands;
@@ -75,6 +74,6 @@
7574
}}
7675
/>
7776
{/if}
78-
<SearcnAndReplace {editor} bind:show={showSearchAndReplace} />
77+
<SearchAndReplace {editor} bind:show={showSearchAndReplace} />
7978
{/if}
8079
</div>

src/lib/edra/shadcn/editor.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@
4343
let {
4444
class: className = '',
4545
content = undefined,
46-
showBubbleMenus = true,
46+
showBubbleMenu = true,
4747
limit = undefined,
4848
editable = true,
4949
editor = $bindable<Editor | undefined>(),
50-
showSlashCommand = true,
50+
showSlashCommands = true,
5151
onUpdate,
5252
children
5353
}: EdraProps = $props();
@@ -75,7 +75,7 @@
7575
ImageExtended(ImageExtendedComponent),
7676
VideoExtended(VideoExtendedComponent),
7777
AudioExtended(AudioExtendedComponent),
78-
...(showSlashCommand ? [slashcommand(SlashCommandList)] : [])
78+
...(showSlashCommands ? [slashcommand(SlashCommandList)] : [])
7979
],
8080
{
8181
editable,
@@ -97,7 +97,7 @@
9797

9898
<div class={cn('edra', className)}>
9999
{@render children?.()}
100-
{#if editor && showBubbleMenus}
100+
{#if editor && showBubbleMenu}
101101
<LinkMenu {editor} />
102102
<TableColMenu {editor} />
103103
<TableRowMenu {editor} />

src/lib/edra/shadcn/toolbar.svelte

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import SearchAndReplace from './components/SearchAndReplace.svelte';
88
import type { Snippet } from 'svelte';
99
import { cn } from '$lib/utils.js';
10-
import type { Snippet } from 'svelte';
1110
1211
interface Props {
1312
class?: string;

src/lib/edra/utils.ts

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -131,62 +131,17 @@ export function focusEditor(editor: Editor | undefined, event?: MouseEvent | Key
131131
}
132132
}
133133

134-
/**
135-
* Returns an ordered array of toolbar items based on allowed commands.
136-
*
137-
* @param allowedCommands - An optional array of allowed command names.
138-
* @param commands - An object with command groups. Each group should have a `commands` array.
139-
* @param specialComponents - An array of command names that are handled as special components.
140-
* @returns An array of toolbar items.
141-
*/
142-
export function getOrderedToolbarItems(
143-
allowedCommands: string[] | undefined,
144-
commands: Record<string, { commands: any[] }>,
145-
specialComponents: string[]
146-
): Array<{ type: string; command?: any }> {
147-
if (!allowedCommands?.length) {
148-
return [
149-
...Object.values(commands).flatMap((group) =>
150-
group.commands.map((cmd) => ({ type: 'command', command: cmd }))
151-
),
152-
...specialComponents.map((comp) => ({ type: comp }))
153-
];
154-
}
155-
156-
return allowedCommands
157-
.map((cmdName) => {
158-
if (specialComponents.includes(cmdName)) {
159-
return { type: cmdName };
160-
}
161-
// Check if it's a group
162-
if (commands[cmdName]) {
163-
return commands[cmdName].commands.map((cmd) => ({
164-
type: 'command',
165-
command: cmd
166-
}));
167-
}
168-
// Find individual command
169-
const command = Object.values(commands)
170-
.flatMap((group) => group.commands)
171-
.find((cmd) => cmd.name === cmdName);
172-
173-
return command ? { type: 'command', command } : null;
174-
})
175-
.flat()
176-
.filter(Boolean) as Array<{ type: string; command?: any }>;
177-
}
178-
179134
/**
180135
* Props for Edra's editor component
181136
*/
182137
export interface EdraProps {
183138
class?: string;
184139
content?: Content;
185140
editable?: boolean;
186-
showBubbleMenus?: boolean;
141+
showBubbleMenu?: boolean;
187142
limit?: number;
188143
editor?: Editor;
189-
showSlashCommand?: boolean;
144+
showSlashCommands?: boolean;
190145
/**
191146
* Callback function to be called when the content is updated
192147
* @param content

src/routes/+page.svelte

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@
1313
import DemoEditorSettings from '$lib/components/custom/demo-editor-settings.svelte';
1414
import EditorOutput from '$lib/components/custom/editor-output.svelte';
1515
16+
// Editor states
1617
let content = $state<Content>();
1718
let editor = $state<Editor>();
19+
let showToolBar = $state(true);
20+
let showBubbleMenu = $state(true);
21+
let editable = $state(true);
1822
1923
$effect(() => {
20-
console.log('Content', content);
24+
$inspect('[DEBUG] Content', content);
2125
localStorage.setItem('edra-content', JSON.stringify(content));
2226
});
2327
24-
let showToolBar = $state(true);
25-
let showBubbleMenus = $state(true);
26-
let editable = $state(true);
27-
2828
$effect(() => {
2929
console.log('editable', editable);
3030
editor?.setEditable(editable);
@@ -76,14 +76,14 @@
7676
</Button>
7777
</div>
7878
<div class="size-full *:my-2">
79-
<div class="text-center text-xl font-bold">Explore Demo</div>
79+
<div class="text-center text-xl font-bold">Explore the demo</div>
8080
<div class="flex flex-col items-center justify-center gap-4 sm:flex-row">
81-
<DemoEditorSettings bind:showToolBar bind:showBubbleMenus bind:editable />
81+
<DemoEditorSettings bind:showToolBar bind:showBubbleMenu bind:editable />
8282
<EditorOutput code={JSON.stringify(content, null, 2)} />
8383
</div>
8484
</div>
8585
</div>
86-
<div class="m-auto flex h-[35rem] w-[95%] flex-col rounded border sm:w-[85%]">
86+
<div class="m-auto flex w-[95%] flex-col rounded border sm:w-[85%]">
8787
{#if editor}
8888
{#if showToolBar}
8989
<div transition:slide>
@@ -92,7 +92,14 @@
9292
{/if}
9393
<EdraBubbleMenu {editor} />
9494
{/if}
95-
<Edra class="overflow-auto" bind:editor {editable} {showBubbleMenus} {content} {onUpdate} />
95+
<Edra
96+
class="h-[32rem] min-h-[32rem] overflow-auto"
97+
bind:editor
98+
{editable}
99+
{showBubbleMenu}
100+
{content}
101+
{onUpdate}
102+
/>
96103
</div>
97104
</div>
98105

src/routes/examples/headless/+page.svelte

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import type { Content, Editor } from '@tiptap/core';
44
import type { Transaction } from '@tiptap/pm/state';
55
import { Edra, EdraToolbar } from '$lib/edra/headless/index.js';
6-
import { slide } from 'svelte/transition';
76
import defaultContent from '$lib/default_content.js';
87
98
// Editor states
@@ -38,7 +37,7 @@
3837
<div class="mx-auto w-[95%] px-4">
3938
{#if editor && showToolBar}
4039
<div class="rounded-t border-x border-t p-1">
41-
<EdraToolbar {editor} allowedCommands={['bulletList', 'headings', 'quickColor']} />
40+
<EdraToolbar {editor} />
4241
</div>
4342
{/if}
4443
<div class="rounded-b border">
@@ -49,7 +48,6 @@
4948
{content}
5049
{onUpdate}
5150
{showSlashCommands}
52-
allowedBubbleMenuCommands={['link', 'bulletList', 'headings', 'quickColor']}
5351
/>
5452
</div>
5553
</div>

src/routes/examples/shadcn/+page.svelte

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import { browser } from '$app/environment';
33
import type { Content, Editor } from '@tiptap/core';
44
import type { Transaction } from '@tiptap/pm/state';
5-
import { Edra, EdraToolbar } from '$lib/edra/shadcn/index.js';
6-
import { slide } from 'svelte/transition';
5+
import { Edra, EdraToolbar, EdraBubbleMenu } from '$lib/edra/shadcn/index.js';
76
import defaultContent from '$lib/default_content.js';
7+
import FontSize from '$lib/edra/shadcn/components/FontSize.svelte';
88
99
// Editor states
1010
let content = $state<Content>();
@@ -14,7 +14,7 @@
1414
let showSlashCommands = $state(true);
1515
1616
$effect(() => {
17-
console.log('Content', content);
17+
$inspect('[DEBUG] Content', content);
1818
localStorage.setItem('edra-content', JSON.stringify(content));
1919
});
2020
@@ -38,20 +38,20 @@
3838
<div class="mx-auto w-[95%] px-4">
3939
{#if editor && showToolBar}
4040
<div class="rounded-t border-x border-t p-1">
41-
<EdraToolbar {editor} allowedCommands={['bulletList', 'headings', 'quickColor']} />
41+
<EdraToolbar {editor}>
42+
<FontSize {editor} />
43+
</EdraToolbar>
4244
</div>
45+
<EdraBubbleMenu {editor} />
4346
{/if}
4447
<div class="rounded-b border">
4548
<Edra
46-
class="overflow-auto h-64"
49+
class="h-64 overflow-auto"
4750
bind:editor
4851
{showBubbleMenu}
4952
{content}
5053
{onUpdate}
5154
{showSlashCommands}
52-
allowedBubbleMenuCommands={['link', 'bulletList', 'headings', 'quickColor']}
5355
/>
5456
</div>
5557
</div>
56-
57-

0 commit comments

Comments
 (0)