142 lines
15 KiB
JavaScript
142 lines
15 KiB
JavaScript
|
/*
|
||
|
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||
|
if you want to view the source, please visit the github repository of this plugin
|
||
|
*/
|
||
|
|
||
|
var __defProp = Object.defineProperty;
|
||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||
|
var __export = (target, all) => {
|
||
|
for (var name in all)
|
||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||
|
};
|
||
|
var __copyProps = (to, from, except, desc) => {
|
||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||
|
for (let key of __getOwnPropNames(from))
|
||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||
|
}
|
||
|
return to;
|
||
|
};
|
||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||
|
|
||
|
// main.ts
|
||
|
var main_exports = {};
|
||
|
__export(main_exports, {
|
||
|
default: () => lineArrange
|
||
|
});
|
||
|
module.exports = __toCommonJS(main_exports);
|
||
|
var import_obsidian = require("obsidian");
|
||
|
var lineArrange = class extends import_obsidian.Plugin {
|
||
|
// Method called when the plugin is loaded
|
||
|
async onload() {
|
||
|
this.addCommand({
|
||
|
id: "lexisort-lines",
|
||
|
name: "Lexisort lines",
|
||
|
editorCallback: (editor) => {
|
||
|
const selection = editor.getSelection();
|
||
|
editor.replaceSelection(lexiSortLines(selection));
|
||
|
}
|
||
|
});
|
||
|
this.addCommand({
|
||
|
id: "reverse-lines",
|
||
|
name: "Reverse lines",
|
||
|
editorCallback: (editor) => {
|
||
|
const selection = editor.getSelection();
|
||
|
editor.replaceSelection(reverseLines(selection));
|
||
|
}
|
||
|
});
|
||
|
this.addCommand({
|
||
|
id: "sort-lines",
|
||
|
name: "Sort lines",
|
||
|
editorCallback: (editor) => {
|
||
|
const selection = editor.getSelection();
|
||
|
editor.replaceSelection(sortLines(selection));
|
||
|
}
|
||
|
});
|
||
|
this.addCommand({
|
||
|
id: "shuffle-lines",
|
||
|
name: "Shuffle lines",
|
||
|
editorCallback: (editor) => {
|
||
|
const selection = editor.getSelection();
|
||
|
editor.replaceSelection(shuffleLines(selection));
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
var textSize = getComputedStyle(document.body).getPropertyValue("--font-text-size");
|
||
|
var textFont = getComputedStyle(document.body).getPropertyValue("--font-text");
|
||
|
function lexiSortLines(orgText) {
|
||
|
const lines = orgText.split("\n");
|
||
|
lines.sort();
|
||
|
let srtLines = "";
|
||
|
lines.forEach((line) => {
|
||
|
if (line.length > 0) {
|
||
|
srtLines += line + "\n";
|
||
|
} else {
|
||
|
srtLines = "\n" + srtLines;
|
||
|
}
|
||
|
});
|
||
|
return srtLines.trimEnd();
|
||
|
}
|
||
|
function reverseLines(orgText) {
|
||
|
const lines = orgText.split("\n");
|
||
|
lines.reverse();
|
||
|
let revLines = "";
|
||
|
lines.forEach((line) => {
|
||
|
if (line.length > 0) {
|
||
|
revLines += line + "\n";
|
||
|
} else {
|
||
|
revLines = "\n" + revLines;
|
||
|
}
|
||
|
});
|
||
|
return revLines.trimEnd();
|
||
|
}
|
||
|
function sortLines(orgText) {
|
||
|
const lines = orgText.split("\n");
|
||
|
const arr1 = new Arrangement(lines, realLineWidth);
|
||
|
return orderedText(arr1);
|
||
|
}
|
||
|
function realLineWidth(line) {
|
||
|
const canvas = document.createElement("canvas");
|
||
|
const context = canvas.getContext("2d");
|
||
|
if (!context) {
|
||
|
throw new Error("Failed to get 2D context");
|
||
|
}
|
||
|
context.font = textSize + " " + textFont;
|
||
|
return Math.round(1e4 * context.measureText(line).width);
|
||
|
}
|
||
|
function shuffleLines(orgText) {
|
||
|
const lines = orgText.split("\n");
|
||
|
const arr2 = new Arrangement(lines, randomLineWidth);
|
||
|
return orderedText(arr2);
|
||
|
}
|
||
|
function randomLineWidth(line) {
|
||
|
if (line.length > 0)
|
||
|
return Math.round(1e4 * (Math.random() + 1));
|
||
|
else
|
||
|
return 0;
|
||
|
}
|
||
|
var Arrangement = class {
|
||
|
// Creates a key for each unique line-width and associates line to respective slot
|
||
|
constructor(linesList, lineFunction) {
|
||
|
linesList.forEach((line) => {
|
||
|
const width = lineFunction(line);
|
||
|
if (!(width in this)) {
|
||
|
this[width] = line;
|
||
|
} else {
|
||
|
this[width] = this[width].concat("\n" + line);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
function orderedText(orderedLines) {
|
||
|
let finalText = "";
|
||
|
for (const lineKey in orderedLines) {
|
||
|
finalText += orderedLines[lineKey] + "\n";
|
||
|
}
|
||
|
return finalText.trimEnd();
|
||
|
}
|
||
|
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsibWFpbi50cyJdLAogICJzb3VyY2VzQ29udGVudCI6IFsiaW1wb3J0IHsgRWRpdG9yLCBQbHVnaW4gfSBmcm9tICdvYnNpZGlhbic7XG5cbmV4cG9ydCBkZWZhdWx0IGNsYXNzIGxpbmVBcnJhbmdlIGV4dGVuZHMgUGx1Z2luIHtcbiAgICAvLyBNZXRob2QgY2FsbGVkIHdoZW4gdGhlIHBsdWdpbiBpcyBsb2FkZWRcbiAgICBhc3luYyBvbmxvYWQoKSB7XG5cbiAgICAgICAgLy8gQWRkIGEgY29tbWFuZCB0byBsZXhpY2FsbHkgc29ydCBsaW5lcyBpbiB0aGUgZWRpdG9yXG4gICAgICAgIHRoaXMuYWRkQ29tbWFuZCh7XG4gICAgICAgICAgICBpZDogJ2xleGlzb3J0LWxpbmVzJyxcbiAgICAgICAgICAgIG5hbWU6ICdMZXhpc29ydCBsaW5lcycsXG4gICAgICAgICAgICBlZGl0b3JDYWxsYmFjazogKGVkaXRvcjogRWRpdG9yKSA9PiB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