refactor(editor): improve query methods in toolbar context (#10714)

This commit is contained in:
fundon
2025-03-19 00:52:22 +00:00
parent 02d707feab
commit 3cce147c60
14 changed files with 317 additions and 359 deletions

View File

@@ -313,33 +313,34 @@ function createToolbarMoreMenuConfigV2(baseUrl?: string) {
actions: [
{
id: 'block-meta-display',
when: cx => {
const featureFlag = cx.std.get(FeatureFlagService);
when: ctx => {
const featureFlag = ctx.std.get(FeatureFlagService);
const isEnabled = featureFlag.getFlag('enable_block_meta');
if (!isEnabled) return false;
// only display when one block is selected by block selection
const { selection, getCurrentBlockBy } = cx;
const hasBlockSelection =
selection.filter(BlockSelection).length === 1;
ctx.selection.filter(BlockSelection).length === 1;
if (!hasBlockSelection) return false;
const block = getCurrentBlockBy.call(cx, BlockSelection);
if (!block) return false;
const model = ctx.getCurrentModelBy(BlockSelection);
if (!model) return false;
const createdAt = 'meta:createdAt';
const createdBy = 'meta:createdBy';
return (
createdAt in block.model.props &&
block.model.props[createdAt] !== undefined &&
createdBy in block.model.props &&
block.model.props[createdBy] !== undefined &&
typeof block.model.props[createdBy] === 'string' &&
typeof block.model.props[createdAt] === 'number'
'props' in model &&
createdAt in model.props &&
model.props[createdAt] !== undefined &&
createdBy in model.props &&
model.props[createdBy] !== undefined &&
typeof model.props[createdBy] === 'string' &&
typeof model.props[createdAt] === 'number'
);
},
content: cx => {
const model = cx.getCurrentModelBy(BlockSelection);
content: ctx => {
const model = ctx.getCurrentModelBy(BlockSelection);
if (!model) return null;
if (!('props' in model)) return null;
const createdAt = 'meta:createdAt';
if (!(createdAt in model.props)) return null;
const createdBy = 'meta:createdBy';
@@ -347,7 +348,7 @@ function createToolbarMoreMenuConfigV2(baseUrl?: string) {
const createdByUserId = model.props[createdBy] as string;
const createdAtTimestamp = model.props[createdAt] as number;
const date = new Date(createdAtTimestamp);
const userProvider = cx.std.get(UserProvider);
const userProvider = ctx.std.get(UserProvider);
userProvider.revalidateUserInfo(createdByUserId);
const userSignal = userProvider.userInfo$(createdByUserId);
const isLoadingSignal = userProvider.isLoading$(createdByUserId);
@@ -415,7 +416,7 @@ function createExternalLinkableToolbarConfig(
tooltip: 'Copy link',
icon: CopyIcon(),
run(ctx) {
const model = ctx.getCurrentBlockComponentBy(klass)?.model;
const model = ctx.getCurrentBlockByType(klass)?.model;
if (!model) return;
const { url } = model.props;
@@ -440,12 +441,12 @@ function createExternalLinkableToolbarConfig(
tooltip: 'Edit',
icon: EditIcon(),
run(ctx) {
const component = ctx.getCurrentBlockComponentBy(klass);
if (!component) return;
const block = ctx.getCurrentBlockByType(klass);
if (!block) return;
ctx.hide();
const model = component.model;
const model = block.model;
const abortController = new AbortController();
abortController.signal.onabort = () => ctx.show();
@@ -457,7 +458,7 @@ function createExternalLinkableToolbarConfig(
undefined,
(_std, _component, props) => {
ctx.store.updateBlock(model, props);
component.requestUpdate();
block.requestUpdate();
},
abortController
);
@@ -514,8 +515,8 @@ function createOpenDocActionGroup(
id: 'A.open-doc',
actions: openDocActions,
content(ctx) {
const component = ctx.getCurrentBlockComponentBy(klass);
if (!component) return null;
const block = ctx.getCurrentBlockByType(klass);
if (!block) return null;
const actions = this.actions
.map<ToolbarAction>(action => {
@@ -528,15 +529,14 @@ function createOpenDocActionGroup(
return {
...action,
disabled: shouldOpenInActiveView
? component.model.props.pageId === ctx.store.id
? block.model.props.pageId === ctx.store.id
: false,
when:
allowed &&
(shouldOpenInCenterPeek ? isPeekable(component) : true),
allowed && (shouldOpenInCenterPeek ? isPeekable(block) : true),
run: shouldOpenInCenterPeek
? (_ctx: ToolbarContext) => peek(component)
? (_ctx: ToolbarContext) => peek(block)
: (_ctx: ToolbarContext) =>
component.open({
block.open({
openMode: action.id as OpenDocMode,
}),
};
@@ -589,10 +589,7 @@ const embedLinkedDocToolbarConfig = {
tooltip: 'Copy link',
icon: CopyIcon(),
run(ctx) {
const model = ctx.getCurrentModelByType(
BlockSelection,
EmbedLinkedDocModel
);
const model = ctx.getCurrentModelByType(EmbedLinkedDocModel);
if (!model) return;
const { pageId, params } = model.props;
@@ -621,21 +618,21 @@ const embedLinkedDocToolbarConfig = {
tooltip: 'Edit',
icon: EditIcon(),
run(ctx) {
const component = ctx.getCurrentBlockComponentBy(
const block = ctx.getCurrentBlockByType(
EmbedLinkedDocBlockComponent
);
if (!component) return;
if (!block) return;
ctx.hide();
const model = component.model;
const model = block.model;
const doc = ctx.workspace.getDoc(model.props.pageId);
const abortController = new AbortController();
abortController.signal.onabort = () => ctx.show();
toggleEmbedCardEditModal(
ctx.host,
component.model,
model,
'card',
doc
? {
@@ -644,12 +641,12 @@ const embedLinkedDocToolbarConfig = {
}
: undefined,
std => {
component.refreshData();
block.refreshData();
notifyLinkedDocClearedAliases(std);
},
(_std, _component, props) => {
ctx.store.updateBlock(model, props);
component.requestUpdate();
block.requestUpdate();
},
abortController
);
@@ -681,10 +678,7 @@ const embedSyncedDocToolbarConfig = {
tooltip: 'Copy link',
icon: CopyIcon(),
run(ctx) {
const model = ctx.getCurrentModelByType(
BlockSelection,
EmbedSyncedDocModel
);
const model = ctx.getCurrentModelByType(EmbedSyncedDocModel);
if (!model) return;
const { pageId, params } = model.props;
@@ -713,14 +707,14 @@ const embedSyncedDocToolbarConfig = {
tooltip: 'Edit',
icon: EditIcon(),
run(ctx) {
const component = ctx.getCurrentBlockComponentBy(
const block = ctx.getCurrentBlockByType(
EmbedSyncedDocBlockComponent
);
if (!component) return;
if (!block) return;
ctx.hide();
const model = component.model;
const model = block.model;
const doc = ctx.workspace.getDoc(model.props.pageId);
const abortController = new AbortController();
abortController.signal.onabort = () => ctx.show();
@@ -732,7 +726,7 @@ const embedSyncedDocToolbarConfig = {
doc ? { title: doc.meta?.title } : undefined,
undefined,
(std, _component, props) => {
component.convertToCard(props);
block.convertToCard(props);
notifyLinkedDocSwitchedToCard(std);
},
@@ -761,8 +755,7 @@ const inlineReferenceToolbarConfig = {
id: 'A.open-doc',
actions: openDocActions,
content(ctx) {
const registry = ctx.toolbarRegistry;
const target = registry.message$.peek()?.element;
const target = ctx.message$.peek()?.element;
if (!(target instanceof AffineReference)) return null;
const actions = this.actions
@@ -917,7 +910,7 @@ const embedIframeToolbarConfig = {
tooltip: 'Copy link',
icon: CopyIcon(),
run(ctx) {
const model = ctx.getCurrentBlockComponentBy(
const model = ctx.getCurrentBlockByType(
EmbedIframeBlockComponent
)?.model;
if (!model) return;
@@ -928,9 +921,6 @@ const embedIframeToolbarConfig = {
toast(ctx.host, 'Copied link to clipboard');
ctx.track('CopiedLink', {
segment: 'doc',
page: 'doc editor',
module: 'toolbar',
category: matchModels(model, [BookmarkBlockModel])
? 'bookmark'
: 'link',
@@ -944,7 +934,7 @@ const embedIframeToolbarConfig = {
tooltip: 'Edit',
icon: EditIcon(),
run(ctx) {
const component = ctx.getCurrentBlockComponentBy(
const component = ctx.getCurrentBlockByType(
EmbedIframeBlockComponent
);
if (!component) return;
@@ -969,9 +959,6 @@ const embedIframeToolbarConfig = {
);
ctx.track('OpenedAliasPopup', {
segment: 'doc',
page: 'doc editor',
module: 'toolbar',
category: matchModels(model, [BookmarkBlockModel])
? 'bookmark'
: 'link',