feat(editor): add WidgetViewExtension (#10180)

Closes: [BS-2282](https://linear.app/affine-design/issue/BS-2282/replace-widgetviewmapextension-with-widgetextension)
This commit is contained in:
Saul-Mirone
2025-02-14 11:00:01 +00:00
parent 9dc81ecb99
commit d111f8ac88
19 changed files with 324 additions and 313 deletions
@@ -1,32 +1,40 @@
import type { ExtensionType } from '@blocksuite/store';
import { WidgetViewMapIdentifier } from '../identifier.js';
import type { WidgetViewMapType } from '../spec/type.js';
import { WidgetViewIdentifier } from '../identifier.js';
import type { WidgetViewType } from '../spec/type.js';
/**
* Create a widget view map extension.
* Create a widget view extension.
*
* @param flavour The flavour of the block that the widget view map is for.
* @param widgetViewMap A map of widget names to widget view lit literal.
* @param flavour The flavour of the block that the widget view is for.
* @param id The id of the widget view.
* @param view The widget view lit literal.
*
* A widget view map is to provide a map of widgets to a block.
* For every target block, it's view will be rendered with the widget views.
* A widget view is to provide a widget view for a block.
* For every target block, it's view will be rendered with the widget view.
*
* @example
* ```ts
* import { WidgetViewMapExtension } from '@blocksuite/block-std';
* import { WidgetViewExtension } from '@blocksuite/block-std';
*
* const MyWidgetViewMapExtension = WidgetViewMapExtension('my-flavour', {
* 'my-widget': literal`my-widget-view`
* });
* const MyWidgetViewExtension = WidgetViewExtension('my-flavour', 'my-widget', literal`my-widget-view`);
*/
export function WidgetViewMapExtension(
export function WidgetViewExtension(
flavour: string,
widgetViewMap: WidgetViewMapType
id: string,
view: WidgetViewType
): ExtensionType {
return {
setup: di => {
di.addImpl(WidgetViewMapIdentifier(flavour), () => widgetViewMap);
if (flavour.includes('|') || id.includes('|')) {
console.error(`Register view failed:`);
console.error(
`flavour or id cannot include '|', flavour: ${flavour}, id: ${id}`
);
return;
}
const key = `${flavour}|${id}`;
di.addImpl(WidgetViewIdentifier(key), view);
},
};
}