| 305 | } |
| 306 | |
| 307 | override async revert(): Promise<void> { |
| 308 | const parent = this.engine.element().find(`[data-character="${this.asset}"]`); |
| 309 | |
| 310 | parent.find(`[data-layer="${this.layer}"]`).remove(); |
| 311 | |
| 312 | |
| 313 | // First, we remove the last instance of the character from the history since |
| 314 | // that's the one being currently displayed and we want the one before that |
| 315 | const characterLayerHistory = this.engine.history('characterLayer') as CharacterLayerHistoryItem[]; |
| 316 | for (let i = characterLayerHistory.length - 1; i >= 0; i--) { |
| 317 | const { layers } = characterLayerHistory[i]; |
| 318 | |
| 319 | const historyStatement = layers.find((s: { statement: string | null; previous: string | null }) => { |
| 320 | const { previous, statement } = s; |
| 321 | const statementStr = statement || previous; |
| 322 | if (statementStr) { |
| 323 | const [, , asset] = statementStr.split(' '); |
| 324 | const [id, layer] = asset.split(':'); |
| 325 | |
| 326 | return id === this.asset && layer === this.layer; |
| 327 | } |
| 328 | return false; |
| 329 | }); |
| 330 | |
| 331 | if (typeof historyStatement === 'object' && historyStatement !== null) { |
| 332 | |
| 333 | const { statement, previous } = historyStatement; |
| 334 | const statementStr = statement || previous; |
| 335 | if (statementStr) { |
| 336 | const [, , asset] = statementStr.split(' '); |
| 337 | const [id, layer] = asset.split(':'); |
| 338 | if (id === this.asset && layer === this.layer) { |
| 339 | characterLayerHistory.splice(i, 1); |
| 340 | |
| 341 | if (typeof previous !== 'undefined' && previous !== null) { |
| 342 | const action = this.engine.prepareAction(previous, { cycle: 'Application' }) as ActionInstance | null; |
| 343 | if (action !== null) { |
| 344 | await action.apply(); |
| 345 | await action.didApply({ updateHistory: false, updateState: true }); |
| 346 | } |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | // If the script didn't return on the for cycle above, it means either the |
| 358 | // history didn't have any items left or, the character was not found. |
| 359 | // In that case, we simply remove the character from the state. |
| 360 | this.engine.state({ |
| 361 | characterLayers: [ |
| 362 | ...this.engine.state('characterLayers').filter((item: any) => { |
| 363 | if (typeof item === 'string') { |
| 364 | const [show, character, asset, sprite] = item.split(' '); |