Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Compendium drag and drop

1685607581

Edited 1685607608
Paul V.
Pro
Sheet Author
Compendium Curator
Hi I have a compendium drag and drop thing working on my sheet, BUT for it to work there has to be an item in the repeating section for it to be dragged onto.  I've seen it working and automatically creating an item when dragged from the compendium, how would i go about doing this? Here is the mostly working code <div class="sheet-spellinfo"> <fieldset class="repeating_spells"> <div class="compendium-drop-target"> <input type="text" name="attr_SpellName" accept="Name" data-i18n="name"/> <input type="text" name="attr_SpellCaster-Spell_Lvl" accept="data-Caster-Spell_Lvl" data-i18n="csl"/> <input type="text" name="attr_SpellCasting-Time" accept="data-Casting-Time" data-i18n="ct"/> <input type="text" name="attr_SpellRange" accept="data-Range" data-i18n="r"/> <input type="text" name="attr_SpellTarget-Area-Of-Effect" accept="data-Target-Area-Of-Effect" data-i18n="t/aoe"/> <input type="text" name="attr_SpellDuration" accept="data-Duration" data-i18n="d"/> <input type="text" name="attr_SpellSaving-Throw" accept="data-Saving-Throw" data-i18n="sv"/> <input type="text" name="attr_SpellSpell-Resistance" accept="data-Spell-Resistance" data-i18n="sr"/> <input type="text" name="attr_SpellComponents" accept="data-Components" data-i18n="comp"/> <input type="hidden" name="attr_SpellDamage" accept="data-Damage" data-i18n="spellDam" value="0" class=""/> <input type="hidden" name="attr_SpellDamageperlevel" accept="data-Damage_per_level" data-i18n="spellDamperlevel" value="0" class=""/> <span data-i18n="Info">Info</span> <button type="roll" value="&{template:spells} {{name=@{CharacterName} @{SpellName} Info}} {{casterspelllvl=@{SpellCaster-Spell_Lvl}}} {{casttime=@{SpellCasting-Time}}} {{range=@{SpellRange}}} {{targetaoe=@{SpellTarget-Area-Of-Effect}}} {{duration=@{SpellDuration}}} {{savingthrow=@{SpellSaving-Throw}}} {{spellresistance=@{SpellSpell-Resistance}}} {{components=@{SpellComponents}}} {{description=@{SpellDescription}}}" name="Spell Info"/> <span data-i18n="Roll">Roll</span> <button type="roll" value="&{template:custom} {{title=@{CharacterName} @{SpellName} Damage}} {{Result=[[@{SpellDamage}+@{Level}@{SpellDamageperlevel}]]}}" name="Spell Damage"/> <textarea type="text" name="attr_SpellDescription" accept="data-Description" data-i18n="desc">description</textarea> </div> </fieldset> </div>
1685646981

Edited 1685647140
Scott C.
Forum Champion
Sheet Author
API Scripter
Compendium Curator
to do that you need to use a sheetworker to process the drop. Something like: <div class="compendium-drop-target"> <input name="attr_drop_data" accepts="data" type="hidden"> <fieldset class="repeating_spells"> <div class="compendium-drop-target"> <input type="text" name="attr_SpellName" accept="Name" data-i18n="name"/> <input type="text" name="attr_SpellCaster-Spell_Lvl" accept="data-Caster-Spell_Lvl" data-i18n="csl"/> <input type="text" name="attr_SpellCasting-Time" accept="data-Casting-Time" data-i18n="ct"/> <input type="text" name="attr_SpellRange" accept="data-Range" data-i18n="r"/> <input type="text" name="attr_SpellTarget-Area-Of-Effect" accept="data-Target-Area-Of-Effect" data-i18n="t/aoe"/> <input type="text" name="attr_SpellDuration" accept="data-Duration" data-i18n="d"/> <input type="text" name="attr_SpellSaving-Throw" accept="data-Saving-Throw" data-i18n="sv"/> <input type="text" name="attr_SpellSpell-Resistance" accept="data-Spell-Resistance" data-i18n="sr"/> <input type="text" name="attr_SpellComponents" accept="data-Components" data-i18n="comp"/> <input type="hidden" name="attr_SpellDamage" accept="data-Damage" data-i18n="spellDam" value="0" class=""/> <input type="hidden" name="attr_SpellDamageperlevel" accept="data-Damage_per_level" data-i18n="spellDamperlevel" value="0" class=""/> <span data-i18n="Info">Info</span> <button type="roll" value="&{template:spells} {{name=@{CharacterName} @{SpellName} Info}} {{casterspelllvl=@{SpellCaster-Spell_Lvl}}} {{casttime=@{SpellCasting-Time}}} {{range=@{SpellRange}}} {{targetaoe=@{SpellTarget-Area-Of-Effect}}} {{duration=@{SpellDuration}}} {{savingthrow=@{SpellSaving-Throw}}} {{spellresistance=@{SpellSpell-Resistance}}} {{components=@{SpellComponents}}} {{description=@{SpellDescription}}}" name="Spell Info"/> <span data-i18n="Roll">Roll</span> <button type="roll" value="&{template:custom} {{title=@{CharacterName} @{SpellName} Damage}} {{Result=[[@{SpellDamage}+@{Level}@{SpellDamageperlevel}]]}}" name="Spell Damage"/> <textarea type="text" name="attr_SpellDescription" accept="data-Description" data-i18n="desc">description</textarea> </div> </fieldset> </div> <script type="text/worker"> //dictionary to convert sheet attribute name to compendium attribute name const spellFields = { name:'Name', 'caster-Spell_Lvl':'data-Caster-Spell_Lvl', 'casting-time':'data-Casting-Time', range:'data-Range', 'target-area-of-effect':'data-Target-Area-Of-Effect', duration:'data-Duration', 'saving-throw':'data-Saving-Throw', 'spell-resistance':'data-Spell-Resistance', components:'data-Components', damage:'data-Damage', damageperlevel:'data-Damage_per_level', description:'data-Description' }; const processSpell = (attributes,data) => { const rowID = generateRowID(); const setObj = {}; //Iterate over the dictionary. Assign attributes for each compendium key return Object.entries(spellFields).reduce((obj,[sheetKey,compendiumKey]) => { obj[`repeating_spells_${rowID}_spell${sheetKey}`] = data[compendiumKey]; return obj; },{}); }; on('change:drop_data',(event)=>{ getAttrs(['drop_data'],(attributes) => { try{ const data = JSON.parse(attributes.data); const funcSwitch = { //Switch the key here to whatever the category name for spells is in your compendium Spells:processSpell }; if(funcSwitch[data.Category]){ //Passing the attributes in case you need to use them for something else in another drop function. const setObj = funcSwitch[data.Category](attributes,data); setAttrs(setObj,{silent:true}); } }catch{         //Do nothing if the thing fails. It means this wasn't a valid drop.     } }) }) </script> EDIT: Also, note that I'd highly recommend switching your naming scheme to use snake_case  for attribute names. There are many reasons to do this, but the big one is that the sheetworker listeners require the attribute names to be all lower case (regardless of what they are in the html).