forked from Tsuzat/Edra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbar.svelte
More file actions
108 lines (101 loc) · 3.37 KB
/
toolbar.svelte
File metadata and controls
108 lines (101 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<script lang="ts">
import type { Editor } from '@tiptap/core';
import { commands } from '../commands/commands.js';
import EdraToolBarIcon from './components/EdraToolBarIcon.svelte';
import SearchAndReplace from './components/SearchAndReplace.svelte';
import type { Snippet } from 'svelte';
interface Props {
class?: string;
editor: Editor;
allowedCommands?: string[];
children?: Snippet;
}
const { class: className = '', editor, allowedCommands, children }: Props = $props();
// Special components that are handled separately
const specialComponents = ['fontSize', 'quickColor', 'searchAndReplace'];
const toolbarItems = getOrderedToolbarItems() as Array<{ type: string; command?: any }>;
let showSearchAndReplace = $state(false);
const colorCommands = commands.colors.commands;
const fontCommands = commands.fonts.commands;
// Function to get ordered toolbar items based on allowedCommands
function getOrderedToolbarItems() {
if (!allowedCommands?.length) {
return [
...Object.values(commands).flatMap((group) =>
group.commands.map((cmd) => ({ type: 'command', command: cmd }))
),
...specialComponents.map((comp) => ({ type: comp }))
];
}
return allowedCommands
.map((cmdName) => {
if (specialComponents.includes(cmdName)) {
return { type: cmdName };
}
// Check if it's a group
if (commands[cmdName]) {
return commands[cmdName].commands.map((cmd) => ({
type: 'command',
command: cmd
}));
}
// Find individual command
const command = Object.values(commands)
.flatMap((group) => group.commands)
.find((cmd) => cmd.name === cmdName);
return command ? { type: 'command', command } : null;
})
.flat()
.filter(Boolean);
}
</script>
<div class={`edra-toolbar ${className}`}>
{#if !showSearchAndReplace}
{#each toolbarItems as item}
{#if item.type === 'command'}
<EdraToolBarIcon command={item.command} {editor} />
{:else if item.type === 'fontSize'}
<EdraToolBarIcon command={fontCommands[0]} {editor} />
<span>{editor.getAttributes('textStyle').fontSize ?? '16px'}</span>
<EdraToolBarIcon command={fontCommands[1]} {editor} />
{:else if item.type === 'quickColor'}
<EdraToolBarIcon
command={colorCommands[0]}
{editor}
style={`color: ${editor.getAttributes('textStyle').color};`}
onclick={() => {
const color = editor.getAttributes('textStyle').color;
const hasColor = editor.isActive('textStyle', { color });
if (hasColor) {
editor.chain().focus().unsetColor().run();
} else {
const color = prompt('Enter the color of the text:');
if (color !== null) {
editor.chain().focus().setColor(color).run();
}
}
}}
/>
{:else if item.type === 'searchAndReplace'}
<EdraToolBarIcon
command={colorCommands[1]}
{editor}
style={`background-color: ${editor.getAttributes('highlight').color};`}
onclick={() => {
const hasHightlight = editor.isActive('highlight');
if (hasHightlight) {
editor.chain().focus().unsetHighlight().run();
} else {
const color = prompt('Enter the color of the highlight:');
if (color !== null) {
editor.chain().focus().setHighlight({ color }).run();
}
}
}}
/>
{/if}
{/each}
{/if}
<SearchAndReplace {editor} bind:show={showSearchAndReplace} />
{@render children?.()}
</div>