Skip to content

Commit

Permalink
chore(core): track copy block to link action
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Sep 4, 2024
1 parent 19ba2c8 commit 389b0e4
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 @@ -263,6 +264,7 @@ const PageEvents = {
slashMenu: ['linkDoc', 'createDoc'],
atMenu: ['linkDoc'],
formatToolbar: ['bold'],
toolbar: ['copyBlockToLink'],
},
inlineDocInfo: {
$: ['toggle'],
Expand Down Expand Up @@ -380,6 +382,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 389b0e4

Please sign in to comment.