|
2 | 2 | import type { Editor } from '@tiptap/core'; |
3 | 3 | import { commands } from '../commands/commands.js'; |
4 | 4 | import EdraToolBarIcon from './components/EdraToolBarIcon.svelte'; |
5 | | - import SearchAndReplace from './components/SearchAndReplace.svelte'; |
| 5 | + import SearcnAndReplace from './components/SearcnAndReplace.svelte'; |
6 | 6 | import type { Snippet } from 'svelte'; |
7 | | - import { getOrderedToolbarItems } from '../utils.js'; |
8 | 7 |
|
9 | 8 | interface Props { |
10 | 9 | class?: string; |
11 | 10 | editor: Editor; |
12 | | - allowedCommands?: string[]; |
13 | | - children?: Snippet; |
| 11 | + children?: Snippet<[]>; |
14 | 12 | } |
15 | 13 |
|
16 | | - const { class: className = '', editor, allowedCommands, children }: Props = $props(); |
| 14 | + const { class: className = '', editor, children }: Props = $props(); |
17 | 15 |
|
18 | 16 | // Special components that are handled separately |
19 | 17 | const specialComponents = ['fontSize', 'quickColor', 'searchAndReplace']; |
20 | 18 | const toolbarItems = getOrderedToolbarItems(allowedCommands, commands, specialComponents) as Array<{ type: string; command?: any }>; |
21 | 19 | let showSearchAndReplace = $state(false); |
22 | 20 | const colorCommands = commands.colors.commands; |
23 | 21 | const fontCommands = commands.fonts.commands; |
24 | | -
|
| 22 | + const excludedCommands = ['colors', 'fonts']; |
25 | 23 | </script> |
26 | 24 |
|
27 | 25 | <div class={`edra-toolbar ${className}`}> |
28 | | - {#if !showSearchAndReplace} |
29 | | - {#each toolbarItems as item} |
30 | | - {#if item.type === 'command'} |
31 | | - <EdraToolBarIcon command={item.command} {editor} /> |
32 | | - {:else if item.type === 'fontSize'} |
33 | | - <EdraToolBarIcon command={fontCommands[0]} {editor} /> |
34 | | - <span>{editor.getAttributes('textStyle').fontSize ?? '16px'}</span> |
35 | | - <EdraToolBarIcon command={fontCommands[1]} {editor} /> |
36 | | - {:else if item.type === 'quickColor'} |
37 | | - <EdraToolBarIcon |
38 | | - command={colorCommands[0]} |
39 | | - {editor} |
40 | | - style={`color: ${editor.getAttributes('textStyle').color};`} |
41 | | - onclick={() => { |
42 | | - const color = editor.getAttributes('textStyle').color; |
43 | | - const hasColor = editor.isActive('textStyle', { color }); |
44 | | - if (hasColor) { |
45 | | - editor.chain().focus().unsetColor().run(); |
46 | | - } else { |
47 | | - const color = prompt('Enter the color of the text:'); |
48 | | - if (color !== null) { |
49 | | - editor.chain().focus().setColor(color).run(); |
50 | | - } |
| 26 | + {#if children} |
| 27 | + {@render children()} |
| 28 | + {:else} |
| 29 | + {#if !showSearchAndReplace} |
| 30 | + {#each Object.keys(commands).filter((key) => !excludedCommands.includes(key)) as keys} |
| 31 | + {@const groups = commands[keys].commands} |
| 32 | + {#each groups as command} |
| 33 | + <EdraToolBarIcon {command} {editor} /> |
| 34 | + {/each} |
| 35 | + <span class="separator"></span> |
| 36 | + {/each} |
| 37 | + |
| 38 | + <EdraToolBarIcon command={fontCommands[0]} {editor} /> |
| 39 | + <span>{editor.getAttributes('textStyle').fontSize ?? '16px'}</span> |
| 40 | + <EdraToolBarIcon command={fontCommands[1]} {editor} /> |
| 41 | + |
| 42 | + <span class="separator"></span> |
| 43 | + |
| 44 | + <EdraToolBarIcon |
| 45 | + command={colorCommands[0]} |
| 46 | + {editor} |
| 47 | + style={`color: ${editor.getAttributes('textStyle').color};`} |
| 48 | + onclick={() => { |
| 49 | + const color = editor.getAttributes('textStyle').color; |
| 50 | + const hasColor = editor.isActive('textStyle', { color }); |
| 51 | + if (hasColor) { |
| 52 | + editor.chain().focus().unsetColor().run(); |
| 53 | + } else { |
| 54 | + const color = prompt('Enter the color of the text:'); |
| 55 | + if (color !== null) { |
| 56 | + editor.chain().focus().setColor(color).run(); |
51 | 57 | } |
52 | | - }} |
53 | | - /> |
54 | | - {:else if item.type === 'searchAndReplace'} |
55 | | - <EdraToolBarIcon |
56 | | - command={colorCommands[1]} |
57 | | - {editor} |
58 | | - style={`background-color: ${editor.getAttributes('highlight').color};`} |
59 | | - onclick={() => { |
60 | | - const hasHightlight = editor.isActive('highlight'); |
61 | | - if (hasHightlight) { |
62 | | - editor.chain().focus().unsetHighlight().run(); |
63 | | - } else { |
64 | | - const color = prompt('Enter the color of the highlight:'); |
65 | | - if (color !== null) { |
66 | | - editor.chain().focus().setHighlight({ color }).run(); |
67 | | - } |
| 58 | + } |
| 59 | + }} |
| 60 | + /> |
| 61 | + <EdraToolBarIcon |
| 62 | + command={colorCommands[1]} |
| 63 | + {editor} |
| 64 | + style={`background-color: ${editor.getAttributes('highlight').color};`} |
| 65 | + onclick={() => { |
| 66 | + const hasHightlight = editor.isActive('highlight'); |
| 67 | + if (hasHightlight) { |
| 68 | + editor.chain().focus().unsetHighlight().run(); |
| 69 | + } else { |
| 70 | + const color = prompt('Enter the color of the highlight:'); |
| 71 | + if (color !== null) { |
| 72 | + editor.chain().focus().setHighlight({ color }).run(); |
68 | 73 | } |
69 | | - }} |
70 | | - /> |
71 | | - {/if} |
72 | | - {/each} |
| 74 | + } |
| 75 | + }} |
| 76 | + /> |
| 77 | + {/if} |
| 78 | + <SearcnAndReplace {editor} bind:show={showSearchAndReplace} /> |
73 | 79 | {/if} |
74 | | - <SearchAndReplace {editor} bind:show={showSearchAndReplace} /> |
75 | | - {@render children?.()} |
76 | 80 | </div> |
0 commit comments