|
| 1 | +import { notify } from '@affine/component'; |
| 2 | +import { |
| 3 | + isMindmapChild, |
| 4 | + isMindMapRoot, |
| 5 | +} from '@affine/core/blocksuite/presets/ai/utils/edgeless'; |
| 6 | +import { EditorService } from '@affine/core/modules/editor'; |
| 7 | +import { apis } from '@affine/electron-api'; |
| 8 | +import { I18n } from '@affine/i18n'; |
| 9 | +import type { BlockStdScope } from '@blocksuite/affine/block-std'; |
| 10 | +import { |
| 11 | + type GfxBlockElementModel, |
| 12 | + type GfxModel, |
| 13 | + GfxPrimitiveElementModel, |
| 14 | + isGfxGroupCompatibleModel, |
| 15 | +} from '@blocksuite/affine/block-std/gfx'; |
| 16 | +import type { |
| 17 | + EdgelessRootService, |
| 18 | + MenuContext, |
| 19 | +} from '@blocksuite/affine/blocks'; |
| 20 | +import { Bound, getCommonBound } from '@blocksuite/affine/global/utils'; |
| 21 | +import { CopyAsImgaeIcon } from '@blocksuite/icons/lit'; |
| 22 | +import type { FrameworkProvider } from '@toeverything/infra'; |
| 23 | + |
| 24 | +const snapshotStyle = ` |
| 25 | + affine-edgeless-root .widgets-container, |
| 26 | + .copy-as-image-transparent { |
| 27 | + opacity: 0; |
| 28 | + } |
| 29 | + .edgeless-background { |
| 30 | + background-image: none; |
| 31 | + } |
| 32 | +`; |
| 33 | + |
| 34 | +function getSelectedRect() { |
| 35 | + const selected = document |
| 36 | + .querySelector('edgeless-selected-rect') |
| 37 | + ?.shadowRoot?.querySelector('.affine-edgeless-selected-rect'); |
| 38 | + if (!selected) { |
| 39 | + throw new Error('Missing edgeless selected rect'); |
| 40 | + } |
| 41 | + return selected.getBoundingClientRect(); |
| 42 | +} |
| 43 | + |
| 44 | +function expandBound(bound: Bound, margin: number) { |
| 45 | + const x = bound.x - margin; |
| 46 | + const y = bound.y - margin; |
| 47 | + const w = bound.w + margin * 2; |
| 48 | + const h = bound.h + margin * 2; |
| 49 | + return new Bound(x, y, w, h); |
| 50 | +} |
| 51 | + |
| 52 | +function isOverlap(target: Bound, source: Bound) { |
| 53 | + const { x, y, w, h } = source; |
| 54 | + const left = target.x; |
| 55 | + const top = target.y; |
| 56 | + const right = target.x + target.w; |
| 57 | + const bottom = target.y + target.h; |
| 58 | + |
| 59 | + return x < right && y < bottom && x + w > left && y + h > top; |
| 60 | +} |
| 61 | + |
| 62 | +function isInside(target: Bound, source: Bound) { |
| 63 | + const { x, y, w, h } = source; |
| 64 | + const left = target.x; |
| 65 | + const top = target.y; |
| 66 | + const right = target.x + target.w; |
| 67 | + const bottom = target.y + target.h; |
| 68 | + |
| 69 | + return x >= left && y >= top && x + w <= right && y + h <= bottom; |
| 70 | +} |
| 71 | + |
| 72 | +function hideEdgelessElements(elements: GfxModel[], std: BlockStdScope) { |
| 73 | + elements.forEach(ele => { |
| 74 | + if (ele instanceof GfxPrimitiveElementModel) { |
| 75 | + (ele as any).lastOpacity = ele.opacity; |
| 76 | + ele.opacity = 0; |
| 77 | + } else { |
| 78 | + const block = std.view.getBlock(ele.id); |
| 79 | + if (!block) return; |
| 80 | + block.classList.add('copy-as-image-transparent'); |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +function showEdgelessElements(elements: GfxModel[], std: BlockStdScope) { |
| 86 | + elements.forEach(ele => { |
| 87 | + if (ele instanceof GfxPrimitiveElementModel) { |
| 88 | + ele.opacity = (ele as any).lastOpacity; |
| 89 | + delete (ele as any).lastOpacity; |
| 90 | + } else { |
| 91 | + const block = std.view.getBlock(ele.id); |
| 92 | + if (!block) return; |
| 93 | + block.classList.remove('copy-as-image-transparent'); |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +function withDescendantElements(elements: GfxModel[]) { |
| 99 | + const set = new Set<GfxModel>(); |
| 100 | + elements.forEach(element => { |
| 101 | + if (set.has(element)) return; |
| 102 | + set.add(element); |
| 103 | + if (isGfxGroupCompatibleModel(element)) { |
| 104 | + element.descendantElements.map(descendant => set.add(descendant)); |
| 105 | + } |
| 106 | + }); |
| 107 | + return [...set]; |
| 108 | +} |
| 109 | + |
| 110 | +const MARGIN = 20; |
| 111 | + |
| 112 | +export function createCopyAsPngMenuItem(framework: FrameworkProvider) { |
| 113 | + return { |
| 114 | + icon: CopyAsImgaeIcon({ width: '20', height: '20' }), |
| 115 | + label: 'Copy as Image', |
| 116 | + type: 'copy-as-image', |
| 117 | + when: (ctx: MenuContext) => { |
| 118 | + if (ctx.isEmpty()) return false; |
| 119 | + const { editor } = framework.get(EditorService); |
| 120 | + const mode = editor.mode$.value; |
| 121 | + return mode === 'edgeless'; |
| 122 | + }, |
| 123 | + action: async (ctx: MenuContext) => { |
| 124 | + if (!apis) { |
| 125 | + notify.error({ |
| 126 | + title: I18n.t('com.affine.copy.asImage.notAvailable.title'), |
| 127 | + message: I18n.t('com.affine.copy.asImage.notAvailable.message'), |
| 128 | + action: { |
| 129 | + label: I18n.t('com.affine.copy.asImage.notAvailable.action'), |
| 130 | + onClick: () => { |
| 131 | + window.open('https://affine.pro/download'); |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + const service = |
| 139 | + ctx.host.std.getService<EdgelessRootService>('affine:page'); |
| 140 | + if (!service) return; |
| 141 | + |
| 142 | + let selected = service.selection.selectedElements; |
| 143 | + // select mindmap if root node selected |
| 144 | + const maybeMindmap = selected[0]; |
| 145 | + const mindmapId = maybeMindmap.group?.id; |
| 146 | + if ( |
| 147 | + selected.length === 1 && |
| 148 | + mindmapId && |
| 149 | + (isMindMapRoot(maybeMindmap) || isMindmapChild(maybeMindmap)) |
| 150 | + ) { |
| 151 | + service.gfx.selection.set({ elements: [mindmapId] }); |
| 152 | + } |
| 153 | + |
| 154 | + // select bound |
| 155 | + selected = service.selection.selectedElements; |
| 156 | + const elements = withDescendantElements(selected); |
| 157 | + const bounds = elements.map(element => Bound.deserialize(element.xywh)); |
| 158 | + const bound = getCommonBound(bounds); |
| 159 | + if (!bound) return; |
| 160 | + const { zoom } = service.viewport; |
| 161 | + const exBound = expandBound(bound, MARGIN * zoom); |
| 162 | + |
| 163 | + // fit to screen |
| 164 | + if ( |
| 165 | + !isInside(service.viewport.viewportBounds, exBound) || |
| 166 | + service.viewport.zoom < 1 |
| 167 | + ) { |
| 168 | + service.viewport.setViewportByBound(bound, [20, 20, 20, 20], false); |
| 169 | + if (service.viewport.zoom > 1) { |
| 170 | + service.viewport.setZoom(1); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + // hide unselected overlap elements |
| 175 | + const overlapElements = service.gfx.gfxElements.filter(ele => { |
| 176 | + const eleBound = Bound.deserialize(ele.xywh); |
| 177 | + const exEleBound = expandBound(eleBound, MARGIN * zoom); |
| 178 | + const isSelected = elements.includes(ele); |
| 179 | + return !isSelected && isOverlap(exBound, exEleBound); |
| 180 | + }); |
| 181 | + hideEdgelessElements(overlapElements, ctx.host.std); |
| 182 | + |
| 183 | + // add css style |
| 184 | + const styleEle = document.createElement('style'); |
| 185 | + styleEle.innerHTML = snapshotStyle; |
| 186 | + document.head.append(styleEle); |
| 187 | + |
| 188 | + // capture image |
| 189 | + setTimeout(() => { |
| 190 | + if (!apis) return; |
| 191 | + try { |
| 192 | + const domRect = getSelectedRect(); |
| 193 | + const { zoom } = service.viewport; |
| 194 | + const isFrameSelected = |
| 195 | + selected.length === 1 && |
| 196 | + (selected[0] as GfxBlockElementModel).flavour === 'affine:frame'; |
| 197 | + const margin = isFrameSelected ? -2 : MARGIN * zoom; |
| 198 | + |
| 199 | + service.selection.clear(); |
| 200 | + // eslint-disable-next-line @typescript-eslint/no-floating-promises |
| 201 | + apis.ui |
| 202 | + .captureArea({ |
| 203 | + x: domRect.left - margin, |
| 204 | + y: domRect.top - margin, |
| 205 | + width: domRect.width + margin * 2, |
| 206 | + height: domRect.height + margin * 2, |
| 207 | + }) |
| 208 | + .then(() => { |
| 209 | + notify.success({ |
| 210 | + title: I18n.t('com.affine.copy.asImage.success'), |
| 211 | + }); |
| 212 | + }) |
| 213 | + .catch(e => { |
| 214 | + notify.error({ |
| 215 | + title: I18n.t('com.affine.copy.asImage.failed'), |
| 216 | + message: String(e), |
| 217 | + }); |
| 218 | + }) |
| 219 | + .finally(() => { |
| 220 | + styleEle.remove(); |
| 221 | + showEdgelessElements(overlapElements, ctx.host.std); |
| 222 | + }); |
| 223 | + } catch (e) { |
| 224 | + styleEle.remove(); |
| 225 | + showEdgelessElements(overlapElements, ctx.host.std); |
| 226 | + notify.error({ |
| 227 | + title: I18n.t('com.affine.copy.asImage.failed'), |
| 228 | + message: String(e), |
| 229 | + }); |
| 230 | + } |
| 231 | + }, 100); |
| 232 | + }, |
| 233 | + }; |
| 234 | +} |
0 commit comments