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",
|
"id": "date-range-expander",
|
||||||
"name": "Date Range Expander",
|
"name": "Date Range Expander",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"minAppVersion": "1.5.8",
|
"minAppVersion": "1.5.8",
|
||||||
"description": "Quickly add a range of day references given a date duration.",
|
"description": "Quickly add a range of day references given a date duration.",
|
||||||
"author": "Mil",
|
"author": "Mil",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "obsidian-date-range-expander",
|
"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.",
|
"description": "Quickly add a range of day references given a date duration.",
|
||||||
"main": "main.js",
|
"main": "main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,6 @@ export class DateInputModal extends Modal {
|
||||||
const { contentEl } = this;
|
const { contentEl } = this;
|
||||||
contentEl.empty();
|
contentEl.empty();
|
||||||
|
|
||||||
this.addStyles();
|
|
||||||
this.setupKeyboardListener();
|
this.setupKeyboardListener();
|
||||||
this.createStartDateInput();
|
this.createStartDateInput();
|
||||||
this.createRangeOptions();
|
this.createRangeOptions();
|
||||||
|
|
@ -93,32 +92,6 @@ export class DateInputModal extends Modal {
|
||||||
return dateCount;
|
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() {
|
private setupKeyboardListener() {
|
||||||
this.keyboardListener = (event: KeyboardEvent) => {
|
this.keyboardListener = (event: KeyboardEvent) => {
|
||||||
if (event.key === 'Enter' && this.validateInput()) {
|
if (event.key === 'Enter' && this.validateInput()) {
|
||||||
|
|
@ -284,7 +257,7 @@ export class DateInputModal extends Modal {
|
||||||
|
|
||||||
if (validInput) {
|
if (validInput) {
|
||||||
const dateCount = this.calculateDateCount();
|
const dateCount = this.calculateDateCount();
|
||||||
this.submitButton.textContent = `Insert ${dateCount} Date${dateCount !== 1 ? 's' : ''}`;
|
this.submitButton.textContent = `Insert ${dateCount} date${dateCount !== 1 ? 's' : ''}`;
|
||||||
} else {
|
} else {
|
||||||
this.submitButton.textContent = 'Insert';
|
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 { DateInputModal } from './dateInputModal';
|
||||||
import { DEFAULT_SETTINGS, PluginSettings } from './types';
|
import { DEFAULT_SETTINGS, PluginSettings } from './types';
|
||||||
import { DateRangeExpanderSettingTab } from './settingsTab';
|
import { DateRangeExpanderSettingTab } from './settingsTab';
|
||||||
|
|
@ -12,41 +12,31 @@ export default class DateRangeExpanderPlugin extends Plugin {
|
||||||
|
|
||||||
async onload() {
|
async onload() {
|
||||||
await this.loadSettings();
|
await this.loadSettings();
|
||||||
|
|
||||||
this.dateUtils = new DateUtils();
|
this.dateUtils = new DateUtils();
|
||||||
this.dateRangeExpander = new DateRangeExpander(this.app, this.settings, this.dateUtils);
|
this.dateRangeExpander = new DateRangeExpander(this.app, this.settings, this.dateUtils);
|
||||||
|
|
||||||
this.addSettingTab(new DateRangeExpanderSettingTab(this.app, this));
|
this.addSettingTab(new DateRangeExpanderSettingTab(this.app, this));
|
||||||
|
|
||||||
this.addCommand({
|
this.addCommand({
|
||||||
id: 'insert-expanded-date-range',
|
id: 'insert-expanded-date-range',
|
||||||
name: 'Insert',
|
name: 'Insert',
|
||||||
checkCallback: (checking: boolean) => {
|
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
new DateInputModal(this.app, (rangeInput) => {
|
||||||
if (markdownView) {
|
const dateStartAndEnd = this.dateUtils.getStartAndEndDates(rangeInput);
|
||||||
if (!checking) {
|
|
||||||
new DateInputModal(this.app, (rangeInput) => {
|
if (dateStartAndEnd) {
|
||||||
const dateStartAndEnd = this.dateUtils.getStartAndEndDates(rangeInput);
|
const cursor = editor.getCursor();
|
||||||
|
let insertedDateRange = this.dateRangeExpander.expandDateRange(dateStartAndEnd) + ' ';
|
||||||
if (dateStartAndEnd) {
|
if (rangeInput.useCallout) {
|
||||||
const editor = this.getActiveEditor();
|
insertedDateRange = this.dateRangeExpander.wrapInCallout(dateStartAndEnd, insertedDateRange);
|
||||||
if (editor) {
|
}
|
||||||
const cursor = editor.getCursor();
|
editor.replaceRange(insertedDateRange, { line: cursor.line, ch: cursor.ch }, cursor);
|
||||||
let insertedDateRange = this.dateRangeExpander.expandDateRange(dateStartAndEnd) + ' ';
|
|
||||||
if (rangeInput.useCallout) {
|
const endPos = { line: cursor.line, ch: cursor.ch + insertedDateRange.length };
|
||||||
insertedDateRange = this.dateRangeExpander.wrapInCallout(dateStartAndEnd, insertedDateRange);
|
editor.setCursor(endPos);
|
||||||
}
|
|
||||||
editor.replaceRange(insertedDateRange, { line: cursor.line, ch: cursor.ch }, cursor);
|
|
||||||
|
|
||||||
const endPos = { line: cursor.line, ch: cursor.ch + insertedDateRange.length };
|
|
||||||
editor.setCursor(endPos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}).open();
|
|
||||||
}
|
}
|
||||||
|
}).open();
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -54,12 +44,4 @@ export default class DateRangeExpanderPlugin extends Plugin {
|
||||||
async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); }
|
async loadSettings() { this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); }
|
||||||
|
|
||||||
async saveSettings() { await this.saveData(this.settings); }
|
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 { App, PluginSettingTab, Setting } from 'obsidian';
|
||||||
import DateRangeExpanderPlugin from './main';
|
import DateRangeExpanderPlugin from './main';
|
||||||
import { DEFAULT_SETTINGS } from './types';
|
import { DEFAULT_SETTINGS } from './types';
|
||||||
|
import { normalizePath } from 'obsidian';
|
||||||
|
|
||||||
export class DateRangeExpanderSettingTab extends PluginSettingTab {
|
export class DateRangeExpanderSettingTab extends PluginSettingTab {
|
||||||
plugin: DateRangeExpanderPlugin;
|
plugin: DateRangeExpanderPlugin;
|
||||||
|
|
@ -98,7 +99,7 @@ export class DateRangeExpanderSettingTab extends PluginSettingTab {
|
||||||
text.setPlaceholder('Enter folder path')
|
text.setPlaceholder('Enter folder path')
|
||||||
.setValue(this.plugin.settings.customFolderPath || '')
|
.setValue(this.plugin.settings.customFolderPath || '')
|
||||||
.onChange(async (value) => {
|
.onChange(async (value) => {
|
||||||
this.plugin.settings.customFolderPath = value;
|
this.plugin.settings.customFolderPath = normalizePath(value);
|
||||||
await this.plugin.saveSettings();
|
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
|
.date-input-row {
|
||||||
available in the app when your plugin is enabled.
|
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