Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Introduce support for sorting Dataview tables in Live Preview.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandru-dinu committed Mar 16, 2022
1 parent b0dd3b9 commit dd986c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ https://user-images.githubusercontent.com/14110183/128138299-fd2a1bb2-6f87-4b50-
</details>

- Sorting numerical and string data types. Custom comparator functions are part of the roadmap (see this [issue](https://github.com/alexandru-dinu/obsidian-sortable/issues/12)).
- No altering of the markdown source code. Sorting is done solely in reading (i.e. non-editing) mode by rearranging rows (i.e. `tr` elements).
- No altering of the markdown source code. Sorting is done by rearranging table rows (i.e. `tr` elements).
- No dependencies.

## Installation
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-sortable",
"name": "Sortable",
"version": "0.2.2",
"version": "0.2.3",
"minAppVersion": "0.12.0",
"description": "Wiki-like table sorting.",
"author": "Alexandru Dinu",
Expand Down
14 changes: 13 additions & 1 deletion src/sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,24 @@ export class TableState {

export type TTableStates = WeakMap<HTMLTableElement, TableState>;

function shouldSort(htmlEl: HTMLElement): boolean {
// dataview table: parent must be a "dataview" HTMLTableElement
const p = htmlEl.matchParent(".dataview");
if (p instanceof HTMLTableElement)
return true;

// reading mode, i.e. non-editing
return null !== htmlEl.matchParent(".markdown-reading-view");
}

export function onHeadClick(evt: MouseEvent, tableStates: TTableStates): void {
// check if the clicked element is a table header
const htmlEl = <HTMLElement>evt.target;
if (!htmlEl.matchParent(".markdown-preview-view")) {

if (!shouldSort(htmlEl)) {
return;
}

const th = htmlEl.closest("thead th");
if (th === null) {
return;
Expand Down

0 comments on commit dd986c9

Please sign in to comment.