Skip to content

Commit

Permalink
feat: add indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
srg-kostyrko committed Aug 26, 2024
1 parent df2b945 commit fbeee9a
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/components/DatePicker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ const props = withDefaults(
placeholder?: string;
previewFormat?: string;
disabled?: boolean;
tooltip?: string;
}>(),
{
placeholder: "Pick a date",
previewFormat: "YYYY-MM-DD",
tooltip: "Pick a date",
},
);
Expand Down Expand Up @@ -48,7 +50,7 @@ function openPickerModal() {
</script>

<template>
<ObsidianButton class="date-picker" :disabled="disabled" @click="openPickerModal">
<ObsidianButton class="date-picker" :disabled="disabled" :tooltip="tooltip" @click="openPickerModal">
<ObsidianIcon name="calendar" /> <span>{{ formattedDate }}</span>
</ObsidianButton>
</template>
Expand Down
8 changes: 7 additions & 1 deletion src/components/obsidian/ObsidianButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ defineProps<{
cta?: boolean;
warning?: boolean;
type?: "button" | "submit" | "reset";
tooltip?: string;
}>();
</script>

<template>
<button :class="{ 'mod-cta': cta, 'mod-warning': warning }" :disabled="disabled" :type="type ?? 'button'">
<button
:class="{ 'mod-cta': cta, 'mod-warning': warning }"
:disabled="disabled"
:type="type ?? 'button'"
:aria-label="tooltip"
>
<slot />
</button>
</template>
Expand Down
8 changes: 8 additions & 0 deletions src/components/obsidian/ObsidianNumberInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defineProps<{
disabled?: boolean;
min?: number;
max?: number;
narrow?: boolean;
}>();
const model = defineModel<number>();
Expand All @@ -13,10 +14,17 @@ const model = defineModel<number>();
<input
v-model="model"
type="number"
:class="{ 'narrow-input': narrow }"
:placeholder="placeholder"
:disabled="disabled"
:min="min"
:max="max"
spellcheck="false"
/>
</template>

<style scoped>
.narrow-input {
width: 60px;
}
</style>
3 changes: 2 additions & 1 deletion src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export const defaultJournalSettings: JournalSettings = {
enabled: false,
anchorDate: "",
anchorIndex: 1,
allowBefore: false,
type: "increment",
resetAfter: 0,
resetAfter: 2,
secondary: false,
secondaryAncorIndex: 1,
},
Expand Down
50 changes: 48 additions & 2 deletions src/journals/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Journal {
return await this.#buildMetadata(interval);
}

async next(metadata: JournalMetadata, existing = false): Promise<JournalMetadata | null> {
async next(metadata: JournalInterval, existing = false): Promise<JournalMetadata | null> {
if (existing) {
const nextExstingMetadata = plugin$.value.index.findNext(this.id, metadata);
if (nextExstingMetadata) return nextExstingMetadata;
Expand All @@ -84,7 +84,7 @@ export class Journal {
return await this.#buildMetadata(interval);
}

async previous(metadata: JournalMetadata, existing = false): Promise<JournalMetadata | null> {
async previous(metadata: JournalInterval, existing = false): Promise<JournalMetadata | null> {
if (existing) {
const previousExstingMetadata = plugin$.value.index.findPrevious(this.id, metadata);
if (previousExstingMetadata) return previousExstingMetadata;
Expand Down Expand Up @@ -198,6 +198,7 @@ export class Journal {
id: this.id,
start_date: interval.start_date,
end_date: interval.start_date,
index: await this.#resolveIndex(interval),
};
return metadata;
}
Expand Down Expand Up @@ -250,4 +251,49 @@ export class Journal {

return true;
}

async #resolveIndex(interval: JournalInterval): Promise<number | undefined> {
if (!this.#config.value.index.enabled) return undefined;
if (!this.#config.value.index.anchorDate || !this.#config.value.index.anchorIndex) return undefined;
const before = await this.previous(interval, true);
if (before && before.index) {
const repeats = this.#intervalResolver.countRepeats(before.end_date, interval.start_date);
let index = before.index + repeats;
if (this.#config.value.index.type === "reset_after") {
index %= this.#config.value.index.resetAfter;
}
return index;
}
const after = await this.next(interval, true);
if (after && after.index) {
const repeats = this.#intervalResolver.countRepeats(interval.end_date, after.start_date);
let index = after.index - repeats;
if (this.#config.value.index.type === "reset_after" && index < 0) {
index *= -1;
}
return index;
}
const anchor = date(this.#config.value.index.anchorDate);
if (!anchor.isValid()) return undefined;
if (
anchor.isAfter(interval.start_date) &&
this.#config.value.index.type === "increment" &&
!this.#config.value.index.allowBefore
)
return undefined;
if (anchor.isBefore(interval.start_date)) {
const repeats = this.#intervalResolver.countRepeats(this.#config.value.index.anchorDate, interval.start_date);
let index = this.#config.value.index.anchorIndex + repeats;
if (this.#config.value.index.type === "reset_after") {
index %= this.#config.value.index.resetAfter;
}
return index;
}
const repeats = this.#intervalResolver.countRepeats(interval.end_date, this.#config.value.index.anchorDate);
let index = this.#config.value.index.anchorIndex - repeats;
if (this.#config.value.index.type === "reset_after" && index < 0) {
index *= -1;
}
return index;
}
}
6 changes: 3 additions & 3 deletions src/journals/journals-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, type TAbstractFile, TFile, moment, type FrontMatterCache } f
import { app$ } from "../stores/obsidian.store";
import { computed, ref, type ComputedRef } from "vue";
import IntervalTree from "@flatten-js/interval-tree";
import type { JournalMetadata } from "../types/journal.types";
import type { JournalInterval, JournalMetadata } from "../types/journal.types";
import {
FRONTMATTER_END_DATE_KEY,
FRONTMATTER_ID_KEY,
Expand Down Expand Up @@ -38,13 +38,13 @@ export class JournalsIndex extends Component {
return list.find((entry) => entry.id === journalId);
}

findNext(journalId: string, metadata: JournalMetadata): JournalMetadata | null {
findNext(journalId: string, metadata: JournalInterval): JournalMetadata | null {
const date = moment(metadata.end_date).add(1, "day").toDate().getTime();
const list = this.#intervalTree.search([date, Infinity]);
return list.find((entry) => entry.id === journalId);
}

findPrevious(journalId: string, metadata: JournalMetadata): JournalMetadata | null {
findPrevious(journalId: string, metadata: JournalInterval): JournalMetadata | null {
const date = moment(metadata.start_date).subtract(1, "day").toDate().getTime();
const list = this.#intervalTree.search([0, date]);
return list.findLast((entry) => entry.id === journalId);
Expand Down
46 changes: 46 additions & 0 deletions src/settings/JournalSettingsEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ObsidianNumberInput from "../components/obsidian/ObsidianNumberInput.vue"
import ObsidianIconButton from "../components/obsidian/ObsidianIconButton.vue";
import ObsidianButton from "../components/obsidian/ObsidianButton.vue";
import ObsidianDropdown from "../components/obsidian/ObsidianDropdown.vue";
import ObsidianToggle from "../components/obsidian/ObsidianToggle.vue";
import FolderInput from "../components/FolderInput.vue";
import DateFormatPreview from "../components/DateFormatPreview.vue";
import VariableReferenceHint from "../components/VariableReferenceHint.vue";
Expand Down Expand Up @@ -51,6 +52,14 @@ function deleteCommand(index: number): void {
pluginSettings$.value.showReloadHint = true;
}
watch(
() => journal.value.start,
(value) => {
if (value) {
journal.value.index.anchorDate = value;
}
},
);
watch(
() => journal.value.end.type,
() => {
Expand Down Expand Up @@ -102,6 +111,43 @@ watch(
</template>
</ObsidianSetting>

<ObsidianSetting name="Index notes">
<template #description> Allows to assign numbers to notes (ex. Day 1, Day 2, etc.). </template>
<ObsidianToggle v-model="journal.index.enabled" />
</ObsidianSetting>

<template v-if="journal.index.enabled">
<ObsidianSetting name="Anchor date">
<template #description>
This date is used to connect some number to it for further calculations.<br />
Start date is used as anchor date if defined.
</template>
<div :aria-label="journal.start ? 'Start date is used' : ''">
<DatePicker v-model="journal.index.anchorDate" :disabled="!!journal.start" />
</div>
</ObsidianSetting>
<ObsidianSetting name="Start number">
<template #description> This number is used to start numbering notes at anchor date. </template>
<ObsidianNumberInput v-model="journal.index.anchorIndex" :min="1" />
</ObsidianSetting>

<ObsidianSetting name="Index change">
<template #description> Define how index number will change with time. </template>
<ObsidianDropdown v-model="journal.index.type">
<option value="increment">Constantly increasing</option>
<option value="reset_after">Resets after</option>
</ObsidianDropdown>
<template v-if="journal.index.type === 'reset_after'">
<ObsidianNumberInput v-model="journal.index.resetAfter" :min="2" narrow />
repeats
</template>
</ObsidianSetting>
<ObsidianSetting v-if="!journal.start && journal.index.type === 'increment'" name="Allow before">
<template #description> Enabled to index before anchor date. Might result in negative numbers. </template>
<ObsidianToggle v-model="journal.index.allowBefore" />
</ObsidianSetting>
</template>

<ObsidianSetting name="Open note">
<ObsidianDropdown v-model="journal.openMode">
<option value="active">Replacing active note</option>
Expand Down
3 changes: 2 additions & 1 deletion src/types/settings.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ export interface JournalSettings {
enabled: boolean;
anchorDate: string;
anchorIndex: number;
type: "increment" | "year_reset" | "reset_after";
allowBefore: boolean;
type: "increment" | "reset_after";
resetAfter: number;
secondary: boolean;
secondaryAncorIndex: number;
Expand Down

0 comments on commit fbeee9a

Please sign in to comment.