Skip to content

Commit b8cca3c

Browse files
author
Yann Leflour
committed
Cleanup extraction
1 parent e353dd9 commit b8cca3c

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

ui-sketcher-webview/src/lib/makeReal.ts renamed to ui-sketcher-webview/src/domain/tldraw.extractor.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { Editor, getSvgAsImage } from "@tldraw/tldraw";
2-
import { sendMessage } from "./messageBus";
32

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(
512
editor: Editor,
6-
partialRenderEnabled: boolean
13+
selectionOnly: boolean
714
) {
815
// 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();
1317

1418
const svg = await editor.getSvg(selectedShapes);
1519
if (!svg) throw Error(`Could not get the SVG.`);
@@ -21,12 +25,8 @@ export async function sendExport(
2125
});
2226

2327
const base64 = await blobToBase64(blob!);
24-
const imageTexts = getSelectionAsText(editor);
2528

26-
await sendMessage({
27-
command: "tldraw:export",
28-
payload: { base64, imageTexts },
29-
});
29+
return base64;
3030
}
3131

3232
export function blobToBase64(blob: Blob): Promise<string> {
@@ -37,11 +37,8 @@ export function blobToBase64(blob: Blob): Promise<string> {
3737
});
3838
}
3939

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());
4542

4643
const selectedShapeDescendantIds =
4744
editor.getShapeAndDescendantIds(selectedShapeIds);

0 commit comments

Comments
 (0)