Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed sort orders based on note properties available in config #47

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ display:

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

### Sort

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:

```yaml
```kanban
sort:
by: title
```

Descending sort order may be specified by prefixing `-`, e.g., `-title`.

The configured sort order will apply to all columns.

## Further information

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)
Expand Down
21 changes: 21 additions & 0 deletions src/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export default class Board {
* i.e. sorted columns, messages, and hidden tags.
*/
async getBoardState(allNotes: NoteData[]): Promise<BoardState> {
const config = this.parsedConfig;
const state: BoardState = {
name: this.boardName,
messages: [],
Expand All @@ -231,8 +232,28 @@ export default class Board {
sortedNotes
).map(([name, notes]) => ({ name, notes }));

let sortCol = config ? config.sort?.by : undefined;
let sortDir = 1;
if (sortCol !== undefined) {
if (sortCol.startsWith('-')) {
sortDir = -1;
sortCol = sortCol.substring(1);
}
}
Object.values(sortedColumns).forEach((col) =>
col.notes.sort((a, b) => {
if (sortCol !== undefined) {
const va = (a as any)[sortCol];
const vb = (b as any)[sortCol];
const v = (
(typeof va === "string")
? va.toLocaleLowerCase().localeCompare(vb.toLocaleLowerCase())
: va - vb
);
return v * sortDir;
}

// Otherwise, use user-order specified on Kanban board
if (a.order < b.order) return +1;
if (a.order > b.order) return -1;
return a.createdTime < b.createdTime ? +1 : -1;
Expand Down
9 changes: 9 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ export const validateConfig = (config: Config | {} | null): Message | null => {
}
}

if ("sort" in config) {
if (typeof config.sort !== "object" || config.sort.by === undefined)
return configErr("Sort must be a dictionary with a single 'by' field");
let cs = config.sort.by;
if (cs.startsWith('-')) cs = cs.substring(1);
if (['createdTime', 'title'].indexOf(cs) === -1)
return configErr("Sort must be one of 'createdTime', 'title'; optionally prefix by '-' for descending order");
}

for (const col of config.columns) {
if (typeof col !== "object" || Array.isArray(col))
return configErr(
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ export interface Config {
[ruleName: string]: RuleValue;
rootNotebookPath?: string;
};
sort: {
by?: string;
};
columns: {
[ruleName: string]: RuleValue;
name: string;
Expand Down