Last active
March 28, 2024 16:07
-
-
Save annabranco/25f12daa3c1954f516020100f539ecdf to your computer and use it in GitHub Desktop.
Generate RollTable from text list (Foundry VTT Macro)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generateRandomId = () => { | |
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
let id = ''; | |
for (let i = 0; i < 16; ++i) { | |
id += chars[Math.floor(Math.random() * chars.length)]; | |
} | |
return id; | |
}; | |
new Dialog({ | |
title: `Add new rolltable`, | |
content: ` | |
<form> | |
<div class="form-group"> | |
<label>Rolltable Name</label> | |
<input type="text" class="rolltableName" name="rolltableName" value="" placeholder="Insert RollTable Name" /> | |
</div> | |
<div class="form-group"> | |
<label>Rolltable Text</label> | |
<textarea class="rolltableText" name="rolltableText" placeholder="Insert List" /> | |
</div> | |
</form> | |
`, | |
buttons: { | |
yes: { | |
icon: "<i class='fas fa-check'></i>", | |
label: `Create`, | |
callback: async (html) => { | |
const name = html.find('[name="rolltableName"]')[0].value || 'New RollTable'; | |
const text = html.find('[name="rolltableText"]')[0].value || ''; | |
const table = await RollTable.create({name: name}); | |
const items = []; | |
const reg = new RegExp(/.*\S/g); | |
let paragraphs = text.match(reg); | |
if (paragraphs) { | |
paragraphs = paragraphs.map((el) => el.trim()); | |
paragraphs.forEach((para, index) => { | |
items.push({key: (index + 1).toString(), text: para}); | |
}); | |
} | |
const results = items.map((i) => ({ | |
_id: generateRandomId(), | |
drawn: false, | |
range: [i.key, i.key], | |
text: i.text, | |
type: 0, | |
weight: 1, | |
})); | |
table.createEmbeddedDocuments('TableResult', results); | |
}, | |
}, | |
no: { | |
icon: "<i class='fas fa-times'></i>", | |
label: `Cancel`, | |
}, | |
}, | |
default: 'yes', | |
}).render(true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment