More Flexible Calendar Items Handling

This commit is contained in:
Ozan Tellioglu 2024-03-31 14:20:34 +02:00
parent 3504b38540
commit f8741bdbf8
3 changed files with 68 additions and 34 deletions

View file

@ -8,6 +8,7 @@ import OZCalendarPlugin from 'main';
import { isMouseEvent, openFile } from '../util/utils'; import { isMouseEvent, openFile } from '../util/utils';
import { Menu, TFile } from 'obsidian'; import { Menu, TFile } from 'obsidian';
import { VIEW_TYPE } from 'view'; import { VIEW_TYPE } from 'view';
import { OZNote } from 'types';
interface NoteListComponentParams { interface NoteListComponentParams {
selectedDay: Date; selectedDay: Date;
@ -57,13 +58,18 @@ export default function NoteListComponent(params: NoteListComponentParams) {
} }
}; };
const selectedDayNotes = useMemo(() => { const selectedDayNotes: OZNote[] = useMemo(() => {
const selectedDayIso = dayjs(selectedDay).format('YYYY-MM-DD'); const selectedDayIso = dayjs(selectedDay).format('YYYY-MM-DD');
let sortedList = let sortedList: OZNote[] = [];
selectedDayIso in plugin.OZCALENDARDAYS_STATE ? plugin.OZCALENDARDAYS_STATE[selectedDayIso] : []; if (selectedDayIso in plugin.OZCALENDARDAYS_STATE) {
sortedList = plugin.OZCALENDARDAYS_STATE[selectedDayIso].filter(
(ozItem) => ozItem.type === 'note'
) as OZNote[];
}
sortedList = sortedList.sort((a, b) => { sortedList = sortedList.sort((a, b) => {
if (plugin.settings.sortingOption === 'name-rev') [a, b] = [b, a]; if (plugin.settings.sortingOption === 'name-rev')
return extractFileName(a).localeCompare(extractFileName(b), 'en', { numeric: true }); [a.displayName, b.displayName] = [b.displayName, a.displayName];
return a.displayName.localeCompare(b.displayName, 'en', { numeric: true });
}); });
return sortedList; return sortedList;
}, [selectedDay, forceValue]); }, [selectedDay, forceValue]);
@ -122,7 +128,7 @@ export default function NoteListComponent(params: NoteListComponentParams) {
No note found No note found
</div> </div>
)} )}
{selectedDayNotes.map((notePath) => { {selectedDayNotes.map((ozNote) => {
return ( return (
<div <div
className={ className={
@ -131,12 +137,12 @@ export default function NoteListComponent(params: NoteListComponentParams) {
? ' oz-calendar-overflow-hide' ? ' oz-calendar-overflow-hide'
: '') : '')
} }
id={notePath} id={ozNote.path}
key={notePath} key={ozNote.path}
onClick={(e) => openFilePath(e, notePath)} onClick={(e) => openFilePath(e, ozNote.path)}
onContextMenu={(e) => triggerFileContextMenu(e, notePath)}> onContextMenu={(e) => triggerFileContextMenu(e, ozNote.path)}>
<HiOutlineDocumentText className="oz-calendar-note-line-icon" /> <HiOutlineDocumentText className="oz-calendar-note-line-icon" />
<span>{extractFileName(notePath)}</span> <span>{ozNote.displayName}</span>
</div> </div>
); );
})} })}

View file

@ -2,7 +2,7 @@ import { CachedMetadata, Menu, Plugin, TAbstractFile, TFile, addIcon } from 'obs
import { OZCalendarView, VIEW_TYPE } from 'view'; import { OZCalendarView, VIEW_TYPE } from 'view';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat'; import customParseFormat from 'dayjs/plugin/customParseFormat';
import { DayChangeCommandAction, OZCalendarDaysMap } from 'types'; import { DayChangeCommandAction, OZCalendarDaysMap, fileToOZItem } from 'types';
import { OZCAL_ICON } from './util/icons'; import { OZCAL_ICON } from './util/icons';
import { OZCalendarPluginSettings, DEFAULT_SETTINGS, OZCalendarPluginSettingsTab } from './settings/settings'; import { OZCalendarPluginSettings, DEFAULT_SETTINGS, OZCalendarPluginSettingsTab } from './settings/settings';
import { CreateNoteModal } from 'modal'; import { CreateNoteModal } from 'modal';
@ -140,13 +140,13 @@ export default class OZCalendarPlugin extends Plugin {
* @param date * @param date
* @param filePath * @param filePath
*/ */
addFilePathToState = (date: string, filePath: string) => { addFilePathToState = (date: string, file: TFile) => {
let newStateMap = this.OZCALENDARDAYS_STATE; let newStateMap = this.OZCALENDARDAYS_STATE;
// if exists, add the new file path // if exists, add the new file path
if (date in newStateMap) { if (date in newStateMap) {
newStateMap[date] = [...newStateMap[date], filePath]; newStateMap[date] = [...newStateMap[date], fileToOZItem({ note: file })];
} else { } else {
newStateMap[date] = [filePath]; newStateMap[date] = [fileToOZItem({ note: file })];
} }
this.OZCALENDARDAYS_STATE = newStateMap; this.OZCALENDARDAYS_STATE = newStateMap;
}; };
@ -160,8 +160,10 @@ export default class OZCalendarPlugin extends Plugin {
let changeFlag = false; let changeFlag = false;
let newStateMap = this.OZCALENDARDAYS_STATE; let newStateMap = this.OZCALENDARDAYS_STATE;
for (let k of Object.keys(newStateMap)) { for (let k of Object.keys(newStateMap)) {
if (newStateMap[k].contains(filePath)) { if (newStateMap[k].some((ozItem) => ozItem.type === 'note' && ozItem.path === filePath)) {
newStateMap[k] = newStateMap[k].filter((p) => p !== filePath); newStateMap[k] = newStateMap[k].filter((ozItem) => {
return !(ozItem.type === 'note' && ozItem.path === filePath);
});
changeFlag = true; changeFlag = true;
} }
} }
@ -183,7 +185,7 @@ export default class OZCalendarPlugin extends Plugin {
if (k === this.settings.yamlKey) { if (k === this.settings.yamlKey) {
let fmValue = String(fm[k]); let fmValue = String(fm[k]);
let parsedDayISOString = dayjs(fmValue, this.settings.dateFormat).format('YYYY-MM-DD'); let parsedDayISOString = dayjs(fmValue, this.settings.dateFormat).format('YYYY-MM-DD');
this.addFilePathToState(parsedDayISOString, file.path); this.addFilePathToState(parsedDayISOString, file);
changeFlag = true; changeFlag = true;
} }
} }
@ -215,11 +217,11 @@ export default class OZCalendarPlugin extends Plugin {
let parsedDayISOString = dayjs(fmValue, this.settings.dateFormat).format('YYYY-MM-DD'); let parsedDayISOString = dayjs(fmValue, this.settings.dateFormat).format('YYYY-MM-DD');
// If date doesn't exist, create a new one // If date doesn't exist, create a new one
if (!(parsedDayISOString in this.OZCALENDARDAYS_STATE)) { if (!(parsedDayISOString in this.OZCALENDARDAYS_STATE)) {
this.addFilePathToState(parsedDayISOString, file.path); this.addFilePathToState(parsedDayISOString, file);
} else { } else {
// if date exists and note is not in the date list // if date exists and note is not in the date list
if (!(file.path in this.OZCALENDARDAYS_STATE[parsedDayISOString])) { if (!(file.path in this.OZCALENDARDAYS_STATE[parsedDayISOString])) {
this.addFilePathToState(parsedDayISOString, file.path); this.addFilePathToState(parsedDayISOString, file);
} }
} }
} }
@ -235,14 +237,16 @@ export default class OZCalendarPlugin extends Plugin {
let changeFlag = false; let changeFlag = false;
if (file instanceof TFile && file.extension === 'md') { if (file instanceof TFile && file.extension === 'md') {
for (let k of Object.keys(this.OZCALENDARDAYS_STATE)) { for (let k of Object.keys(this.OZCALENDARDAYS_STATE)) {
for (let filePath of this.OZCALENDARDAYS_STATE[k]) { for (let ozItem of this.OZCALENDARDAYS_STATE[k]) {
if (filePath === oldPath) { if (ozItem.type === 'note' && ozItem.path === oldPath) {
let oldIndex = this.OZCALENDARDAYS_STATE[k].indexOf(filePath);
if (this.settings.dateSource === 'yaml') { if (this.settings.dateSource === 'yaml') {
this.OZCALENDARDAYS_STATE[k][oldIndex] = file.path; ozItem.path = file.path;
ozItem.displayName = file.basename;
changeFlag = true; changeFlag = true;
} else if (this.settings.dateSource === 'filename') { } else if (this.settings.dateSource === 'filename') {
this.OZCALENDARDAYS_STATE[k].splice(oldIndex, 1); this.OZCALENDARDAYS_STATE[k] = this.OZCALENDARDAYS_STATE[k].filter((ozItem) => {
return !(ozItem.type === 'note' && ozItem.path === oldPath);
});
changeFlag = true; changeFlag = true;
} }
} }
@ -256,10 +260,10 @@ export default class OZCalendarPlugin extends Plugin {
if (parsedDayISOString in this.OZCALENDARDAYS_STATE) { if (parsedDayISOString in this.OZCALENDARDAYS_STATE) {
this.OZCALENDARDAYS_STATE[parsedDayISOString] = [ this.OZCALENDARDAYS_STATE[parsedDayISOString] = [
...this.OZCALENDARDAYS_STATE[parsedDayISOString], ...this.OZCALENDARDAYS_STATE[parsedDayISOString],
file.path, fileToOZItem({ note: file }),
]; ];
} else { } else {
this.OZCALENDARDAYS_STATE[parsedDayISOString] = [file.path]; this.OZCALENDARDAYS_STATE[parsedDayISOString] = [fileToOZItem({ note: file })];
} }
changeFlag = true; changeFlag = true;
} }
@ -280,10 +284,10 @@ export default class OZCalendarPlugin extends Plugin {
if (parsedDayISOString in this.OZCALENDARDAYS_STATE) { if (parsedDayISOString in this.OZCALENDARDAYS_STATE) {
this.OZCALENDARDAYS_STATE[parsedDayISOString] = [ this.OZCALENDARDAYS_STATE[parsedDayISOString] = [
...this.OZCALENDARDAYS_STATE[parsedDayISOString], ...this.OZCALENDARDAYS_STATE[parsedDayISOString],
file.path, fileToOZItem({ note: file }),
]; ];
} else { } else {
this.OZCALENDARDAYS_STATE[parsedDayISOString] = [file.path]; this.OZCALENDARDAYS_STATE[parsedDayISOString] = [fileToOZItem({ note: file })];
} }
} }
this.calendarForceUpdate(); this.calendarForceUpdate();
@ -335,10 +339,10 @@ export default class OZCalendarPlugin extends Plugin {
if (parsedDayISOString in OZCalendarDays) { if (parsedDayISOString in OZCalendarDays) {
OZCalendarDays[parsedDayISOString] = [ OZCalendarDays[parsedDayISOString] = [
...OZCalendarDays[parsedDayISOString], ...OZCalendarDays[parsedDayISOString],
mdFile.path, fileToOZItem({ note: mdFile }),
]; ];
} else { } else {
OZCalendarDays[parsedDayISOString] = [mdFile.path]; OZCalendarDays[parsedDayISOString] = [fileToOZItem({ note: mdFile })];
} }
} }
} }
@ -351,10 +355,10 @@ export default class OZCalendarPlugin extends Plugin {
if (parsedDayISOString in OZCalendarDays) { if (parsedDayISOString in OZCalendarDays) {
OZCalendarDays[parsedDayISOString] = [ OZCalendarDays[parsedDayISOString] = [
...OZCalendarDays[parsedDayISOString], ...OZCalendarDays[parsedDayISOString],
mdFile.path, fileToOZItem({ note: mdFile }),
]; ];
} else { } else {
OZCalendarDays[parsedDayISOString] = [mdFile.path]; OZCalendarDays[parsedDayISOString] = [fileToOZItem({ note: mdFile })];
} }
} }
} }

View file

@ -1,5 +1,29 @@
import { TFile } from 'obsidian';
export type OZNote = {
type: 'note';
displayName: string;
path: string;
};
export type OZReminder = {
type: 'task' | 'periodic';
displayName: string;
date: string;
};
type OZItem = OZNote | OZReminder;
export interface OZCalendarDaysMap { export interface OZCalendarDaysMap {
[key: string]: string[]; [key: string]: OZItem[];
} }
export type DayChangeCommandAction = 'next-day' | 'previous-day' | 'today'; export type DayChangeCommandAction = 'next-day' | 'previous-day' | 'today';
export const fileToOZItem = (params: { note: TFile }): OZItem => {
return {
type: 'note',
displayName: params.note.basename,
path: params.note.path,
};
};