Skip to content

Commit

Permalink
fix(pdf): build images and supported extensions default empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Novout committed Nov 27, 2021
1 parent 3c93051 commit d6aa96f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/use/pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export const usePDF = () => {

const image = (entity: Entity) => {
if (entity.external?.image?.alignment === 'full') {
if (entity.raw.includes('svg')) {
if (entity.external.image.name.includes('svg')) {
return {
svg: entity.raw,
width:
Expand Down Expand Up @@ -161,7 +161,7 @@ export const usePDF = () => {
],
}
} else {
if (entity.raw.includes('svg')) {
if (entity.external?.image?.name.includes('svg')) {
return {
svg: entity.raw,
width: entity.external?.image?.size.width,
Expand Down
20 changes: 20 additions & 0 deletions src/use/raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,12 @@ export const useRaw = () => {

const make = () => {
const image = (entity: Entity) => {
if (!support().images(entity.external?.image?.name as string)) {
return `<div class="flex wb-text text-xl items-end w-full justify-center py-5">
<svg id="unsupported-extension-image" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="h-7 w-7" preserveAspectRatio="xMidYMid meet" viewBox="0 0 20 20"><g stroke-width="1.5" fill="none"><path d="M12 8v4" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M12 16.01l.01-.011" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9 3H4v3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M4 11v2" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M20 11v2" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M15 3h5v3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M9 21H4v-3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path><path d="M15 21h5v-3" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"></path></g></svg>
</div>`
}

return `<div class="flex wb-text text-xl items-end w-full justify-center py-5">
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down Expand Up @@ -658,6 +664,19 @@ export const useRaw = () => {
})
}

const support = () => {
const images = (file: string) => {
return (
file.includes('svg') ||
file.includes('png') ||
file.includes('jpeg') ||
file.includes('jpg')
)
}

return { images }
}

return {
html,
caret,
Expand All @@ -668,6 +687,7 @@ export const useRaw = () => {
style,
normalize,
copy,
support,
}
}

Expand Down
23 changes: 23 additions & 0 deletions test/raw/raw_v2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,27 @@ describe('Raw v2 - Make', () => {
})
).toContain(file_name)
})

it('should not create image because unsupported extension', () => {
const file_name = 'foo.bw'

expect(
make.image({
type: 'image',
raw: 'dsaKDSADaskASHDGYasdahbdasnASD...',
createdAt: format.actually(),
updatedAt: format.actually(),
external: {
image: {
name: file_name,
size: {
width: 0,
height: 0,
},
alignment: 'full',
},
},
})
).toContain('id="unsupported-extension-image"')
})
})

0 comments on commit d6aa96f

Please sign in to comment.