Skip to content

Commit dee5015

Browse files
committed
refactor(getOrderedToolbarItems): moved to utils file
1 parent 32f238f commit dee5015

File tree

4 files changed

+50
-73
lines changed

4 files changed

+50
-73
lines changed

src/lib/edra/headless/toolbar.svelte

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import EdraToolBarIcon from './components/EdraToolBarIcon.svelte';
55
import SearchAndReplace from './components/SearchAndReplace.svelte';
66
import type { Snippet } from 'svelte';
7+
import { getOrderedToolbarItems } from '../utils.js';
78
89
interface Props {
910
class?: string;
@@ -16,44 +17,11 @@
1617
1718
// Special components that are handled separately
1819
const specialComponents = ['fontSize', 'quickColor', 'searchAndReplace'];
19-
const toolbarItems = getOrderedToolbarItems() as Array<{ type: string; command?: any }>;
20+
const toolbarItems = getOrderedToolbarItems(allowedCommands, commands, specialComponents) as Array<{ type: string; command?: any }>;
2021
let showSearchAndReplace = $state(false);
2122
const colorCommands = commands.colors.commands;
2223
const fontCommands = commands.fonts.commands;
2324
24-
// Function to get ordered toolbar items based on allowedCommands
25-
function getOrderedToolbarItems() {
26-
if (!allowedCommands?.length) {
27-
return [
28-
...Object.values(commands).flatMap((group) =>
29-
group.commands.map((cmd) => ({ type: 'command', command: cmd }))
30-
),
31-
...specialComponents.map((comp) => ({ type: comp }))
32-
];
33-
}
34-
35-
return allowedCommands
36-
.map((cmdName) => {
37-
if (specialComponents.includes(cmdName)) {
38-
return { type: cmdName };
39-
}
40-
// Check if it's a group
41-
if (commands[cmdName]) {
42-
return commands[cmdName].commands.map((cmd) => ({
43-
type: 'command',
44-
command: cmd
45-
}));
46-
}
47-
// Find individual command
48-
const command = Object.values(commands)
49-
.flatMap((group) => group.commands)
50-
.find((cmd) => cmd.name === cmdName);
51-
52-
return command ? { type: 'command', command } : null;
53-
})
54-
.flat()
55-
.filter(Boolean);
56-
}
5725
</script>
5826

5927
<div class={`edra-toolbar ${className}`}>

src/lib/edra/shadcn/components/QuickColor.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@
2626
2727
const currentColor = $derived.by(() => editor.getAttributes('textStyle').color);
2828
const currentHighlight = $derived.by(() => editor.getAttributes('highlight').color);
29-
let isOpen = $state(false);
3029
</script>
3130

32-
<Popover.Root bind:open={isOpen}>
31+
<Popover.Root>
3332
<Popover.Trigger>
3433
<EdraToolTip content="Quick Colors">
3534
<Button
@@ -60,7 +59,6 @@
6059
style={`color: ${color.value}; background-color: ${color.value}50; border-color: ${color.value};`}
6160
title={color.label}
6261
onclick={() => {
63-
isOpen = false;
6462
if (color.value === '' || color.label === 'Default')
6563
editor.chain().focus().unsetColor().run();
6664
else
@@ -89,7 +87,6 @@
8987
style={`background-color: ${color.value}50; border-color: ${color.value};`}
9088
title={color.label}
9189
onclick={() => {
92-
isOpen = false;
9390
if (color.value === '' || color.label === 'Default')
9491
editor.chain().focus().unsetHighlight().run();
9592
else editor.chain().focus().toggleHighlight({ color: color.value }).run();

src/lib/edra/shadcn/toolbar.svelte

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import SearchAndReplace from './components/SearchAndReplace.svelte';
88
import type { Snippet } from 'svelte';
99
import { cn } from '$lib/utils.js';
10+
import { getOrderedToolbarItems } from '../utils.js';
1011
1112
interface Props {
1213
class?: string;
@@ -19,41 +20,7 @@
1920
2021
// Special components that are handled separately
2122
const specialComponents = ['fontSize', 'quickColor', 'searchAndReplace'];
22-
const toolbarItems = getOrderedToolbarItems() as Array<{ type: string; command?: any }>;
23-
24-
// Function to get ordered toolbar items based on allowedCommands
25-
function getOrderedToolbarItems() {
26-
if (!allowedCommands?.length) {
27-
return [
28-
...Object.values(commands).flatMap((group) =>
29-
group.commands.map((cmd) => ({ type: 'command', command: cmd }))
30-
),
31-
...specialComponents.map((comp) => ({ type: comp }))
32-
];
33-
}
34-
35-
return allowedCommands
36-
.map((cmdName) => {
37-
if (specialComponents.includes(cmdName)) {
38-
return { type: cmdName };
39-
}
40-
// Check if it's a group
41-
if (commands[cmdName]) {
42-
return commands[cmdName].commands.map((cmd) => ({
43-
type: 'command',
44-
command: cmd
45-
}));
46-
}
47-
// Find individual command
48-
const command = Object.values(commands)
49-
.flatMap((group) => group.commands)
50-
.find((cmd) => cmd.name === cmdName);
51-
52-
return command ? { type: 'command', command } : null;
53-
})
54-
.flat()
55-
.filter(Boolean);
56-
}
23+
const toolbarItems = getOrderedToolbarItems(allowedCommands, commands, specialComponents) as Array<{ type: string; command?: any }>;
5724
</script>
5825

5926
<div

src/lib/edra/utils.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,51 @@ export function focusEditor(editor: Editor | undefined, event?: MouseEvent | Key
130130
}
131131
}
132132

133+
/**
134+
* Returns an ordered array of toolbar items based on allowed commands.
135+
*
136+
* @param allowedCommands - An optional array of allowed command names.
137+
* @param commands - An object with command groups. Each group should have a `commands` array.
138+
* @param specialComponents - An array of command names that are handled as special components.
139+
* @returns An array of toolbar items.
140+
*/
141+
export function getOrderedToolbarItems(
142+
allowedCommands: string[] | undefined,
143+
commands: Record<string, { commands: any[] }>,
144+
specialComponents: string[]
145+
): Array<{ type: string; command?: any }> {
146+
if (!allowedCommands?.length) {
147+
return [
148+
...Object.values(commands).flatMap((group) =>
149+
group.commands.map((cmd) => ({ type: 'command', command: cmd }))
150+
),
151+
...specialComponents.map((comp) => ({ type: comp }))
152+
];
153+
}
154+
155+
return allowedCommands
156+
.map((cmdName) => {
157+
if (specialComponents.includes(cmdName)) {
158+
return { type: cmdName };
159+
}
160+
// Check if it's a group
161+
if (commands[cmdName]) {
162+
return commands[cmdName].commands.map((cmd) => ({
163+
type: 'command',
164+
command: cmd
165+
}));
166+
}
167+
// Find individual command
168+
const command = Object.values(commands)
169+
.flatMap((group) => group.commands)
170+
.find((cmd) => cmd.name === cmdName);
171+
172+
return command ? { type: 'command', command } : null;
173+
})
174+
.flat()
175+
.filter(Boolean) as Array<{ type: string; command?: any }>;
176+
}
177+
133178
/**
134179
* Props for Edra's editor component
135180
*/

0 commit comments

Comments
 (0)