chore(core): track copy block to link action (#8056)

part of [BS-1254](https://linear.app/affine-design/issue/BS-1254/增加-block-reference-的埋点)
This commit is contained in:
fundon
2024-09-05 03:21:25 +00:00
parent c6840c8674
commit 2bdf8c6ef3
3 changed files with 35 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ import {
generateUrl, generateUrl,
type UseSharingUrl, type UseSharingUrl,
} from '@affine/core/hooks/affine/use-share-url'; } from '@affine/core/hooks/affine/use-share-url';
import { track } from '@affine/core/mixpanel';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch'; import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { EditorService } from '@affine/core/modules/editor'; import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n'; import { I18n } from '@affine/i18n';
@@ -71,6 +72,8 @@ function createCopyLinkToBlockMenuItem(
const str = generateUrl(options); const str = generateUrl(options);
if (!str) return; if (!str) return;
track.doc.editor.toolbar.copyBlockToLink({ type: model.flavour });
navigator.clipboard navigator.clipboard
.writeText(str) .writeText(str)
.then(() => { .then(() => {

View File

@@ -3,10 +3,15 @@ import {
generateUrl, generateUrl,
type UseSharingUrl, type UseSharingUrl,
} from '@affine/core/hooks/affine/use-share-url'; } from '@affine/core/hooks/affine/use-share-url';
import { track } from '@affine/core/mixpanel';
import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch'; import { getAffineCloudBaseUrl } from '@affine/core/modules/cloud/services/fetch';
import { EditorService } from '@affine/core/modules/editor'; import { EditorService } from '@affine/core/modules/editor';
import { I18n } from '@affine/i18n'; import { I18n } from '@affine/i18n';
import type { MenuItemGroup } from '@blocksuite/affine-components/toolbar'; import type { MenuItemGroup } from '@blocksuite/affine-components/toolbar';
import type {
GfxBlockElementModel,
GfxPrimitiveElementModel,
} from '@blocksuite/block-std/gfx';
import type { MenuContext } from '@blocksuite/blocks'; import type { MenuContext } from '@blocksuite/blocks';
import { LinkIcon } from '@blocksuite/icons/lit'; import { LinkIcon } from '@blocksuite/icons/lit';
import type { FrameworkProvider } from '@toeverything/infra'; import type { FrameworkProvider } from '@toeverything/infra';
@@ -74,27 +79,42 @@ function createCopyLinkToBlockMenuItem(
...item, ...item,
action: (ctx: MenuContext) => { action: (ctx: MenuContext) => {
const baseUrl = getAffineCloudBaseUrl(); const baseUrl = getAffineCloudBaseUrl();
if (!baseUrl) return; if (!baseUrl) {
ctx.close();
return;
}
const { editor } = framework.get(EditorService); const { editor } = framework.get(EditorService);
const mode = editor.mode$.value; const mode = editor.mode$.value;
const pageId = editor.doc.id; const pageId = editor.doc.id;
const workspaceId = editor.doc.workspace.id; const workspaceId = editor.doc.workspace.id;
const options: UseSharingUrl = { workspaceId, pageId, shareMode: mode }; const options: UseSharingUrl = { workspaceId, pageId, shareMode: mode };
let type = '';
if (mode === 'page') { if (mode === 'page') {
// maybe multiple blocks // maybe multiple blocks
const blockIds = ctx.selectedBlockModels.map(model => model.id); const blockIds = ctx.selectedBlockModels.map(model => model.id);
options.blockIds = blockIds; options.blockIds = blockIds;
type = ctx.selectedBlockModels[0].flavour;
} else if (mode === 'edgeless' && ctx.firstElement) { } else if (mode === 'edgeless' && ctx.firstElement) {
// single block/element // single block/element
const id = ctx.firstElement.id; const id = ctx.firstElement.id;
const key = ctx.isElement() ? 'element' : 'block'; if (ctx.isElement()) {
options[`${key}Ids`] = [id]; options.elementIds = [id];
type = (ctx.firstElement as GfxPrimitiveElementModel).type;
} else {
options.blockIds = [id];
type = (ctx.firstElement as GfxBlockElementModel).flavour;
}
} }
const str = generateUrl(options); const str = generateUrl(options);
if (!str) return; if (!str) {
ctx.close();
return;
}
track.doc.editor.toolbar.copyBlockToLink({ type });
navigator.clipboard navigator.clipboard
.writeText(str) .writeText(str)
@@ -104,6 +124,8 @@ function createCopyLinkToBlockMenuItem(
}); });
}) })
.catch(console.error); .catch(console.error);
ctx.close();
}, },
}; };
} }

View File

@@ -44,7 +44,8 @@ type DocEvents =
| 'restoreDoc' | 'restoreDoc'
| 'switchPageMode' | 'switchPageMode'
| 'openDocOptionsMenu' | 'openDocOptionsMenu'
| 'openDocInfo'; | 'openDocInfo'
| 'copyBlockToLink';
type EditorEvents = 'bold' | 'italic' | 'underline' | 'strikeThrough'; type EditorEvents = 'bold' | 'italic' | 'underline' | 'strikeThrough';
// END SECTION // END SECTION
@@ -265,6 +266,7 @@ const PageEvents = {
atMenu: ['linkDoc'], atMenu: ['linkDoc'],
formatToolbar: ['bold'], formatToolbar: ['bold'],
pageRef: ['navigate'], pageRef: ['navigate'],
toolbar: ['copyBlockToLink'],
}, },
inlineDocInfo: { inlineDocInfo: {
$: ['toggle'], $: ['toggle'],
@@ -382,6 +384,9 @@ export type EventArgs = {
type: 'default' | 'doc' | 'whiteboard' | 'block' | 'element'; type: 'default' | 'doc' | 'whiteboard' | 'block' | 'element';
}; };
export: { type: string }; export: { type: string };
copyBlockToLink: {
type: string;
};
}; };
// for type checking // for type checking