|
|
|
/*
|
|
|
|
THIS IS A GENERATED/BUNDLED FILE BY ROLLUP
|
|
|
|
if you want to view the source visit the plugins github repository
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var obsidian = require('obsidian');
|
|
|
|
|
|
|
|
/*! *****************************************************************************
|
|
|
|
Copyright (c) Microsoft Corporation.
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
purpose with or without fee is hereby granted.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
***************************************************************************** */
|
|
|
|
|
|
|
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var SortOrder;
|
|
|
|
(function (SortOrder) {
|
|
|
|
SortOrder[SortOrder["DEFAULT"] = 0] = "DEFAULT";
|
|
|
|
SortOrder[SortOrder["ASCENDING"] = 1] = "ASCENDING";
|
|
|
|
SortOrder[SortOrder["DESCENDING"] = 2] = "DESCENDING";
|
|
|
|
})(SortOrder || (SortOrder = {}));
|
|
|
|
var AttributeName;
|
|
|
|
(function (AttributeName) {
|
|
|
|
AttributeName["tableHeader"] = "sortable-style";
|
|
|
|
AttributeName["cssAscending"] = "sortable-asc";
|
|
|
|
AttributeName["cssDescending"] = "sortable-desc";
|
|
|
|
})(AttributeName || (AttributeName = {}));
|
|
|
|
class TableState {
|
|
|
|
constructor() {
|
|
|
|
this.columnIdx = null;
|
|
|
|
this.sortOrder = SortOrder.DEFAULT;
|
|
|
|
this.defaultOrdering = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function shouldSort(htmlEl) {
|
|
|
|
// 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");
|
|
|
|
}
|
|
|
|
function onHeadClick(evt, tableStates) {
|
|
|
|
// check if the clicked element is a table header
|
|
|
|
const htmlEl = evt.target;
|
|
|
|
if (!shouldSort(htmlEl)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const th = htmlEl.closest("thead th");
|
|
|
|
if (th === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const table = htmlEl.closest("table");
|
|
|
|
const tableBody = table.querySelector("tbody");
|
|
|
|
const thArray = Array.from(th.parentNode.children);
|
|
|
|
const thIdx = thArray.indexOf(th);
|
|
|
|
// create a new table state if does not previously exist
|
|
|
|
if (!tableStates.has(table)) {
|
|
|
|
tableStates.set(table, new TableState());
|
|
|
|
}
|
|
|
|
const tableState = tableStates.get(table);
|
|
|
|
thArray.forEach((th, i) => {
|
|
|
|
if (i !== thIdx) {
|
|
|
|
th.removeAttribute(AttributeName.tableHeader);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (tableState.defaultOrdering === null) {
|
|
|
|
tableState.defaultOrdering = Array.from(tableBody.rows);
|
|
|
|
}
|
|
|
|
// sorting the same column, again
|
|
|
|
if (tableState.columnIdx === thIdx) {
|
|
|
|
tableState.sortOrder = (tableState.sortOrder + 1) % 3;
|
|
|
|
}
|
|
|
|
// sorting a new column
|
|
|
|
else {
|
|
|
|
tableState.columnIdx = thIdx;
|
|
|
|
tableState.sortOrder = SortOrder.ASCENDING;
|
|
|
|
}
|
|
|
|
sortTable(tableState, tableBody);
|
|
|
|
switch (tableState.sortOrder) {
|
|
|
|
case SortOrder.ASCENDING:
|
|
|
|
th.setAttribute(AttributeName.tableHeader, AttributeName.cssAscending);
|
|
|
|
break;
|
|
|
|
case SortOrder.DESCENDING:
|
|
|
|
th.setAttribute(AttributeName.tableHeader, AttributeName.cssDescending);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// if the current state is now the default one, then forget about this table
|
|
|
|
if (tableState.sortOrder === SortOrder.DEFAULT) {
|
|
|
|
tableStates.delete(table);
|
|
|
|
th.removeAttribute(AttributeName.tableHeader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function sortTable(tableState, tableBody) {
|
|
|
|
emptyTable(tableBody, tableState.defaultOrdering);
|
|
|
|
if (tableState.sortOrder === SortOrder.DEFAULT) {
|
|
|
|
fillTable(tableBody, tableState.defaultOrdering);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const xs = [...tableState.defaultOrdering];
|
|
|
|
xs.sort((a, b) => compareRows(a, b, tableState.columnIdx, tableState.sortOrder));
|
|
|
|
fillTable(tableBody, xs);
|
|
|
|
}
|
|
|
|
function resetTable(tableState, tableBody) {
|
|
|
|
emptyTable(tableBody, tableState.defaultOrdering);
|
|
|
|
fillTable(tableBody, tableState.defaultOrdering);
|
|
|
|
}
|
|
|
|
function compareRows(a, b, index, order) {
|
|
|
|
let valueA = valueFromCell(a.cells[index]);
|
|
|
|
let valueB = valueFromCell(b.cells[index]);
|
|
|
|
if (order === SortOrder.DESCENDING) {
|
|
|
|
[valueA, valueB] = [valueB, valueA];
|
|
|
|
}
|
|
|
|
if (typeof valueA === "number" && typeof valueA === "number") {
|
|
|
|
return valueA < valueB ? -1 : 1;
|
|
|
|
}
|
|
|
|
return valueA.toString().localeCompare(valueB.toString());
|
|
|
|
}
|
|
|
|
function tryParseFloat(x) {
|
|
|
|
const y = parseFloat(x);
|
|
|
|
return isNaN(y) ? x : y;
|
|
|
|
}
|
|
|
|
function valueFromCell(element) {
|
|
|
|
// TODO: extend to other data-types.
|
|
|
|
return tryParseFloat(element.textContent);
|
|
|
|
}
|
|
|
|
function emptyTable(tableBody, rows) {
|
|
|
|
rows.forEach(() => tableBody.deleteRow(-1));
|
|
|
|
}
|
|
|
|
function fillTable(tableBody, rows) {
|
|
|
|
rows.forEach((row) => tableBody.appendChild(row));
|
|
|
|
}
|
|
|
|
|
|
|
|
class SortablePlugin extends obsidian.Plugin {
|
|
|
|
onload() {
|
|
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
|
|
console.log("Sortable: loading plugin...");
|
|
|
|
this.tableStates = new WeakMap();
|
|
|
|
this.registerDomEvent(document, "click", (ev) => onHeadClick(ev, this.tableStates));
|
|
|
|
console.log("Sortable: loaded plugin.");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onunload() {
|
|
|
|
// get all HTMLTableElements present in the map and reset their state
|
|
|
|
const tables = Array.from(document.querySelectorAll("table"));
|
|
|
|
for (const table of tables) {
|
|
|
|
if (this.tableStates.has(table)) {
|
|
|
|
const state = this.tableStates.get(table);
|
|
|
|
resetTable(state, table.querySelector("tbody"));
|
|
|
|
const th = table.querySelector(`thead th:nth-child(${state.columnIdx + 1})`);
|
|
|
|
th.removeAttribute(AttributeName.tableHeader);
|
|
|
|
this.tableStates.delete(table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete this.tableStates;
|
|
|
|
console.log("Sortable: unloaded plugin.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = SortablePlugin;
|
|
|
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsInNyYy9zb3J0YWJsZS50cyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJQbHVnaW4iXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUF1REE7QUFDTyxTQUFTLFNBQVMsQ0FBQyxPQUFPLEVBQUUsVUFBVSxFQUFFLENBQUMsRUFBRSxTQUFTLEVBQUU7QUFDN0QsSUFBSSxTQUFTLEtBQUssQ0FBQyxLQUFLLEVBQUUsRUFBRSxPQUFPLEtBQUssWUFBWSxDQUFDLEdBQUcsS0FBSyxHQUFHLElBQUksQ0FBQyxDQUFDLFVBQVUsT0FBTyxFQUFFLEVBQUUsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLEVBQUU7QUFDaEgsSUFBSSxPQUFPLEtBQUssQ0FBQyxLQUFLLENBQUMsR0FBRyxPQUFPLENBQUMsRUFBRSxVQUFVLE9BQU8sRUFBRSxNQUFNLEVBQUU7QUFDL0QsUUFBUSxTQUFTLFNBQVMsQ0FBQyxLQUFLLEVBQUUsRUFBRSxJQUFJLEVBQUUsSUFBSSxDQUFDLFNBQVMsQ0FBQyxJQUFJLENBQUMsS0FBSyxDQUFDLENBQUMsQ0FBQyxFQUFFLENBQUMsT0FBTyxDQUFDLEVBQUUsRUFBRSxNQUFNLEN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