Files
o2o-weather/admin/src/cards/share.ts
T

27 lines
1.0 KiB
TypeScript

import type { Copy } from './templates'
/** 고친 문구를 주소에 싣는 꼴 (/render?copy=...). npm run card --copy 도 같은 꼴을 쓴다 */
export function encodeCopy(copy: Copy) {
const bytes = new TextEncoder().encode(JSON.stringify(copy))
return btoa(String.fromCharCode(...bytes)).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
export function decodeCopy(value: string | null): Copy {
if (!value) return {}
try {
const bin = atob(value.replace(/-/g, '+').replace(/_/g, '/'))
return JSON.parse(new TextDecoder().decode(Uint8Array.from(bin, (c) => c.charCodeAt(0))))
} catch (e) {
console.warn('copy 값을 읽지 못해 기본 문구로 그립니다', e)
return {}
}
}
/** 이미지가 다 그려지고 글꼴이 다 받아진 뒤 */
export async function whenPainted(root: HTMLElement) {
await document.fonts.ready
await Promise.all(
Array.from(root.querySelectorAll('img'), (img) => (img.complete ? Promise.resolve() : img.decode().catch(() => undefined))),
)
}