fix issue with undo because this.elem was overwritten with node.remove()

master
JFH 2020-06-28 21:52:27 +02:00
parent 18d73cfefa
commit 61591f6a2e
4 changed files with 33 additions and 6 deletions

View File

@ -0,0 +1,26 @@
import {
visitAndApproveStorage
} from '../../../support/ui-test-helper.js';
// See https://github.com/SVG-Edit/svgedit/issues/359
describe('Fix issue 359', function () {
beforeEach(() => {
visitAndApproveStorage();
});
it('can undo without throwing', function () {
cy.get('#tool_source').click();
cy.get('#svg_source_textarea')
.type('{selectall}')
.type(`<svg width="640" height="480" xmlns="http://www.w3.org/2000/svg">
<g class="layer">
<title>Layer 1</title>
<rect fill="#ffff00" height="70" width="165" x="179.5" y="146.5"/>
</g>
</svg>`, {parseSpecialCharSequences: false});
cy.get('#tool_source_save').click();
cy.get('#tool_undo').click();
cy.get('#tool_redo').click(); // test also redo to make the test more comprehensive
// if the undo throws an error to the console, the test will fail
});
});

View File

@ -573,18 +573,19 @@ export default {
}, },
elementChanged (opts) { elementChanged (opts) {
let elem = opts.elems[0]; let elem = opts.elems[0];
if (elem && elem.tagName === 'svg' && elem.id === 'svgcontent') { if (!elem) return;
if (elem.tagName === 'svg' && elem.id === 'svgcontent') {
// Update svgcontent (can change on import) // Update svgcontent (can change on import)
svgcontent = elem; svgcontent = elem;
init(); init();
} }
// Has marker, so change offset // Has marker, so change offset
if (elem && ( if (
elem.getAttribute('marker-start') || elem.getAttribute('marker-start') ||
elem.getAttribute('marker-mid') || elem.getAttribute('marker-mid') ||
elem.getAttribute('marker-end') elem.getAttribute('marker-end')
)) { ) {
const start = elem.getAttribute('marker-start'); const start = elem.getAttribute('marker-start');
const mid = elem.getAttribute('marker-mid'); const mid = elem.getAttribute('marker-mid');
const end = elem.getAttribute('marker-end'); const end = elem.getAttribute('marker-end');

View File

@ -223,7 +223,7 @@ export class InsertElementCommand extends Command {
} }
this.parent = this.elem.parentNode; this.parent = this.elem.parentNode;
this.elem = this.elem.remove(); this.elem.remove();
if (handler) { if (handler) {
handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this); handler.handleHistoryEvent(HistoryEventTypes.AFTER_UNAPPLY, this);
@ -280,7 +280,7 @@ export class RemoveElementCommand extends Command {
removeElementFromListMap(this.elem); removeElementFromListMap(this.elem);
this.parent = this.elem.parentNode; this.parent = this.elem.parentNode;
this.elem = this.elem.remove(); this.elem.remove();
if (handler) { if (handler) {
handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this); handler.handleHistoryEvent(HistoryEventTypes.AFTER_APPLY, this);

View File

@ -516,7 +516,7 @@ const undoMgr = canvas.undoMgr = new UndoManager({
} else if (!isApply) { } else if (!isApply) {
restoreRefElems(cmd.elem); restoreRefElems(cmd.elem);
} }
if (cmd.elem.tagName === 'use') { if (cmd.elem && cmd.elem.tagName === 'use') {
setUseData(cmd.elem); setUseData(cmd.elem);
} }
} else if (cmdType === ChangeElementCommand.type()) { } else if (cmdType === ChangeElementCommand.type()) {