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.

205 lines
18 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');
/******************************************************************************
3 years ago
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());
});
}
// allows time to be optional
const ISO_DATE_REGEX = /^(\d{4}-\d{2}-\d{2})(T\d{2}:\d{2}:\d{2}(\.\d{3})?(Z|[+-]\d{2}:\d{2})?)?$/;
var CellTypes;
(function (CellTypes) {
CellTypes[CellTypes["TEXT"] = 0] = "TEXT";
CellTypes[CellTypes["NUMBER"] = 1] = "NUMBER";
CellTypes[CellTypes["ISO_DATE"] = 2] = "ISO_DATE";
})(CellTypes || (CellTypes = {}));
3 years ago
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");
}
3 years ago
function onHeadClick(evt, tableStates) {
// check if the clicked element is a table header
3 years ago
const htmlEl = evt.target;
if (!shouldSort(htmlEl)) {
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];
const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" });
xs.sort((a, b) => compareRows(a, b, tableState.columnIdx, tableState.sortOrder, collator));
3 years ago
fillTable(tableBody, xs);
}
function resetTable(tableState, tableBody) {
emptyTable(tableBody, tableState.defaultOrdering);
fillTable(tableBody, tableState.defaultOrdering);
}
function compareRows(a, b, index, order, collator) {
3 years ago
if (order === SortOrder.DESCENDING) {
[a, b] = [b, a];
3 years ago
}
const [valueA, typeA] = valueFromCell(a.cells[index]);
const [valueB, typeB] = valueFromCell(b.cells[index]);
if (typeA !== typeB) {
return collator.compare(valueA.toString(), valueB.toString());
}
switch (typeA) {
case CellTypes.NUMBER:
case CellTypes.ISO_DATE:
return valueA === valueB ? 0 : valueA < valueB ? -1 : 1;
case CellTypes.TEXT:
return collator.compare(valueA.toString(), valueB.toString());
default:
// unreachable
return 0;
3 years ago
}
}
function valueFromCell(element) {
// TODO: extend to other data-types.
const text = element.textContent;
if (ISO_DATE_REGEX.test(text)) {
return [new Date(text), CellTypes.ISO_DATE];
}
// use `Number` in favour of `parseFloat` to disallow "false positives"
const value = Number(text);
if (!isNaN(value)) {
return [value, CellTypes.NUMBER];
}
// fallback: text value, will use collator for comparison
return [text, CellTypes.TEXT];
3 years ago
}
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(window, "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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsInNyYy9zb3J0YWJsZS50cyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpudWxsLCJuYW1lcyI6WyJQbHVnaW4iXSwibWFwcGluZ3MiOiI7Ozs7Ozs7OztBQUFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUF1REE7QUFDTyxTQUFTLFNBQVMsQ0FBQyxPQUFPLEVBQUUsVUFBVSxFQUFFLENBQUMsRUFBRSxTQUFTLEVBQUU7QUFDN0QsSUFBSSxTQUFTLEtBQUssQ0FBQyxLQUFLLEVBQUUsRUFBRSxPQUFPLEtBQUssWUFBWSxDQUFDLEdBQUcsS0FBSyxHQUFHLElBQUksQ0FBQyxDQUFDLFVBQVUsT0FBTyxFQUFFLEVBQUUsT0FBTyxDQUFDLEtBQUssQ0FBQyxDQUFDLEVBQUUsQ0FBQyxDQUFDLEVBQUU7QUFDaEgsSUFBSSxPQUFPLEtBQUssQ0FBQyxLQUFLLENBQUMsR0FBRyxPQUFPLENBQUMsRUFBRSxVQU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