@@ -5,40 +5,56 @@ import {
5
5
type ToolbarAction ,
6
6
type ToolbarActionGroup ,
7
7
type ToolbarModuleConfig ,
8
+ ToolbarModuleExtension ,
8
9
} from '@blocksuite/affine-shared/services' ;
9
10
import { getBlockProps } from '@blocksuite/affine-shared/utils' ;
11
+ import { BlockFlavourIdentifier } from '@blocksuite/block-std' ;
10
12
import {
11
13
CaptionIcon ,
12
14
CopyIcon ,
13
15
DeleteIcon ,
14
16
DuplicateIcon ,
15
17
ExpandFullIcon ,
16
18
} from '@blocksuite/icons/lit' ;
17
- import { Slice } from '@blocksuite/store' ;
19
+ import { type ExtensionType , Slice } from '@blocksuite/store' ;
18
20
import { html } from 'lit' ;
19
21
import { keyed } from 'lit/directives/keyed.js' ;
20
22
21
23
import { EmbedHtmlBlockComponent } from '../embed-html-block' ;
22
24
23
25
const trackBaseProps = {
24
- segment : 'doc' ,
25
- page : 'doc editor' ,
26
- module : 'toolbar' ,
27
26
category : 'html' ,
28
27
type : 'card view' ,
29
28
} ;
30
29
31
- export const builtinToolbarConfig = {
30
+ const openDocAction = {
31
+ id : 'a.open-doc' ,
32
+ icon : ExpandFullIcon ( ) ,
33
+ tooltip : 'Open this doc' ,
34
+ run ( ctx ) {
35
+ const block = ctx . getCurrentBlockByType ( EmbedHtmlBlockComponent ) ;
36
+ block ?. open ( ) ;
37
+ } ,
38
+ } as const satisfies ToolbarAction ;
39
+
40
+ const captionAction = {
41
+ id : 'c.caption' ,
42
+ tooltip : 'Caption' ,
43
+ icon : CaptionIcon ( ) ,
44
+ run ( ctx ) {
45
+ const block = ctx . getCurrentBlockByType ( EmbedHtmlBlockComponent ) ;
46
+ block ?. captionEditor ?. show ( ) ;
47
+
48
+ ctx . track ( 'OpenedCaptionEditor' , {
49
+ ...trackBaseProps ,
50
+ control : 'add caption' ,
51
+ } ) ;
52
+ } ,
53
+ } as const satisfies ToolbarAction ;
54
+
55
+ const builtinToolbarConfig = {
32
56
actions : [
33
- {
34
- id : 'a.open-doc' ,
35
- icon : ExpandFullIcon ( ) ,
36
- tooltip : 'Open this doc' ,
37
- run ( ctx ) {
38
- const block = ctx . getCurrentBlockByType ( EmbedHtmlBlockComponent ) ;
39
- block ?. open ( ) ;
40
- } ,
41
- } ,
57
+ openDocAction ,
42
58
{
43
59
id : 'b.style' ,
44
60
actions : [
@@ -68,6 +84,7 @@ export const builtinToolbarConfig = {
68
84
} ,
69
85
} ) ) ;
70
86
const onToggle = ( e : CustomEvent < boolean > ) => {
87
+ e . stopPropagation ( ) ;
71
88
const opened = e . detail ;
72
89
if ( ! opened ) return ;
73
90
@@ -88,20 +105,7 @@ export const builtinToolbarConfig = {
88
105
) } `;
89
106
} ,
90
107
} satisfies ToolbarActionGroup < ToolbarAction > ,
91
- {
92
- id : 'c.caption' ,
93
- tooltip : 'Caption' ,
94
- icon : CaptionIcon ( ) ,
95
- run ( ctx ) {
96
- const block = ctx . getCurrentBlockByType ( EmbedHtmlBlockComponent ) ;
97
- block ?. captionEditor ?. show ( ) ;
98
-
99
- ctx . track ( 'OpenedCaptionEditor' , {
100
- ...trackBaseProps ,
101
- control : 'add caption' ,
102
- } ) ;
103
- } ,
104
- } ,
108
+ captionAction ,
105
109
{
106
110
placement : ActionPlacement . More ,
107
111
id : 'a.clipboard' ,
@@ -156,4 +160,34 @@ export const builtinToolbarConfig = {
156
160
} ,
157
161
} ,
158
162
] ,
163
+
164
+ when : ctx => ctx . getSurfaceModelsByType ( EmbedHtmlModel ) . length === 1 ,
159
165
} as const satisfies ToolbarModuleConfig ;
166
+
167
+ const builtinSurfaceToolbarConfig = {
168
+ actions : [
169
+ openDocAction ,
170
+ {
171
+ ...captionAction ,
172
+ id : 'b.caption' ,
173
+ } ,
174
+ ] ,
175
+ } as const satisfies ToolbarModuleConfig ;
176
+
177
+ export const createBuiltinToolbarConfigExtension = (
178
+ flavour : string
179
+ ) : ExtensionType [ ] => {
180
+ const name = flavour . split ( ':' ) . pop ( ) ;
181
+
182
+ return [
183
+ ToolbarModuleExtension ( {
184
+ id : BlockFlavourIdentifier ( flavour ) ,
185
+ config : builtinToolbarConfig ,
186
+ } ) ,
187
+
188
+ ToolbarModuleExtension ( {
189
+ id : BlockFlavourIdentifier ( `affine:surface:${ name } ` ) ,
190
+ config : builtinSurfaceToolbarConfig ,
191
+ } ) ,
192
+ ] ;
193
+ } ;
0 commit comments