Skip to content

Commit

Permalink
Merge pull request #54 from pkolt/53-autofill-name-for-import-from-co…
Browse files Browse the repository at this point in the history
…llections

53-autofill-name-for-import-from-collections
  • Loading branch information
pkolt authored Jul 6, 2024
2 parents ff14a81 + 1f398f9 commit 8a0d373
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ npm start

## FAQ

### Why is my image distorted?
### Why my image show wrong?

![Distorted image](./docs/distorted_image.jpg)

For some LCD displays such as the SSD1306, the image width must be a multiple of 8. If this requirement is not met, you will see a distorted image when displayed.
For some LCD displays such as the SSD1306, the image width must be a multiple of 8. If this requirement is not met, you will see a wrong image when displayed.

Just use an image width that is a multiple of 8.

Expand Down
10 changes: 6 additions & 4 deletions src/pages/ImportFromImage/ImportForm/hooks/useImageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { capitalize } from '@/utils/string';
import { useEffect } from 'react';

interface ImageUrlHookParams {
imageUrl?: string;
setImage: (value: FileList) => void;
setImage: (files: FileList, name: string) => void;
}

export const useImageUrl = ({ imageUrl, setImage }: ImageUrlHookParams): void => {
Expand All @@ -13,9 +14,10 @@ export const useImageUrl = ({ imageUrl, setImage }: ImageUrlHookParams): void =>
const blob = await res.blob();
const dataTransfer = new DataTransfer();
const mimeType = 'image/svg+xml';
const name = imageUrl.split('/').at(-1) ?? '';
dataTransfer.items.add(new File([blob], name, { type: mimeType }));
setImage(dataTransfer.files);
const filename = imageUrl.split('/').at(-1) ?? '';
const name = capitalize(filename.split('.')[0].replaceAll('-', ' '));
dataTransfer.items.add(new File([blob], filename, { type: mimeType }));
setImage(dataTransfer.files, name);
})();
}
}, [imageUrl, setImage]);
Expand Down
8 changes: 5 additions & 3 deletions src/pages/ImportFromImage/ImportForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,18 @@ export const ImportForm = ({ setBitmap, onSubmit, imageUrl }: ImportFormProps) =
}, [onReset, reset]);

const setImage = useCallback(
(value: FileList) => {
(files: FileList, name: string) => {
const elem = document.getElementById(ID_FILE_INPUT);
if (elem) {
setValue('files', value);
(elem as HTMLInputElement).files = value;
setValue('files', files);
(elem as HTMLInputElement).files = files;
setValue('name', name);
setFocus('name');
}
},
[setValue, setFocus],
);

useImageUrl({ imageUrl, setImage });

useEffect(() => {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/string.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { test, expect } from '@/test-utils';
import { capitalize } from './string';

test('capitalize', () => {
expect(capitalize('foo')).toBe('Foo');
});

test('capitalize empty', () => {
expect(capitalize('')).toBe('');
});
4 changes: 4 additions & 0 deletions src/utils/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const capitalize = (value: string) => {
const firstLetter: string = value[0] ?? '';
return firstLetter.toUpperCase() + value.slice(1);
};

0 comments on commit 8a0d373

Please sign in to comment.