|
| 1 | +import Events from '../Events'; |
| 2 | +import { Command } from '../command.js'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Update an attribute of an asset element in <a-assets> (e.g., a-material, |
| 6 | + * a-mixin, img, audio, video, a-asset-item). |
| 7 | + * |
| 8 | + * Consecutive updates to the same asset attribute are merged in history like |
| 9 | + * entity updates (entityId/component/property are set for that purpose). |
| 10 | + * |
| 11 | + * @param editor Editor |
| 12 | + * @param payload Object containing assetEl (element or ID string), attribute |
| 13 | + * and value (attribute string, or null to remove the attribute). |
| 14 | + * @constructor |
| 15 | + */ |
| 16 | +export class AssetUpdateCommand extends Command { |
| 17 | + constructor(editor, payload = null) { |
| 18 | + super(editor); |
| 19 | + |
| 20 | + this.type = 'assetupdate'; |
| 21 | + this.name = 'Update Asset'; |
| 22 | + this.updatable = true; |
| 23 | + |
| 24 | + if (payload === null) return; |
| 25 | + |
| 26 | + let assetEl; |
| 27 | + if (typeof payload.assetEl === 'string') { |
| 28 | + assetEl = document.getElementById(payload.assetEl); |
| 29 | + if (!assetEl) { |
| 30 | + console.error('Asset not found with ID:', payload.assetEl); |
| 31 | + return; |
| 32 | + } |
| 33 | + } else { |
| 34 | + assetEl = payload.assetEl; |
| 35 | + } |
| 36 | + // Inline materials (`material(...)`) have no id; keep a direct reference |
| 37 | + // for those, and use the inline string as history merge key. |
| 38 | + this.assetEl = assetEl; |
| 39 | + this.entityId = assetEl.id || assetEl.inlineString; |
| 40 | + this.component = assetEl.tagName.toLowerCase(); |
| 41 | + this.property = payload.attribute; |
| 42 | + this.newValue = payload.value; |
| 43 | + this.oldValue = assetEl.getAttribute(payload.attribute); |
| 44 | + } |
| 45 | + |
| 46 | + resolveAssetEl() { |
| 47 | + if (this.assetEl && this.assetEl.isConnected) { |
| 48 | + return this.assetEl; |
| 49 | + } |
| 50 | + return document.getElementById(this.entityId) || this.assetEl; |
| 51 | + } |
| 52 | + |
| 53 | + apply(value, nextCommandCallback) { |
| 54 | + const assetEl = this.resolveAssetEl(); |
| 55 | + if (!assetEl) return; |
| 56 | + if (value === null || value === undefined) { |
| 57 | + assetEl.removeAttribute(this.property); |
| 58 | + } else { |
| 59 | + assetEl.setAttribute(this.property, value); |
| 60 | + } |
| 61 | + // For A-Frame elements like <a-material>, attribute changes are picked up |
| 62 | + // by a MutationObserver (asynchronous); apply synchronously so the UI can |
| 63 | + // refresh right away. |
| 64 | + if (typeof assetEl.attributeChangedCallback === 'function') { |
| 65 | + assetEl.attributeChangedCallback( |
| 66 | + this.property.toLowerCase(), |
| 67 | + null, |
| 68 | + value ?? null |
| 69 | + ); |
| 70 | + } |
| 71 | + Events.emit('assetupdate', { |
| 72 | + assetEl, |
| 73 | + attribute: this.property, |
| 74 | + value |
| 75 | + }); |
| 76 | + nextCommandCallback?.(assetEl); |
| 77 | + } |
| 78 | + |
| 79 | + execute(nextCommandCallback) { |
| 80 | + if (this.editor.config.debugUndoRedo) { |
| 81 | + console.log('execute', this.entityId, this.property, this.newValue); |
| 82 | + } |
| 83 | + this.apply(this.newValue, nextCommandCallback); |
| 84 | + } |
| 85 | + |
| 86 | + undo(nextCommandCallback) { |
| 87 | + if (this.editor.config.debugUndoRedo) { |
| 88 | + console.log('undo', this.entityId, this.property, this.oldValue); |
| 89 | + } |
| 90 | + this.apply(this.oldValue, nextCommandCallback); |
| 91 | + } |
| 92 | + |
| 93 | + update(command) { |
| 94 | + this.newValue = command.newValue; |
| 95 | + } |
| 96 | + |
| 97 | + toJSON() { |
| 98 | + const output = super.toJSON(this); |
| 99 | + output.entityId = this.entityId; |
| 100 | + output.component = this.component; |
| 101 | + output.property = this.property; |
| 102 | + output.oldValue = this.oldValue; |
| 103 | + output.newValue = this.newValue; |
| 104 | + return output; |
| 105 | + } |
| 106 | + |
| 107 | + fromJSON(json) { |
| 108 | + super.fromJSON(json); |
| 109 | + this.entityId = json.entityId; |
| 110 | + this.component = json.component; |
| 111 | + this.property = json.property; |
| 112 | + this.oldValue = json.oldValue; |
| 113 | + this.newValue = json.newValue; |
| 114 | + this.assetEl = document.getElementById(this.entityId); |
| 115 | + } |
| 116 | +} |
0 commit comments