Skip to content

Commit f9d0a34

Browse files
committed
feat(core): add custom configuration to database options (#7933)
Upstreams: toeverything/blocksuite#8022 * add custom configuration to database options * add `Copy link to block` button to database options <img width="518" alt="Screenshot 2024-09-03 at 08 57 59" src="https://github.com/user-attachments/assets/a421cd82-abd4-456e-af17-c4db6c4ff3ae">
1 parent c3ae219 commit f9d0a34

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { notify } from '@affine/component';
2+
import {
3+
generateUrl,
4+
type UseSharingUrl,
5+
} from '@affine/core/hooks/affine/use-share-url';
6+
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
7+
import { EditorService } from '@affine/core/modules/editor';
8+
import { I18n } from '@affine/i18n';
9+
import type { DatabaseBlockModel, MenuOptions } from '@blocksuite/blocks';
10+
import { LinkIcon } from '@blocksuite/icons/lit';
11+
import { type FrameworkProvider } from '@toeverything/infra';
12+
import type { TemplateResult } from 'lit';
13+
14+
export function createDatabaseOptionsConfig(framework: FrameworkProvider) {
15+
return {
16+
configure: (model: DatabaseBlockModel, options: MenuOptions) => {
17+
const items = options.items;
18+
19+
const copyIndex = items.findIndex(
20+
item => item.type === 'action' && item.name === 'Copy'
21+
);
22+
23+
items.splice(
24+
copyIndex + 1,
25+
0,
26+
createCopyLinkToBlockMenuItem(framework, model)
27+
);
28+
29+
return options;
30+
},
31+
};
32+
}
33+
34+
function createCopyLinkToBlockMenuItem(
35+
framework: FrameworkProvider,
36+
model: DatabaseBlockModel
37+
): {
38+
type: 'action';
39+
name: string;
40+
icon?: TemplateResult<1>;
41+
hide?: () => boolean;
42+
select: () => void;
43+
} {
44+
return {
45+
type: 'action',
46+
name: 'Copy link to block',
47+
icon: LinkIcon({ width: '20', height: '20' }),
48+
hide: () => {
49+
const { editor } = framework.get(EditorService);
50+
const mode = editor.mode$.value;
51+
return mode === 'edgeless';
52+
},
53+
select: () => {
54+
const baseUrl = getAffineCloudBaseUrl();
55+
if (!baseUrl) return;
56+
57+
const { editor } = framework.get(EditorService);
58+
const mode = editor.mode$.value;
59+
60+
if (mode === 'edgeless') return;
61+
62+
const pageId = editor.doc.id;
63+
const workspaceId = editor.doc.workspace.id;
64+
const options: UseSharingUrl = {
65+
workspaceId,
66+
pageId,
67+
shareMode: mode,
68+
blockIds: [model.id],
69+
};
70+
71+
const str = generateUrl(options);
72+
if (!str) return;
73+
74+
navigator.clipboard
75+
.writeText(str)
76+
.then(() => {
77+
notify.success({
78+
title: I18n['Copied link to clipboard'](),
79+
});
80+
})
81+
.catch(console.error);
82+
},
83+
};
84+
}

packages/frontend/core/src/components/blocksuite/block-suite-editor/specs/custom/root-block.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '@blocksuite/blocks';
2020
import { type FrameworkProvider } from '@toeverything/infra';
2121

22+
import { createDatabaseOptionsConfig } from './database-block';
2223
import { createLinkedWidgetConfig } from './widgets/linked';
2324
import { createToolbarMoreMenuConfig } from './widgets/toolbar';
2425

@@ -72,6 +73,7 @@ export function createPageRootBlockSpec(
7273
linkedWidget: createLinkedWidgetConfig(framework),
7374
editorSetting: editorSettingService.editorSetting.settingSignal,
7475
toolbarMoreMenu: createToolbarMoreMenuConfig(framework),
76+
databaseOptions: createDatabaseOptionsConfig(framework),
7577
}),
7678
];
7779
}
@@ -95,6 +97,7 @@ export function createEdgelessRootBlockSpec(
9597
linkedWidget: createLinkedWidgetConfig(framework),
9698
editorSetting: editorSettingService.editorSetting.settingSignal,
9799
toolbarMoreMenu: createToolbarMoreMenuConfig(framework),
100+
databaseOptions: createDatabaseOptionsConfig(framework),
98101
}),
99102
];
100103
}

0 commit comments

Comments
 (0)