Skip to content

Commit

Permalink
keyle:0.1.0 (#836)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicwenli committed Jul 24, 2024
1 parent 87c1b9d commit 8536904
Show file tree
Hide file tree
Showing 8 changed files with 299 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/keyle/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 MagicWenli

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.
25 changes: 25 additions & 0 deletions packages/preview/keyle/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# keyle

This package provides a simple way to style keyboard shortcuts in your documentation.

It was inspired by [auth0/kbd](https://auth0.github.io/kbd/) and [dogezen/badgery](https://github.com/dogezen/badgery).

Send them respect and love.

## Usage

Please see the [keyle.pdf](doc/keyle.pdf) for more documentation.

`keyle` is imported using:

```typst
#import "@preview/keyle:0.1.0"
```

### Example

![About](doc/keyle.png)

## License

MIT
Binary file added packages/preview/keyle/0.1.0/doc/keyle.pdf
Binary file not shown.
Binary file added packages/preview/keyle/0.1.0/doc/keyle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
95 changes: 95 additions & 0 deletions packages/preview/keyle/0.1.0/doc/keyle.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#import "@preview/mantys:0.1.4": *
// Vendored because of https://github.com/jneug/typst-mantys/pull/20
#let cmdref(name) = {
link(cmd-label(name), cmd-(name))
}
// End Vendored

#import "../src/keyle.typ"

#let lib-name = package[keyle]

#show: mantys.with(..toml("../typst.toml"), date: datetime.today(), examples-scope: (keyle: keyle))
#show link: underline

// end of preamble

= About

#lib-name is a library that allows you to create HTML `<kbd>` like keyboard shorts simple and easy.

The name, `keyle`, is a combination of `key` and `style`.

This project is inspired by #link("http://github.com/auth0/kbd")[auth0/kbd] and #link("https://github.com/dogezen/badgery")[dogezen/badgery].
Send them respect!

#v(2em)

#keyle.kbd-example
#keyle.kbd-mac-example
#keyle.kbd-com-example

#v(2em)
#keyle.kbd-set-style(keyle.kbd-styles.deep-blue)
#keyle.kbd-example

#v(2em)
#keyle.kbd-set-style(keyle.kbd-styles.type-writer)
#keyle.kbd-example

= Usage

#lib-name is imported using
#codesnippet[```typ
#import "@preview/keyle:0.1.0"
```]

Basic usage is as follows, see #cmdref("kbd") for more details:

#keyle.kbd-set-style(keyle.kbd-styles.standard)

#example(```typst
#keyle.kbd("Ctrl", "C")
```)

Also you can change the style of the keyboard. See #cmdref("kbd-set-style") for more details.

#example(```typst
#keyle.kbd-set-style("deep-blue")
#keyle.kbd("Alt")
#keyle.kbd-set-style("type-writer")
#keyle.kbd("Enter")
#keyle.kbd-set-style("standard")
```)

You can also use the constants `mac-key` and `com-key` to get the key names for Mac and Windows/Linux respectively.

#example(```typst
#keyle.kbd(keyle.mac-key.control, keyle.mac-key.shift, "A")
```)

== Available constants

=== `kbd-styles`

Use `kbd-styles` to get available styles.

#example(```typ
#keyle.kbd-styles
```)

=== `mac-key` and `com-key`

Use `mac-key` and `com-key` to get the key names for Mac and Windows/Linux respectively.

#example(```typ
#keyle.mac-key
```)

#example(```typ
#keyle.com-key
```)

= Available commands

#tidy-module(read("../src/keyle.typ"), name: "keyle", include-example-scope: true)
105 changes: 105 additions & 0 deletions packages/preview/keyle/0.1.0/src/keyle.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#import "sym.typ": com-key, mac-key

#let _inset = 4pt
#let _outset = 2pt
#let _radius = 3pt

#let _kbd-stardard-style(sym) = box(rect(
inset: (x: _inset),
outset: (top: _outset),
stroke: rgb("#555"),
radius: _radius,
fill: rgb("#eee"),
text(fill: black, sym),
))

#let _kbd-deep-blue-style(sym) = box(rect(
inset: (x: _inset),
outset: (top: _outset),
stroke: rgb("#2a6596"),
radius: _radius,
fill: rgb("#4682b4"),
smallcaps(text(fill: white, sym)),
))

#let _kbd-type-writer-style(sym) = box(
rect(inset: (x: _inset), stroke: rgb("#2b2b2b"), radius: 50%, fill: rgb("#333"), smallcaps(text(fill: white, sym))),
)

#let _kbd-style-funcs = (standard: _kbd-stardard-style, deep-blue: _kbd-deep-blue-style, type-writer: _kbd-type-writer-style)

#let kbd-styles = (standard: "standard", deep-blue: "deep-blue", type-writer: "type-writer")

#let kbd-state = state("kbd-style-state", kbd-styles.standard)

/// Set the keyboard style.
///
/// #example(```typ
/// #keyle.kbd[A] // standard style
/// #keyle.kbd-set-style("deep-blue")
/// #keyle.kbd[A] // deep-blue style
/// #keyle.kbd-set-style(keyle.kbd-styles.standard) // back to standard style
/// ```)
///
/// - s (string): The style name. Use `kbd-styles` to get available styles.
#let kbd-set-style(s) = {
kbd-state.update(s)
}

