gaoluyang
2026-06-24 c0cb161bb52ce0fbdce5c66ec391d107c75e2452
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type { Editor } from '@tiptap/vue-3';
 
import type { ShallowRef } from 'vue';
 
import type { ToolbarAction, ToolbarMenuItem } from './types';
 
import { cn } from '..\..\..\..\@core\base\shared\src\utils';
 
interface UseTiptapToolbarOptions {
  editable: () => boolean;
  editor: Readonly<ShallowRef<Editor | undefined>>;
}
 
export function useTiptapToolbar(options: UseTiptapToolbarOptions) {
  const getEditor = () => options.editor.value;
 
  function getActionIndicatorColor(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !action.indicatorColor) {
      return undefined;
    }
 
    return action.indicatorColor(currentEditor);
  }
 
  function getPaletteCurrentColor(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !action.palette?.currentColor) {
      return undefined;
    }
 
    return action.palette.currentColor(currentEditor);
  }
 
  function canRunAction(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !options.editable()) {
      return false;
    }
 
    return action.can ? action.can(currentEditor) : true;
  }
 
  function canRunMenuItem(item: ToolbarMenuItem) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !options.editable()) {
      return false;
    }
 
    return item.can ? item.can(currentEditor) : true;
  }
 
  function isActionActive(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor) {
      return false;
    }
 
    if (action.isActive) {
      return action.isActive(currentEditor);
    }
 
    if (!action.active) {
      return false;
    }
 
    return currentEditor.isActive(action.active.name, action.active.attrs);
  }
 
  function isMenuItemActive(item: ToolbarMenuItem, currentEditor?: Editor) {
    const targetEditor = currentEditor ?? getEditor();
 
    if (!targetEditor || !item.isActive) {
      return false;
    }
 
    return item.isActive(targetEditor);
  }
 
  function runAction(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !options.editable()) {
      return;
    }
 
    if (action.menu || action.palette) {
      return;
    }
 
    action.action(currentEditor);
  }
 
  function runMenuItem(item: ToolbarMenuItem) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !options.editable()) {
      return;
    }
 
    item.action(currentEditor);
  }
 
  function applyPaletteColor(action: ToolbarAction, color: string) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !action.palette) {
      return;
    }
 
    action.palette.apply(currentEditor, color);
  }
 
  function clearPaletteColor(action: ToolbarAction) {
    const currentEditor = getEditor();
 
    if (!currentEditor || !action.palette?.clear) {
      return;
    }
 
    action.palette.clear(currentEditor);
  }
 
  function getToolbarButtonClass(action: ToolbarAction) {
    return cn(
      'text-muted-foreground relative rounded-[10px] border border-transparent bg-transparent shadow-none',
      'transition-[transform,color,background-color,border-color,box-shadow] duration-200 ease-out',
      'enabled:hover:border-border enabled:hover:-translate-y-px disabled:opacity-45',
      'enabled:hover:bg-accent enabled:hover:text-foreground',
      isActionActive(action) &&
        'bg-accent border-primary/30 shadow-primary text-primary',
    );
  }
 
  function getPaletteSwatchClass(action: ToolbarAction, color: string) {
    return cn(
      'border-border inline-flex size-8 items-center justify-center rounded-full border',
      'shadow-accent',
      'transition-[transform,box-shadow,border-color] duration-200 ease-out',
      'hover:-translate-y-px hover:scale-[1.04]',
      getPaletteCurrentColor(action) === color &&
        'border-primary shadow-primary',
    );
  }
 
  function getMenuItemClass(item: ToolbarMenuItem) {
    return cn(
      'flex items-center gap-2 rounded-lg p-2 text-left text-sm transition-colors',
      'disabled:cursor-not-allowed disabled:opacity-45',
      isMenuItemActive(item)
        ? 'bg-accent text-foreground'
        : 'hover:bg-accent hover:text-foreground text-muted-foreground',
    );
  }
 
  return {
    applyPaletteColor,
    canRunAction,
    canRunMenuItem,
    clearPaletteColor,
    getActionIndicatorColor,
    getMenuItemClass,
    getPaletteCurrentColor,
    getPaletteSwatchClass,
    getToolbarButtonClass,
    isActionActive,
    isMenuItemActive,
    runAction,
    runMenuItem,
  };
}