From 734889c24fd29d0ef8cb37d5aab8be239c588187 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 28 Jun 2022 13:23:35 +0200 Subject: [PATCH] Improve Component.move method --- package.json | 2 +- src/dom_components/model/Component.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 45699b675..9d90e169b 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ }, "lint-staged": { "{src,test}/**/*.(t|j)s": [ - "npm run lint -- --fix", + "eslint --ext .ts,.js --fix", "prettier --single-quote --print-width 120 --arrow-parens avoid --write", "git add" ] diff --git a/src/dom_components/model/Component.js b/src/dom_components/model/Component.js index 596989814..037245b2b 100644 --- a/src/dom_components/model/Component.js +++ b/src/dom_components/model/Component.js @@ -1703,8 +1703,20 @@ export default class Component extends StyleableModel { * editor.getSelected().move(dest, { at: 0 }); */ move(component, opts = {}) { - this.remove({ temporary: 1 }); - component && component.append(this, opts); + if (component) { + const { at } = opts; + const index = this.index(); + const sameParent = component === this.parent(); + const sameIndex = index === at || index === at - 1; + + if (!sameParent || !sameIndex) { + if (sameParent && at && at > index) { + opts.at = at - 1; + } + this.remove({ temporary: 1 }); + component.append(this, opts); + } + } return this; }