Skip to content

Commit

Permalink
feat: add the position meta-directive
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasKruckenberg committed Mar 8, 2021
1 parent 1c81807 commit 2ee5362
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ ___

When both a `width` and `height` are provided, this directive can be used to specify the method by which the image should **fit**.

___

### Position
**Keyword**: `position`
**Type**: _top_ \| _right top_ \| _right_ \| _right bottom_ \| _bottom_ \| _left bottom_ \| _left_ \| _left top_ \|
_north_ \| _northeast_ \| _east_ \| _southeast_ \| _south_ \| _southwest_ \| _west_ \| _northwest_ \| _center_ \| _centre_ \|
_cover_ \| _entropy_ \| _attention_

When both `width` and `height` are provided AND the fit is one of `fit` of `cover` or `contain`,
this directive can be used to set the position of the image.

See sharps [resize options](https://sharp.pixelplumbing.com/api-resize#resize) for a detailed explanation of each.

___
### Rotate
**Keyword**: `rotate`<br>
Expand Down
14 changes: 14 additions & 0 deletions src/directives/position.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DirectiveContext, DirectiveOptions } from "../types";

export const position = ({ position }: DirectiveOptions, ctx: DirectiveContext) => {
if (!position || !positionValues.includes(position)) return null

ctx.useParam('position')
ctx.setMetadata('position', position)

return position
}

const positionValues = ['top', 'right top', 'right', 'right bottom', 'bottom', 'left bottom', 'left', 'left top',
'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'northwest', 'center', 'centre',
'cover', 'entropy', 'attention']

0 comments on commit 2ee5362

Please sign in to comment.