Skip to content

Commit

Permalink
chore(core): track copy block to link action (#8056)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 5, 2024
1 parent c6840c8 commit 2bdf8c6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
generateUrl,
type UseSharingUrl,
} from '@affine/core/hooks/affine/use-share-url';
import { track } from '@affine/core/mixpanel';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n';
Expand Down Expand Up @@ -71,6 +72,8 @@ function createCopyLinkToBlockMenuItem(
const str = generateUrl(options);
if (!str) return;

track.doc.editor.toolbar.copyBlockToLink({ type: model.flavour });

navigator.clipboard
.writeText(str)
.then(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import {
generateUrl,
type UseSharingUrl,
} from '@affine/core/hooks/affine/use-share-url';
import { track } from '@affine/core/mixpanel';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n';
import type { MenuItemGroup } from '@blocksuite/affine-components/toolbar';
import type {
GfxBlockElementModel,
GfxPrimitiveElementModel,
} from '@blocksuite/block-std/gfx';
import type { MenuContext } from '@blocksuite/blocks';
import { LinkIcon } from '@blocksuite/icons/lit';
import type { FrameworkProvider } from '@toeverything/infra';
Expand Down Expand Up @@ -74,27 +79,42 @@ function createCopyLinkToBlockMenuItem(
...item,
action: (ctx: MenuContext) => {
const baseUrl = getAffineCloudBaseUrl();
if (!baseUrl) return;
if (!baseUrl) {
ctx.close();
return;
}

const { editor } = framework.get(EditorService);
const mode = editor.mode$.value;
const pageId = editor.doc.id;
const workspaceId = editor.doc.workspace.id;
const options: UseSharingUrl = { workspaceId, pageId, shareMode: mode };
let type = '';

if (mode === 'page') {
// maybe multiple blocks
const blockIds = ctx.selectedBlockModels.map(model => model.id);
options.blockIds = blockIds;
type = ctx.selectedBlockModels[0].flavour;
} else if (mode === 'edgeless' && ctx.firstElement) {
// single block/element
const id = ctx.firstElement.id;
const key = ctx.isElement() ? 'element' : 'block';
options[`${key}Ids`] = [id];
if (ctx.isElement()) {
options.elementIds = [id];
type = (ctx.firstElement as GfxPrimitiveElementModel).type;
} else {
options.blockIds = [id];
type = (ctx.firstElement as GfxBlockElementModel).flavour;
}
}

const str = generateUrl(options);
if (!str) return;
if (!str) {
ctx.close();
return;
}

track.doc.editor.toolbar.copyBlockToLink({ type });

navigator.clipboard
.writeText(str)
Expand All @@ -104,6 +124,8 @@ function createCopyLinkToBlockMenuItem(
});
})
.catch(console.error);

ctx.close();
},
};
}
7 changes: 6 additions & 1 deletion packages/frontend/core/src/mixpanel/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ type DocEvents =
| 'restoreDoc'
| 'switchPageMode'
| 'openDocOptionsMenu'
| 'openDocInfo';
| 'openDocInfo'
| 'copyBlockToLink';
type EditorEvents = 'bold' | 'italic' | 'underline' | 'strikeThrough';
// END SECTION

Expand Down Expand Up @@ -265,6 +266,7 @@ const PageEvents = {
atMenu: ['linkDoc'],
formatToolbar: ['bold'],
pageRef: ['navigate'],
toolbar: ['copyBlockToLink'],
},
inlineDocInfo: {
$: ['toggle'],
Expand Down Expand Up @@ -382,6 +384,9 @@ export type EventArgs = {
type: 'default' | 'doc' | 'whiteboard' | 'block' | 'element';
};
export: { type: string };
copyBlockToLink: {
type: string;
};
};

// for type checking
Expand Down

0 comments on commit 2bdf8c6

Please sign in to comment.