-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): add toolbar registry extension
- Loading branch information
Showing
14 changed files
with
323 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import type { | ||
ToolbarActionGroup, | ||
ToolbarModuleConfig, | ||
} from '@blocksuite/affine-shared/services'; | ||
|
||
export const BuiltinToolbarConfig = { | ||
actions: [ | ||
{ | ||
id: 'rename', | ||
tooltip: 'Rename', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'switch-view', | ||
actions: [ | ||
{ | ||
id: 'card-view', | ||
label: 'Card view', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'embed-view', | ||
label: 'Embed view', | ||
run(_cx) {}, | ||
}, | ||
], | ||
content(_cx) { | ||
this.actions; | ||
return null; | ||
}, | ||
} satisfies ToolbarActionGroup, | ||
{ | ||
id: 'download', | ||
tooltip: 'Download', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'caption', | ||
tooltip: 'Caption', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'clipboard', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'copy', | ||
label: 'Copy', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'duplicate', | ||
label: 'Duplicate', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'refresh', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'reload', | ||
label: 'Reload', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'delete', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'delete', | ||
label: 'Delete', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
], | ||
} as const satisfies ToolbarModuleConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { ToolbarModuleConfig } from '@blocksuite/affine-shared/services'; | ||
|
||
export const BuiltinToolbarConfig = { | ||
actions: [ | ||
{ | ||
id: 'download', | ||
tooltip: 'Download', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'caption', | ||
tooltip: 'Caption', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'clipboard', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'copy', | ||
label: 'Copy', | ||
run(_cx) {}, | ||
}, | ||
{ | ||
id: 'duplicate', | ||
label: 'Duplicate', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'conversions', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'turn-into-card-view', | ||
label: 'Turn into card view', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'delete', | ||
placement: 'more', | ||
actions: [ | ||
{ | ||
id: 'delete', | ||
label: 'Delete', | ||
run(_cx) {}, | ||
}, | ||
], | ||
}, | ||
], | ||
} as const satisfies ToolbarModuleConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
blocksuite/affine/shared/src/services/toolbar-service/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './module'; | ||
export * from './registry'; | ||
// export type { ToolbarAction, ToolbarContext, ToolbarModuleConfig } from './types'; | ||
export * from './types'; | ||
export * from './utils'; |
9 changes: 9 additions & 0 deletions
9
blocksuite/affine/shared/src/services/toolbar-service/module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type { BlockFlavourIdentifier } from '@blocksuite/block-std'; | ||
|
||
import type { ToolbarModuleConfig } from './types'; | ||
|
||
export type ToolbarModule = { | ||
readonly id: ReturnType<typeof BlockFlavourIdentifier>; | ||
|
||
readonly config: ToolbarModuleConfig; | ||
}; |
59 changes: 59 additions & 0 deletions
59
blocksuite/affine/shared/src/services/toolbar-service/registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
type BlockStdScope, | ||
LifeCycleWatcher, | ||
StdIdentifier, | ||
} from '@blocksuite/block-std'; | ||
import { | ||
type Container, | ||
createIdentifier, | ||
createScope, | ||
} from '@blocksuite/global/di'; | ||
import type { ExtensionType } from '@blocksuite/store'; | ||
|
||
import type { ToolbarModule } from './module'; | ||
|
||
export const ToolbarModuleIdentifier = createIdentifier<ToolbarModule>( | ||
'AffineToolbarModuleIdentifier' | ||
); | ||
|
||
export const ToolbarModulesIdentifier = createIdentifier< | ||
Map<string, ToolbarModule> | ||
>('AffineToolbarModulesIdentifier'); | ||
|
||
export const ToolbarRegistryScope = createScope('AffineToolbarRegistryScope'); | ||
|
||
export const ToolbarRegistryIdentifier = | ||
createIdentifier<ToolbarRegistryExtension>('AffineToolbarRegistryIdentifier'); | ||
|
||
export function ToolbarModuleExtension(module: ToolbarModule): ExtensionType { | ||
return { | ||
setup: di => { | ||
di.scope(ToolbarRegistryScope).addImpl( | ||
ToolbarModuleIdentifier(module.id.variant), | ||
module | ||
); | ||
}, | ||
}; | ||
} | ||
|
||
export class ToolbarRegistryExtension extends LifeCycleWatcher { | ||
constructor( | ||
std: BlockStdScope, | ||
readonly modules: Map<string, ToolbarModule> | ||
) { | ||
super(std); | ||
} | ||
|
||
static override readonly key = 'toolbar-registry'; | ||
|
||
static override setup(di: Container) { | ||
di.scope(ToolbarRegistryScope) | ||
.addImpl(ToolbarModulesIdentifier, provider => | ||
provider.getAll(ToolbarModuleIdentifier) | ||
) | ||
.addImpl(ToolbarRegistryIdentifier, this, [ | ||
StdIdentifier, | ||
ToolbarModulesIdentifier, | ||
]); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
blocksuite/affine/shared/src/services/toolbar-service/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { BlockStdScope } from '@blocksuite/block-std'; | ||
import type { TemplateResult } from 'lit'; | ||
|
||
type ActionBase = { | ||
id: string; | ||
score?: number; | ||
when?: (cx: ToolbarContext) => boolean; | ||
placement?: 'start' | 'end' | 'more'; | ||
}; | ||
|
||
export type ToolbarAction = ActionBase & { | ||
label?: string; | ||
icon?: TemplateResult; | ||
tooltip?: string; | ||
content?: (cx: ToolbarContext) => TemplateResult | null; | ||
run: (cx: ToolbarContext) => void; | ||
}; | ||
|
||
// Generates an action at runtime | ||
export type ToolbarActionGenerator = ActionBase & { | ||
generate: (cx: ToolbarContext) => ToolbarAction; | ||
}; | ||
|
||
export type ToolbarActionGroup = ActionBase & { | ||
actions: ToolbarAction[]; | ||
content?: (cx: ToolbarContext) => TemplateResult | null; | ||
}; | ||
|
||
// Generates an action group at runtime | ||
export type ToolbarActionGroupGenerator = ActionBase & { | ||
generate: (cx: ToolbarContext) => ToolbarActionGroup; | ||
}; | ||
|
||
export type ToolbarActions< | ||
T extends ActionBase = | ||
| ToolbarAction | ||
| ToolbarActionGenerator | ||
| ToolbarActionGroup | ||
| ToolbarActionGroupGenerator, | ||
> = T[]; | ||
|
||
abstract class ToolbarContextBase { | ||
constructor(readonly std: BlockStdScope) {} | ||
|
||
get isReadonly() { | ||
return this.std.store.readonly; | ||
} | ||
} | ||
|
||
export class ToolbarContext extends ToolbarContextBase {} | ||
|
||
export interface ToolbarModuleConfig { | ||
actions: ToolbarActions; | ||
} |
7 changes: 7 additions & 0 deletions
7
blocksuite/affine/shared/src/services/toolbar-service/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function generateActionIdWith( | ||
flavour: string, | ||
name: string, | ||
prefix = 'com.affine.toolbar.internal' | ||
) { | ||
return `${prefix}.${flavour}.${name}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,18 @@ | ||
import { | ||
ToolbarRegistryIdentifier, | ||
ToolbarRegistryScope, | ||
} from '@blocksuite/affine-shared/services'; | ||
import { WidgetComponent } from '@blocksuite/block-std'; | ||
|
||
export const AFFINE_TOOLBAR_WIDGET = 'affine-toolbar-widget'; | ||
|
||
export class AffineToolbarWidget extends WidgetComponent {} | ||
export class AffineToolbarWidget extends WidgetComponent { | ||
override connectedCallback() { | ||
super.connectedCallback(); | ||
|
||
const toolbarRegistry = this.std.container | ||
.provider(ToolbarRegistryScope, this.std.provider) | ||
.get(ToolbarRegistryIdentifier); | ||
console.log(toolbarRegistry); | ||
} | ||
} |
Oops, something went wrong.