1
1
import { Editor , getSvgAsImage } from "@tldraw/tldraw" ;
2
- import { sendMessage } from "./messageBus" ;
3
2
4
- export async function sendExport (
3
+ export async function extractImageAndText ( editor : Editor , selectionOnly : boolean ) {
4
+ const [ base64 , text ] = await Promise . all ( [
5
+ extractImage ( editor , selectionOnly ) ,
6
+ extractText ( editor , selectionOnly ) ,
7
+ ] ) ;
8
+ return { base64, text } ;
9
+ }
10
+
11
+ export async function extractImage (
5
12
editor : Editor ,
6
- partialRenderEnabled : boolean
13
+ selectionOnly : boolean
7
14
) {
8
15
// TODO: clean this up, partialRenderEnabled shouldn't be passed in
9
- let selectedShapes = partialRenderEnabled ? editor . getSelectedShapes ( ) : [ ] ;
10
-
11
- if ( selectedShapes . length === 0 )
12
- selectedShapes = editor . getCurrentPageShapes ( ) ;
16
+ const selectedShapes = selectionOnly ? editor . getSelectedShapes ( ) : editor . getCurrentPageShapes ( ) ;
13
17
14
18
const svg = await editor . getSvg ( selectedShapes ) ;
15
19
if ( ! svg ) throw Error ( `Could not get the SVG.` ) ;
@@ -21,12 +25,8 @@ export async function sendExport(
21
25
} ) ;
22
26
23
27
const base64 = await blobToBase64 ( blob ! ) ;
24
- const imageTexts = getSelectionAsText ( editor ) ;
25
28
26
- await sendMessage ( {
27
- command : "tldraw:export" ,
28
- payload : { base64, imageTexts } ,
29
- } ) ;
29
+ return base64 ;
30
30
}
31
31
32
32
export function blobToBase64 ( blob : Blob ) : Promise < string > {
@@ -37,11 +37,8 @@ export function blobToBase64(blob: Blob): Promise<string> {
37
37
} ) ;
38
38
}
39
39
40
- function getSelectionAsText ( editor : Editor ) {
41
- let selectedShapeIds = editor . getSelectedShapeIds ( ) ;
42
-
43
- if ( selectedShapeIds . length === 0 )
44
- selectedShapeIds = Array . from ( editor . getCurrentPageShapeIds ( ) ) ;
40
+ function extractText ( editor : Editor , selectionOnly : boolean ) {
41
+ const selectedShapeIds = selectionOnly ? editor . getSelectedShapeIds ( ) : Array . from ( editor . getCurrentPageShapeIds ( ) ) ;
45
42
46
43
const selectedShapeDescendantIds =
47
44
editor . getShapeAndDescendantIds ( selectedShapeIds ) ;
0 commit comments