talon-one/.obsidian/plugins/obsidian-timestamper/main.js

222 lines
43 KiB
JavaScript
Raw Normal View History

/*
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());
});
}
/* Changelog:
*
* V1.0.0 - Initial version
* V1.1.0 - Migrated from dateformat to moment.js
* V1.2.0 - Introduced option for linebreak after insert
*
*/
const DEFAULT_SETTINGS = {
timeStampFormat: 'hh:mm:ss',
dateStampFormat: 'YYYY-MM-DD',
lastFormat: '',
newLine: false
};
// logThreshold: 0 ... only error messages
// 9 ... verbose output
const logThreshold = 9;
const logger = (logString, logLevel = 0) => { if (logLevel <= logThreshold)
console.log('TimeStamper: ' + logString); };
const version = '1.2.0-0001';
class TimeStamperPlugin extends obsidian.Plugin {
onload() {
return __awaiter(this, void 0, void 0, function* () {
logger('Loading Plugin v' + version, 9);
yield this.loadSettings();
this.addSettingTab(new TimeStamperSettingTab(this.app, this));
this.addCommand({
id: 'obsidian-custom-timestamp',
name: 'Insert custom time/date stamp',
editorCallback: (editor) => {
new TimeStamperModal(this.app, editor, this.settings, this).open();
},
});
this.addCommand({
id: 'obsidian-fast-timestamp',
name: 'Insert preconfigured time stamp',
editorCallback: (editor) => {
const now = new Date();
const stamp = obsidian.moment(now).format(this.settings.timeStampFormat);
if (this.settings.newLine) {
editor.replaceSelection(stamp + '\n');
logger('new line', 9);
}
else {
editor.replaceSelection(stamp);
logger('no new line');
}
}
});
this.addCommand({
id: 'obsidian-fast-datestamp',
name: 'Insert preconfigured date stamp',
editorCallback: (editor) => {
const now = new Date();
const stamp = obsidian.moment(now).format(this.settings.dateStampFormat);
if (this.settings.newLine) {
editor.replaceSelection(stamp + '\n');
logger('new line', 9);
}
else {
editor.replaceSelection(stamp);
logger('no new line');
}
}
});
});
}
onunload() {
logger('Bye!', 9);
}
loadSettings() {
return __awaiter(this, void 0, void 0, function* () {
logger('Loading Settings...', 6);
this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
logger('timeStampFormat: ' + this.settings.timeStampFormat, 9);
logger('dateStampFormat: ' + this.settings.dateStampFormat, 9);
logger('lastFormat: ' + this.settings.lastFormat, 9);
});
}
saveSettings() {
return __awaiter(this, void 0, void 0, function* () {
logger('Saving Settings...', 9);
yield this.saveData(this.settings);
});
}
}
class TimeStamperModal extends obsidian.Modal {
constructor(app, editor, settings, plugin) {
super(app);
this.editor = editor;
this.settings = settings;
this.plugin = plugin;
}
onOpen() {
const { contentEl, editor, modalEl } = this;
const rowClass = 'row';
const divClass = 'div';
modalEl.addClass('timestamper-modal');
// Create label and text field
const containerEl = document.createElement(divClass);
containerEl.addClass(rowClass);
const targetEl = document.createElement(divClass);
targetEl.addClass('input-wrapper');
const labelEl = document.createElement(divClass);
labelEl.addClass('input-label');
labelEl.setText('Format string:');
const formatComponent = new obsidian.TextComponent(targetEl);
formatComponent.setPlaceholder('e.g. YYYY-MM-DD');
formatComponent.setValue(this.settings.lastFormat);
// Create Button
const buttonContainerEl = document.createElement(divClass);
buttonContainerEl.addClass(rowClass);
const submitButtonTarget = document.createElement(divClass);
submitButtonTarget.addClass('button-wrapper');
const submitButtonComponent = new obsidian.ButtonComponent(submitButtonTarget);
submitButtonComponent.setButtonText('Insert Date/Time Stamp');
submitButtonComponent.setCta();
submitButtonComponent.onClick(() => {
const now = new Date();
const stampFormat = formatComponent.getValue();
const stamp = obsidian.moment(now).format(stampFormat);
if (this.settings.newLine) {
editor.replaceSelection(stamp + '\n');
logger('new line', 9);
}
else {
editor.replaceSelection(stamp);
logger('no new line');
}
this.settings.lastFormat = stampFormat;
this.plugin.saveData(this.settings);
this.close();
});
// Add components to layout
containerEl.appendChild(labelEl);
containerEl.appendChild(targetEl);
buttonContainerEl.appendChild(submitButtonTarget);
contentEl.append(containerEl);
contentEl.append(buttonContainerEl);
submitButtonComponent.buttonEl.focus();
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}
class TimeStamperSettingTab extends obsidian.PluginSettingTab {
constructor(app, plugin) {
super(app, plugin);
this.plugin = plugin;
}
display() {
const { containerEl } = this;
containerEl.empty();
new obsidian.Setting(containerEl)
.setName('Date Stamp Template')
.setDesc('Template String for inserting a date stamp')
.addText(text => text
.setValue(this.plugin.settings.dateStampFormat)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
logger('Settings update: ' + value, 9);
this.plugin.settings.dateStampFormat = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName('Time Stamp Template')
.setDesc('Template String for inserting a time stamp')
.addText(text => text
.setValue(this.plugin.settings.timeStampFormat)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
logger('Settings update: ' + value, 9);
this.plugin.settings.timeStampFormat = value;
yield this.plugin.saveSettings();
})));
new obsidian.Setting(containerEl)
.setName('Insert line break')
.setDesc('Add a line break after the time/date stamp')
.addToggle(toggle => toggle
.setValue(this.plugin.settings.newLine)
.onChange((value) => __awaiter(this, void 0, void 0, function* () {
this.plugin.settings.newLine = value;
yield this.plugin.saveSettings();
})));
}
}
module.exports = TimeStamperPlugin;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWFpbi5qcyIsInNvdXJjZXMiOlsibm9kZV9tb2R1bGVzL3RzbGliL3RzbGliLmVzNi5qcyIsInNyYy9tYWluLnRzIl0sInNvdXJjZXNDb250ZW50IjpbIi8qISAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKlxyXG5Db3B5cmlnaHQgKGMpIE1pY3Jvc29mdCBDb3Jwb3JhdGlvbi5cclxuXHJcblBlcm1pc3Npb24gdG8gdXNlLCBjb3B5LCBtb2RpZnksIGFuZC9vciBkaXN0cmlidXRlIHRoaXMgc29mdHdhcmUgZm9yIGFueVxyXG5wdXJwb3NlIHdpdGggb3Igd2l0aG91dCBmZWUgaXMgaGVyZWJ5IGdyYW50ZWQuXHJcblxyXG5USEUgU09GVFdBUkUgSVMgUFJPVklERUQgXCJBUyBJU1wiIEFORCBUSEUgQVVUSE9SIERJU0NMQUlNUyBBTEwgV0FSUkFOVElFUyBXSVRIXHJcblJFR0FSRCBUTyBUSElTIFNPRlRXQVJFIElOQ0xVRElORyBBTEwgSU1QTElFRCBXQVJSQU5USUVTIE9GIE1FUkNIQU5UQUJJTElUWVxyXG5BTkQgRklUTkVTUy4gSU4gTk8gRVZFTlQgU0hBTEwgVEhFIEFVVEhPUiBCRSBMSUFCTEUgRk9SIEFOWSBTUEVDSUFMLCBESVJFQ1QsXHJcbklORElSRUNULCBPUiBDT05TRVFVRU5USUFMIERBTUFHRVMgT1IgQU5ZIERBTUFHRVMgV0hBVFNP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