Skip to content

Commit

Permalink
feat: the left and top support passing function
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Sep 20, 2023
1 parent 9efd7d1 commit 711e58f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
27 changes: 17 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { isNonEmptyArray, withResolvers } from '@inottn/fp-utils';
import { binarySearch, calculateLeftOffset, mergePosition } from './utils';
import {
binarySearch,
calculateLeftOffset,
mergePosition,
normalizeConfig,
} from './utils';
import type {
Canvas,
Config,
ContainerConfig,
Element,
ElementConfig,
ExportOptions,
ImageConfig,
NormalizedConfig,
Options,
Radius,
TextConfig,
Expand Down Expand Up @@ -51,19 +57,20 @@ export class MiniPoster {
});
}

async draw(data: Element | Element[]) {
async draw(data: ElementConfig | ElementConfig[]) {
if (Array.isArray(data)) {
for (const item of data) {
await this.draw(item);
}
} else {
if (data.type === 'container') await this.renderContainer(data);
if (data.type === 'image') await this.renderImage(data);
if (data.type === 'text') await this.renderText(data);
if (data.type === 'container')
await this.renderContainer(normalizeConfig(data));
if (data.type === 'image') await this.renderImage(normalizeConfig(data));
if (data.type === 'text') await this.renderText(normalizeConfig(data));
}
}

async renderContainer(data: ContainerConfig) {
async renderContainer(data: NormalizedConfig<ContainerConfig>) {
const { context } = this;
const {
left,
Expand Down Expand Up @@ -103,7 +110,7 @@ export class MiniPoster {
}
}

async renderImage(data: ImageConfig) {
async renderImage(data: NormalizedConfig<ImageConfig>) {
const { context } = this;
const { src, backgroundColor, borderRadius = 0, objectFit = 'fill' } = data;
const [img, loadPromise] = this.images.get(src);
Expand Down Expand Up @@ -146,7 +153,7 @@ export class MiniPoster {
context.restore();
}

async renderText(data: TextConfig) {
async renderText(data: NormalizedConfig<TextConfig>) {
const { context } = this;
const {
id,
Expand Down Expand Up @@ -283,7 +290,7 @@ export class MiniPoster {
context.restore();
}

loadAssets(data: Element[]) {
loadAssets(data: ElementConfig[]) {
data.forEach((item) => {
const { type } = item;

Expand Down
14 changes: 11 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ export type Radius =
| [number, number, number]
| [number, number, number, number];

export type Element = ContainerConfig | ImageConfig | TextConfig;
export type ElementConfig = ContainerConfig | ImageConfig | TextConfig;

export type Config = {
backgroundColor?: string;
borderRadius?: Radius;
overflow?: 'visible' | 'hidden';
children?: Element[];
children?: ElementConfig[];
};

export type PositionConfig = {
left: number | (() => number);
top: number | (() => number);
};

type NormalizedPositionConfig = {
left: number;
top: number;
};
Expand All @@ -55,13 +60,16 @@ export type TextDecoration = 'none' | 'line-through';

export type ObjectFit = 'fill' | 'contain' | 'cover';

export type NormalizedConfig<Config> = Omit<Config, 'left' | 'top'> &
NormalizedPositionConfig;

export type ContainerConfig = PositionConfig &
SizeConfig & {
type: 'container';
backgroundColor?: string;
borderRadius?: Radius;
overflow?: 'visible' | 'hidden';
children?: Element[];
children?: ElementConfig[];
};

export type ImageConfig = PositionConfig &
Expand Down
26 changes: 21 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { isUndefined } from '@inottn/fp-utils';
import { PositionConfig, TextAlign } from './types';
import { isFunction, isUndefined } from '@inottn/fp-utils';
import {
ElementConfig,
NormalizedConfig,
PositionConfig,
TextAlign,
} from './types';

type BinarySearchValidate = (index: number) => boolean;

Expand Down Expand Up @@ -50,13 +55,24 @@ export const calculateLeftOffset = function ({

export const mergePosition = function <T extends PositionConfig>(
value1: T,
value2?: PositionConfig,
value2?: NormalizedConfig<PositionConfig>,
): T {
if (isUndefined(value2)) return value1;

const left = isFunction(value1.left) ? value1.left() : value1.left;
const top = isFunction(value1.top) ? value1.top() : value1.top;

return {
...value1,
left: value1.left + value2.left,
top: value1.top + value2.top,
left: value2.left + left,
top: value2.top + top,
};
};

export const normalizeConfig = function <T extends ElementConfig>(config: T) {
const normalizedConfig = { ...config };
if (isFunction(config.left)) normalizedConfig.left = config.left();
if (isFunction(config.top)) normalizedConfig.top = config.top();

return normalizedConfig as NormalizedConfig<T>;
};

0 comments on commit 711e58f

Please sign in to comment.