Skip to content

Commit

Permalink
Add todonotes 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Tinggaard committed Nov 22, 2024
1 parent 78fedda commit cd9cae8
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/todonotes/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Jens Tinggaard

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
83 changes: 83 additions & 0 deletions packages/preview/todonotes/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Todonotes

This package is inspired by the [drafting](https://typst.app/universe/package/drafting) package, whilst trying to mimic
the [todonotes](https://ctan.org/pkg/todonotes) LaTeX package, with a todo outline,
and placement on pages with inside / outside margins.

## Usage

Import the package

```typ
#import "@preview/todonotes:0.1.0": todo, todo-outline
```

The `todo` function takes the following parameters

- `side`: `auto` | `left` | `right` - The side of the page to display the note.
Auto selects the side with the largest margin available, and right if they are equal. (**default**: `auto`)
- `font-size`: `length` - Font size of the text in the todo (**default**: `9pt`)
- `font-color`: `color` | `gradient` - Color of the font (**default**: `black`)
- `color`: `color` | `gradient` - Color of the box and line (**default**: `orange`)
- `body`: `content` - Body of the todo

The `todo-outline` function takes a single optional argument, specifying the title of the outline.
As the name suggests it prints an outline of all the todos in the document, along with a box with the color of the todo.

### `.with` Macros

Create different todos for different authors / purposes.

```typ
#set page("a6", flipped: true, margin: (x: 30mm))
#let urgent = todo.with(color: red)
#let todo-later = todo.with(color: green)
```

We can then use them like this

```typ
#lorem(20)
#todo[This is the default todo]
#lorem(10)
#urgent[Remember to change this!]
#lorem(15)
#todo-later[We should probably do X at some point...]
#lorem(20)
#todo[side: left](I am manually placed on the left)
#todo[color: blue, font-color: white](I am very customized)
```

![Custom macros](assets/todo-macros.png)

### Uneven Margins

It even works with inside / outside margins.

```typ
#set page(margin: (inside: 20mm, outside: 40mm))
#lorem(30)
#todo[Wauw, so much space! I am just soooooo long, I can't even fit on two lines!]
#lorem(3)
#todo[side: right, font-size: 7pt](I am on the right side, good thing my font is so small...)
```

![Inside / outside margins](assets/inside-outside-margin.png)

### Outline

We can also create an outline of the document, optionally supplying custom title

```typ
#todo-outline(title: "Todos")
```

![Todo outline](assets/todo-outline.png)

## Compiler Warnings

Please note, that the compiler will give a warning if 4 or more notes overlap,
due to the dynamic placement of the notes, combined with Typst's iterative typesetting.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
205 changes: 205 additions & 0 deletions packages/preview/todonotes/0.1.0/lib.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#import "@preview/t4t:0.3.2": get

#let note-descent = state("note-descent", (:))

#let todo-outline(title: "List of Todos") = context {
show outline.entry.where(
level: 1
): it => {
// box with color, followed by content
[#it.body.children.first() #it.body.children.last()]
box(width: 1fr, repeat[.])
it.page
}
// only show if there actually are any todos
if (counter(figure.where(kind: "todo")).final().first() > 0) {
page(outline(target: figure.where(kind: "todo"), title: title))
}
}

#let _get-current-descent(descents-dict, page-number: auto) = {
if page-number == auto {
page-number = descents-dict.keys().at(-1, default: "0")
} else {
page-number = str(page-number)
}
(page-number, descents-dict.at(page-number, default: (left: 0pt, right: 0pt)))
}

#let _update-descent(side, dy, anchor-y, note-rect, page-number) = {
let height = measure(note-rect).height
// let dy = measure(v(dy + height)).height + anchor-y // needed if any values or not just pt.
let dy = dy + height + anchor-y
note-descent.update(old => {
let (cnt, props) = _get-current-descent(old, page-number: page-number)
props.insert(side, dy)
old.insert(cnt, props)
old
})
}

// invisible figure, s.t. we can reference it in the outline
#let _todo-outline-entry(color, body) = hide(
box(
height: 0pt,
width: 0pt,
figure(
none,
kind: "todo",
// colored box in outline
supplement: box(fill: color, height: 11pt, width: 11pt, stroke: black + .5pt),
caption: get.text(body),
outlined: true,
)
)
)

#let _get-margin(side: auto, page-num) = {
let w = if page.flipped {
page.height
} else {
page.width
}
let default-margin = 2.5 / 21 * w
let defaults = (
"left": default-margin,
"right": default-margin
)
// no margin is specified at all
if (page.margin == auto) {
return defaults
}

