fix: tags arrangement doesn't properly work

This commit is contained in:
kotaindah55 2025-04-18 15:38:12 +02:00
parent 9c424d9a51
commit af6350b898
3 changed files with 15 additions and 20 deletions

View file

@ -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,
});
}
}

View file

@ -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 {

View file

@ -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);
}