Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 170395b

Browse files
feat: new export format for offline support (#11)
1 parent b848944 commit 170395b

7 files changed

Lines changed: 238 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.0.0 (2021-XX-YY)
2+
3+
### Features
4+
5+
- new export format for offline support
6+
17
# 1.0.4 (2021-04-05)
28

39
### Features

package-lock.json

Lines changed: 43 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "figma-deckdeckgo-plugin",
3-
"version": "1.0.4",
3+
"version": "2.0.0",
44
"description": "Export Figma frames to DeckDeckGo",
55
"main": "dist/plugin.js",
66
"scripts": {
@@ -14,6 +14,7 @@
1414
},
1515
"homepage": "https://deckdeckgo.com",
1616
"devDependencies": {
17+
"@deckdeckgo/editor": "file:../deckdeckgo/utils/editor",
1718
"@figma/plugin-typings": "^1.34.1",
1819
"@types/jest": "^27.0.1",
1920
"esbuild": "^0.12.28",
@@ -33,6 +34,7 @@
3334
}
3435
},
3536
"dependencies": {
37+
"date-fns": "^2.24.0",
3638
"jszip": "^3.7.1"
3739
}
3840
}

src/types/output.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
interface OutputFrame {
2+
element: HTMLElement;
3+
filename: string;
4+
}
5+
6+
interface OutputFrameGroup {
7+
canvas: OutputFrame;
8+
aside: OutputFrame | undefined;
9+
}

src/utils/download.utils.ts

Lines changed: 20 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
import JSZip from 'jszip';
22

3-
interface OutputFrame {
4-
element: HTMLElement;
5-
filename: string;
6-
}
3+
import {FileImportData, UserAsset} from '@deckdeckgo/editor';
74

8-
interface OutputFrameGroup {
9-
canvas: OutputFrame;
10-
aside: OutputFrame | undefined;
11-
}
5+
import {importData, userAssets} from './meta.utils';
126

137
export const downloadFrames = async () => {
148
const container: HTMLDivElement | null = document.querySelector('div.container');
@@ -29,7 +23,10 @@ export const downloadFrames = async () => {
2923
return;
3024
}
3125

32-
const blob: Blob = await zip(svgList);
26+
const data: FileImportData = importData(svgList);
27+
const assets: UserAsset[] = await Promise.all(userAssets(svgList));
28+
29+
const blob: Blob = await zip({assets, data});
3330

3431
download(blob);
3532
};
@@ -60,76 +57,47 @@ const prepare = (frames: HTMLDivElement[]): OutputFrameGroup[] => {
6057
return svgList;
6158
};
6259

