Skip to content

Commit

Permalink
fix: 🐛 utils/getAnimation handling of inlined assets (#41)
Browse files Browse the repository at this point in the history
* fix: fix for utils/getAnimation with inlined assets

* refactor: 💡 apply suggested changes

* chore: 🤖 update changeset

* chore: 🤖 update utils-browser test

---------

Co-authored-by: Abdelrahman Ashraf <a.theashraf@gmail.com>
  • Loading branch information
samuelOsborne and theashraf authored Aug 14, 2023
1 parent da46709 commit 2fac1b7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-jokes-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@dotlottie/dotlottie-js": patch
---

fix: 🐛 utils/getAnimation handling of inlined assets
31 changes: 17 additions & 14 deletions packages/dotlottie-js/src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,11 @@ export async function getImage(
* @public
*/
export async function getImages(dotLottie: Uint8Array, filter?: UnzipFileFilter): Promise<Record<string, string>> {
const unzippedImages = await unzipDotLottie(
dotLottie,
(file) => file.name.startsWith('images/') && (!filter || filter(file)),
);
const unzippedImages = await unzipDotLottie(dotLottie, (file) => {
const name = file.name.replace('images/', '');

return file.name.startsWith('images/') && (!filter || filter({ ...file, name }));
});

const images: Record<string, string> = {};

Expand Down Expand Up @@ -642,7 +643,7 @@ export async function inlineImageAssets(
const unzippedImages = await getImages(dotLottie, (file) => imagesMap.has(file.name));

for (const [imageId, animationIdsSet] of imagesMap) {
const imageDataURL = unzippedImages[`images/${imageId}`];
const imageDataURL = unzippedImages[imageId];

if (imageDataURL) {
for (const animationId of animationIdsSet) {
Expand Down Expand Up @@ -703,7 +704,7 @@ export async function getAnimation(
}

const animationsMap = {
animationId: animationData,
[animationId]: animationData,
};

await inlineImageAssets(dotLottie, animationsMap);
Expand Down Expand Up @@ -737,10 +738,11 @@ export async function getAnimations(
filter?: UnzipFileFilter,
): Promise<Record<string, AnimationData>> {
const animationsMap: Record<string, AnimationData> = {};
const unzippedAnimations = await unzipDotLottie(
dotLottie,
(file) => file.name.startsWith('animations/') && (!filter || filter(file)),
);
const unzippedAnimations = await unzipDotLottie(dotLottie, (file) => {
const filename = file.name.replace('animations/', '').replace('.json', '');

return file.name.startsWith('animations/') && (!filter || filter({ ...file, name: filename }));
});

for (const animationPath in unzippedAnimations) {
const data = unzippedAnimations[animationPath];
Expand Down Expand Up @@ -782,10 +784,11 @@ export async function getAnimations(
export async function getThemes(dotLottie: Uint8Array, filter?: UnzipFileFilter): Promise<Record<string, string>> {
const themesMap: Record<string, string> = {};

const unzippedThemes = await unzipDotLottie(
dotLottie,
(file) => file.name.startsWith('themes/') && (!filter || filter(file)),
);
const unzippedThemes = await unzipDotLottie(dotLottie, (file) => {
const name = file.name.replace('themes/', '').replace('.lss', '');

return file.name.startsWith('themes/') && (!filter || filter({ ...file, name }));
});

for (const themePath in unzippedThemes) {
const data = unzippedThemes[themePath];
Expand Down

Large diffs are not rendered by default.

Binary file not shown.
12 changes: 12 additions & 0 deletions packages/dotlottie-js/src/tests/utils-browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {
} from '..';

import dotLottieAnimation from './__fixtures__/simple/animation.lottie';
import bullJson from './__fixtures__/simple/animation/animations/bull.json';
import dotLottieLottie1 from './__fixtures__/simple/animation/animations/lottie1.json';
import dotLottieManifest from './__fixtures__/simple/animation/manifest.json';
import dotLottieTheme from './__fixtures__/simple/animation/themes/theme1.lss';
import dotLottieAnimationWithImages from './__fixtures__/simple/big-merged-dotlottie.lottie';
import bullAnimation from './__fixtures__/simple/bull.lottie';

describe('createError', () => {
it('returns an instance of Error with the correct message', () => {
Expand Down Expand Up @@ -179,6 +181,16 @@ describe('getAnimation', () => {

expect(animation).toEqual(dotLottieLottie1 as AnimationData);
});

it('returns inlined images within the animation', async () => {
const manifest = await getManifest(bullAnimation);

const animationId = manifest?.animations[0]?.id || '';

const animation = await getAnimation(bullAnimation, animationId, { inlineAssets: true });

expect(JSON.stringify(animation?.assets)).toEqual(JSON.stringify(bullJson.assets));
});
});

describe('getTheme', () => {
Expand Down
12 changes: 12 additions & 0 deletions packages/dotlottie-js/src/tests/utils-node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ import {
} from '../node';

import dotLottieAnimation from './__fixtures__/simple/animation.lottie';
import bullJson from './__fixtures__/simple/animation/animations/bull.json';
import dotLottieLottie1 from './__fixtures__/simple/animation/animations/lottie1.json';
import dotLottieManifest from './__fixtures__/simple/animation/manifest.json';
import dotLottieTheme from './__fixtures__/simple/animation/themes/theme1.lss';
import dotLottieAnimationWithImages from './__fixtures__/simple/big-merged-dotlottie.lottie';
import bullAnimation from './__fixtures__/simple/bull.lottie';

describe('createError', () => {
it('returns an instance of Error with the correct message', () => {
Expand Down Expand Up @@ -187,6 +189,16 @@ describe('getAnimation', () => {

expect(animation).toEqual(dotLottieLottie1 as AnimationData);
});

it('returns inlined images within the animation', async () => {
const manifest = await getManifest(bullAnimation);

const animationId = manifest?.animations[0]?.id || '';

const animation = await getAnimation(bullAnimation, animationId, { inlineAssets: true });

expect(JSON.stringify(animation?.assets)).toEqual(JSON.stringify(bullJson.assets));
});
});

describe('getTheme', () => {
Expand Down

0 comments on commit 2fac1b7

Please sign in to comment.