Skip to content

Commit

Permalink
feat: add ImageRun
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Aug 31, 2023
1 parent 8f11a63 commit 5e5b114
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 18 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,12 @@ See which features are covered in the dedicated [doc file](https://github.com/Em
* 🚧 Sections
* ✅ Paragraph
* 🚧 Text
* Images
* 🚧 Images (_Missing correct modelization of the `ImageRun.options.data` type_)
* ✅ Headers & Footers
* ❌ Bullet Points
* ✅ Hyperlinks
* ❌ Numbering
* 🚧 Tables
* ✅ Table
* ✅ TableCell
* ✅ TableRow
* ❌ TableColumn
* ✅ Tables
* ❌ Tabs
* ❌ Table Of Contents
* ✅ Page Numbers
Expand Down
35 changes: 35 additions & 0 deletions examples/Example.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
open Util

let table = Table.make({
rows: [
TableRow.make({
children: [
TableCell.make({
children: [
Paragraph.make'({
children: [
ImageRun.make({
data: NodeJs.Fs.readFileSync("./images/1.jpg"),
transformation: {
width: 100.0,
height: 100.0,
},
}),
],
})->Types.ParagraphOrTable.fromParagraph,
],
verticalAlign: #center,
}),
TableCell.make({
children: [
Paragraph.make'({
text: "Hello",
heading: #Heading1,
})->Types.ParagraphOrTable.fromParagraph,
],
verticalAlign: #center,
}),
],
}),
],
})
19 changes: 19 additions & 0 deletions src/Floating.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type margins = {
bottom?: float,
left?: float,
right?: float,
top?: float,
}

/** @see https://docx.js.org/api/interfaces/IFloating.html */
type t = {
allowOverlap?: bool,
behindDocument?: bool,
horizontalPosition?: HorizontalPositionOptions.t,
layoutInCell?: bool,
lockAnchor?: bool,
margins?: margins,
verticalPosition: VerticalPositionOptions.t,
wrap?: TextWrapping.t,
zIndex?: int,
}
25 changes: 25 additions & 0 deletions src/HorizontalPositionOptions.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type horizontalPositionAlign = [
| #center
| #inside
| #left
| #outside
| #right
]

type horizontalPositionRelativeFrom = [
| #character
| #column
| #insideMargin
| #leftMargin
| #margin
| #outsideMargin
| #page
| #rightMargin
]

/** @see https://docx.js.org/api/interfaces/IHorizontalPositionOptions.html */
type t = {
align?: horizontalPositionAlign,
offset?: float,
relative?: horizontalPositionRelativeFrom,
}
20 changes: 20 additions & 0 deletions src/ImageRun.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type docPropertiesOptions = {
name: string,
dscription: string,
title: string,
}

type options<'a> = {
/**
* FIXME: should be equivalent to: string | Buffer | Uint8Array | ArrayBuffer
*
* @see https://docx.js.org/api/interfaces/IImageOptions.html#data
*/
data: 'a,
transformation: MediaTransformation.t,
altText?: docPropertiesOptions,
floating?: Floating.t,
}

@module("docx") @new
external make: options<'a> => ParagraphChild.t = "ImageRun"
11 changes: 11 additions & 0 deletions src/MediaTransformation.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @see https://docx.js.org/api/interfaces/IMediaTransformation.html */
type rec t = {
height: float,
width: float,
flip?: flip,
rotation?: float,
}
and flip = {
horizontal?: bool,
vertical?: bool,
}
27 changes: 27 additions & 0 deletions src/TextWrapping.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type distance = {
distB?: float,
distL?: float,
distR?: float,
distT?: float,
}

type textWrappingSide = [
| #bothSides
| #largest
| #left
| #right
]

/** @see https://docx.js.org/api/enums/TextWrappingType.html */
type textWrappingType =
| None
| Square
| Tight
| Top_And_Bottom

/** @see https://docx.js.org/api/interfaces/ITextWrapping.html */
type t = {
margins?: distance,
side?: textWrappingSide,
@as("type") type_: textWrappingType,
}
31 changes: 19 additions & 12 deletions src/Util.res
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,42 @@ module Types = {
let make = (~negative=false, ~val: float) =>
`${negative ? "-" : ""}${val->Belt.Float.toString}%`
}
// TODO: should be more precise and model the fact that the value could be a 'positive' universal measure
module NumberOrUniversalMeasure = {

module Number = {
type t

external fromFloat: float => t = "%identity"
external fromString: string => t = "%identity"
}

module NumberOrPositiveUniversalMeasure = {
module Bool = {
type t

external fromFloat: float => t = "%identity"
external fromString: string => t = "%identity"
external fromBool: bool => t = "%identity"
}

// TODO: should be more precise and model the fact that the value could be a 'positive' universal measure
module NumberOrUniversalMeasure = {
include Number

external fromUniversalMeasure: string => t = "%identity"
}

module NumberOrPositiveUniversalMeasure = {
include Number

external fromPositiveUniversalMeasure: string => t = "%identity"
}

module BoolOrNumberOrUniversalMeasure = {
type t
include NumberOrUniversalMeasure

external fromBool: bool => t = "%identity"
external fromFloat: float => t = "%identity"
external fromString: string => t = "%identity"
}

module NumberOrPercentageOrUniversalMeasure = {
type t
include NumberOrUniversalMeasure

external fromPercentage: Percentage.t => t = "%identity"
external fromFloat: float => t = "%identity"
external fromString: string => t = "%identity"
}

module BoolOrString = {
Expand Down
25 changes: 25 additions & 0 deletions src/VerticalPositionOptions.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
type horizontalPositionAlign = [
| #center
| #inside
| #bottom
| #outside
| #top
]

type horizontalPositionRelativeFrom = [
| #character
| #column
| #insideMargin
| #bottomMargin
| #margin
| #outsideMargin
| #page
| #topMargin
]

/** @see https://docx.js.org/api/interfaces/IHorizontalPositionOptions.html */
type t = {
align?: horizontalPositionAlign,
offset?: float,
relative?: horizontalPositionRelativeFrom,
}

0 comments on commit 5e5b114

Please sign in to comment.