/// Renders a keyboard key.
///
/// #example(```typ
/// #keyle.kbd("Ctrl", "A")
/// ```)
///
/// #example(```typ
/// #keyle.kbd("Ctrl", "A", compact: true)
/// ```)
///
/// #example(```typ
/// #keyle.kbd("Ctrl", "A", compact: true, delim: "-")
/// ```)
///
/// - ..keys (string): The key to render.
/// - compact (bool): If true, the keys will be rendered in a single box.
/// - delim (string): The delimiter to use when rendering in compact mode.
#let kbd(..keys, compact: false, delim: "+") = {
locate(loc => {
let style-str = kbd-state.at(loc)
let style-func = _kbd-style-funcs.at(style-str)
if compact {
style-func(keys.pos().join(delim))
} else {
keys.pos().map(k => [#style-func(k)]).join([ #box(height: 1.2em, delim) ])
}
})
}

#let kbd-example = align(center)[
#box([
#kbd[1] #kbd[2] #kbd[3] #kbd[4] #kbd[5] #kbd[6] #kbd[7] #kbd[8] #kbd[9] #kbd[0]\
#kbd[Q] #kbd[W] #kbd[E] #kbd[R] #kbd[T] #kbd[Y] #kbd[U] #kbd[I] #kbd[O] #kbd[P]\
#kbd[A] #kbd[S] #kbd[D] #kbd[F] #kbd[G] #kbd[H] #kbd[J] #kbd[K] #kbd[L]\
#kbd[Z] #kbd[X] #kbd[C] #kbd[V] #kbd[B] #kbd[N] #kbd[M]
])
]

#let kbd-mac-example = align(
center,
)[
#box(
[
#kbd(mac-key.command) #kbd(mac-key.shift) #kbd(mac-key.option) #kbd(mac-key.control) #kbd(mac-key.return) #kbd(mac-key.delete) #kbd(mac-key.forward-delete) #kbd(mac-key.escape) #kbd(mac-key.left) #kbd(mac-key.right) #kbd(mac-key.up) #kbd(mac-key.down) #kbd(mac-key.pageup) #kbd(mac-key.pagedown) #kbd(mac-key.home) #kbd(mac-key.end) #kbd(mac-key.tab-right) #kbd(mac-key.tab-left)
],
)
]

#let kbd-com-example = align(
center,
)[
#box(
[
#kbd(com-key.control) #kbd(com-key.shift) #kbd(com-key.alt) #kbd(com-key.return) #kbd(com-key.delete) #kbd(com-key.escape) #kbd(com-key.left) #kbd(com-key.right) #kbd(com-key.up) #kbd(com-key.down) #kbd(com-key.pageup) #kbd(com-key.pagedown) #kbd(com-key.home) #kbd(com-key.end) #kbd(com-key.tab)
],
)
]
39 changes: 39 additions & 0 deletions packages/preview/keyle/0.1.0/src/sym.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// https://support.apple.com/en-hk/guide/mac-help/cpmh0011/mac
#let mac-key = (
command: "",
shift: "",
option: "",
control: "",
"return": "",
delete: "",
forward-delete: "",
escape: "",
left: "",
right: "",
up: "",
down: "",
pageup: "",
pagedown: "",
home: "",
end: "",
tab-right: "",
tab-left: "",
)

#let com-key = (
control: "Ctrl",
shift: "Shift",
alt: "Alt",
"return": "Enter",
delete: "Del",
escape: "Esc",
left: "",
right: "",
up: "",
down: "",
pageup: "PgUp",
pagedown: "PgDn",
home: "Home",
end: "End",
tab: "Tab",
)
14 changes: 14 additions & 0 deletions packages/preview/keyle/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "keyle"
version = "0.1.0"
authors = ["magicwenli <yxnian@outlook.com>"]
license = "MIT"
description = "This package provides a simple way to style keyboard shortcuts in your documentation."
repository = "https://github.com/magicwenli/keyle"

compiler = "0.11.1"
entrypoint = "src/keyle.typ"
keywords = ["kbd", "keyboard", "shortcut"]
categories = ["utility", "fun"]

exclude = ["*.pdf"]

0 comments on commit 8536904

Please sign in to comment.