forked from Tsuzat/Edra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble-menu.svelte
More file actions
148 lines (138 loc) · 3.99 KB
/
bubble-menu.svelte
File metadata and controls
148 lines (138 loc) · 3.99 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<script lang="ts">
import { BubbleMenu } from 'svelte-tiptap';
import { isTextSelection, type Editor } from '@tiptap/core';
import { commands } from '../../commands/commands.js';
import EdraToolBarIcon from '../components/EdraToolBarIcon.svelte';
import type { ShouldShowProps } from '../../utils.js';
import type { Snippet } from 'svelte';
interface Props {
class?: string;
editor: Editor;
children?: Snippet<[]>;
}
const { class: className = '', editor, children }: Props = $props();
const bubbleMenuCommands = [
...commands['text-formatting'].commands,
...commands.alignment.commands,
...commands.lists.commands
];
const excludeCommands = ['undo-redo', 'media', 'table', 'colors', 'fonts', 'lists'];
const colorCommands = commands.colors.commands;
const fontCommands = commands.fonts.commands;
function shouldShow(props: ShouldShowProps) {
const { view, editor } = props;
if (!view || editor.view.dragging) {
return false;
}
if (editor.isActive('link')) return false;
if (editor.isActive('codeBlock')) return false;
const {
state: {
doc,
selection,
selection: { empty, from, to }
}
} = editor;
// check if the selection is a table grip
const domAtPos = view.domAtPos(from || 0).node as HTMLElement;
const nodeDOM = view.nodeDOM(from || 0) as HTMLElement;
const node = nodeDOM || domAtPos;
if (isTableGripSelected(node)) {
return false;
}
// Sometime check for `empty` is not enough.
// Doubleclick an empty paragraph returns a node size of 2.
// So we check also for an empty text size.
const isEmptyTextBlock = !doc.textBetween(from, to).length && isTextSelection(selection);
if (empty || isEmptyTextBlock || !editor.isEditable) {
return false;
}
return true;
}
const isTableGripSelected = (node: HTMLElement) => {
let container = node;
while (container && !['TD', 'TH'].includes(container.tagName)) {
container = container.parentElement!;
}
const gripColumn =
container && container.querySelector && container.querySelector('a.grip-column.selected');
const gripRow =
container && container.querySelector && container.querySelector('a.grip-row.selected');
if (gripColumn || gripRow) {
return true;
}
return false;
};
</script>
<BubbleMenu
{editor}
class={`bubble-menu-wrapper ${className}`}
{shouldShow}
pluginKey="bubble-menu"
updateDelay={100}
tippyOptions={{
popperOptions: {
placement: 'top-start',
modifiers: [
{
name: 'preventOverflow',
options: {
boundary: 'viewport',
padding: 8
}
},
{
name: 'flip',
options: {
fallbackPlacements: ['bottom-start', 'top-end', 'bottom-end']
}
}
]
},
maxWidth: 'calc(100vw - 16px)'
}}
>
{#if children}
{@render children()}
{:else}
{#each bubbleMenuCommands as command}
<EdraToolBarIcon {command} {editor} />
{/each}
<EdraToolBarIcon command={fontCommands[0]} {editor} />
<span>{editor.getAttributes('textStyle').fontSize ?? '16px'}</span>
<EdraToolBarIcon command={fontCommands[1]} {editor} />
<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();
}
}
}}
/>
<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}
</BubbleMenu>