|
| 1 | +export class DropEffectsOnItems { |
| 2 | + static MODULE_NAME = "drop-effects-on-items"; |
| 3 | + static MODULE_TITLE = "Drop Effects on Items"; |
| 4 | + |
| 5 | + static log(...args) { |
| 6 | + if (game.modules.get('_dev-mode')?.api?.getPackageDebugValue(this.MODULE_NAME)) { |
| 7 | + console.log(this.MODULE_TITLE, '|', ...args); |
| 8 | + } |
| 9 | + } |
| 10 | + |
| 11 | + static init = async () => { |
| 12 | + console.log(`${DropEffectsOnItems.MODULE_NAME} | Initializing ${DropEffectsOnItems.MODULE_TITLE}`); |
| 13 | + |
| 14 | + Hooks.on('renderItemSheet', this.handleItemSheetRender); |
| 15 | + } |
| 16 | + |
| 17 | + static handleItemSheetRender = (app, html) => { |
| 18 | + const effectsList = html.find('.effects-list'); |
| 19 | + |
| 20 | + if (app.item.isOwned || !effectsList || !app.isEditable) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + this.log('binding dragdrop'); |
| 25 | + |
| 26 | + const dragDrop = new DragDrop({ |
| 27 | + dragSelector: '[data-effect-id]', |
| 28 | + dropSelector: '.effects-list', |
| 29 | + permissions: { drag: () => app.isEditable, drop: () => app.isEditable }, |
| 30 | + callbacks: { dragstart: this._onDragStart(app.object), drop: this._onDrop(app.object) } |
| 31 | + }); |
| 32 | + |
| 33 | + dragDrop.bind(html[0]); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * The Drag Start event which populates data to create an effect on drop |
| 39 | + * @param {*} event |
| 40 | + */ |
| 41 | + static _onDragStart = (effectParent) => (event) => { |
| 42 | + if (!effectParent) { |
| 43 | + this.log('DragDrop _onDragStart no parent', { |
| 44 | + effectParent |
| 45 | + }); |
| 46 | + |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + const li = event.currentTarget; |
| 51 | + const effectId = li.dataset?.effectId; |
| 52 | + |
| 53 | + if (!effectId) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + const effect = effectParent.effects.get(effectId); |
| 58 | + |
| 59 | + if (!effect) { |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + // Create drag data |
| 64 | + const dragData = { |
| 65 | + type: 'ActiveEffect', |
| 66 | + data: effect.data, |
| 67 | + }; |
| 68 | + |
| 69 | + this.log('DragDrop dragStart:', { |
| 70 | + effect, |
| 71 | + dragData, |
| 72 | + }); |
| 73 | + |
| 74 | + // Set data transfer |
| 75 | + event.dataTransfer.setData("text/plain", JSON.stringify(dragData)); |
| 76 | + }; |
| 77 | + |
| 78 | + /** |
| 79 | + * When an effect is dropped on the sheet, create a copy of that effect |
| 80 | + */ |
| 81 | + static _onDrop = (effectParent) => async (event) => { |
| 82 | + if (!effectParent) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // Try to extract the data |
| 87 | + let dropData; |
| 88 | + try { |
| 89 | + dropData = JSON.parse(event.dataTransfer.getData('text/plain')); |
| 90 | + |
| 91 | + this.log('DragDrop drop', { |
| 92 | + event, |
| 93 | + dropData, |
| 94 | + }); |
| 95 | + } catch (err) { |
| 96 | + this.log('DragDrop drop', { |
| 97 | + err, |
| 98 | + }); |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + if (dropData.type !== 'ActiveEffect') return false; |
| 105 | + |
| 106 | + |
| 107 | + if (!!effectParent.effects.get(dropData.data._id)) { |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + this.log('DragDrop drop starting:', { |
| 112 | + effectParent, |
| 113 | + dropData, |
| 114 | + }); |
| 115 | + |
| 116 | + return ActiveEffect.create( |
| 117 | + { |
| 118 | + ...dropData.data, |
| 119 | + origin: effectParent.uuid, |
| 120 | + _id: null, |
| 121 | + }, {parent: effectParent} |
| 122 | + ); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +Hooks.on("ready", DropEffectsOnItems.init); |
| 127 | + |
| 128 | +Hooks.once('devModeReady', ({ registerPackageDebugFlag }) => { |
| 129 | + registerPackageDebugFlag(DropEffectsOnItems.MODULE_NAME); |
| 130 | +}); |
0 commit comments