Skip to content

Commit

Permalink
feature: ability to have multiple frames
Browse files Browse the repository at this point in the history
  • Loading branch information
marlonmarcello committed Oct 21, 2024
1 parent e8e33f7 commit 6ad0d5d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 59 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/lint-and-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Lint and Build

on:
pull_request:
types: [opened, edited, reopened, synchronize]

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
lint:
name: Lint and build
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"

- name: Install Dependencies
run: npm install

- name: Lint
run: npm run lint

- name: Typecheck
run: npm run typecheck

- name: Build
run: npm run build
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ dist-ssr
*.njsproj
*.sln
*.sw?

tsconfig.tsbuildinfo
3 changes: 0 additions & 3 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wethegit/preact-stickerbook",
"version": "4.0.7",
"version": "5.0.0",
"description": "Easily create collage apps that are accessible by default.",
"files": [
"dist/*"
Expand All @@ -23,6 +23,7 @@
"lint:scripts": "eslint --fix --ext .jsx,.js,.ts,.tsx --ignore-path .gitignore .",
"lint:styles": "stylelint src/**/*.{css,scss}",
"lint": "npm run lint:scripts && npm run lint:styles",
"typecheck": "tsc --noEmit",
"format": "prettier --write ."
},
"dependencies": {
Expand Down
14 changes: 8 additions & 6 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function App() {
setStickers,
backgrounds,
foregrounds,
frame,
frames,
onReorderSticker,
onDeleteSticker,
onPositionSticker,
Expand All @@ -43,9 +43,11 @@ export function App() {
order: 0,
},
],
initialFrame: {
image: frameImage,
},
initialFrames: [
{
image: frameImage,
},
],
initialBackgrounds: [
{
image: backgroundImage,
Expand Down Expand Up @@ -102,7 +104,7 @@ export function App() {
outputHeight: CANVAS_SIZE.height,
stickers,
backgrounds,
frame,
frames,
foregrounds,
format: "image",
})
Expand All @@ -128,7 +130,7 @@ export function App() {
outputWidth={CANVAS_SIZE.width}
outputHeight={CANVAS_SIZE.height}
backgrounds={backgrounds}
frame={frame}
frames={frames}
foregrounds={foregrounds}
>
{stickers.map((sticker) => (
Expand Down
23 changes: 14 additions & 9 deletions src/lib/helpers/exportStickerbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface ExportStickerbookOptions<T extends ExportFormat> {
*/
canvas?: HTMLCanvasElement
backgrounds?: BackgroundItem[]
frame?: Frame
frames?: Frame[]
stickers?: StickerItem[]
foregrounds?: ForegroundItem[]
/**
Expand Down Expand Up @@ -51,7 +51,7 @@ type ExportReturn<K extends ExportFormat> =
export async function exportStickerbook<T extends ExportFormat>({
canvas,
backgrounds,
frame,
frames,
stickers,
foregrounds,
outputWidth,
Expand Down Expand Up @@ -88,13 +88,18 @@ export async function exportStickerbook<T extends ExportFormat>({
}

// draw frame
if (frame && frame.image)
await coverCanvas({
ctx: outputCtx,
img: frame.image,
width: outputWidth,
height: outputHeight,
})
if (frames && frames.length) {
for (const frame of frames) {
if (!frame.image) continue

await coverCanvas({
ctx: outputCtx,
img: frame.image,
width: outputWidth,
height: outputHeight,
})
}
}

if (stickers && stickers.length > 0) {
// sort by order
Expand Down
64 changes: 36 additions & 28 deletions src/lib/stickerbook.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import styles from "./stickerbook.module.scss"
export function Stickerbook({
backgrounds = [],
foregrounds = [],
frame,
frames,
outputHeight = 500,
outputWidth = 500,
children,
Expand Down Expand Up @@ -194,33 +194,41 @@ export function Stickerbook({
}}
data-stickerbook-container
>
{backgrounds.length > 0 &&
backgrounds.map((bg, i) => {
if (!bg.image) return null

return (
<div
key={i}
role="img"
className={styles.Stickerbook__background}
style={{
backgroundImage: `url(${bg.image})`,
...backgroundStyles[i],
}}
aria-label={bg.alt || ""}
data-stickerbook-background={i}
/>
)
})}

{frame && frame.image && (
<img
src={frame.image}
alt={frame.alt || ""}
className={styles.Stickerbook__frame}
data-stickerbook-frame
/>
)}
{backgrounds.length > 0
? backgrounds.map((bg, i) => {
if (!bg.image) return null

return (
<div
key={i}
role="img"
className={styles.Stickerbook__background}
style={{
backgroundImage: `url(${bg.image})`,
...backgroundStyles[i],
}}
aria-label={bg.alt || ""}
data-stickerbook-background={i}
/>
)
})
: null}

{frames && frames?.length > 0
? frames.map(({ image, alt }, i) => {
if (!image) return null

return (
<img
key={`frame-${image}-${i}`}
src={image}
alt={alt || ""}
className={styles.Stickerbook__frame}
data-stickerbook-frame
/>
)
})
: null}

<StickerbookContext.Provider
value={{
Expand Down
16 changes: 8 additions & 8 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ComponentChildren } from "preact"
import type { Vec2 } from "wtc-math"

import { EXPORT_FORMATS, ORDER_DIRECTIONS, OVERLAY_TYPES } from "./helpers"
import { StateUpdater } from "preact/hooks"
import type { Dispatch, StateUpdater } from "preact/hooks"

export interface StickerItem {
/**
Expand Down Expand Up @@ -69,7 +69,7 @@ export interface StickerbookProps {
/**
* The `frame` will appear on top of all the `background`s but behind all the `Sticker`s. Useful for borders.
*/
frame?: Frame
frames?: Frame[]
/**
* The height of your artboard.
*/
Expand Down Expand Up @@ -177,19 +177,19 @@ export interface StickerProps extends StickerItem {
export interface UseStickerbookProps {
initialStickers?: StickerItem[]
initialBackgrounds?: BackgroundItem[]
initialFrame?: Frame
initialFrames?: Frame[]
initialForegrounds?: ForegroundItem[]
}

export interface UseStickerbookReturn {
stickers: StickerItem[]
setStickers: StateUpdater<StickerItem[]>
setStickers: Dispatch<StateUpdater<StickerItem[]>>
backgrounds: BackgroundItem[]
setBackgrounds: StateUpdater<BackgroundItem[]>
frame: Frame | undefined
setFrame: StateUpdater<Overlay | undefined>
setBackgrounds: Dispatch<StateUpdater<BackgroundItem[]>>
frames: Frame[]
setFrames: Dispatch<StateUpdater<Frame[]>>
foregrounds: ForegroundItem[]
setForegrounds: StateUpdater<ForegroundItem[]>
setForegrounds: Dispatch<StateUpdater<ForegroundItem[]>>

onReorderSticker: OnReorderHandler
onDeleteSticker: OnDeleteHandler
Expand Down
8 changes: 4 additions & 4 deletions src/lib/use-stickerbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export function useStickerbook({
initialStickers = [],
initialBackgrounds = [],
initialForegrounds = [],
initialFrame,
initialFrames = [],
}: UseStickerbookProps): UseStickerbookReturn {
const [stickers, setStickers] = useState<StickerItem[]>(initialStickers)

const [backgrounds, setBackgrounds] =
useState<BackgroundItem[]>(initialBackgrounds)

const [frame, setFrame] = useState<Frame | undefined>(initialFrame)
const [frames, setFrames] = useState<Frame[]>(initialFrames)

const [foregrounds, setForegrounds] =
useState<ForegroundItem[]>(initialForegrounds)
Expand Down Expand Up @@ -78,8 +78,8 @@ export function useStickerbook({
setStickers,
backgrounds,
setBackgrounds,
frame,
setFrame,
frames,
setFrames,
foregrounds,
setForegrounds,

Expand Down

0 comments on commit 6ad0d5d

Please sign in to comment.