You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
14 KiB

3 years ago
/*
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 onHeadClick(evt, tableStates) {
// check if the clicked element is a table header
3 years ago
const htmlEl = evt.target;
if (!htmlEl.matchParent(".markdown-preview-view")) {
return;
}
3 years ago
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());
3 years ago
}
const tableState = tableStates.get(table);
3 years ago
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
3 years ago
if (tableState.sortOrder === SortOrder.DEFAULT) {
tableStates.delete(table);
3 years ago
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);
}
3 years ago
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") {
3 years ago
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.");
3 years ago
});
}
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.");
3 years ago
}
}
module.exports = SortablePlugin;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsInNyYy9zb3J0YWJsZS50cyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJQbHVnaW4iXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTt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