-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(qr): export top-level headless helper, img now render base64 png…
… (previously base64 svg)
- Loading branch information
1 parent
7282584
commit 0c8dc62
Showing
19 changed files
with
306 additions
and
65 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@svelte-put/qr": minor | ||
--- | ||
|
||
now render `img` as base64 png (previously base64 svg) |
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,5 @@ | ||
--- | ||
"@svelte-put/qr": minor | ||
--- | ||
|
||
export top-level headless helpers `createQrSvgString` and `createQrSvgDataUrl` for svg (browser & server), and `createQrPngDataUrl` for png (browser only) |
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 |
---|---|---|
@@ -1,53 +1,80 @@ | ||
<script> | ||
import { createEventDispatcher, onMount } from 'svelte'; | ||
import { createBase64Image } from '../qr/index.js'; | ||
import { createQrSvgDataUrl, createQrPngDataUrl } from '../qr/index.js'; | ||
import { DEFAULT_FILLS, toDataURL } from './index.js'; | ||
import { toDataURL } from './index.js'; | ||
$: ({ data, anchorInnerFill, anchorOuterFill, logo, logoRatio, margin, moduleFill, shape, errorCorrectionLevel, typeNumber, ...rest } = /** @type {import('../qr/types.js').QRConfig} */($$props)); | ||
// FIXME: svelte v5 for better props declaration & dependency tracking here | ||
// FIXME: svelte v5 for better dependency tracking here | ||
let logoData = logo; | ||
$: fetchLogo(logo); | ||
/** @type {string} */ | ||
export let data; | ||
/** @type {number | undefined} */ | ||
export let margin = undefined; | ||
/** @type {import('./QR.svelte.js').QRProps['shape'] | undefined} */ | ||
export let shape = undefined; | ||
$: src = createBase64Image( | ||
/** @type {import('./QR.svelte').QRProps} */ ({ | ||
...DEFAULT_FILLS, | ||
/** @type {string | undefined} */ | ||
export let logo = undefined; | ||
/** @type {number | undefined} */ | ||
export let logoRatio = undefined; | ||
/** @type {string | undefined} */ | ||
export let moduleFill = undefined; | ||
/** @type {string | undefined} */ | ||
export let anchorInnerFill = undefined; | ||
/** @type {string | undefined} */ | ||
export let anchorOuterFill = undefined; | ||
/** @type {string | undefined} */ | ||
export let backgroundFill = undefined; | ||
/** @type {import('./QR.svelte.js').QRProps['typeNumber'] | undefined} */ | ||
export let typeNumber = undefined; | ||
/** @type {import('./QR.svelte.js').QRProps['errorCorrectionLevel'] | undefined} */ | ||
export let errorCorrectionLevel = undefined; | ||
function getConfig() { | ||
return { | ||
data, | ||
anchorInnerFill, | ||
anchorOuterFill, | ||
logo: logoData, | ||
logoRatio, | ||
margin, | ||
moduleFill, | ||
shape, | ||
typeNumber, | ||
errorCorrectionLevel, | ||
}), | ||
); | ||
}; | ||
} | ||
let src = createQrSvgDataUrl(getConfig()); | ||
/** @type {SVGElement | HTMLImageElement}*/ | ||
let element; | ||
/** @type {ReturnType<typeof createEventDispatcher<{ 'qr:init': typeof element, 'qr:logofetch': string }>>}*/ | ||
const dispatch = createEventDispatcher(); | ||
onMount(async () => { | ||
if (element) dispatch('qr:init', element); | ||
}); | ||
/** | ||
* @param {string} logo | ||
*/ | ||
async function fetchLogo(logo) { | ||
/** @type {string | undefined}*/ | ||
let logoData = undefined; | ||
if (logo?.startsWith('http')) { | ||
logoData = await toDataURL(logo); | ||
dispatch('qr:logofetch', logo); | ||
} | ||
} | ||
const base64png = await createQrPngDataUrl({ | ||
...getConfig(), | ||
backgroundFill, | ||
width: $$restProps.width, | ||
height: $$restProps.height, | ||
logo: logoData, | ||
}); | ||
src = base64png; | ||
}); | ||
</script> | ||
{#key src} | ||
<slot {src}> | ||
<img {src} alt={$$props.data ?? $$restProps.alt} {...rest} bind:this={element} /> | ||
<img {src} alt={$$props.data ?? $$restProps.alt} {...$$restProps} bind:this={element} /> | ||
</slot> | ||
{/key} |
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
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
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.