-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1181fa6
commit 3de8416
Showing
51 changed files
with
1,380 additions
and
594 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ logs | |
|
||
.wrangler | ||
coverage | ||
cache | ||
cache | ||
.zed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
packages/app-client/src/modules/files/files.models.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { values } from 'lodash-es'; | ||
import { describe, expect, test } from 'vitest'; | ||
import { icons as tablerIconSet } from '@iconify-json/tabler'; | ||
import { getFileIcon, iconByFileType } from './files.models'; | ||
|
||
describe('files models', () => { | ||
describe('iconByFileType', () => { | ||
const icons = values(iconByFileType); | ||
|
||
test('they must at least have the default icon', () => { | ||
expect(iconByFileType['*']).toBeDefined(); | ||
}); | ||
|
||
test('all the icons should be from tabler icon set', () => { | ||
for (const icon of icons) { | ||
expect(icon).to.match(/^i-tabler-/, `Icon ${icon} is not from tabler icon set`); | ||
} | ||
}); | ||
|
||
test('icons should not contain any spaces', () => { | ||
for (const icon of icons) { | ||
expect(icon).not.to.match(/\s/, `Icon ${icon} contains spaces`); | ||
} | ||
}); | ||
|
||
test('the icons used for showing file types should exists with current iconify configuration', () => { | ||
for (const icon of icons) { | ||
const iconName = icon.replace('i-tabler-', ''); | ||
const iconData = tablerIconSet.icons[iconName] ?? tablerIconSet.aliases?.[iconName]; | ||
|
||
expect(iconData).to.not.eql(undefined, `Icon ${icon} does not exist in tabler icon set`); | ||
} | ||
}); | ||
}); | ||
|
||
describe('getFileIcon', () => { | ||
test('a file icon is selected based on the file type', () => { | ||
const file = new File([''], 'test.txt', { type: 'text/plain' }); | ||
const iconsMap = { | ||
'*': 'i-tabler-file', | ||
'text/plain': 'i-tabler-file-text', | ||
}; | ||
const icon = getFileIcon({ file, iconsMap }); | ||
|
||
expect(icon).to.eql('i-tabler-file-text'); | ||
}); | ||
|
||
test('if a file type is not associated with an icon, the default icon is used', () => { | ||
const file = new File([''], 'test.txt', { type: 'text/html' }); | ||
const iconsMap = { | ||
'*': 'i-tabler-file', | ||
'text/plain': 'i-tabler-file-text', | ||
}; | ||
const icon = getFileIcon({ file, iconsMap }); | ||
|
||
expect(icon).to.eql('i-tabler-file'); | ||
}); | ||
|
||
test('a file icon can be selected based on the file type group', () => { | ||
const file = new File([''], 'test.html', { type: 'text/html' }); | ||
const iconsMap = { | ||
'*': 'i-tabler-file', | ||
'text': 'i-tabler-file-text', | ||
}; | ||
const icon = getFileIcon({ file, iconsMap }); | ||
|
||
expect(icon).to.eql('i-tabler-file-text'); | ||
}); | ||
|
||
test('when an icon is defined for both the whole type and the group type, the file type icon is used', () => { | ||
const file = new File([''], 'test.html', { type: 'text/html' }); | ||
const iconsMap = { | ||
'*': 'i-tabler-file', | ||
'text': 'i-tabler-file-text', | ||
'text/html': 'i-tabler-file-type-html', | ||
}; | ||
const icon = getFileIcon({ file, iconsMap }); | ||
|
||
expect(icon).to.eql('i-tabler-file-type-html'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export { getFileIcon }; | ||
|
||
// Available icons : | ||
// i-tabler-file-3d i-tabler-file-ai i-tabler-file-alert i-tabler-file-analytics i-tabler-file-arrow-left i-tabler-file-arrow-right i-tabler-file-barcode i-tabler-file-bitcoin i-tabler-file-broken i-tabler-file-certificate i-tabler-file-chart i-tabler-file-check i-tabler-file-code i-tabler-file-code-2 i-tabler-file-cv i-tabler-file-database i-tabler-file-delta i-tabler-file-description i-tabler-file-diff i-tabler-file-digit i-tabler-file-dislike i-tabler-file-dollar i-tabler-file-dots i-tabler-file-download i-tabler-file-euro i-tabler-file-excel i-tabler-file-export i-tabler-file-filled i-tabler-file-function i-tabler-file-horizontal i-tabler-file-import i-tabler-file-infinity i-tabler-file-info i-tabler-file-invoice i-tabler-file-isr i-tabler-file-lambda i-tabler-file-like i-tabler-file-minus i-tabler-file-music i-tabler-file-neutral i-tabler-file-off i-tabler-file-orientation i-tabler-file-pencil i-tabler-file-percent i-tabler-file-phone i-tabler-file-plus i-tabler-file-power i-tabler-file-report i-tabler-file-rss i-tabler-file-sad i-tabler-file-scissors i-tabler-file-search i-tabler-file-settings i-tabler-file-shredder i-tabler-file-signal i-tabler-file-smile i-tabler-file-spreadsheet i-tabler-file-stack i-tabler-file-star i-tabler-file-symlink i-tabler-file-text i-tabler-file-text-ai i-tabler-file-time i-tabler-file-type-bmp i-tabler-file-type-css i-tabler-file-type-csv i-tabler-file-type-doc i-tabler-file-type-docx i-tabler-file-type-html i-tabler-file-type-jpg i-tabler-file-type-js i-tabler-file-type-jsx i-tabler-file-type-pdf i-tabler-file-type-php i-tabler-file-type-png i-tabler-file-type-ppt i-tabler-file-type-rs i-tabler-file-type-sql i-tabler-file-type-svg i-tabler-file-type-ts i-tabler-file-type-tsx i-tabler-file-type-txt i-tabler-file-type-vue i-tabler-file-type-xls i-tabler-file-type-xml i-tabler-file-type-zip i-tabler-file-typography i-tabler-file-unknown i-tabler-file-upload i-tabler-file-vector i-tabler-file-word i-tabler-file-x i-tabler-file-x-filled i-tabler-file-zip | ||
|
||
export const iconByFileType = { | ||
'*': 'i-tabler-file', | ||
'image': 'i-tabler-photo', | ||
'video': 'i-tabler-video', | ||
'audio': 'i-tabler-file-music', | ||
'application': 'i-tabler-file-code', | ||
'application/pdf': 'i-tabler-file-type-pdf', | ||
'application/zip': 'i-tabler-file-zip', | ||
'application/vnd.ms-excel': 'i-tabler-file-excel', | ||
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'i-tabler-file-excel', | ||
'application/msword': 'i-tabler-file-word', | ||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'i-tabler-file-word', | ||
'application/json': 'i-tabler-file-code', | ||
'application/xml': 'i-tabler-file-code', | ||
'application/javascript': 'i-tabler-file-type-js', | ||
'application/typescript': 'i-tabler-file-type-ts', | ||
'application/vnd.ms-powerpoint': 'i-tabler-file-type-ppt', | ||
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'i-tabler-file-type-ppt', | ||
'text/plain': 'i-tabler-file-text', | ||
'text/html': 'i-tabler-file-type-html', | ||
'text/css': 'i-tabler-file-type-css', | ||
'text/csv': 'i-tabler-file-type-csv', | ||
'text/xml': 'i-tabler-file-type-xml', | ||
'text/javascript': 'i-tabler-file-type-js', | ||
'text/typescript': 'i-tabler-file-type-ts', | ||
}; | ||
|
||
type FileTypes = keyof typeof iconByFileType; | ||
|
||
function getFileIcon({ file, iconsMap = iconByFileType }: { file: File; iconsMap?: Record<string, string> & { '*': string } }): string { | ||
const fileType = file.type; | ||
const fileTypeGroup = fileType?.split('/')[0]; | ||
|
||
const icon = iconsMap[fileType as FileTypes] ?? iconsMap[fileTypeGroup as FileTypes] ?? iconsMap['*']; | ||
|
||
return icon; | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/app-client/src/modules/notes/components/file-uploader.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { type ComponentProps, type ParentComponent, createSignal, splitProps } from 'solid-js'; | ||
import { Button } from '@/modules/ui/components/button'; | ||
|
||
export const FileUploaderButton: ParentComponent<{ | ||
onFileUpload?: (args: { file: File }) => void; | ||
onFilesUpload?: (args: { files: File[] }) => void; | ||
multiple?: boolean; | ||
} & ComponentProps<typeof Button>> = (props) => { | ||
const [fileInputRef, setFileInputRef] = createSignal<HTMLInputElement | null>(null); | ||
const [local, rest] = splitProps(props, ['onFileUpload', 'multiple', 'onFilesUpload']); | ||
|
||
const onFileChange = (e: Event) => { | ||
const target = e.target as HTMLInputElement; | ||
if (!target.files) { | ||
return; | ||
} | ||
|
||
const files = Array.from(target.files); | ||
|
||
local.onFilesUpload?.({ files }); | ||
|
||
for (const file of files) { | ||
local.onFileUpload?.({ file }); | ||
} | ||
}; | ||
|
||
const onButtonClick = () => { | ||
fileInputRef()?.click(); | ||
}; | ||
|
||
return ( | ||
<> | ||
<input | ||
type="file" | ||
class="hidden" | ||
onChange={onFileChange} | ||
ref={setFileInputRef} | ||
multiple={local.multiple} | ||
/> | ||
<Button onClick={onButtonClick} {...rest}> | ||
{props.children ?? 'Upload File'} | ||
</Button> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.