Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit f9dec74

Browse files
authored
Merge pull request #47 from wwoods/dev
Fixed sort orders based on note properties available in config
2 parents 892feb0 + f75cffe commit f9dec74

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

Diff for: README.md

+14
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ display:
8585

8686
Visual examples and more info: https://github.com/joplin/plugin-kanban/pull/19
8787

88+
### Sort
89+
90+
By default, the kanban plugin sorts each column based on the user's dragging and dropping of notes across the kanban board, with new notes going at the top. To specify a fixed sort pattern based on note properties instead, use the following config:
91+
92+
```yaml
93+
```kanban
94+
sort:
95+
by: title
96+
```
97+
98+
Descending sort order may be specified by prefixing `-`, e.g., `-title`.
99+
100+
The configured sort order will apply to all columns.
101+
88102
## Further information
89103

90104
If you want to know more about the workings of the plugin or its development check out the [original proposal](https://discourse.joplinapp.org/t/kanban-board-project/17469) and the [progress reports](https://discourse.joplinapp.org/t/kanban-board-project/17469)

Diff for: src/board.ts

+21
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ export default class Board {
213213
* i.e. sorted columns, messages, and hidden tags.
214214
*/
215215
async getBoardState(allNotes: NoteData[]): Promise<BoardState> {
216+
const config = this.parsedConfig;
216217
const state: BoardState = {
217218
name: this.boardName,
218219
messages: [],
@@ -231,8 +232,28 @@ export default class Board {
231232
sortedNotes
232233
).map(([name, notes]) => ({ name, notes }));
233234

235+
let sortCol = config ? config.sort?.by : undefined;
236+
let sortDir = 1;
237+
if (sortCol !== undefined) {
238+
if (sortCol.startsWith('-')) {
239+
sortDir = -1;
240+
sortCol = sortCol.substring(1);
241+
}
242+
}
234243
Object.values(sortedColumns).forEach((col) =>
235244
col.notes.sort((a, b) => {
245+
if (sortCol !== undefined) {
246+
const va = (a as any)[sortCol];
247+
const vb = (b as any)[sortCol];
248+
const v = (
249+
(typeof va === "string")
250+
? va.toLocaleLowerCase().localeCompare(vb.toLocaleLowerCase())
251+
: va - vb
252+
);
253+
return v * sortDir;
254+
}
255+
256+
// Otherwise, use user-order specified on Kanban board
236257
if (a.order < b.order) return +1;
237258
if (a.order > b.order) return -1;
238259
return a.createdTime < b.createdTime ? +1 : -1;

Diff for: src/parser.ts

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ export const validateConfig = (config: Config | {} | null): Message | null => {
6161
}
6262
}
6363

64+
if ("sort" in config) {
65+
if (typeof config.sort !== "object" || config.sort.by === undefined)
66+
return configErr("Sort must be a dictionary with a single 'by' field");
67+
let cs = config.sort.by;
68+
if (cs.startsWith('-')) cs = cs.substring(1);
69+
if (['createdTime', 'title'].indexOf(cs) === -1)
70+
return configErr("Sort must be one of 'createdTime', 'title'; optionally prefix by '-' for descending order");
71+
}
72+
6473
for (const col of config.columns) {
6574
if (typeof col !== "object" || Array.isArray(col))
6675
return configErr(

Diff for: src/types.ts

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export interface Config {
2525
[ruleName: string]: RuleValue;
2626
rootNotebookPath?: string;
2727
};
28+
sort: {
29+
by?: string;
30+
};
2831
columns: {
2932
[ruleName: string]: RuleValue;
3033
name: string;

0 commit comments

Comments
 (0)