if ("right" in page.margin.keys() and page.margin.right != auto) {
defaults.at("right") = page.margin.right
}
if ("left" in page.margin.keys() and page.margin.left != auto) {
defaults.at("left") = page.margin.left
}
if ("inside" in page.margin.keys() and page.margin.inside != auto) {
if (calc.odd(page-num)) {
defaults.at("left") = page.margin.inside
} else {
defaults.at("right") = page.margin.inside
}
}
if ("outside" in page.margin.keys() and page.margin.outside != auto) {
if (calc.even(page-num)) {
defaults.at("left") = page.margin.outside
} else {
defaults.at("right") = page.margin.outside
}
}
return defaults
}

#let _todo-right(body, dy, anchor-x, anchor-y, font-size, font-color, color) = {
let w = if page.flipped {
page.height
} else {
page.width
}
let m = _get-margin(side: right, here().page()).right
let dist-to-margin = w - anchor-x - m

let path-pts = (
(0pt, -.2em),
(0pt, 2pt),
(dist-to-margin - 2pt, 2pt),
(dist-to-margin - 2pt, dy + 2pt),
(dist-to-margin + 2pt, dy + 2pt),
)

dy += 1pt // todo-spacing
let note-rect = rect(
stroke: .5pt,
fill: color,
width: m - 10pt, // todo-margin
inset: 4pt,
radius: 4pt,
text(size: font-size,
fill: font-color,
body)
)
// Boxing prevents forced paragraph breaks
box[
#place(path(stroke: 1pt + color, ..path-pts))
#place(dx: dist-to-margin + 2pt, dy: dy - 10pt, note-rect) // lift todo a bit
#_todo-outline-entry(color, body)
]
_update-descent("right", dy, anchor-y, note-rect, here().page())
}

#let _todo-left(body, dy, anchor-x, anchor-y, font-size, font-color, color) = {
let w = page.width
let m = _get-margin(side: left, here().page()).left
let dist-to-margin = - anchor-x

let path-pts = (
(0pt, -.2em),
(0pt, 2pt),
(dist-to-margin + m - 8pt, 2pt),
(dist-to-margin + m - 8pt, 2pt),
(dist-to-margin + m - 8pt, dy + 2pt),
(dist-to-margin + m - 10pt - 2pt, dy + 2pt), // todo-margin
)

dy += 1pt // todo-spacing
let note-rect = rect(
stroke: .5pt,
fill: color,
width: m - 10pt, // todo-margin
inset: 4pt,
radius: 4pt,
text(size: font-size, body)
)

let foo = measure(note-rect)
// Boxing prevents forced paragraph breaks
box[
#place(path(stroke: color, ..path-pts))
#place(dx: dist-to-margin - 2pt, dy: dy - 10pt, note-rect) // lift todo a bit
#_todo-outline-entry(color, body)
]
_update-descent("left", dy, anchor-y, note-rect, here().page())
}

#let todo(side: auto, font-size: 9pt, font-color: black, color: orange, body) = [
#h(0pt) // ensure here().position() accounts for indented paragraphs
#context {
let pos = here().position()
let margin = _get-margin(side: side, pos.page)
// shadow argument
let side = if (side == auto) {
let left = margin.at("left")
let right = margin.at("right")
if (left > right) {
"left"
} else {
"right"
}
} else {
repr(side)
}
let margin-func = if (side == "right") {
_todo-right
} else {
_todo-left
}
let (anchor-x, anchor-y) = (pos.x - 5pt, pos.y)

let (cur-page, descents) = _get-current-descent(note-descent.get(), page-number: pos.page)
let cur-descent = descents.at(side)
let dy = calc.max(0pt, cur-descent - pos.y)

margin-func(
body,
dy,
anchor-x,
anchor-y,
font-size,
font-color,
color
)
}
]
11 changes: 11 additions & 0 deletions packages/preview/todonotes/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "todonotes"
version = "0.1.0"
entrypoint = "lib.typ"
authors = ["Jens Tinggaard <@Tinggaard>"]
license = "MIT"
description = "A simple margin note package inspired by the LaTeX todonotes package."
repository = "https://github.com/Tinggaard/todonotes"

Check failure on line 8 in packages/preview/todonotes/0.1.0/typst.toml

View check run for this annotation

Typst package check / @preview/todonotes:0.1.0

packages/preview/todonotes/0.1.0/typst.toml#L8

We could not fetch this URL. Details: reqwest::Error { kind: Status( 404, ), }
keywords = ["Todo", "note", "Margin"]
compiler = "0.11.0"
exclude = ["assets/"]

0 comments on commit cd9cae8

Please sign in to comment.