Skip to content

Commit

Permalink
fix(core): link generation for selected blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 4, 2024
1 parent 7873192 commit c92aa60
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const AFFiNESharePage = (props: ShareMenuProps) => {
workspaceMetadata: { id: workspaceId },
} = props;
const editor = useService(EditorService).editor;
const currentMode = useLiveData(editor.mode$);
const shareInfoService = useService(ShareInfoService);
const serverConfig = useService(ServerConfigService).serverConfig;
useEffect(() => {
Expand Down Expand Up @@ -165,9 +166,8 @@ export const AFFiNESharePage = (props: ShareMenuProps) => {
onClickCopyLink('edgeless' as DocMode);
}, [onClickCopyLink]);
const onCopyBlockLink = useCallback(() => {
// TODO(@JimmFly): handle frame
onClickCopyLink();
}, [onClickCopyLink]);
onClickCopyLink(currentMode);
}, [currentMode, onClickCopyLink]);

if (isLoading) {
// TODO(@eyhn): loading and error UI
Expand Down
51 changes: 38 additions & 13 deletions packages/frontend/core/src/hooks/affine/use-share-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { notify } from '@affine/component';
import { track } from '@affine/core/mixpanel';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { useI18n } from '@affine/i18n';
import type { BaseSelection } from '@blocksuite/block-std';
import type { EditorHost } from '@blocksuite/block-std';
import type { DocMode } from '@blocksuite/blocks';
import { useCallback } from 'react';

Expand All @@ -18,7 +18,9 @@ export type UseSharingUrl = {
};

/**
* to generate a url like https://app.affine.pro/workspace/workspaceId/docId?mode=DocMode?element=seletedBlockid#seletedBlockid
* To generate a url like
*
* https://app.affine.pro/workspace/workspaceId/docId?mode=DocMode&elementIds=seletedElementIds&blockIds=selectedBlockIds
*/
export const generateUrl = ({
workspaceId,
Expand Down Expand Up @@ -75,17 +77,39 @@ const getShareLinkType = ({
}
};

const getSelectionIds = (selections?: BaseSelection[]) => {
if (!selections || selections.length === 0) {
return { blockIds: [], elementIds: [] };
}
const getSelectionIds = (host: EditorHost | null, mode: DocMode = 'page') => {
const std = host?.std;
const blockIds: string[] = [];
const elementIds: string[] = [];
// TODO(@JimmFly): handle multiple selections and elementIds
if (selections[0].type === 'block') {
blockIds.push(selections[0].blockId);
const result = { blockIds, elementIds };

if (!std) {
return result;
}
return { blockIds, elementIds };

if (mode === 'edgeless') {
const selections = host.selection.value.filter(selection =>
selection.is('surface')
);
for (const { blockId, elements } of selections) {
// See https://github.com/toeverything/blocksuite/blob/ba75c6a77c48d53b61a45d88cec473af383f2d84/packages/blocks/src/root-block/edgeless/services/selection-manager.ts#L253-L277
if (elements.length === 1 && blockId === elements[0]) {
blockIds.push(blockId);
continue;
}
elementIds.push(...elements);
}

return result;
}

const { success, selectedModels } = std.command.exec('getSelectedModels');

if (!success || !selectedModels) return result;

blockIds.push(...selectedModels.map(model => model.id));

return result;
};

export const useSharingUrl = ({ workspaceId, pageId }: UseSharingUrl) => {
Expand All @@ -94,9 +118,10 @@ export const useSharingUrl = ({ workspaceId, pageId }: UseSharingUrl) => {

const onClickCopyLink = useCallback(
(shareMode?: DocMode) => {
const selectManager = editor?.host?.selection;
const selections = selectManager?.value;
const { blockIds, elementIds } = getSelectionIds(selections);
const { blockIds, elementIds } = getSelectionIds(
editor?.host || null,
shareMode
);

const sharingUrl = generateUrl({
workspaceId,
Expand Down

0 comments on commit c92aa60

Please sign in to comment.