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 { Menu, TFile } from 'obsidian';
import { VIEW_TYPE } from 'view';
import { OZNote } from 'types';
interface NoteListComponentParams {
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');
let sortedList =
selectedDayIso in plugin.OZCALENDARDAYS_STATE ? plugin.OZCALENDARDAYS_STATE[selectedDayIso] : [];
let sortedList: OZNote[] = [];
if (selectedDayIso in plugin.OZCALENDARDAYS_STATE) {
sortedList = plugin.OZCALENDARDAYS_STATE[selectedDayIso].filter(
(ozItem) => ozItem.type === 'note'
) as OZNote[];
}
sortedList = sortedList.sort((a, b) => {
if (plugin.settings.sortingOption === 'name-rev') [a, b] = [b, a];
return extractFileName(a).localeCompare(extractFileName(b), 'en', { numeric: true });
if (plugin.settings.sortingOption === 'name-rev')
[a.displayName, b.displayName] = [b.displayName, a.displayName];
return a.displayName.localeCompare(b.displayName, 'en', { numeric: true });
});
return sortedList;
}, [selectedDay, forceValue]);
@ -122,7 +128,7 @@ export default function NoteListComponent(params: NoteListComponentParams) {
No note found
</div>
)}
{selectedDayNotes.map((notePath) => {
{selectedDayNotes.map((ozNote) => {
return (
<div
className={
@ -131,12 +137,12 @@ export default function NoteListComponent(params: NoteListComponentParams) {
? ' oz-calendar-overflow-hide'
: '')
}
id={notePath}
key={notePath}
onClick={(e) => openFilePath(e, notePath)}
onContextMenu={(e) => triggerFileContextMenu(e, notePath)}>
id={ozNote.path}
key={ozNote.path}
onClick={(e) => openFilePath(e, ozNote.path)}
onContextMenu={(e) => triggerFileContextMenu(e, ozNote.path)}>
<HiOutlineDocumentText className="oz-calendar-note-line-icon" />
<span>{extractFileName(notePath)}</span>
<span>{ozNote.displayName}</span>
</div>
);
})}

View file

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