]> BookStack Code Mirror - bookstack/commitdiff
Added prevention of nested chapters on sort 3999/head
authorDan Brown <redacted>
Fri, 27 Jan 2023 17:39:51 +0000 (17:39 +0000)
committerDan Brown <redacted>
Fri, 27 Jan 2023 17:39:51 +0000 (17:39 +0000)
resources/js/components/book-sort.js

index 6e56e43a52315e691cbff2757b9817b09e8848a5..5ae283fd017520b02adbb10fc0c56980eda2bd8f 100644 (file)
@@ -260,7 +260,8 @@ export class BookSort extends Component {
                 animation: 150,
                 fallbackOnBody: true,
                 swapThreshold: 0.65,
-                onSort: () => {
+                onSort: (event) => {
+                    this.ensureNoNestedChapters()
                     this.updateMapInput();
                     this.updateMoveActionStateForAll();
                 },
@@ -273,6 +274,20 @@ export class BookSort extends Component {
         }
     }
 
+    /**
+     * Handle nested chapters by moving them to the parent book.
+     * Needed since sorting with multi-sort only checks group rules based on the active item,
+     * not all in group, therefore need to manually check after a sort.
+     * Must be done before updating the map input.
+     */
+    ensureNoNestedChapters() {
+        const nestedChapters = this.container.querySelectorAll('[data-type="chapter"] [data-type="chapter"]');
+        for (const chapter of nestedChapters) {
+            const parentChapter = chapter.parentElement.closest('[data-type="chapter"]');
+            parentChapter.insertAdjacentElement('afterend', chapter);
+        }
+    }
+
     /**
      * Update the input with our sort data.
      */