mirror of
https://github.com/mildeveloper/obsidian-date-range-expander.git
synced 2026-07-22 05:43:20 +00:00
Changes following code review.
This commit is contained in:
parent
b7e11986b9
commit
68c79845db
6 changed files with 43 additions and 73 deletions
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"id": "date-range-expander",
|
||||
"name": "Date Range Expander",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"minAppVersion": "1.5.8",
|
||||
"description": "Quickly add a range of day references given a date duration.",
|
||||
"author": "Mil",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "obsidian-date-range-expander",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"description": "Quickly add a range of day references given a date duration.",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,6 @@ export class DateInputModal extends Modal {
|
|||
const { contentEl } = this;
|
||||
contentEl.empty();
|
||||
|
||||
this.addStyles();
|
||||
this.setupKeyboardListener();
|
||||
this.createStartDateInput();
|
||||
this.createRangeOptions();
|
||||
|
|
@ -93,32 +92,6 @@ export class DateInputModal extends Modal {
|
|||
return dateCount;
|
||||
}
|
||||
|
||||
private addStyles() {
|
||||
const styleEl = document.createElement('style');
|
||||
styleEl.textContent = `
|
||||
.date-option-container {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
.date-input-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
.date-input-row input[type="radio"] {
|
||||
margin-right: 8px;
|
||||
}
|
||||
.date-input-row input[type="text"],
|
||||
.date-input-row input[type="number"] {
|
||||
margin-left: 5px;
|
||||
}
|
||||
.date-input-row select {
|
||||
margin-left: 5px;
|
||||
}
|
||||
`;
|
||||
|
||||
this.contentEl.appendChild(styleEl);
|
||||
}
|
||||
|
||||
private setupKeyboardListener() {
|
||||
this.keyboardListener = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Enter' && this.validateInput()) {
|
||||
|
|
@ -284,7 +257,7 @@ export class DateInputModal extends Modal {
|
|||
|
||||
if (validInput) {
|
||||
const dateCount = this.calculateDateCount();
|
||||
this.submitButton.textContent = `Insert ${dateCount} Date${dateCount !== 1 ? 's' : ''}`;
|
||||
this.submitButton.textContent = `Insert ${dateCount} date${dateCount !== 1 ? 's' : ''}`;
|
||||
} else {
|
||||
this.submitButton.textContent = 'Insert';
|
||||
}
|
||||
|
|
|
|||
56
src/main.ts
56
src/main.ts
|
|
@ -1,4 +1,4 @@
|
|||
import { MarkdownView, Plugin } from 'obsidian';
|
||||
import { Editor, MarkdownView, Plugin } from 'obsidian';
|
||||
import { DateInputModal } from './dateInputModal';
|
||||
import { DEFAULT_SETTINGS, PluginSettings } from './types';
|
||||
import { DateRangeExpanderSettingTab } from './settingsTab';
|
||||
|
|
@ -12,41 +12,31 @@ export default class DateRangeExpanderPlugin extends Plugin {
|
|||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
|
||||
this.dateUtils = new DateUtils();
|
||||
this.dateRangeExpander = new DateRangeExpander(this.app, this.settings, this.dateUtils);
|
||||
|
||||
|
||||
this.addSettingTab(new DateRangeExpanderSettingTab(this.app, this));
|
||||
|
||||
|
||||
this.addCommand({
|
||||
id: 'insert-expanded-date-range',
|
||||
name: 'Insert',
|
||||
checkCallback: (checking: boolean) => {
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
if (!checking) {
|
||||
new DateInputModal(this.app, (rangeInput) => {
|
||||
const dateStartAndEnd = this.dateUtils.getStartAndEndDates(rangeInput);
|
||||
|
||||
if (dateStartAndEnd) {
|
||||
const editor = this.getActiveEditor();
|
||||
if (editor) {
|
||||
const cursor = editor.getCursor();
|
||||
let insertedDateRange = this.dateRangeExpander.expandDateRange(dateStartAndEnd) + ' ';
|
||||
if (rangeInput.useCallout) {
|
||||
insertedDateRange = this.dateRangeExpander.wrapInCallout(dateStartAndEnd, insertedDateRange);
|
||||
}
|
||||
editor.replaceRange(insertedDateRange, { line: cursor.line, ch: cursor.ch }, cursor);
|
||||
|
||||
const endPos = { line: cursor.line, ch: cursor.ch + insertedDateRange.length };
|
||||
editor.setCursor(endPos);
|
||||
}
|
||||
}
|
||||
}).open();
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
new DateInputModal(this.app, (rangeInput) => {
|
||||
const dateStartAndEnd = this.dateUtils.getStartAndEndDates(rangeInput);
|
||||
|
||||
if (dateStartAndEnd) {
|
||||
const cursor = editor.getCursor();
|
||||
let insertedDateRange = this.dateRangeExpander.expandDateRange(dateStartAndEnd) + ' ';
|
||||
if (rangeInput.useCallout) {
|
||||
insertedDateRange = this.dateRangeExpander.wrapInCallout(dateStartAndEnd, insertedDateRange);
|
||||
}
|
||||
editor.replaceRange(insertedDateRange, { line: cursor.line, ch: cursor.ch }, cursor);
|
||||
|
||||
const endPos = { line: cursor.line, ch: cursor.ch + insertedDateRange.length };
|
||||
editor.setCursor(endPos);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}).open();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -54,12 +44,4 @@ export default class DateRangeExpanderPlugin extends Plugin {
|
|||
async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); }
|
||||
|
||||
async saveSettings() { await this.saveData(this.settings); }
|
||||
|
||||
getActiveEditor() {
|
||||
const activeLeaf = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (activeLeaf) {
|
||||
return activeLeaf.editor;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
import { App, PluginSettingTab, Setting } from 'obsidian';
|
||||
import DateRangeExpanderPlugin from './main';
|
||||
import { DEFAULT_SETTINGS } from './types';
|
||||
import { normalizePath } from 'obsidian';
|
||||
|
||||
export class DateRangeExpanderSettingTab extends PluginSettingTab {
|
||||
plugin: DateRangeExpanderPlugin;
|
||||
|
|
@ -98,7 +99,7 @@ export class DateRangeExpanderSettingTab extends PluginSettingTab {
|
|||
text.setPlaceholder('Enter folder path')
|
||||
.setValue(this.plugin.settings.customFolderPath || '')
|
||||
.onChange(async (value) => {
|
||||
this.plugin.settings.customFolderPath = value;
|
||||
this.plugin.settings.customFolderPath = normalizePath(value);
|
||||
await this.plugin.saveSettings();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
24
styles.css
24
styles.css
|
|
@ -1,8 +1,22 @@
|
|||
/*
|
||||
.date-option-container {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
This CSS file will be included with your plugin, and
|
||||
available in the app when your plugin is enabled.
|
||||
.date-input-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
If your plugin does not need CSS, delete this file.
|
||||
.date-input-row input[type="radio"] {
|
||||
margin-right: 8px;
|
||||
}
|
||||
|
||||
*/
|
||||
.date-input-row input[type="text"],
|
||||
.date-input-row input[type="number"] {
|
||||
margin-left: 5px;
|
||||
}
|
||||
|
||||
.date-input-row select {
|
||||
margin-left: 5px;
|
||||
}
|
||||
Loading…
Reference in a new issue