diff --git a/src/settings/ui/setting-tab.ts b/src/settings/ui/setting-tab.ts index 6ed2daa..daa9ae0 100644 --- a/src/settings/ui/setting-tab.ts +++ b/src/settings/ui/setting-tab.ts @@ -203,11 +203,10 @@ export class ExtendedSettingTab extends PluginSettingTab { } this._makeDragable(groupEl, evt => { - let oldIndex = evt.oldDraggableIndex, - newIndex = evt.newDraggableIndex; - if (this.isHidden || oldIndex === undefined || newIndex === undefined || oldIndex == newIndex) return; + let { oldIndex, newIndex } = evt; + if (oldIndex === undefined || newIndex === undefined || oldIndex == newIndex) return; tagManager.move(spec.type, oldIndex, newIndex); - spec.onMove?.(this, oldIndex, newIndex); + spec.onMoved?.(this, oldIndex, newIndex); }); } @@ -349,7 +348,7 @@ export class ExtendedSettingTab extends PluginSettingTab { }); } - private _makeDragable(groupEl: HTMLElement, onEnd: (evt: SortableEvent) => unknown): void { + private _makeDragable(groupEl: HTMLElement, onMoved: (evt: SortableEvent) => unknown): void { Sortable.create(groupEl, { handle: ".ems-button-drag-handle", animation: 100, @@ -358,7 +357,7 @@ export class ExtendedSettingTab extends PluginSettingTab { fallbackOnBody: true, fallbackClass: "ems-setting-item-dragged", fallbackTolerance: 4, - onEnd: onEnd, + onEnd: onMoved, }); } } \ No newline at end of file diff --git a/src/stylesheet.ts b/src/stylesheet.ts index a24f453..352edd8 100644 --- a/src/stylesheet.ts +++ b/src/stylesheet.ts @@ -77,14 +77,13 @@ export class StyleSheetHandler { this._subHandlers.forEach(handler => handler.moveSingleRule(index, to)); } - public moveRule(fromIndex: number, toIndex: number): void { - if (fromIndex == toIndex) { return } - let greaterIndex = Math.max(fromIndex, toIndex), - smallerIndex = Math.min(fromIndex, toIndex), - ruleStr = this._stylesheet.cssRules.item(greaterIndex)!.cssText; - this.removeSingle(greaterIndex); - this.insert(ruleStr, smallerIndex); - this._subHandlers.forEach(handler => handler.moveRule(fromIndex, toIndex)); + public moveRule(oldIndex: number, newIndex: number): void { + if (oldIndex == newIndex) return; + if (newIndex > oldIndex) newIndex--; + let ruleStr = this._stylesheet.cssRules.item(oldIndex)!.cssText; + this.removeSingle(oldIndex); + this.insert(ruleStr, newIndex); + this._subHandlers.forEach(handler => handler.moveRule(oldIndex, newIndex)); } public removeSingle(index: number): void { diff --git a/src/tag-manager.ts b/src/tag-manager.ts index af4578e..c56afde 100644 --- a/src/tag-manager.ts +++ b/src/tag-manager.ts @@ -70,13 +70,10 @@ export class TagManager { } public move(format: TagSupportFormat, oldIndex: number, newIndex: number): void { + if (newIndex == oldIndex) return; let configs = this.configsMap[format], - movedConfig = configs[oldIndex]; - configs.splice( - Math.min(oldIndex, newIndex), - 0, - ...configs.splice(Math.max(oldIndex, newIndex), 1) - ); + movedConfig = configs.splice(oldIndex, 1)[0]; + configs.splice(newIndex, 0, movedConfig); this._fireHandlers(format, "move", movedConfig, oldIndex, newIndex); }