|
8 | 8 | waitForFunction, |
9 | 9 | OneHundred, |
10 | 10 | Second, |
| 11 | + checkCanvasDimensions, |
11 | 12 | } from '@dolphin/common' |
12 | 13 | import { toMarkdown, type Options } from 'mdast-util-to-markdown' |
13 | 14 | import { gfmStrikethroughToMarkdown } from 'mdast-util-gfm-strikethrough' |
@@ -318,16 +319,43 @@ interface RatioApp { |
318 | 319 | i: 24, |
319 | 320 | o: [''], |
320 | 321 | r: false, |
321 | | - n: 2, |
| 322 | + n: number, |
322 | 323 | ) => Promise<ImageDataWrapper | null> |
323 | 324 | } |
| 325 | + app?: { |
| 326 | + application: { |
| 327 | + nodeManager: { |
| 328 | + getNodesBounds: () => { |
| 329 | + minX: number |
| 330 | + maxX: number |
| 331 | + minY: number |
| 332 | + maxY: number |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + renderManager: { |
| 337 | + getImageOffscreenCanvas: ( |
| 338 | + bounds: { |
| 339 | + minX: number |
| 340 | + maxX: number |
| 341 | + minY: number |
| 342 | + maxY: number |
| 343 | + }, |
| 344 | + r: number, |
| 345 | + bgColor: string, |
| 346 | + ) => HTMLCanvasElement | null |
| 347 | + } |
| 348 | + } |
324 | 349 | } |
325 | 350 |
|
326 | 351 | interface WhiteboardBlock { |
327 | 352 | isolateEnv: { |
328 | 353 | hasRatioApp: () => boolean |
329 | 354 | getRatioApp: () => RatioApp |
330 | 355 | } |
| 356 | + abilityKit: { |
| 357 | + getRatioApp: () => RatioApp |
| 358 | + } |
331 | 359 | } |
332 | 360 |
|
333 | 361 | interface Whiteboard extends Block { |
@@ -910,27 +938,54 @@ const fetchImageSources = (imageBlock: ImageBlock) => |
910 | 938 | .catch(reject) |
911 | 939 | }) |
912 | 940 |
|
913 | | -const whiteboardToImageData = async ( |
| 941 | +const whiteboardToBlob = async ( |
914 | 942 | whiteboard: Whiteboard, |
915 | | -): Promise<ImageDataWrapper | null> => { |
| 943 | +): Promise<Blob | null> => { |
916 | 944 | if (!whiteboard.whiteboardBlock) return null |
917 | 945 |
|
918 | | - const { isolateEnv } = whiteboard.whiteboardBlock |
| 946 | + const padding = 24 |
| 947 | + const ratio = window.devicePixelRatio |
| 948 | + const backgroundColor = '#ffffff' |
919 | 949 |
|
920 | | - if (!isolateEnv.hasRatioApp()) return null |
| 950 | + let rationApp = whiteboard.whiteboardBlock.abilityKit.getRatioApp() |
921 | 951 |
|
922 | | - const rationApp = isolateEnv.getRatioApp() |
| 952 | + if (rationApp.app) { |
| 953 | + const bounds = rationApp.app.application.nodeManager.getNodesBounds() |
| 954 | + bounds.maxX += padding |
| 955 | + bounds.minX -= padding |
| 956 | + bounds.maxY += padding |
| 957 | + bounds.minY -= padding |
| 958 | + |
| 959 | + const canvas = rationApp.app.renderManager.getImageOffscreenCanvas( |
| 960 | + bounds, |
| 961 | + ratio, |
| 962 | + backgroundColor, |
| 963 | + ) |
923 | 964 |
|
924 | | - const imageData = await rationApp.ratioAppProxy.getOriginImageDataByNodeId( |
925 | | - 24, |
926 | | - [''], |
927 | | - false, |
928 | | - 2, |
929 | | - ) |
| 965 | + if (!canvas) return null |
930 | 966 |
|
931 | | - if (!imageData) return null |
| 967 | + return new Promise(resolve => { |
| 968 | + checkCanvasDimensions(canvas) |
932 | 969 |
|
933 | | - return imageData |
| 970 | + canvas.toBlob(resolve) |
| 971 | + }) |
| 972 | + } |
| 973 | + |
| 974 | + rationApp = whiteboard.whiteboardBlock.isolateEnv.getRatioApp() |
| 975 | + |
| 976 | + const imageDataWrapper = |
| 977 | + await rationApp.ratioAppProxy.getOriginImageDataByNodeId( |
| 978 | + padding, |
| 979 | + [''], |
| 980 | + false, |
| 981 | + ratio, |
| 982 | + ) |
| 983 | + |
| 984 | + if (!imageDataWrapper) return null |
| 985 | + |
| 986 | + return await imageDataToBlob(imageDataWrapper.data, { |
| 987 | + onDispose: imageDataWrapper.release, |
| 988 | + }) |
934 | 989 | } |
935 | 990 |
|
936 | 991 | const diagramToSVGElement = (diagram: DiagramBlock): SVGElement | null => { |
@@ -1321,12 +1376,7 @@ export class Transformer { |
1321 | 1376 | console.error(error) |
1322 | 1377 | } |
1323 | 1378 |
|
1324 | | - const imageDataWrapper = await whiteboardToImageData(whiteboard) |
1325 | | - if (!imageDataWrapper) return null |
1326 | | - |
1327 | | - return await imageDataToBlob(imageDataWrapper.data, { |
1328 | | - onDispose: imageDataWrapper.release, |
1329 | | - }) |
| 1379 | + return await whiteboardToBlob(whiteboard) |
1330 | 1380 | }, |
1331 | 1381 | }, |
1332 | 1382 | } |
|
0 commit comments