Notepad/enter/.obsidian/plugins/obsidian-matrix/main.js

173 lines
13 KiB
JavaScript
Raw Normal View History

2023-07-05 18:29:11 +00:00
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
__markAsModule(target);
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// main.ts
__export(exports, {
default: () => MyPlugin
});
var import_obsidian3 = __toModule(require("obsidian"));
// CreationModal.ts
var import_obsidian2 = __toModule(require("obsidian"));
// ErrorModal.ts
var import_obsidian = __toModule(require("obsidian"));
var ErrorModal = class extends import_obsidian.Modal {
constructor(app, error) {
super(app);
this.error = error;
}
onOpen() {
this.contentEl.createEl("h1", { text: this.error.message });
}
onClose() {
this.contentEl.empty();
}
};
// CreationModal.ts
var CreationModal = class extends import_obsidian2.Modal {
constructor(app) {
super(app);
this.matrixWidth = 2;
this.matrixHeight = 2;
}
onOpen() {
this.createHTML();
new import_obsidian2.Setting(this.settingsDiv).setName("Matrix width").addSlider((slider) => {
slider.setValue(2);
slider.setLimits(1, 10, 1);
slider.showTooltip();
slider.setDynamicTooltip();
slider.onChange((value) => {
this.matrixWidth = value;
this.regenerateMatrix();
});
});
new import_obsidian2.Setting(this.settingsDiv).setName("Matrix height").addSlider((slider) => {
slider.setValue(2);
slider.setLimits(1, 10, 1);
slider.showTooltip();
slider.setDynamicTooltip();
slider.onChange((value) => {
this.matrixHeight = value;
this.regenerateMatrix();
});
});
new import_obsidian2.Setting(this.settingsDiv).setName("Create").addButton((button) => {
button.setIcon("checkmark");
button.setCta();
button.onClick(() => {
let chunks = Array.from(this.matrixDiv.children).map((child) => {
return child.value;
}).reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / this.matrixWidth);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
let latexString = chunks.map((chunk) => {
return chunk.join(" & ");
}).join(" \\\\\n");
this.writeAtCursor(`\\begin{pmatrix}
${latexString}
\\end{pmatrix}`);
this.close();
});
});
this.createInputs();
this.applyCorrectStyle();
}
onClose() {
this.contentEl.empty();
}
createHTML() {
this.parentDiv = this.contentEl.createEl("div", { cls: "parentDiv" });
this.settingsDiv = this.parentDiv.createEl("div", { cls: "settingsDiv" });
this.matrixDiv = this.parentDiv.createEl("div", { cls: "matrixDiv" });
}
applyCorrectStyle() {
this.matrixDiv.style.gridTemplateColumns = `repeat(${this.matrixWidth}, 1fr)`;
this.matrixDiv.style.gridTemplateRows = `repeat(${this.matrixHeight}, 1fr)`;
}
createInputs() {
for (let i = 0; i < this.matrixWidth * this.matrixHeight; i++) {
this.matrixDiv.createEl("input", { type: "text", cls: "matrixInput" });
}
}
regenerateMatrix() {
this.matrixDiv.empty();
this.createInputs();
this.applyCorrectStyle();
}
writeAtCursor(toWrite) {
const mdView = this.app.workspace.getActiveViewOfType(import_obsidian2.MarkdownView);
if (mdView) {
mdView.editor.replaceRange(toWrite, mdView.editor.getCursor());
} else {
this.close();
new ErrorModal(this.app, new Error("No markdown view open")).open();
}
}
};
// main.ts
var MyPlugin = class extends import_obsidian3.Plugin {
onload() {
return __async(this, null, function* () {
this.addRibbonIcon("pane-layout", "Obsidian Matrix", () => {
new CreationModal(this.app).open();
});
});
}
};
//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsibWFpbi50cyIsICJDcmVhdGlvbk1vZGFsLnRzIiwgIkVycm9yTW9kYWwudHMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbImltcG9ydCB7UGx1Z2lufSBmcm9tICdvYnNpZGlhbic7XHJcbmltcG9ydCBDcmVhdGlvbk1vZGFsIGZyb20gXCIuL0NyZWF0aW9uTW9kYWxcIjtcclxuXHJcbmV4cG9ydCBkZWZhdWx0IGNsYXNzIE15UGx1Z2luIGV4dGVuZHMgUGx1Z2luIHtcclxuXHJcblx0YXN5bmMgb25sb2FkKCkge1xyXG5cdFx0dGhpcy5hZGRSaWJib25JY29uKFwicGFuZS1sYXlvdXRcIiwgXCJPYnNpZGlhbiBNYXRyaXhcIiwgKCkgPT4ge1xyXG5cdFx0XHRuZXcgQ3JlYXRpb25Nb2RhbCh0aGlzLmFwcCkub3BlbigpXHJcblx0XHR9KTtcclxuXHR9XHJcbn1cclxuIiwgImltcG9ydCB7QXBwLCBNYXJrZG93blZpZXcsIE1vZGFsLCBTZXR0aW5nfSBmcm9tIFwib2JzaWRpYW5cIjtcbmltcG9ydCBFcnJvck1vZGFsIGZyb20gXCIuL0Vycm9yTW9kYWxcIjtcblxuZXhwb3J0IGRlZmF1bHQgY2xhc3MgQ3JlYXRpb25Nb2RhbCBleHRlbmRzIE1vZGFsIHtcblxuXHRwcml2YXRl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