63-
const zip = async (svgList: OutputFrameGroup[]): Promise<Blob> => {
60+
const zip = async ({assets, data}: {assets: UserAsset[]; data: FileImportData}): Promise<Blob> => {
6461
const zip = new JSZip();
6562

66-
const addImageZip = async ({dataUrl, filename}: {dataUrl: string; filename: string}) => {
67-
const blob: Blob = await (await fetch(dataUrl)).blob();
68-
69-
zip.file(filename, blob, {
63+
const addImageZip = async ({key, blob}: UserAsset) => {
64+
zip.file(key, blob, {
7065
base64: true
7166
});
7267
};
7368

74-
const addHtmlZip = ({data, filename}: {data: string; filename: string}) => {
75-
const blob: Blob = new Blob([data], {type: 'text/html'});
69+
const addDeckMetaZip = (deck: FileImportData) => {
70+
const blob: Blob = new Blob([JSON.stringify(deck)], {type: 'application/json'});
7671

77-
zip.file(filename, blob, {
72+
zip.file('deck.json', blob, {
7873
base64: true
7974
});
8075
};
8176

82-
const addMetaZip = (meta: Metadata) => {
83-
const blob: Blob = new Blob([JSON.stringify(meta)], {type: 'application/json'});
77+
const addAssetMetaZip = (assets: UserAsset[]) => {
78+
const blob: Blob = new Blob([JSON.stringify(assets.map(({key}) => ({key})))], {type: 'application/json'});
8479

85-
zip.file('meta.json', blob, {
80+
zip.file('assets.json', blob, {
8681
base64: true
8782
});
8883
};
8984

90-
const promises: Promise<void>[] = svgList.map(async ({canvas, aside}: OutputFrameGroup) => {
91-
await addImageZip({
92-
dataUrl: (canvas.element as HTMLCanvasElement).toDataURL('image/webp'),
93-
filename: canvas.filename
94-
});
95-
96-
if (aside) {
97-
addHtmlZip({
98-
data: aside.element.innerHTML,
99-
filename: aside.filename
100-
});
101-
}
102-
});
85+
const imagePromises: Promise<void>[] = assets.map((userAsset: UserAsset) => addImageZip(userAsset));
86+
await Promise.all(imagePromises);
10387

104-
await Promise.all(promises);
88+
addDeckMetaZip(data);
10589

106-
addMetaZip(meta(svgList));
90+
addAssetMetaZip(assets);
10791

10892
return zip.generateAsync({type: 'blob'});
10993
};
11094

111-
const meta = (svgList: OutputFrameGroup[]): Metadata => {
112-
const fonts: FontsComponent | null = document.querySelector('deckgo-fonts');
113-
114-
const slides: MetadataSlide[] = svgList.map(({canvas, aside}: OutputFrameGroup) => {
115-
return {
116-
background: canvas.filename,
117-
...(aside && {text: aside.filename})
118-
};
119-
});
120-
121-
return {
122-
slides,
123-
...((fonts?.value && fonts?.value !== 'Default') && {fontFamily: fonts.value})
124-
}
125-
};
126-
12795
const download = (blob) => {
12896
const blobURL = window.URL.createObjectURL(blob);
12997

13098
const link = document.createElement('a');
13199
link.href = blobURL;
132-
link.download = `deckdeckgo.zip`;
100+
link.download = `deckdeckgo.ddg`;
133101
link.click();
134102

135103
window.URL.revokeObjectURL(blobURL);

src/utils/google-fonts.utils.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// TODO: Extract to utilities because it is also use in studio
2+
3+
export interface GoogleFont {
4+
id: string;
5+
name: string;
6+
family: string;
7+
}
8+
9+
// TODO fetch(`https://app.deckdeckgo.com/assests/assets.json`) at build time
10+
export const fonts: GoogleFont[] = [
11+
{
12+
id: 'google-fonts-lora',
13+
name: 'Lora',
14+
family: "'Lora', serif"
15+
},
16+
{
17+
id: 'google-fonts-roboto',
18+
name: 'Roboto',
19+
family: "'Roboto', sans-serif"
20+
},
21+
{
22+
id: 'google-fonts-open-sans',
23+
name: 'Open Sans',
24+
family: "'Open Sans', sans-serif"
25+
},
26+
{
27+
id: 'google-fonts-montserrat',
28+
name: 'Montserrat',
29+
family: "'Montserrat', sans-serif"
30+
},
31+
{
32+
id: 'google-fonts-cabin',
33+
name: 'Cabin',
34+
family: "'Cabin', sans-serif"
35+
},
36+
{
37+
id: 'google-fonts-lato',
38+
name: 'Lato',
39+
family: "'Lato', sans-serif"
40+
},
41+
{
42+
id: 'google-fonts-muli',
43+
name: 'Muli',
44+
family: "'Muli', sans-serif"
45+
},
46+
{
47+
id: 'google-fonts-source-sans-pro',
48+
name: 'Source Sans Pro',
49+
family: "'Source Sans Pro', sans-serif"
50+
},
51+
{
52+
id: 'google-fonts-libre-baskerville',
53+
name: 'Libre Baskerville',
54+
family: "'Libre Baskerville', serif"
55+
},
56+
{
57+
id: 'google-fonts-oswald',
58+
name: 'Oswald',
59+
family: "'Oswald', sans-serif"
60+
},
61+
{
62+
id: 'google-fonts-jura',
63+
name: 'Jura',
64+
family: "'Jura', sans-serif"
65+
},
66+
{
67+
id: 'google-fonts-fjord-one',
68+
name: 'Fjord One',
69+
family: "'Fjord One', serif"
70+
},
71+
{
72+
id: 'google-fonts-josefin-slab',
73+
name: 'Josefin Slab',
74+
family: "'Josefin Slab', serif"
75+
}
76+
];

0 commit comments

Comments
 (0)