« get me outta code hell

data: DocumentSortingRule.selectDocumentsUnder - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2025-02-22 21:24:32 -0400
committer(quasar) nebula <qznebula@protonmail.com>2025-02-22 21:24:32 -0400
commit3a88bab962ca0d7613e9fd5e3146be195bcb0e70 (patch)
treec2495c30c0a230280cd1076d8dfc4b370bbe4201
parent01fba0a54a42223e9aabd9ce86433af76dc8ead8 (diff)
data: DocumentSortingRule.selectDocumentsUnder
-rw-r--r--src/data/things/sorting-rule.js35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/data/things/sorting-rule.js b/src/data/things/sorting-rule.js
index 31f0a263..18da452e 100644
--- a/src/data/things/sorting-rule.js
+++ b/src/data/things/sorting-rule.js
@@ -198,13 +198,26 @@ export class DocumentSortingRule extends ThingSortingRule {
         },
       },
     },
+
+    selectDocumentsUnder: {
+      flags: {update: true, expose: true},
+      update: {validate: isStringNonEmpty},
+    },
   });
 
   static [Thing.yamlDocumentSpec] = Thing.extendDocumentSpec(ThingSortingRule, {
     fields: {
       'Sort Documents': {property: 'filename'},
       'Select Documents Following': {property: 'selectDocumentsFollowing'},
+      'Select Documents Under': {property: 'selectDocumentsUnder'},
     },
+
+    invalidFieldCombinations: [
+      {message: `Specify only one of these`, fields: [
+        'Select Documents Following',
+        'Select Documents Under',
+      ]},
+    ],
   });
 
   static check(rule, {wikiData}) {
@@ -329,6 +342,28 @@ export class DocumentSortingRule extends ThingSortingRule {
       this.sort(subsortable);
 
       sortable.splice(after + 1, before - after - 1, ...subsortable);
+    } else if (this.selectDocumentsUnder) {
+      const field = this.selectDocumentsUnder;
+
+      const indices =
+        Array.from(sortable.entries())
+          .filter(([_index, thing]) =>
+            Object.hasOwn(thing[Thing.yamlSourceDocument], field))
+          .map(([index, _thing]) => index);
+
+      for (const [indicesIndex, after] of indices.entries()) {
+        const before =
+          (indicesIndex === indices.length - 1
+            ? sortable.length
+            : indices[indicesIndex + 1]);
+
+        const subsortable =
+          sortable.slice(after + 1, before);
+
+        this.sort(subsortable);
+
+        sortable.splice(after + 1, before - after - 1, ...subsortable);
+      }
     } else {
       this.sort(sortable);
     }