|
| 1 | +import Events from '../Events'; |
| 2 | +import { Command } from '../command.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Remove an asset element from <a-assets> (e.g., a-material, a-mixin, img, |
| 6 | + * audio, video, a-asset-item). |
| 7 | + * |
| 8 | + * The tag name, attributes and position are captured so undo can recreate the |
| 9 | + * asset in place. For <a-material>, entities referencing the asset are |
| 10 | + * re-resolved on undo so they use the recreated THREE.Material instance. |
| 11 | + * |
| 12 | + * @param editor Editor |
| 13 | + * @param payload Object containing assetEl (element or ID string). |
| 14 | + * @constructor |
| 15 | + */ |
| 16 | +export class AssetRemoveCommand extends Command { |
| 17 | + constructor(editor, payload = null) { |
| 18 | + super(editor); |
| 19 | + |
| 20 | + this.type = 'assetremove'; |
| 21 | + this.name = 'Remove Asset'; |
| 22 | + |
| 23 | + if (payload === null) return; |
| 24 | + |
| 25 | + let assetEl; |
| 26 | + if (typeof payload.assetEl === 'string') { |
| 27 | + assetEl = document.getElementById(payload.assetEl); |
| 28 | + if (!assetEl) { |
| 29 | + console.error('Asset not found with ID:', payload.assetEl); |
| 30 | + return; |
| 31 | + } |
| 32 | + } else { |
| 33 | + assetEl = payload.assetEl; |
| 34 | + } |
| 35 | + this.assetId = assetEl.id; |
| 36 | + this.tagName = assetEl.tagName.toLowerCase(); |
| 37 | + // Capture attributes and position for undo. |
| 38 | + this.attributes = {}; |
| 39 | + for (const attr of assetEl.attributes) { |
| 40 | + if (attr.name === 'id') continue; |
| 41 | + this.attributes[attr.name] = attr.value; |
| 42 | + } |
| 43 | + this.index = assetEl.parentNode |
| 44 | + ? Array.prototype.indexOf.call(assetEl.parentNode.children, assetEl) |
| 45 | + : -1; |
| 46 | + } |
| 47 | + |
| 48 | + execute(nextCommandCallback) { |
| 49 | + const assetEl = document.getElementById(this.assetId); |
| 50 | + if (assetEl && assetEl.parentNode) { |
| 51 | + assetEl.parentNode.removeChild(assetEl); |
| 52 | + Events.emit('assetremove', this.assetId); |
| 53 | + } |
| 54 | + nextCommandCallback?.(); |
| 55 | + } |
| 56 | + |
| 57 | + undo(nextCommandCallback) { |
| 58 | + const sceneEl = AFRAME.scenes[0]; |
| 59 | + let assetsEl = sceneEl.querySelector('a-assets'); |
| 60 | + if (!assetsEl) { |
| 61 | + assetsEl = document.createElement('a-assets'); |
| 62 | + sceneEl.appendChild(assetsEl); |
| 63 | + } |
| 64 | + const assetEl = document.createElement(this.tagName); |
| 65 | + assetEl.id = this.assetId; |
| 66 | + for (const name in this.attributes) { |
| 67 | + assetEl.setAttribute(name, this.attributes[name]); |
| 68 | + } |
| 69 | + assetsEl.insertBefore(assetEl, assetsEl.children[this.index] ?? null); |
| 70 | + Events.emit('assetcreate', assetEl); |
| 71 | + |
| 72 | + // The recreated <a-material> is a new THREE.Material instance; re-resolve |
| 73 | + // the entities referencing it so they pick it up. |
| 74 | + if (assetEl.isMaterialAsset) { |
| 75 | + const ref = '#' + this.assetId; |
| 76 | + document.querySelectorAll('a-scene [material]').forEach((entity) => { |
| 77 | + if ( |
| 78 | + entity.isEntity && |
| 79 | + entity.getDOMAttribute('material')?.material === ref |
| 80 | + ) { |
| 81 | + entity.setAttribute('material', 'material', ref); |
| 82 | + } |
| 83 | + }); |
| 84 | + } |
| 85 | + nextCommandCallback?.(assetEl); |
| 86 | + } |
| 87 | + |
| 88 | + toJSON() { |
| 89 | + const output = super.toJSON(this); |
| 90 | + output.tagName = this.tagName; |
| 91 | + output.assetId = this.assetId; |
| 92 | + output.attributes = this.attributes; |
| 93 | + output.index = this.index; |
| 94 | + return output; |
| 95 | + } |
| 96 | + |
| 97 | + fromJSON(json) { |
| 98 | + super.fromJSON(json); |
| 99 | + this.tagName = json.tagName; |
| 100 | + this.assetId = json.assetId; |
| 101 | + this.attributes = json.attributes; |
| 102 | + this.index = json.index; |
| 103 | + } |
| 104 | +} |
0 commit comments