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.
248 lines
20 KiB
248 lines
20 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());
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Unique ID creation requires a high quality random # generator. In the browser we therefore
|
||
|
// require the crypto API and do not support built-in fallback to lower quality random number
|
||
|
// generators (like Math.random()).
|
||
|
var getRandomValues;
|
||
|
var rnds8 = new Uint8Array(16);
|
||
|
function rng() {
|
||
|
// lazy load so that environments that need to polyfill have a chance to do so
|
||
|
if (!getRandomValues) {
|
||
|
// getRandomValues needs to be invoked in a context where "this" is a Crypto implementation. Also,
|
||
|
// find the complete implementation of crypto (msCrypto) on IE11.
|
||
|
getRandomValues = typeof crypto !== 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto) || typeof msCrypto !== 'undefined' && typeof msCrypto.getRandomValues === 'function' && msCrypto.getRandomValues.bind(msCrypto);
|
||
|
|
||
|
if (!getRandomValues) {
|
||
|
throw new Error('crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return getRandomValues(rnds8);
|
||
|
}
|
||
|
|
||
|
var REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
||
|
|
||
|
function validate(uuid) {
|
||
|
return typeof uuid === 'string' && REGEX.test(uuid);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Convert array of 16 byte values to UUID string format of the form:
|
||
|
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
||
|
*/
|
||
|
|
||
|
var byteToHex = [];
|
||
|
|
||
|
for (var i = 0; i < 256; ++i) {
|
||
|
byteToHex.push((i + 0x100).toString(16).substr(1));
|
||
|
}
|
||
|
|
||
|
function stringify(arr) {
|
||
|
var offset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
|
||
|
// Note: Be careful editing this code! It's been tuned for performance
|
||
|
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
|
||
|
var uuid = (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + '-' + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + '-' + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + '-' + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + '-' + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase(); // Consistency check for valid UUID. If this throws, it's likely due to one
|
||
|
// of the following:
|
||
|
// - One or more input array values don't map to a hex octet (leading to
|
||
|
// "undefined" in the uuid)
|
||
|
// - Invalid input values for the RFC `version` or `variant` fields
|
||
|
|
||
|
if (!validate(uuid)) {
|
||
|
throw TypeError('Stringified UUID is invalid');
|
||
|
}
|
||
|
|
||
|
return uuid;
|
||
|
}
|
||
|
|
||
|
function v4(options, buf, offset) {
|
||
|
options = options || {};
|
||
|
var rnds = options.random || (options.rng || rng)(); // Per 4.4, set bits for version and `clock_seq_hi_and_reserved`
|
||
|
|
||
|
rnds[6] = rnds[6] & 0x0f | 0x40;
|
||
|
rnds[8] = rnds[8] & 0x3f | 0x80; // Copy bytes to buffer, if provided
|
||
|
|
||
|
if (buf) {
|
||
|
offset = offset || 0;
|
||
|
|
||
|
for (var i = 0; i < 16; ++i) {
|
||
|
buf[offset + i] = rnds[i];
|
||
|
}
|
||
|
|
||
|
return buf;
|
||
|
}
|
||
|
|
||
|
return stringify(rnds);
|
||
|
}
|
||
|
|
||
|
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["table"] = "sortable-id";
|
||
|
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) {
|
||
|
const htmlEl = evt.target;
|
||
|
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);
|
||
|
let tableID = table.getAttribute(AttributeName.table);
|
||
|
if (tableID === null) {
|
||
|
tableID = v4().slice(0, 8);
|
||
|
table.setAttribute(AttributeName.table, tableID);
|
||
|
tableStates[tableID] = new TableState();
|
||
|
}
|
||
|
const tableState = tableStates[tableID];
|
||
|
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;
|
||
|
}
|
||
|
// TODO: closing the table page will
|
||
|
// If the current state is now the default one, then forget about this table
|
||
|
if (tableState.sortOrder === SortOrder.DEFAULT) {
|
||
|
// TODO -- verify whether this removes the memory for value (e.g. array etc)
|
||
|
delete tableStates[tableID];
|
||
|
table.removeAttribute(AttributeName.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 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));
|
||
|
}
|
||
|
|
||
|
const DEFAULT_SETTINGS = {
|
||
|
mySetting: 'default'
|
||
|
};
|
||
|
class SortablePlugin extends obsidian.Plugin {
|
||
|
onload() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
console.log('Sortable: loading plugin...');
|
||
|
yield this.loadSettings();
|
||
|
this.tableStates = {};
|
||
|
this.registerDomEvent(document, 'click', (ev) => onHeadClick(ev, this.tableStates));
|
||
|
console.log('Sortable: loaded plugin.');
|
||
|
});
|
||
|
}
|
||
|
onunload() {
|
||
|
// TODO: delete tableStates
|
||
|
console.log('Sortable: unloaded plugin.');
|
||
|
}
|
||
|
loadSettings() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
|
||
|
});
|
||
|
}
|
||
|
saveSettings() {
|
||
|
return __awaiter(this, void 0, void 0, function* () {
|
||
|
yield this.saveData(this.settings);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = SortablePlugin;
|
||
|
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsIm5vZGVfbW9kdWxlcy91dWlkL2Rpc3QvZXNtLWJyb3dzZXIvcm5nLmpzIiwibm9kZV9tb2R1bGVzL3V1aWQvZGlzdC9lc20tYnJvd3Nlci9yZWdleC5qcyIsIm5vZGVfbW9kdWxlcy91dWlkL2Rpc3QvZXNtLWJyb3dzZXIvdmFsaWRhdGUuanMiLCJub2RlX21vZHVsZXMvdXVpZC9kaXN0L2VzbS1icm93c2VyL3N0cmluZ2lmeS5qcyIsIm5vZGVfbW9kdWxlcy91dWlkL2Rpc3QvZXNtLWJyb3dzZXIvdjQuanMiLCJzcmMvc29ydGFibGUudHMiLCJzcmMvbWFpbi50cyJdLCJzb3VyY2VzQ29udGVudCI6bnVsbCwibmFtZXMiOlsidXVpZHY0IiwiUGx1Z2luIl0sIm1hcHBpbmdzIjoiOzs7Ozs7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