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.

300 lines
24 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());
});
}
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
function createCommonjsModule(fn) {
var module = { exports: {} };
return fn(module, module.exports), module.exports;
}
/* global define */
var compareVersions = createCommonjsModule(function (module, exports) {
(function (root, factory) {
/* istanbul ignore next */
{
module.exports = factory();
}
}(commonjsGlobal, function () {
var semver = /^v?(?:\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+)(\.(?:[x*]|\d+))?(?:-[\da-z\-]+(?:\.[\da-z\-]+)*)?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
function indexOrEnd(str, q) {
return str.indexOf(q) === -1 ? str.length : str.indexOf(q);
}
function split(v) {
var c = v.replace(/^v/, '').replace(/\+.*$/, '');
var patchIndex = indexOrEnd(c, '-');
var arr = c.substring(0, patchIndex).split('.');
arr.push(c.substring(patchIndex + 1));
return arr;
}
function tryParse(v) {
return isNaN(Number(v)) ? v : Number(v);
}
function validate(version) {
if (typeof version !== 'string') {
throw new TypeError('Invalid argument expected string');
}
if (!semver.test(version)) {
throw new Error('Invalid argument not valid semver (\''+version+'\' received)');
}
}
function compareVersions(v1, v2) {
[v1, v2].forEach(validate);
var s1 = split(v1);
var s2 = split(v2);
for (var i = 0; i < Math.max(s1.length - 1, s2.length - 1); i++) {
var n1 = parseInt(s1[i] || 0, 10);
var n2 = parseInt(s2[i] || 0, 10);
if (n1 > n2) return 1;
if (n2 > n1) return -1;
}
var sp1 = s1[s1.length - 1];
var sp2 = s2[s2.length - 1];
if (sp1 && sp2) {
var p1 = sp1.split('.').map(tryParse);
var p2 = sp2.split('.').map(tryParse);
for (i = 0; i < Math.max(p1.length, p2.length); i++) {
if (p1[i] === undefined || typeof p2[i] === 'string' && typeof p1[i] === 'number') return -1;
if (p2[i] === undefined || typeof p1[i] === 'string' && typeof p2[i] === 'number') return 1;
if (p1[i] > p2[i]) return 1;
if (p2[i] > p1[i]) return -1;
}
} else if (sp1 || sp2) {
return sp1 ? -1 : 1;
}
return 0;
}
var allowedOperators = [
'>',
'>=',
'=',
'<',
'<='
];
var operatorResMap = {
'>': [1],
'>=': [0, 1],
'=': [0],
'<=': [-1, 0],
'<': [-1]
};
function validateOperator(op) {
if (typeof op !== 'string') {
throw new TypeError('Invalid operator type, expected string but got ' + typeof op);
}
if (allowedOperators.indexOf(op) === -1) {
throw new TypeError('Invalid operator, expected one of ' + allowedOperators.join('|'));
}
}
compareVersions.validate = function(version) {
return typeof version === 'string' && semver.test(version);
};
compareVersions.compare = function (v1, v2, operator) {
// Validate operator
validateOperator(operator);
// since result of compareVersions can only be -1 or 0 or 1
// a simple map can be used to replace switch
var res = compareVersions(v1, v2);
return operatorResMap[operator].indexOf(res) > -1;
};
return compareVersions;
}));
});
const DEFAULT_SETTINGS = {
jsFiles: '',
jsFolder: '',
};
class CustomJS extends obsidian.Plugin {
onload() {
return __awaiter(this, void 0, void 0, function* () {
console.log('Loading CustomJS');
yield this.loadSettings();
this.registerEvent(this.app.vault.on('modify', this.reloadIfNeeded, this));
3 years ago
// @ts-ignore
window.forceLoadCustomJS = () => __awaiter(this, void 0, void 0, function* () {
yield this.loadClasses();
});
3 years ago
this.app.workspace.onLayoutReady(() => {
this.loadClasses();
});
this.addSettingTab(new CustomJSSettingsTab(this.app, this));
});
}
onunload() {
// @ts-ignore
delete window.customJS;
}
reloadIfNeeded(f) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
if (f.path.endsWith('.js')) {
yield this.loadClasses();
// reload dataviewjs blocks if installed & version >= 0.4.11
if (this.app.plugins.enabledPlugins.has("dataview")) {
// @ts-ignore
const version = (_b = (_a = this.app.plugins.plugins) === null || _a === void 0 ? void 0 : _a.dataview) === null || _b === void 0 ? void 0 : _b.manifest.version;
if (compareVersions(version, '0.4.11') < 0)
return;
(_e = (_d = (_c = this.app.plugins.plugins.dataview) === null || _c === void 0 ? void 0 : _c.api) === null || _d === void 0 ? void 0 : _d.index) === null || _e === void 0 ? void 0 : _e.touch();
}
}
});
}
loadSettings() {
return __awaiter(this, void 0, void 0, function* () {
const settings = yield this.loadData();
this.settings = Object.assign(Object.assign({}, DEFAULT_SETTINGS), settings);
});
}
saveSettings() {
return __awaiter(this, void 0, void 0, function* () {
yield this.saveData(this.settings);
});
}
evalFile(f) {
3 years ago
return __awaiter(this, void 0, void 0, function* () {
try {
const file = yield this.app.vault.adapter.read(f);
const def = eval('(' + file + ')');
const cls = new def();
// @ts-ignore
window.customJS[cls.constructor.name] = cls;
3 years ago
}
catch (e) {
console.error(`CustomJS couldn\'t import ${f}`);
console.error(e);
}
});
}
loadClasses() {
return __awaiter(this, void 0, void 0, function* () {
// @ts-ignore
window.customJS = {};
3 years ago
const filesToLoad = [];
// Get individual paths
if (this.settings.jsFiles != '') {
const individualFiles = this.settings.jsFiles.split(',').map(s => s.trim()).sort();
for (const f of individualFiles) {
if (f != '' && f.endsWith('.js')) {
filesToLoad.push(f);
}
}
}
// Get paths in folder
if (this.settings.jsFolder != '') {
const prefix = this.settings.jsFolder;
const files = this.app.vault.getFiles();
const scripts = files.filter(f => f.path.startsWith(prefix) && f.path.endsWith('.js'));
for (const s of scripts) {
if (s.path != '' && s.path.endsWith('.js')) {
filesToLoad.push(s.path);
}
}
}
this.sortByFileName(filesToLoad);
// load all scripts
for (const f of filesToLoad) {
yield this.evalFile(f);
3 years ago
}
});
}
sortByFileName(files) {
files.sort((a, b) => {
const nameA = a.split('/').last();
const nameB = b.split('/').last();
return nameA.localeCompare(nameB);
});
}
}
class CustomJSSettingsTab extends obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
let { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'CustomJS' });
// individual files
new obsidian.Setting(containerEl)
.setName('Individual files')
.setDesc('Comma-separated list of files to load')
.addText(text => text
.setPlaceholder('jsfile1.js,jsfile2.js')
.setValue(this.plugin.settings.jsFiles)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.jsFiles = value;
yield this.plugin.saveSettings();
yield this.plugin.loadClasses();
})));
// folder
new obsidian.Setting(containerEl)
.setName('Folder')
.setDesc('Path to folder containing JS files to load')
.addText(text => text
.setPlaceholder('js/scripts')
.setValue(this.plugin.settings.jsFolder)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.jsFolder = value;
yield this.plugin.saveSettings();
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