Skip to content

Instantly share code, notes, and snippets.

@skizzerz
Last active September 28, 2017 20:39
Show Gist options
  • Save skizzerz/81bcb6486fa302bf1058b14baefae88c to your computer and use it in GitHub Desktop.
Save skizzerz/81bcb6486fa302bf1058b14baefae88c to your computer and use it in GitHub Desktop.
tinymce.PluginManager.add('dieroller', function (editor, url) {
"use strict";
var activeTab;
// Adds a menu item to the tools menu
editor.addButton('dieroller', {
image: url + '/img/dice.jpg',
context: 'tools',
tooltip: 'Dice roller',
onclick: function () {
editor.windowManager.open({
bodyType: 'tabpanel',
body: [
{
title: 'Basic',
type: 'form',
onshowtab: function (e) { activeTab = this; },
items: [
{
type: 'textbox',
subtype: 'hidden',
name: 'type',
value: 'basic'
},
{
type: 'container',
label: 'Roll',
items: [
{
type: 'textbox',
name: 'numdice',
subtype: 'number',
size: 4,
min: 1,
max: 100,
value: '1',
style: 'text-align: right'
},
{
type: 'listbox',
name: 'dietype',
values: [
{ text: 'd100', value: 'd100' },
{ text: 'd20', value: 'd20' },
{ text: 'd12', value: 'd12' },
{ text: 'd10', value: 'd10' },
{ text: 'd8', value: 'd8' },
{ text: 'd6', value: 'd6' },
{ text: 'd4', value: 'd4' }
],
value: 'd20'
}
]
},
{
type: 'textbox',
name: 'modifier',
label: 'Add to result',
pattern: '[+-]?[0-9]+',
placeholder: '+0'
},
{
type: 'listbox',
name: 'advdrop',
label: 'Advantage or drop dice?',
values: [
{ text: 'No', value: '' },
{ text: 'Advantage', value: 'ad' },
{ text: 'Disadvantage', value: 'da' },
{ text: 'Drop Lowest', value: 'dl' },
{ text: 'Drop Highest', value: 'dh' }
]
},
{
type: 'textbox',
name: 'dropnum',
subtype: 'number',
size: 4,
min: 0,
max: 100,
label: 'How many dice to drop?',
tooltip: 'This is ignored unless you selected Drop Lowest or Drop Highest',
placeholder: '0'
}
]
},
{
title: 'Combat',
type: 'form',
onshowtab: function (e) { activeTab = this; },
items: [
{
type: 'textbox',
subtype: 'hidden',
name: 'type',
value: 'combat'
},
{
type: 'textbox',
subtype: 'number',
name: 'atkmod',
label: 'Attack modifier',
pattern: '[+-]?[0-9]+',
placeholder: '+0'
},
{
type: 'listbox',
name: 'adv',
label: 'Advantage?',
values: [
{ text: 'No', value: '' },
{ text: 'Advantage', value: 'ad' },
{ text: 'Disadvantage', value: 'da' },
]
},
{
type: 'textbox',
name: 'dmgdice',
label: 'Damage dice',
placeholder: 'e.g. 2d4+1d6',
tooltip: 'All damage dice, including extra dice like Sneak Attack or Divine Smite'
},
{
type: 'textbox',
subtype: 'number',
name: 'dmgmod',
label: 'Damage modifier',
placeholder: '+0'
}
]
},
{
title: 'Ability scores',
type: 'form',
onshowtab: function (e) { activeTab = this; },
items: [
{
type: 'textbox',
subtype: 'hidden',
name: 'type',
value: 'stats'
},
{
type: 'textbox',
subtype: 'number',
min: 6,
max: 8,
size: 2,
name: 'num',
label: 'Number of abilities',
value: '6',
tooltip: 'Enter a number higher than 6 if you are using the optional Sanity or Honor scores'
},
{
type: 'listbox',
name: 'rr1',
label: 'Reroll ones?',
values: [
{ text: 'Never', value: '' },
{ text: 'Once', value: 'ro1' },
{ text: 'Always', value: 'rr1' }
],
tooltip: 'A common house-rule lets you reroll dice that show as 1'
}
]
}
],
title: 'Dice roller',
onsubmit: function (e) {
var data = activeTab.toJSON(),
func;
e.preventDefault();
switch (data.type) {
case 'basic':
func = getBasicRoll;
break;
case 'combat':
func = getCombatRoll;
break;
case 'stats':
func = getStatsRoll;
break;
}
editor.insertContent(func(data));
tinyMCE.activeEditor.windowManager.close();
}
});
}
});
function getBasicRoll(data) {
var numdice = parseInt(data.numdice, 10) || 1,
modifier = parseInt(data.modifier, 10) || 0,
dropnum = parseInt(data.dropnum, 10) || 0,
amod = Math.abs(modifier),
smod = Math.sign(modifier) < 0 ? '-' : '+',
str = '[roll]';
str += numdice;
str += data.dietype;
str += data.advdrop;
if (data.advdrop === 'dl' || data.advdrop === 'dh') {
str += dropnum;
}
if (modifier !== 0) {
str += smod + amod;
}
str += '[/roll]';
return str;
}
function getCombatRoll(data) {
var atkmod = parseInt(data.atkmod, 10) || 0,
dmgmod = parseInt(data.dmgmod, 10) || 0,
aamod = Math.abs(atkmod),
samod = Math.sign(atkmod) < 0 ? '-' : '+',
admod = Math.abs(dmgmod),
sdmod = Math.sign(dmgmod) < 0 ? '-' : '+',
ddice = data.dmgdice || '0',
str = 'Attack: [roll]1d20';
str += data.adv;
if (atkmod !== 0) {
str += samod + aamod;
}
str += '[/roll] Damage: [roll]if([roll:-1:critical], =0, ';
str += ddice + ', 2{' + ddice + '}.expand())'
if (dmgmod !== 0) {
str += sdmod + admod;
}
str += '[/roll]';
return str;
}
function getStatsRoll(data) {
var str = 'Ability scores:',
dice = '4d6dl1' + data.rr1,
num = parseInt(data.num, 10) || 6,
i;
for (i = 0; i < num; i++) {
str += ' [roll]' + dice + '[/roll]'
}
return str;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment