Skip to content

Commit

Permalink
Add hideroll attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
rg-wood committed Apr 5, 2024
1 parent b8affa7 commit 4df797c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ Web component for interactive random tables.

**[Demo](https://grislyeye.github.io/vellum-random-table/)**

| Attribute | Description | Default |
| --------- | -------------------------------------------------------- | ------- |
| `select` | CSS selector for the container/input to display results. | |
| `preroll` | Load table with pre-rolled result. | `false` |
| Attribute | Description | Default |
| ---------- | -------------------------------------------------------- | ------- |
| `select` | CSS selector for the container/input to display results. | |
| `preroll` | Load table with pre-rolled result. | `false` |
| `hideroll` | Display dice roll in parenthesis after roll. | `false` |

### Examples

Simple, one-column table (elements are selected at random with equal weight):

```html
<vellum-random-table select="#result" preroll>
<vellum-random-table select="#result" preroll hideroll>
<caption>
Random Encounters
</caption>
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<body>
<h2>One-column random table</h2>

<vellum-random-table select="#result" preroll>
<vellum-random-table select="#result" preroll hideroll>
<caption>
Random Encounters
</caption>
Expand Down
14 changes: 11 additions & 3 deletions src/vellum-random-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export class VellumRandomTable extends LitElement {
@property({ type: Boolean })
preroll: boolean = false

@property({ type: Boolean })
hideroll: boolean = false

connectedCallback(): void {
super.connectedCallback()

Expand Down Expand Up @@ -78,8 +81,11 @@ export class VellumRandomTable extends LitElement {

if (this.mode == TableMode.FirstColumn) {
const selection = this.selection(0)
const result = selection[Math.floor(Math.random() * selection.length)]
this.display(result)
const roll = Math.floor(Math.random() * selection.length)
const result = selection[roll]

if (!this.hideroll) this.display(`${result} (${roll + 1})`)
else this.display(result)
} else if (this.mode == TableMode.TwoColumn) {
const ranges = this.ranges(0)
const selection = this.selection(1)
Expand All @@ -90,7 +96,9 @@ export class VellumRandomTable extends LitElement {
const index = ranges.findIndex((range) => range.includes(roll))

const result = selection[index]
this.display(`${result} (${roll})`)

if (!this.hideroll) this.display(`${result} (${roll})`)
else this.display(result)
}
}
}
Expand Down

0 comments on commit 4df797c

Please sign in to comment.