fix(editor): missing copy as png implementation (#11716)

This commit is contained in:
Saul-Mirone
2025-04-15 13:08:47 +00:00
parent 681c61a300
commit b2694003c9
2 changed files with 116 additions and 108 deletions

View File

@@ -104,18 +104,7 @@ function withDescendantElements(elements: GfxModel[]) {
const MARGIN = 20;
export function createCopyAsPngMenuItem(framework: FrameworkProvider) {
return {
icon: CopyAsImgaeIcon({ width: '20', height: '20' }),
label: 'Copy as Image',
type: 'copy-as-image',
when: (ctx: MenuContext) => {
if (ctx.isEmpty()) return false;
const { editor } = framework.get(EditorService);
const mode = editor.mode$.value;
return mode === 'edgeless';
},
action: async (ctx: MenuContext) => {
export function copyAsImage(std: BlockStdScope) {
if (!apis) {
notify.error({
title: I18n.t('com.affine.copy.asImage.notAvailable.title'),
@@ -130,7 +119,7 @@ export function createCopyAsPngMenuItem(framework: FrameworkProvider) {
return;
}
const gfx = ctx.host.std.get(GfxControllerIdentifier);
const gfx = std.get(GfxControllerIdentifier);
let selected = gfx.selection.selectedElements;
// select mindmap if root node selected
@@ -171,7 +160,7 @@ export function createCopyAsPngMenuItem(framework: FrameworkProvider) {
const isSelected = elements.includes(ele);
return !isSelected && isOverlap(exBound, exEleBound);
});
hideEdgelessElements(overlapElements, ctx.host.std);
hideEdgelessElements(overlapElements, std);
// add css style
const styleEle = document.createElement('style');
@@ -211,17 +200,33 @@ export function createCopyAsPngMenuItem(framework: FrameworkProvider) {
})
.finally(() => {
styleEle.remove();
showEdgelessElements(overlapElements, ctx.host.std);
showEdgelessElements(overlapElements, std);
});
} catch (e) {
styleEle.remove();
showEdgelessElements(overlapElements, ctx.host.std);
showEdgelessElements(overlapElements, std);
notify.error({
title: I18n.t('com.affine.copy.asImage.failed'),
message: String(e),
});
}
}, 100);
}
export function createCopyAsPngMenuItem(framework: FrameworkProvider) {
return {
icon: CopyAsImgaeIcon({ width: '20', height: '20' }),
label: 'Copy as Image',
type: 'copy-as-image',
when: (ctx: MenuContext) => {
if (ctx.isEmpty()) return false;
const { editor } = framework.get(EditorService);
const mode = editor.mode$.value;
return mode === 'edgeless';
},
action: (ctx: MenuContext) => {
const std = ctx.std;
return copyAsImage(std);
},
};
}

View File

@@ -88,7 +88,7 @@ import { repeat } from 'lit/directives/repeat.js';
import { styleMap } from 'lit/directives/style-map.js';
import { openDocActions } from '../../open-doc';
import { createCopyAsPngMenuItem } from './copy-as-image';
import { copyAsImage, createCopyAsPngMenuItem } from './copy-as-image';
export function createToolbarMoreMenuConfig(framework: FrameworkProvider) {
return {
@@ -224,6 +224,9 @@ function createToolbarMoreMenuConfigV2(baseUrl?: string) {
!flags.isHovering() &&
isEdgelessMode &&
gfx.selection.selectedElements.length > 0,
run: ({ std }) => {
copyAsImage(std);
},
},
{
id: 'copy-link-to-block',