-
-
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
12 changed files
with
187 additions
and
7 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
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
3 changes: 3 additions & 0 deletions
3
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,3 @@ | ||
export * from './module'; | ||
export * from './registry'; | ||
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, | ||
]); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
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,42 @@ | ||
import type { BlockStdScope } from '@blocksuite/block-std'; | ||
import type { TemplateResult } from 'lit'; | ||
|
||
type ActionBase = { | ||
id: string; | ||
label?: string; | ||
icon?: TemplateResult; | ||
score?: number; | ||
when?: (cx: ToolbarContext) => boolean; | ||
}; | ||
|
||
type Action = ActionBase & { | ||
run: (cx: ToolbarContext) => void; | ||
}; | ||
|
||
type ActionGenerator = ActionBase & { | ||
// Generates an action at runtime | ||
generate: (cx: ToolbarContext) => Pick<Action, 'run'>; | ||
}; | ||
|
||
type ActionGroup = ActionBase & { | ||
actions: Action[]; | ||
}; | ||
|
||
type ActionGroupGenerator = ActionBase & { | ||
// Generates an action group at runtime | ||
generate: (cx: ToolbarContext) => Pick<ActionGroup, 'actions'>; | ||
}; | ||
|
||
export type ToolbarAction = | ||
| Action | ||
| ActionGenerator | ||
| ActionGroup | ||
| ActionGroupGenerator; | ||
|
||
export type ToolbarContext = { | ||
std: BlockStdScope; | ||
}; | ||
|
||
export type ToolbarModuleConfig = { | ||
actions: ToolbarAction[]; | ||
}; |
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.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); | ||
} | ||
} |
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
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