Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(sort): group folders with `folders first`/`folders last` in #sorted
Elian Doran36cfd3232572
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex eaa32b1e2b..78d218ff0b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -627,9 +627,9 @@"run_on_instance": "Define which trilium instance should run this on. Default to all instances.","run_at_hour": "On which hour should this run. Should be used together with <code>#run=hourly</code>. Can be defined multiple times for more runs during the day.","disable_inclusion": "Scripts with this label won't be included into parent script execution.",- "sorted": "Keeps child notes sorted by title alphabetically. A value sorts by other criteria instead: comma-separated levels, each a label written as <code>#name</code> or one of <code>title</code>, <code>dateCreated</code> and <code>dateModified</code>, optionally followed by <code>asc</code> or <code>desc</code>, e.g. <code>#priority desc, title</code>. A label followed by <code>first</code> or <code>last</code> groups the notes that have it, e.g. <code>#archived last, title</code>. A single name without <code>#</code> sorts by that property, or by the label of that name.",+ "sorted": "Keeps child notes sorted by title alphabetically. A value sorts by other criteria instead: comma-separated levels, each a label written as <code>#name</code> or one of <code>title</code>, <code>dateCreated</code> and <code>dateModified</code>, optionally followed by <code>asc</code> or <code>desc</code>, e.g. <code>#priority desc, title</code>. A label or <code>folders</code> followed by <code>first</code> or <code>last</code> groups the notes that have it, e.g. <code>#archived last, folders first, title</code>. A single name without <code>#</code> sorts by that property, or by the label of that name.","sort_direction": "Direction <code>#sorted</code> orders the child notes in: <code>desc</code> for descending, anything else for ascending. A level followed by its own <code>asc</code>, <code>desc</code>, <code>first</code> or <code>last</code> ignores it. Ignored unless <code>#sorted</code> is set.",- "sort_folders_first": "Folders (notes with children) should be sorted on top.",+ "sort_folders_first": "Folders (notes with children) should be sorted on top. Ignored when <code>#sorted</code> places <code>folders</code> itself, as in <code>folders last</code>.","top": "Keep given note on top in its parent (applies only on sorted parents).","hide_promoted_attributes": "Hide promoted attributes on this note.","read_only": "Editor is in read only mode. Works only for text and code notes.",diff --git a/apps/server/src/assets/help/help_content.json b/apps/server/src/assets/help/help_content.jsonindex 3df2be2741..c7a1f8cdd9 100644--- a/apps/server/src/assets/help/help_content.json+++ b/apps/server/src/assets/help/help_content.json@@ -70,7 +70,7 @@"_help_bwg0e8ewQMak": "<p>Trilium is designed to store a wide variety of data, including sensitive information such as personal journals, credentials, or confidential documents. To safeguard this type of content, Trilium offers the option to protect notes, which involves the following measures:</p><ul><li><strong>Encryption:</strong> Protected notes are encrypted using a key derived from your password. This ensures that without the correct password, protected notes remain indecipherable. Even if someone gains access to your Trilium <a href=\"#root/_help_wX4HbRucYSDD\">database</a>, they won't be able to read your encrypted notes.</li><li><strong>Time-limited access:</strong> To access protected notes, you must first enter your password, which decrypts the note for reading and writing. However, after a specified period of inactivity (10 minutes by default), the note is unloaded from memory, requiring you to re-enter your password to access it again.<ul><li>The session timeout is extended automatically while you're interacting with the protected note, so if you're actively editing, the session remains open. However, if you switch to an unprotected note, the session timer starts, and the session expires after 10 minutes of inactivity unless you return to the protected notes.</li></ul></li><li><strong>Protection scope:</strong> Protected notes ensure the confidentiality of their content and partially their integrity. While unauthorized users cannot read or edit protected notes, they can still delete or move them outside of the protected session.</li></ul><h2>Using Protected Notes</h2><p>By default, notes are unprotected. To protect a note, simply click on the shield icon next to the note's title, as shown here:</p><p><img src=\"{{helpAssets}}/User%20Guide/Basic%20Concepts%20and%20Features/Notes/Protected%20Notes_protecting-note.gif\" alt=\"example animation of unlocking protected notes\"></p><h2>What is Encrypted?</h2><p>Trilium encrypts the data within protected notes but not their metadata. Specifically:</p><p><strong>Encrypted:</strong></p><ul><li>Note title</li><li>Note content</li><li>Images</li><li>File attachments</li></ul><p><strong>Not encrypted:</strong></p><ul><li>Note structure (i.e., it remains visible that there are protected notes)</li><li>Metadata, such as the last modified date</li><li><a href=\"#root/_help_zEY4DaJG4YT5\">Attributes</a></li></ul><h2>Encryption Details</h2><p>The following steps outline how encryption and decryption work in Trilium:</p><ol><li>The user enters a password.</li><li>The password is passed through the <a href=\"https://en.wikipedia.org/wiki/Scrypt\">scrypt</a> algorithm along with a \"password verification\" <a href=\"https://en.wikipedia.org/wiki/Salt_(cryptography)\">salt</a> to confirm that the password is correct.</li><li>The password is then processed again through scrypt with an \"encryption\" salt, which generates a hash.<ul><li>Scrypt is used for <a href=\"https://en.wikipedia.org/wiki/Key_stretching\">key stretching</a> to make the password harder to guess.</li></ul></li><li>The generated hash is used to decrypt the actual <em>data encryption key</em>.<ul><li>The data encryption key is encrypted using <a href=\"https://en.wikipedia.org/wiki/Advanced_Encryption_Standard\">AES-128</a> with a random <a href=\"https://en.wikipedia.org/wiki/Initialization_vector\">IV</a>.</li><li>The data encryption key is randomly generated during the <a href=\"#root/_help_wX4HbRucYSDD\">database</a> initialization and remains constant throughout the document’s lifetime. When the password is changed, only this key is re-encrypted.</li></ul></li><li>The data encryption key is then used to decrypt the actual content of the note, including its title and body.<ul><li>The encryption algorithm used is AES-128 with <a href=\"https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation\">CBC mode</a>, where a unique IV is generated for each encryption operation and stored with the cipher text.</li></ul></li></ol><h2>Sharing Protected Notes</h2><p>Protected notes cannot be shared in the same way as regular notes. Their encryption ensures that only authorized users with the correct password can access them.</p>","_help_MKmLg5x6xkor": "<p>Archived notes are notes which have <code>archived</code> <a href=\"#root/_help_zEY4DaJG4YT5\">attribute</a> - either directly or <a href=\"#root/_help_bwZpz2ajCEwO\">inherited</a>.</p><p>Such notes are then by default not shown in the autocomplete and in the full text <a href=\"#root/_help_eIg8jdvaoNNd\">search</a>.</p><p>This can be useful for notes which are no longer very useful but still valuable enough to keep around without them getting too much in the way.</p><p>You can control whether archived notes are displayed in the note tree with a setting:</p><p><img src=\"{{helpAssets}}/User%20Guide/Basic%20Concepts%20and%20Features/Notes/Archived%20Notes_hide-archived.png\"></p>","_help_vZWERwf8U3nx": "<figure class=\"image\"><img style=\"aspect-ratio:2089/1515;\" src=\"{{helpAssets}}/User%20Guide/Basic%20Concepts%20and%20Features/Notes/Note%20Revisions_image.png\" width=\"2089\" height=\"1515\"></figure><p>Trilium supports seamless versioning of notes by storing snapshots (\"revisions\") of notes at regular intervals.</p><p>To display the revisions, press the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> and select <em>Note revisions…</em> (<span class=\"tn-icon bx bx-history\"></span>).</p><h2>Interaction</h2><aside class=\"admonition note\"><p>This documentation matches the redesign of the note revisions dialog on v0.103.0, older versions have a similar dialog but with some differences.</p></aside><ul><li>The full list of revisions are displayed on the left in reverse chronological order.<ul><li>The revisions are grouped by the date the revision was taken.</li><li>This list does not contain the <em>current state</em> of the note, so it is possible to have notes with no revisions/snapshots saved.</li></ul></li><li>The icon of a revision indicates the <em>source</em> of that revision (e.g. a <span class=\"tn-icon bx bx-save\"></span> icon for a manually saved revision).</li><li>Pressing the <span class=\"tn-icon bx bx-dots-horizontal-rounded\"></span> button on the top-right of the dialog displays multiple options, including:<ul><li><span class=\"tn-icon bx bx-save\"></span> Saving a new revision now.</li><li>Checking the interval and limit for this note (see below).</li><li><span class=\"tn-icon bx bx-trash\"></span> Deleting all the revisions of this note.</li></ul></li><li>For supported notes (text, code), changes are highlighted. This behavior can be toggled via the <em>Highlight changes</em> at the top of the dialog.<ul><li>The highlighted changes are relative to the <strong>current state of the note</strong>, not to the revision prior to this one.</li></ul></li><li>For any given revision, the buttons on the top-right allow operating on it:<ul><li><span class=\"tn-icon bx bx-trash\"></span> Deleting the revision.</li><li><span class=\"tn-icon bx bx-download\"></span> Downloading the revision locally.</li><li><span class=\"tn-icon bx bx-history\"></span> Restoring the revision, which replaces the current content of the note with the one from the revision. Another revision is saved containing the current content of the note.</li></ul></li></ul><h2>Named revisions</h2><p>Named revisions are a new feature of Trilium v0.103.0 which allows adding a short description of what the changes in the snapshot contain.</p><p>In the list of note revisions:</p><ul><li>The name of the revision is displayed underneath the time of the revision in the sidebar, as well as at the top of the dialog where it is displayed in full.</li><li>Clicking on the <span class=\"tn-icon bx bx-edit-alt\"></span> edit button near the name of the revision allows it to be changed.</li></ul><p>To create a named revision, either:</p><ul><li>Go to the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a>, select <em>Save named revision…</em> (<span class=\"tn-icon bx bx-purchase-tag\"></span>), enter the name of revision and confirm.</li><li>Use the corresponding <a href=\"#root/_help_A9Oc6YKKc65v\">keyboard shortcut</a> or the <a class=\"reference-link\" href=\"#root/_help_F1r9QtzQLZqm\">Jump to & command palette</a> command with the same name.</li><li>Save a revision normally, and adjust the name afterwards from the note revision list.</li></ul><h2>When revisions are saved</h2><p>Revisions are saved:</p><ul><li>Automatically at a fixed interval. This behavior can be configured (see below).</li><li>Manually, by:<ul><li>Going to the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> and selecting <em>Save revision</em> (<span class=\"tn-icon bx bx-save\"></span>).</li><li>Using the <em>Force Save Revision</em> <a href=\"#root/_help_A9Oc6YKKc65v\">keyboard shortcut</a>.</li><li>In the <em>Note Revisions</em> dialog, pressing the <span class=\"tn-icon bx bx-dots-horizontal-rounded\"></span> button in the top-right and selecting <em>Save a revision now</em>.</li></ul></li></ul><p>Additionally, revisions can also come from somewhere else, and this is indicated via the icon of the revision:</p><ul><li><span class=\"tn-icon bx bx-code-alt\"></span> Generated externally, by <a class=\"reference-link\" href=\"#root/_help_pgxEVkzLl1OP\">ETAPI (REST API)</a>.</li><li><span class=\"tn-icon bx bx-bot\"></span> A modification created by <a class=\"reference-link\" href=\"#root/_help_GBBMSlVSOIGP\">AI</a>.</li><li><span class=\"tn-icon bx bx-history\"></span> A revision is restored, causing the existing note content to be saved as a revision to prevent potential data loss.</li></ul><h4>Snapshot interval</h4><p>Time interval of taking note snapshot is configurable in the Options → Other dialog. This provides a trade-off between more revisions and more data to store.</p><p>To turn off note versioning for a particular note (or sub-tree), add <code>disableVersioning</code> <a href=\"#root/_help_zEY4DaJG4YT5\">label</a> to the note.</p><h4>Maximum revisions</h4><p>The limit on the number of note snapshots can be configured in the Options → Other dialog. The note revision snapshot number limit refers to the maximum number of revisions that can be saved for each note. Where -1 means no limit, 0 means delete all revisions. You can set the maximum revisions for a single note through the <code>versioningLimit=X</code> label.</p><p>The note limit will not take effect immediately; it will only apply when the note is modified.</p><p>You can click the <em>Erase now</em> button under <em>Erase excess revision snapshots</em> to apply the changes immediately.</p>",- "_help_aGlEvb9hyDhS": "<h2>Manual sorting</h2><p>You can sort notes by right-clicking the parent note in the <a class=\"reference-link\" href=\"#root/_help_oPVyFC7WL2Lp\">Note Tree</a> and selecting Advanced -> Sort notes by ... This will sort existing notes, but will not automatically sort future notes added to this parent note.</p><p>The sorting dialog allows:</p><ul><li>Sorting by title, creation or modification date, or by the value of a label on the child notes.</li><li>Sorting by several such criteria in turn, each ascending or descending: the next level only decides between notes the previous ones left equal.</li><li>Ensuring folders are displayed at the top.</li><li>Natural sort, based on the sorting rules of a particular language.</li></ul><h2>Automatic/Permanent Sorting</h2><p>Child notes can be automatically sorted by attaching specific <a href=\"#root/_help_zEY4DaJG4YT5\">labels</a> to the parent note:</p><figure class=\"table\"><table><thead><tr><th>Label</th><th>Description</th></tr></thead><tbody><tr><td><code>#sorted</code></td><td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each a label on the child notes written with <code>#</code>, or one of the properties <code>title</code>, <code>dateCreated</code> and <code>dateModified</code>, optionally followed by <code>asc</code> or <code>desc</code>, as in a search's <code>orderBy</code>. For example <code>#sorted=\"#priority desc, #dueDate\"</code> sorts by priority, highest first, and notes of equal priority by due date. A child note without a level's label sorts after every child that has it, whichever direction the level runs in. When neither note has the label, the next level decides.</p><p>A label followed by <code>first</code> or <code>last</code> instead groups the child notes that have it before or after the rest, whatever its value. For example <code>#sorted=\"#archived last, title\"</code> keeps <a href=\"#root/_help_MKmLg5x6xkor\">archived notes</a> below the others.</p></td></tr><tr><td><code>#sortDirection</code></td><td><p>If <code>sorted</code> is applied, specifies the direction of the sort:</p><ul><li><code>ASC</code>, ascending (default)</li><li><code>DESC</code>, descending</li></ul><p>A level of <code>sorted</code> followed by its own <code>asc</code> or <code>desc</code> keeps that direction regardless, and so does a <code>first</code> or <code>last</code> group.</p></td></tr><tr><td><code>#sortFoldersFirst</code></td><td>If <code>sorted</code> is applied, folders (notes with children) will be sorted as a group at the top (at the bottom when <code>#sortDirection</code> is <code>desc</code>), and the rest will be sorted.</td></tr><tr><td><code>#sortNatural</code></td><td>Sort numbers naturally instead of alphabetically, so 2 comes before 10.</td></tr><tr><td><code>#sortLocale</code></td><td>The language code driving the natural sort (e.g. <code>zh-CN</code>, <code>de</code>). Only meaningful together with <code>#sortNatural</code>.</td></tr><tr><td><code>#top</code></td><td>If <code>sorted</code> is applied to the parent note, keeps given note on top in its parent.</td></tr><tr><td><code>#bottom</code></td><td>If <code>sorted</code> is applied to the parent note, keeps given note on bottom in its parent.</td></tr></tbody></table></figure><p>Sorting is done by comparing note properties or specific labels on child notes. There are four sorting levels, with the first having the highest priority. Lower priority levels are applied only if higher priority comparisons result in equality.</p><ol><li><strong>Top Label Sorting</strong>: Child notes with the <code>#top</code> label will appear at the top of the folder, unless <code>#sorted</code> places <code>#top</code> itself (see below).</li><li><strong>Bottom Label Sorting</strong>: (Introduced in Trilium 0.62) Child notes with the <code>#bottom</code> label will appear at the bottom of the folder, unless <code>#sorted</code> places <code>#bottom</code> itself.</li><li><strong>Property/Label-Based Sorting</strong>: Sorting is based on the parent note's <code>#sorted</code> label:<ul><li><strong>Default Sorting</strong>: If <code>#sorted</code> has no value, notes are sorted alphabetically.</li><li><strong>Property Sorting</strong>: If <code>#sorted</code> is set to <code>title</code>, <code>dateModified</code>, or <code>dateCreated</code>, notes are sorted based on the specified property.</li><li><strong>Label Sorting</strong>: If <code>#sorted</code> is set to the name of a child note's label, sorting is based on the values of this label. For example, setting <code>#sorted=#myOrder</code> on the parent note and using <code>#myOrder=001</code>, <code>#myOrder=002</code>, etc., on child notes. A single name without <code>#</code> that is not one of the properties above is also treated as a label, so <code>#sorted=myOrder</code> works the same way.</li><li><strong>Multi-Level Sorting</strong>: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. A label is written with <code>#</code> and a property without it. Every level can carry its own direction as a word after the name, <code>asc</code> or <code>desc</code>, otherwise it follows <code>#sortDirection</code>. For example <code>#sorted=\"#priority desc, #area, dateCreated\"</code> sorts by priority, highest first, then by area, then by creation date.</li><li><strong>Grouping</strong>: A label followed by <code>first</code> or <code>last</code> puts the child notes that have it, with any value, before or after the rest, regardless of <code>#sortDirection</code>. The following levels then sort within each group. For example <code>#sorted=\"#archived last, #priority desc\"</code>. Notes with <code>#top</code> or <code>#bottom</code> stay at the top or bottom even when they belong to a group, unless <code>#sorted</code> names those labels too.</li><li><strong>Placing <code>#top</code> and <code>#bottom</code></strong>: Naming <code>#top</code> or <code>#bottom</code> in <code>#sorted</code> applies it at that position instead of before every level. For example <code>#sorted=\"#archived last, #top first, #bottom last, title\"</code> keeps archived notes below all the others, including those with <code>#bottom</code>, and <code>#top</code> and <code>#bottom</code> then apply within each of the two groups. <code>#sortFoldersFirst</code> groups folders right after the last of <code>#top</code> and <code>#bottom</code>, so here archived folders also sort below the other notes. Written with <code>first</code> or <code>last</code>, the labels ignore their values; to order several pinned notes by value, add a level such as <code>#top</code> or <code>#bottom desc</code> after them.</li></ul></li><li><strong>Alphabetical Sorting</strong>: Used as a last resort when other criteria result in equality.</li></ol><aside class=\"admonition warning\"><p>Since v0.107.0, a label in a multi-level <code>#sorted</code> must be written with <code>#</code>. In v0.106.0 the names were written bare, as in <code>#sorted=\"priority desc, dueDate\"</code>; such a value now reads both names as properties that do not exist and falls back to sorting by title. Add a <code>#</code> before each label name to restore it.</p></aside><p>All comparisons are made string-wise (e.g., \"1\" < \"2\" or \"2020-10-10\" < \"2021-01-15\", but also \"2\" > \"10\").</p>",+ "_help_aGlEvb9hyDhS": "<h2>Manual sorting</h2><p>You can sort notes by right-clicking the parent note in the <a class=\"reference-link\" href=\"#root/_help_oPVyFC7WL2Lp\">Note Tree</a> and selecting Advanced -> Sort notes by ... This will sort existing notes, but will not automatically sort future notes added to this parent note.</p><p>The sorting dialog allows:</p><ul><li>Sorting by title, creation or modification date, or by the value of a label on the child notes.</li><li>Sorting by several such criteria in turn, each ascending or descending: the next level only decides between notes the previous ones left equal.</li><li>Ensuring folders are displayed at the top.</li><li>Natural sort, based on the sorting rules of a particular language.</li></ul><h2>Automatic/Permanent Sorting</h2><p>Child notes can be automatically sorted by attaching specific <a href=\"#root/_help_zEY4DaJG4YT5\">labels</a> to the parent note:</p><figure class=\"table\"><table><thead><tr><th>Label</th><th>Description</th></tr></thead><tbody><tr><td><code>#sorted</code></td><td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each a label on the child notes written with <code>#</code>, or one of the properties <code>title</code>, <code>dateCreated</code>, <code>dateModified</code> and <code>folders</code>, optionally followed by <code>asc</code> or <code>desc</code>, as in a search's <code>orderBy</code>. For example <code>#sorted=\"#priority desc, #dueDate\"</code> sorts by priority, highest first, and notes of equal priority by due date. A child note without a level's label sorts after every child that has it, whichever direction the level runs in. When neither note has the label, the next level decides.</p><p>A label followed by <code>first</code> or <code>last</code> instead groups the child notes that have it before or after the rest, whatever its value. For example <code>#sorted=\"#archived last, title\"</code> keeps <a href=\"#root/_help_MKmLg5x6xkor\">archived notes</a> below the others.</p></td></tr><tr><td><code>#sortDirection</code></td><td><p>If <code>sorted</code> is applied, specifies the direction of the sort:</p><ul><li><code>ASC</code>, ascending (default)</li><li><code>DESC</code>, descending</li></ul><p>A level of <code>sorted</code> followed by its own <code>asc</code> or <code>desc</code> keeps that direction regardless, and so does a <code>first</code> or <code>last</code> group.</p></td></tr><tr><td><code>#sortFoldersFirst</code></td><td>If <code>sorted</code> is applied, folders (notes with children) will be sorted as a group at the top (at the bottom when <code>#sortDirection</code> is <code>desc</code>), and the rest will be sorted. Ignored when <code>sorted</code> places <code>folders</code> itself.</td></tr><tr><td><code>#sortNatural</code></td><td>Sort numbers naturally instead of alphabetically, so 2 comes before 10.</td></tr><tr><td><code>#sortLocale</code></td><td>The language code driving the natural sort (e.g. <code>zh-CN</code>, <code>de</code>). Only meaningful together with <code>#sortNatural</code>.</td></tr><tr><td><code>#top</code></td><td>If <code>sorted</code> is applied to the parent note, keeps given note on top in its parent.</td></tr><tr><td><code>#bottom</code></td><td>If <code>sorted</code> is applied to the parent note, keeps given note on bottom in its parent.</td></tr></tbody></table></figure><p>Sorting is done by comparing note properties or specific labels on child notes. There are four sorting levels, with the first having the highest priority. Lower priority levels are applied only if higher priority comparisons result in equality.</p><ol><li><strong>Top Label Sorting</strong>: Child notes with the <code>#top</code> label will appear at the top of the folder, unless <code>#sorted</code> places <code>#top</code> itself (see below).</li><li><strong>Bottom Label Sorting</strong>: (Introduced in Trilium 0.62) Child notes with the <code>#bottom</code> label will appear at the bottom of the folder, unless <code>#sorted</code> places <code>#bottom</code> itself.</li><li><strong>Property/Label-Based Sorting</strong>: Sorting is based on the parent note's <code>#sorted</code> label:<ul><li><strong>Default Sorting</strong>: If <code>#sorted</code> has no value, notes are sorted alphabetically.</li><li><strong>Property Sorting</strong>: If <code>#sorted</code> is set to <code>title</code>, <code>dateModified</code>, or <code>dateCreated</code>, notes are sorted based on the specified property. The <code>folders</code> property, which notes with children have, is meant for grouping, as in <code>folders first</code>; on its own, <code>#sorted=folders</code> still sorts by a <code>#folders</code> label.</li><li><strong>Label Sorting</strong>: If <code>#sorted</code> is set to the name of a child note's label, sorting is based on the values of this label. For example, setting <code>#sorted=#myOrder</code> on the parent note and using <code>#myOrder=001</code>, <code>#myOrder=002</code>, etc., on child notes. A single name without <code>#</code> that is not one of the properties above is also treated as a label, so <code>#sorted=myOrder</code> works the same way.</li><li><strong>Multi-Level Sorting</strong>: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. A label is written with <code>#</code> and a property without it. Every level can carry its own direction as a word after the name, <code>asc</code> or <code>desc</code>, otherwise it follows <code>#sortDirection</code>. For example <code>#sorted=\"#priority desc, #area, dateCreated\"</code> sorts by priority, highest first, then by area, then by creation date.</li><li><strong>Grouping</strong>: A label, or <code>folders</code>, followed by <code>first</code> or <code>last</code> puts the child notes that have it, with any value, before or after the rest, regardless of <code>#sortDirection</code>. The following levels then sort within each group. For example <code>#sorted=\"#archived last, #priority desc\"</code>. Notes with <code>#top</code> or <code>#bottom</code> stay at the top or bottom even when they belong to a group, unless <code>#sorted</code> names those labels too.</li><li><strong>Placing <code>#top</code> and <code>#bottom</code></strong>: Naming <code>#top</code> or <code>#bottom</code> in <code>#sorted</code> applies it at that position instead of before every level. For example <code>#sorted=\"#archived last, #top first, #bottom last, title\"</code> keeps archived notes below all the others, including those with <code>#bottom</code>, and <code>#top</code> and <code>#bottom</code> then apply within each of the two groups. Adding <code>folders first</code> after them, as in <code>#sorted=\"#archived last, #top first, #bottom last, folders first, title\"</code>, groups folders within each group too, so archived folders sort below the other notes. <code>#sortFoldersFirst</code> places <code>folders first</code> the same way, right after the last of <code>#top</code> and <code>#bottom</code>. Written with <code>first</code> or <code>last</code>, the labels ignore their values; to order several pinned notes by value, add a level such as <code>#top</code> or <code>#bottom desc</code> after them.</li></ul></li><li><strong>Alphabetical Sorting</strong>: Used as a last resort when other criteria result in equality.</li></ol><aside class=\"admonition warning\"><p>Since v0.107.0, a label in a multi-level <code>#sorted</code> must be written with <code>#</code>. In v0.106.0 the names were written bare, as in <code>#sorted=\"priority desc, dueDate\"</code>; such a value now reads both names as properties that do not exist and falls back to sorting by title. Add a <code>#</code> before each label name to restore it.</p></aside><p>All comparisons are made string-wise (e.g., \"1\" < \"2\" or \"2020-10-10\" < \"2021-01-15\", but also \"2\" > \"10\").</p>","_help_NRnIZmSMc5sj": "<figure class=\"image\"><img style=\"aspect-ratio:2023/1488;\" src=\"{{helpAssets}}/User%20Guide/Basic%20Concepts%20and%20Features/Notes/Printing%20%26%20Exporting%20as%20PDF_image.png\" width=\"2023\" height=\"1488\"><figcaption>Screenshot of the Print preview functionality introduced in v0.103.0.</figcaption></figure><p>Trilium allows printing notes to a real printer, or generating a structured PDF for a single note or for multiple notes through <a class=\"reference-link\" href=\"#root/_help_GTwFsgaA0lCt\">Collections</a>.</p><p>Note that not all note types are printable as of now. We do plan to increase the coverage of supported note types in the future.</p><p>Printing and exporting as PDF are not perfect. Due to technical limitations, and sometimes even browser or Electron glitches the text might appear cut off in some circumstances. </p><h2>Printing a note or exporting as PDF on the desktop</h2><aside class=\"admonition note\"><p>Versions prior to v0.103.0 had two different options, one for printing and another one for exporting to PDF. With the introduction of the print preview, these functions have been unified.</p></aside><p>On the desktop application of Trilium it is possible to export a note as PDF. To print a note:</p><ul><li>Press the menu button in the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> and select <em>Print note</em>.</li><li>Alternatively, printing can be triggered from a <a href=\"#root/_help_A9Oc6YKKc65v\">keyboard shortcut</a> (unassigned by default) or through the <a href=\"#root/_help_F1r9QtzQLZqm\">command palette</a>.</li></ul><p>This will trigger the print preview screen next.</p><h3>Print preview & print options</h3><p>The print preview dialog allows the following printing options to be adjusted:</p><ul><li>The printer to use<ul><li><em>Save as PDF</em> generates a PDF that is structured (maintains a table of contents, keeps the text selectable). Prefer this over other virtual PDF printers that ship with the operating system.</li></ul></li><li>Page orientation: <em>Portrait</em> (default) or <em>Landscape</em>.</li><li>Page size</li><li>Scale the entire content from 10% to 200% to improve the fit within the page.</li><li>Margins, which can be removed completely or adjusted individually for all the four edges.</li><li>Print only a subset of pages. Individual page numbers are separated by colons and hyphen-based ranges are supported (e.g. 3-5 for pages 3 to 5).</li></ul><p>Additional interaction:</p><ul><li><em>Print using system dialog</em> allows setting more options that are otherwise not available in Trilium.</li></ul><p>If the note cannot be rendered with the current options, or stops making progress for 30 seconds, the preview is replaced by an error message. Adjust the options to try again.</p><aside class=\"admonition note\"><p>Most of the options here (except printer & which pages to print) are managed at note level through <a class=\"reference-link\" href=\"#root/_help_zEY4DaJG4YT5\">Attributes</a> (such as <code>#printLandscape</code>, <code>#printPageSize</code>, <code>#printScale</code>, <code>#printMargins</code>).</p><p>This means that the print settings will be restored when printing the same note. There are no default settings that can be configured for all the notes, but this can be achieved via <a href=\"#root/_help_bwZpz2ajCEwO\">inheritable attributes</a>.</p></aside><h2>Printing on the browser</h2><p>This feature allows printing of notes. It works on both the desktop client, but also on the web.</p><p>To print a note, click the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> and select <em>Print note</em>. Depending on the size and type of the note, this can take up to a few seconds. Afterwards you will be redirected to the system/browser printing dialog.</p><p>On the server or PWA (mobile), the option is not available due to technical constraints and it will be hidden.</p><h2>Reporting issues with the rendering</h2><p>Should you encounter any visual issues in the resulting PDF file (e.g. a table does not fit properly, there is cut off text, etc.) feel free to <a href=\"#root/_help_wy8So3yZZlH9\">report the issue</a>. In this case, it's best to offer a sample note (click on the <span class=\"tn-icon cke cke-three-vertical-dots\"></span> button, select <em>Export note</em> → <em>This note and all of its descendants</em> → <em>HTML in ZIP archive</em>). Make sure not to accidentally leak any personal information.</p><p>Consider adjusting font sizes and using <a href=\"#root/_help_CohkqWQC1iBv\">page breaks</a> to work around the layout.</p><aside class=\"admonition tip\"><p>Although direct export as PDF is not available in the browser version of the application, it's still possible to generate a PDF by selecting the <em>Print</em> option instead and selecting “Save to PDF” as the printer (depending on the browser). Generally, Mozilla Firefox has better printing capabilities.</p></aside><h3>Automatic opening of the file</h3><p>When the PDF is exported, it is automatically opened with the system default application for easy preview.</p><p>Note that if you are using Linux with the GNOME desktop environment, sometimes the default application might seem incorrect (such as opening in GIMP). This is because it uses Gnome's “Recommended applications” list.</p><p>To solve this, you can change the recommended application for PDFs via this command line. First, list the available applications via <code>gio mime application/pdf</code> and then set the desired one. For example to use GNOME's Evince:</p><pre><code class=\"language-text-x-trilium-auto\">gio mime application/pdf</code></pre><h2>Printing multiple notes</h2><p>Since v0.100.0, it is possible to print more than one note at the time by using <a class=\"reference-link\" href=\"#root/_help_GTwFsgaA0lCt\">Collections</a>:</p><ol><li>First create a collection.</li><li>Configure it to use <a class=\"reference-link\" href=\"#root/_help_mULW0Q3VojwY\">List View</a>.</li><li>Print the collection note normally.</li></ol><p>The resulting collection will contain all the children of the collection, while maintaining the hierarchy.</p><aside class=\"admonition note\"><p>Not all note types are supported when printing or exporting to PDF. When an unsupported note is encountered, it is skipped. At the end, if any of the notes were skipped, a message will be displayed with the possibility of viewing the full list of skipped notes. The same limitations as the ones described in <em>Constraints & limitations</em> apply.</p></aside><h2>Keyboard shortcut</h2><p>It's possible to trigger both printing and export as PDF from the keyboard by going to <em>Keyboard shortcuts</em> in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> and assigning a key combination for:</p><ul><li><em>Print Active Note</em></li><li><em>Export Active Note as PDF</em></li></ul><h2>Constraints & limitations</h2><p>Not all <a class=\"reference-link\" href=\"#root/_help_KSZ04uQ2D1St\">Note Types</a> are supported when printing, in which case the <em>Print</em> and <em>Export as PDF</em> options will be disabled.</p><ul><li>For <a class=\"reference-link\" href=\"#root/_help_iPIMuisry3hd\">Text</a> notes, font, background and table colors are printed in the shades of light themes, whatever the current theme (see <a class=\"reference-link\" href=\"#root/_help_Gr6xFaF6ioJ5\">General formatting</a>).</li><li>For <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes:<ul><li>Line numbers are not printed.</li><li>Syntax highlighting is enabled, however a default theme (Visual Studio) is enforced.</li></ul></li><li>A <a class=\"reference-link\" href=\"#root/_help_HcABDtFCkbFN\">Render Note</a> is printed as it is displayed, without running its script again, once its <code>~renderNote</code> relation is set.</li><li>For <a class=\"reference-link\" href=\"#root/_help_GTwFsgaA0lCt\">Collections</a>, the following are supported:<ul><li><a class=\"reference-link\" href=\"#root/_help_mULW0Q3VojwY\">List View</a>, allowing to print multiple notes at once while preserving hierarchy (similar to a book).</li><li><a class=\"reference-link\" href=\"#root/_help_zP3PMqaG71Ct\">Presentation</a>, where each slide/sub-note is displayed.<ul><li>Most note types are supported, especially the ones that have an image representation such as <a class=\"reference-link\" href=\"#root/_help_grjYqerjn243\">Canvas</a> and <a class=\"reference-link\" href=\"#root/_help_gBbsAeiuUxI5\">Mind Map</a>.</li></ul></li><li><a class=\"reference-link\" href=\"#root/_help_2FvYrpmOXm29\">Table</a>, where the table is rendered in a print-friendly way.<ul><li>Tables that are too complex (especially if they have multiple columns) might not fit properly, however tables with a large number of rows are supported thanks to pagination.</li><li>Consider printing in landscape mode, or using <code>#printLandscape</code> if exporting to PDF.</li></ul></li><li>The rest of the collections are not supported, but we plan to add support for all the collection types at some point.</li></ul></li><li>Using <a class=\"reference-link\" href=\"#root/_help_AlhDUqhENtH7\">Custom app-wide CSS</a> for printing is no longer supported, instead a custom <code>printCss</code> relation needs to be used (see below).</li></ul><h2>Customizing the print CSS</h2><p>As an advanced use case, it's possible to customize the CSS used for printing such as adjusting the fonts, sizes or margins. Note that <a class=\"reference-link\" href=\"#root/_help_AlhDUqhENtH7\">Custom app-wide CSS</a> will not work for printing.</p><p>To do so:</p><ul><li>Create a CSS <a href=\"#root/_help_6f9hih2hXXZk\">code note</a>.</li><li>On the note being printed, apply the <code>~printCss</code> relation to point to the newly created CSS code note.</li><li>To apply the CSS to multiple notes, consider using <a href=\"#root/_help_bwZpz2ajCEwO\">inheritable attributes</a> or <a class=\"reference-link\" href=\"#root/_help_KC1HB96bqqHX\">Templates</a>.</li></ul><p>For example, to change the font of the document from the one defined by the theme or the user to a serif one:</p><pre><code class=\"language-text-x-trilium-auto\">body {\n\t--print-font-family: serif;\n --print-font-size: 11pt;\n}</code></pre><aside class=\"admonition important\"><p>When altering <code>--print-font-family</code>, make sure the change is done at <code>body</code> level and not <code>:root</code>, since otherwise it won't be picked up due to specificity rules.</p></aside><p>To remark:</p><ul><li>Multiple CSS notes can be add by using multiple <code>~printCss</code> relations.</li><li>If the note pointing to the <code>printCss</code> doesn't have the right note type or mime type, it will be ignored.</li><li>If migrating from a previous version where <a class=\"reference-link\" href=\"#root/_help_AlhDUqhENtH7\">Custom app-wide CSS</a>, there's no need for <code>@media print {</code> since the style-sheet is used only for printing.</li></ul><h2>Under the hood</h2><p>Both printing and exporting as PDF use the same mechanism: a note is rendered individually in a separate webpage that is then sent to the browser or the Electron application either for printing or exporting as PDF.</p><p>The webpage that renders a single note can actually be accessed in a web browser. For example <code>http://localhost:8080/#root/WWRGzqHUfRln/RRZsE9Al8AIZ?ntxId=0o4fzk</code> becomes <code>http://localhost:8080/?print#root/WWRGzqHUfRln/RRZsE9Al8AIZ</code>.</p><p>Accessing the print note in a web browser allows for easy debugging to understand why a particular note doesn't render well. The mechanism for rendering is similar to the one used in <a class=\"reference-link\" href=\"#root/_help_0ESUbbAxVnoK\">Note List</a>.</p>","_help_CoFPLs3dRlXc": "<p>Some note types such as <a class=\"reference-link\" href=\"#root/_help_iPIMuisry3hd\">Text</a>, <a class=\"reference-link\" href=\"#root/_help_6f9hih2hXXZk\">Code</a> notes and even some of the <a class=\"reference-link\" href=\"#root/_help_GTwFsgaA0lCt\">Collections</a> can be set to read-only. When a note is in read-only mode, it is presented to the user in a non-editable view, with the option to switch to editing mode if needed.</p><h2>Automatic read-only mode</h2><p>For optimization purposes, Trilium will automatically set very large notes to read-only. Displaying such lengthy notes in editing mode can slow down performance, especially when editing is unnecessary.</p><p>This behavior can be disabled on a per-note basis, by following the instructions of the next section.</p><p>In addition, it's possible to change the number of characters at which the automatic read-only mode will trigger in <a class=\"reference-link\" href=\"#root/_help_4TIF1oA4VQRO\">Options</a> by going to the options for <a class=\"reference-link\" href=\"#root/_hidden/_options/_optionsTextNotes\">Text Notes</a> and <a class=\"reference-link\" href=\"#root/_hidden/_options/_optionsCodeNotes\">Code Notes</a>.</p><h2>Changing a note's read-only behavior</h2><p>A note's read-only behavior can be changed via <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> → <em>Editable.</em> The following options are possible:</p><ul><li><strong>Auto</strong><br>This is the default behavior in which the note will be editable by default, unless it becomes large enough to trigger read-only mode.</li><li><strong>Read-only</strong><br>The note will be always marked as read-only, regardless of its size. Nevertheless, it's still possible to temporarily edit the note if needed. This is generally useful for notes that are not prone to change.</li><li><strong>Always Editable</strong><br>This option will bypass the automatic read-only activation for this particular note. It's useful for large notes that are frequently edited.</li></ul><p>If the <em>Editable</em> section is missing from the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a>, then the note type does not support read-only mode.</p><h3>Manually setting the options</h3><p>Apart from using the <a class=\"reference-link\" href=\"#root/_help_8YBEPzcpUgxw\">Note menu</a> as previously mentioned, it's also possible to use <a href=\"#root/_help_zEY4DaJG4YT5\">labels</a> to change the behavior:</p><ul><li>To set as read-only, apply the <code>readOnly</code> label to the note.</li><li>To disable automatic read-only (always editable), apply the <code>autoReadOnlyDisabled</code> label.</li></ul><h2>Temporarily editing a read-only note</h2><p>When accessing a read-only note, it's possible to temporarily edit it pressing the <em>Read-only</em> badge.</p><p>When pressed, the note will become editable but will become read-only again after navigating to a different note.</p><p>The pages of the built-in user guide are an exception: they are part of Trilium itself and cannot be edited, not even temporarily. Their <em>Read-only</em> badge cannot be pressed, the note menu does not offer to edit them, and their attributes are shown in the right sidebar without a way to change them.</p>","_help_0ESUbbAxVnoK": "<figure class=\"image\"><img style=\"aspect-ratio:990/590;\" src=\"{{helpAssets}}/User%20Guide/Basic%20Concepts%20and%20Features/Notes/Note%20List_image.png\" width=\"990\" height=\"590\"></figure><p>When a note has one or more child notes, they will be listed at the end of the note for easy navigation.</p><h2>Configuration</h2><ul><li>To hide the note list for a particular note, simply apply the <code>hideChildrenOverview</code> <a href=\"#root/_help_zEY4DaJG4YT5\">label</a>.</li><li>For some view types, such as Grid view, only a subset of notes will be displayed and pagination can be used to navigate through all of them for performance reasons. To adjust the number of notes per page, set <code>pageSize</code> to the desired number.</li></ul><h2>View types</h2><p>The view types dictate how the child notes are represented. By default, the notes will be displayed in a grid, however there are also some other view types available.</p><p>Generally the view type can only be changed from the <a class=\"reference-link\" href=\"#root/_help_CssoWBu8I7jF\">Collection Properties</a>, but it can also be changed manually on any type of note using the <code>#viewType</code> <a href=\"#root/_help_HI6GBBIduIgv\">label</a>.</p>",diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.mdindex 8a3c7137c1..58d7641c7a 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Sorting Notes.md@@ -24,7 +24,7 @@ Child notes can be automatically sorted by attaching specific [labels](../../Adv<tbody><tr><td><code>#sorted</code></td>- <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each a label on the child notes written with <code>#</code>, or one of the properties <code>title</code>, <code>dateCreated</code> and <code>dateModified</code>, optionally followed by <code>asc</code> or <code>desc</code>, as in a search's <code>orderBy</code>. For example <code>#sorted="#priority desc, #dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. A child note without a level's label sorts after every child that has it, whichever direction the level runs in. When neither note has the label, the next level decides.</p><p>A label followed by <code>first</code> or <code>last</code> instead groups the child notes that have it before or after the rest, whatever its value. For example <code>#sorted="#archived last, title"</code> keeps <a href="Archived%20Notes.md">archived notes</a> below the others.</p></td>+ <td><p>Keeps child notes sorted by title alphabetically.</p><p>When given a value, it will sort by other criteria instead: a comma-separated list of levels, each a label on the child notes written with <code>#</code>, or one of the properties <code>title</code>, <code>dateCreated</code>, <code>dateModified</code> and <code>folders</code>, optionally followed by <code>asc</code> or <code>desc</code>, as in a search's <code>orderBy</code>. For example <code>#sorted="#priority desc, #dueDate"</code> sorts by priority, highest first, and notes of equal priority by due date. A child note without a level's label sorts after every child that has it, whichever direction the level runs in. When neither note has the label, the next level decides.</p><p>A label followed by <code>first</code> or <code>last</code> instead groups the child notes that have it before or after the rest, whatever its value. For example <code>#sorted="#archived last, title"</code> keeps <a href="Archived%20Notes.md">archived notes</a> below the others.</p></td></tr><tr><td><code>#sortDirection</code></td>@@ -32,7 +32,7 @@ Child notes can be automatically sorted by attaching specific [labels](../../Adv</tr><tr><td><code>#sortFoldersFirst</code></td>- <td>If <code>sorted</code> is applied, folders (notes with children) will be sorted as a group at the top (at the bottom when <code>#sortDirection</code> is <code>desc</code>), and the rest will be sorted.</td>+ <td>If <code>sorted</code> is applied, folders (notes with children) will be sorted as a group at the top (at the bottom when <code>#sortDirection</code> is <code>desc</code>), and the rest will be sorted. Ignored when <code>sorted</code> places <code>folders</code> itself.</td></tr><tr><td><code>#sortNatural</code></td>@@ -59,11 +59,11 @@ Sorting is done by comparing note properties or specific labels on child notes.2. **Bottom Label Sorting**: (Introduced in Trilium 0.62) Child notes with the `#bottom` label will appear at the bottom of the folder, unless `#sorted` places `#bottom` itself.3. **Property/Label-Based Sorting**: Sorting is based on the parent note's `#sorted` label:* **Default Sorting**: If `#sorted` has no value, notes are sorted alphabetically.- * **Property Sorting**: If `#sorted` is set to `title`, `dateModified`, or `dateCreated`, notes are sorted based on the specified property.+ * **Property Sorting**: If `#sorted` is set to `title`, `dateModified`, or `dateCreated`, notes are sorted based on the specified property. The `folders` property, which notes with children have, is meant for grouping, as in `folders first`; on its own, `#sorted=folders` still sorts by a `#folders` label.* **Label Sorting**: If `#sorted` is set to the name of a child note's label, sorting is based on the values of this label. For example, setting `#sorted=#myOrder` on the parent note and using `#myOrder=001`, `#myOrder=002`, etc., on child notes. A single name without `#` that is not one of the properties above is also treated as a label, so `#sorted=myOrder` works the same way.* **Multi-Level Sorting**: Several of the above can be combined, separated by commas; each level is applied only where the previous ones are equal. A label is written with `#` and a property without it. Every level can carry its own direction as a word after the name, `asc` or `desc`, otherwise it follows `#sortDirection`. For example `#sorted="#priority desc, #area, dateCreated"` sorts by priority, highest first, then by area, then by creation date.- * **Grouping**: A label followed by `first` or `last` puts the child notes that have it, with any value, before or after the rest, regardless of `#sortDirection`. The following levels then sort within each group. For example `#sorted="#archived last, #priority desc"`. Notes with `#top` or `#bottom` stay at the top or bottom even when they belong to a group, unless `#sorted` names those labels too.- * **Placing `#top` and `#bottom`**: Naming `#top` or `#bottom` in `#sorted` applies it at that position instead of before every level. For example `#sorted="#archived last, #top first, #bottom last, title"` keeps archived notes below all the others, including those with `#bottom`, and `#top` and `#bottom` then apply within each of the two groups. `#sortFoldersFirst` groups folders right after the last of `#top` and `#bottom`, so here archived folders also sort below the other notes. Written with `first` or `last`, the labels ignore their values; to order several pinned notes by value, add a level such as `#top` or `#bottom desc` after them.+ * **Grouping**: A label, or `folders`, followed by `first` or `last` puts the child notes that have it, with any value, before or after the rest, regardless of `#sortDirection`. The following levels then sort within each group. For example `#sorted="#archived last, #priority desc"`. Notes with `#top` or `#bottom` stay at the top or bottom even when they belong to a group, unless `#sorted` names those labels too.+ * **Placing `#top` and `#bottom`**: Naming `#top` or `#bottom` in `#sorted` applies it at that position instead of before every level. For example `#sorted="#archived last, #top first, #bottom last, title"` keeps archived notes below all the others, including those with `#bottom`, and `#top` and `#bottom` then apply within each of the two groups. Adding `folders first` after them, as in `#sorted="#archived last, #top first, #bottom last, folders first, title"`, groups folders within each group too, so archived folders sort below the other notes. `#sortFoldersFirst` places `folders first` the same way, right after the last of `#top` and `#bottom`. Written with `first` or `last`, the labels ignore their values; to order several pinned notes by value, add a level such as `#top` or `#bottom desc` after them.4. **Alphabetical Sorting**: Used as a last resort when other criteria result in equality.> [!WARNING]diff --git a/packages/commons/src/lib/sort_criteria.spec.ts b/packages/commons/src/lib/sort_criteria.spec.tsindex 260b39bb4d..7c747fa292 100644--- a/packages/commons/src/lib/sort_criteria.spec.ts+++ b/packages/commons/src/lib/sort_criteria.spec.ts@@ -9,6 +9,12 @@ describe("sort criteria", () => {expect(parseSortCriteria(" dateCreated ")).toStrictEqual([{ key: "dateCreated", isLabel: false }]);+ // Only `title`, `dateCreated` and `dateModified` are properties on their own.+ expect(parseSortCriteria("folders")).toStrictEqual([{ key: "folders", isLabel: true }]);+ expect(parseSortCriteria("folders, title")).toStrictEqual([+ { key: "folders", isLabel: false },+ { key: "title", isLabel: false }+ ]);expect(parseSortCriteria("myOrder")).toStrictEqual([{ key: "myOrder", isLabel: true }]);expect(parseSortCriteria("calendar:view")).toStrictEqual([{ key: "calendar:view", isLabel: true }diff --git a/packages/commons/src/lib/sort_criteria.ts b/packages/commons/src/lib/sort_criteria.tsindex a58e138762..b23c868228 100644--- a/packages/commons/src/lib/sort_criteria.ts+++ b/packages/commons/src/lib/sort_criteria.ts@@ -1,15 +1,16 @@/*** The grammar of the `#sorted` label: comma-separated sort keys, each optionally followed by* `asc` or `desc`, the way a search's `orderBy` is written — `#priority desc, #dueDate, title`. A- * key is a label on the child notes written with `#`, or a bare property: `title`, `dateCreated` or- * `dateModified`. A key followed by `first` or `last` instead groups the children that have that- * label before or after the rest — `#archived last, title`.+ * key is a label on the child notes written with `#`, or a bare property: `title`, `dateCreated`,+ * `dateModified` or `folders`, which a note with children has. A key followed by `first` or `last`+ * instead groups the children that have it before or after the rest — `#archived last, title`.*- * A value that is a single bare key keeps its original meaning: a property when it names one,- * otherwise a label, so `#sorted=myOrder` sorts by `#myOrder`.+ * A value that is a single bare key keeps its original meaning: a property when it is `title`,+ * `dateCreated` or `dateModified`, otherwise a label, so `#sorted=myOrder` sorts by `#myOrder` and+ * `#sorted=folders` by `#folders`.*/-export const SORT_PROPERTIES = ["title", "dateCreated", "dateModified"];+const SINGLE_KEY_PROPERTIES = ["title", "dateCreated", "dateModified"];export interface SortCriterion {/** A label name where {@link isLabel} is set, a property name otherwise. */@@ -17,7 +18,7 @@ export interface SortCriterion {isLabel: boolean;/** Set by an explicit `asc`/`desc` after the key; absent follows `#sortDirection`. */descending?: boolean;- /** Set by `first`/`last` after the key: groups by whether a child has the label. */+ /** Set by `first`/`last` after the key: groups by whether a child has the key at all. */group?: "first" | "last";}@@ -25,7 +26,7 @@ export interface SortCriterion {export function parseSortCriteria(value: string | null | undefined): SortCriterion[] {const trimmed = (value ?? "").trim();if (/^[^\s,#]+$/.test(trimmed)) {- return [{ key: trimmed, isLabel: !SORT_PROPERTIES.includes(trimmed) }];+ return [{ key: trimmed, isLabel: !SINGLE_KEY_PROPERTIES.includes(trimmed) }];}const criteria: SortCriterion[] = [];diff --git a/packages/trilium-core/src/services/tree.spec.ts b/packages/trilium-core/src/services/tree.spec.tsindex 7da85a0548..fca75b8d20 100644--- a/packages/trilium-core/src/services/tree.spec.ts+++ b/packages/trilium-core/src/services/tree.spec.ts@@ -407,6 +407,48 @@ describe("Tree", () => {]);});+ it("groups folders with `folders first` or `folders last`, overriding #sortFoldersFirst", () => {+ const children: Parameters<typeof buildNote>[0]["children"] = [+ {title: "b"},+ {title: "p2", children: [{title: "2.1"}]},+ {title: "a"},+ {title: "p1", children: [{title: "1.1"}]}+ ];+ for (const [sorted, direction, expected] of [+ ["folders first, title", "desc", ["p2", "p1", "b", "a"]],+ ["folders last, title", "asc", ["a", "b", "p1", "p2"]]+ ] as const) {+ const note = buildNote({+ children, "#sorted": sorted, "#sortDirection": direction, "#sortFoldersFirst": ""+ });+ getContext().init(() => {+ tree.sortNotesIfNeeded(note.noteId);+ });+ expect(note.children.map((child) => child.title)).toStrictEqual([...expected]);+ }+ });++ it("places a folders level where #sorted writes it", () => {+ const note = buildNote({+ children: [+ {title: "archived folder", "#archived": "", children: [{title: "1.1"}]},+ {title: "archived", "#archived": ""},+ {title: "folder", children: [{title: "2.1"}]},+ {title: "leaf"},+ {title: "bottom folder", "#bottom": "", children: [{title: "3.1"}]},+ {title: "top", "#top": ""}+ ],+ "#sorted": "#archived last, #top first, #bottom last, folders first, title"+ });+ getContext().init(() => {+ tree.sortNotesIfNeeded(note.noteId);+ });+ const orderedTitles = note.children.map((child) => child.title);+ expect(orderedTitles).toStrictEqual([+ "top", "folder", "leaf", "bottom folder", "archived folder", "archived"+ ]);+ });+it("orders the same children the same way whatever order they start in", () => {const titles = Array.from({length: 53}, (_, index) => `Week ${index + 1}`);const orders = [titles, [...titles].reverse()].map((startingOrder) => {diff --git a/packages/trilium-core/src/services/tree.ts b/packages/trilium-core/src/services/tree.tsindex b23e259ef8..fbe11d430e 100644--- a/packages/trilium-core/src/services/tree.ts+++ b/packages/trilium-core/src/services/tree.ts@@ -92,8 +92,8 @@ function wouldAddingBranchCreateCycle(parentNoteId: string, childNoteId: string)/*** Sorts the children of `parentNoteId` by the levels of `sortBy`; `reverse` is the direction of a- * level without its own, of the folders grouping and of ties, never of a `first`/`last` grouping- * level or of the `#top` and `#bottom` levels {@link withPinningLevels} adds.+ * level without its own, of the `#sortFoldersFirst` grouping and of ties, never of a `first`/`last`+ * grouping level or of the `#top` and `#bottom` levels {@link withPinningLevels} adds.*/function sortNotes(parentNoteId: string,@@ -103,7 +103,7 @@ function sortNotes(sortNatural = false,_sortLocale?: string | null) {- const levels = withPinningLevels(parseSortCriteria(sortBy), foldersFirst);+ const levels = withPinningLevels(parseSortCriteria(sortBy), foldersFirst, reverse);// sortLocale can not be empty string or null value, default value must be set to undefined.const sortLocale = _sortLocale || undefined;@@ -129,6 +129,8 @@ function sortNotes(rawValue = prefix ? `${prefix} - ${note.title}` : note.title;} else if (key === "dateCreated" || key === "dateModified") {rawValue = note[key] ?? null;+ } else if (key === "folders") {+ rawValue = note.hasChildren() ? "" : null;}return typeof rawValue === "string" ? rawValue.toLowerCase() : rawValue;@@ -166,25 +168,11 @@ function sortNotes(return (hasA ? -1 : 1) * (group === "last" ? -1 : 1);}- // Folders group first ascending and last descending.- function compareFolders(a: BNote, b: BNote) {- const aHasChildren = a.hasChildren();- if (aHasChildren === b.hasChildren()) {- return 0;- }- return (aHasChildren ? -1 : 1) * (reverse ? -1 : 1);- }-notes.sort((a, b) => {for (const level of levels) {- let result: number;- if (level === FOLDERS_LEVEL) {- result = compareFolders(a, b);- } else if (level.group) {- result = compareGroup(a, b, level);- } else {- result = compareLevel(a, b, level, level.descending ?? reverse);- }+ const result = level.group+ ? compareGroup(a, b, level)+ : compareLevel(a, b, level, level.descending ?? reverse);if (result !== 0) {return result;}@@ -222,23 +210,20 @@ function sortNotes(});}-const FOLDERS_LEVEL = "folders";-/*** Puts `#top asc` before `criteria` unless they name `#top`, then `#bottom last, #bottom desc`- * unless they name `#bottom`, and the folders grouping right after the last `#top` or `#bottom`- * level.+ * unless they name `#bottom`. Unless they name `folders`, `#sortFoldersFirst` adds `folders first`+ * (`folders last` when `reverse`) right after the last `#top` or `#bottom` level.*/-function withPinningLevels(criteria: SortCriterion[], foldersFirst: boolean) {- const isPinning = (level: SortCriterion) =>- level.isLabel && (level.key === "top" || level.key === "bottom");- const names = (key: string) => criteria.some((level) => level.isLabel && level.key === key);+function withPinningLevels(criteria: SortCriterion[], foldersFirst: boolean, reverse: boolean) {+ const names = (key: string, isLabel: boolean) =>+ criteria.some((level) => level.isLabel === isLabel && level.key === key);- const levels: (SortCriterion | typeof FOLDERS_LEVEL)[] = [];- if (!names("top")) {+ const levels: SortCriterion[] = [];+ if (!names("top", true)) {levels.push({ key: "top", isLabel: true, descending: false });}- if (!names("bottom")) {+ if (!names("bottom", true)) {levels.push({ key: "bottom", isLabel: true, group: "last" },{ key: "bottom", isLabel: true, descending: true }@@ -246,11 +231,13 @@ function withPinningLevels(criteria: SortCriterion[], foldersFirst: boolean) {}levels.push(...criteria);- if (foldersFirst) {+ if (foldersFirst && !names("folders", false)) {const lastPinning = levels.findLastIndex(- (level) => level !== FOLDERS_LEVEL && isPinning(level)+ ({ key, isLabel }) => isLabel && (key === "top" || key === "bottom"));- levels.splice(lastPinning + 1, 0, FOLDERS_LEVEL);+ levels.splice(lastPinning + 1, 0, {+ key: "folders", isLabel: false, group: reverse ? "last" : "first"+ });}return levels;}