Skip to content

Commit

Permalink
✨Introduce API to create blocks
Browse files Browse the repository at this point in the history
This commit introduces functions to create blocks more conveniently. The
functions `text()` and `image()` can be used to create blocks that
contain text or images. The functions `columns()` and `rows()` can be
used to create blocks that contain other blocks.

Using these functions enables better suggestions in the editor. Since
they return objects with a specific type, less type annotations are
required in the code.

For example, the following line of code

```ts
const block: Block = {
  rows: [
    { text: 'Hello World!', { fontWeight: 'bold' } },
    { image: 'liberty.jpg', height: 55, imageAlign: 'left' },
  ],
  margin: { x: 75, y: 10 },
};
```

can now be replaced with:

```ts
const block = rows(
  [
    text('Hello World!', { fontWeight: 'bold' }),
    image('liberty.jpg', { height: 55, imageAlign: 'left' }),
  ],
  { margin: { x: 75, y: 10 } },
);
```
  • Loading branch information
ralfstx committed Oct 5, 2024
1 parent 189fc3b commit 7ceb5cb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Minimum requirements bumped to Node 20 and npm 10.
- The function `span()` to create text spans with less code and better
tool support.

- The functions `text()`, `image()`, `rows()`, and `columns()` to create
blocks with less code and better tool support.

### Deprecated

- `TextAttrs` in favor of `TextProps`.
Expand Down
45 changes: 45 additions & 0 deletions src/api/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ export type TextBlock = {
} & TextProps &
BlockProps;

/**
* Creates a text block. The text can contain nested text spans to apply
* different text properties to different parts of the text. Use
* `span()` to create text spans.
*
* @param text The text to display in this block.
* @param props Optional properties for the block.
*/
export function text(
text: string | TextSpan | (string | TextSpan)[],
props?: Omit<TextBlock, 'text'>,
): TextBlock {
return { ...props, text };
}

/**
* A block that contains an image.
*/
Expand All @@ -49,6 +64,16 @@ export type ImageBlock = {
imageAlign?: Alignment;
} & BlockProps;

/**
* Creates a block that contains an image.
*
* @param image The name or path of an image to display in this block.
* @param props Optional properties for the block.
*/
export function image(image: string, props?: Omit<ImageBlock, 'image'>): ImageBlock {
return { ...props, image };
}

/**
* A block that contains other blocks arranged horizontally.
*/
Expand All @@ -60,6 +85,16 @@ export type ColumnsBlock = {
} & TextProps &
BlockProps;

/**
* Creates a block that contains other blocks arranged horizontally.
*
* @param columns Content blocks to arrange horizontally.
* @param props Optional properties for the block.
*/
export function columns(columns: Block[], props?: Omit<ColumnsBlock, 'columns'>): ColumnsBlock {
return { ...props, columns };
}

/**
* A block that contains other blocks arranged vertically.
*/
Expand All @@ -83,6 +118,16 @@ export type RowsBlock = {
} & TextProps &
BlockProps;

/**
* Creates a block that contains other blocks arranged vertically.
*
* @param rows Content blocks to arrange vertically.
* @param props Optional properties for the block.
*/
export function rows(rows: Block[], props?: Omit<RowsBlock, 'rows'>): RowsBlock {
return { ...props, rows };
}

/**
* A block that doesn't contain any content. It can be used to include
* graphics.
Expand Down

0 comments on commit 7ceb5cb

Please sign in to comment.