mirror of
https://github.com/ozntel/oz-calendar.git
synced 2026-07-22 04:34:40 +00:00
Initial Commit
This commit is contained in:
parent
61c697beb6
commit
baf260d491
17 changed files with 4572 additions and 293 deletions
10
.prettierrc
Normal file
10
.prettierrc
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"trailingComma": "es5",
|
||||
"tabWidth": 4,
|
||||
"semi": true,
|
||||
"singleQuote": true,
|
||||
"printWidth": 115,
|
||||
"jsxBracketSameLine": true,
|
||||
"bracketSpacing": true,
|
||||
"arrowParens": "always"
|
||||
}
|
||||
97
README.md
97
README.md
|
|
@ -1,96 +1,3 @@
|
|||
# Obsidian Sample Plugin
|
||||
# OZ Calendar Plugin
|
||||
|
||||
This is a sample plugin for Obsidian (https://obsidian.md).
|
||||
|
||||
This project uses Typescript to provide type checking and documentation.
|
||||
The repo depends on the latest plugin API (obsidian.d.ts) in Typescript Definition format, which contains TSDoc comments describing what it does.
|
||||
|
||||
**Note:** The Obsidian API is still in early alpha and is subject to change at any time!
|
||||
|
||||
This sample plugin demonstrates some of the basic functionality the plugin API can do.
|
||||
- Changes the default font color to red using `styles.css`.
|
||||
- Adds a ribbon icon, which shows a Notice when clicked.
|
||||
- Adds a command "Open Sample Modal" which opens a Modal.
|
||||
- Adds a plugin setting tab to the settings page.
|
||||
- Registers a global click event and output 'click' to the console.
|
||||
- Registers a global interval which logs 'setInterval' to the console.
|
||||
|
||||
## First time developing plugins?
|
||||
|
||||
Quick starting guide for new plugin devs:
|
||||
|
||||
- Check if [someone already developed a plugin for what you want](https://obsidian.md/plugins)! There might be an existing plugin similar enough that you can partner up with.
|
||||
- Make a copy of this repo as a template with the "Use this template" button (login to GitHub if you don't see it).
|
||||
- Clone your repo to a local development folder. For convenience, you can place this folder in your `.obsidian/plugins/your-plugin-name` folder.
|
||||
- Install NodeJS, then run `npm i` in the command line under your repo folder.
|
||||
- Run `npm run dev` to compile your plugin from `main.ts` to `main.js`.
|
||||
- Make changes to `main.ts` (or create new `.ts` files). Those changes should be automatically compiled into `main.js`.
|
||||
- Reload Obsidian to load the new version of your plugin.
|
||||
- Enable plugin in settings window.
|
||||
- For updates to the Obsidian API run `npm update` in the command line under your repo folder.
|
||||
|
||||
## Releasing new releases
|
||||
|
||||
- Update your `manifest.json` with your new version number, such as `1.0.1`, and the minimum Obsidian version required for your latest release.
|
||||
- Update your `versions.json` file with `"new-plugin-version": "minimum-obsidian-version"` so older versions of Obsidian can download an older version of your plugin that's compatible.
|
||||
- Create new GitHub release using your new version number as the "Tag version". Use the exact version number, don't include a prefix `v`. See here for an example: https://github.com/obsidianmd/obsidian-sample-plugin/releases
|
||||
- Upload the files `manifest.json`, `main.js`, `styles.css` as binary attachments. Note: The manifest.json file must be in two places, first the root path of your repository and also in the release.
|
||||
- Publish the release.
|
||||
|
||||
> You can simplify the version bump process by running `npm version patch`, `npm version minor` or `npm version major` after updating `minAppVersion` manually in `manifest.json`.
|
||||
> The command will bump version in `manifest.json` and `package.json`, and add the entry for the new version to `versions.json`
|
||||
|
||||
## Adding your plugin to the community plugin list
|
||||
|
||||
- Check https://github.com/obsidianmd/obsidian-releases/blob/master/plugin-review.md
|
||||
- Publish an initial version.
|
||||
- Make sure you have a `README.md` file in the root of your repo.
|
||||
- Make a pull request at https://github.com/obsidianmd/obsidian-releases to add your plugin.
|
||||
|
||||
## How to use
|
||||
|
||||
- Clone this repo.
|
||||
- `npm i` or `yarn` to install dependencies
|
||||
- `npm run dev` to start compilation in watch mode.
|
||||
|
||||
## Manually installing the plugin
|
||||
|
||||
- Copy over `main.js`, `styles.css`, `manifest.json` to your vault `VaultFolder/.obsidian/plugins/your-plugin-id/`.
|
||||
|
||||
## Improve code quality with eslint (optional)
|
||||
- [ESLint](https://eslint.org/) is a tool that analyzes your code to quickly find problems. You can run ESLint against your plugin to find common bugs and ways to improve your code.
|
||||
- To use eslint with this project, make sure to install eslint from terminal:
|
||||
- `npm install -g eslint`
|
||||
- To use eslint to analyze this project use this command:
|
||||
- `eslint main.ts`
|
||||
- eslint will then create a report with suggestions for code improvement by file and line number.
|
||||
- If your source code is in a folder, such as `src`, you can use eslint with this command to analyze all files in that folder:
|
||||
- `eslint .\src\`
|
||||
|
||||
## Funding URL
|
||||
|
||||
You can include funding URLs where people who use your plugin can financially support it.
|
||||
|
||||
The simple way is to set the `fundingUrl` field to your link in your `manifest.json` file:
|
||||
|
||||
```json
|
||||
{
|
||||
"fundingUrl": "https://buymeacoffee.com"
|
||||
}
|
||||
```
|
||||
|
||||
If you have multiple URLs, you can also do:
|
||||
|
||||
```json
|
||||
{
|
||||
"fundingUrl": {
|
||||
"Buy Me a Coffee": "https://buymeacoffee.com",
|
||||
"GitHub Sponsor": "https://github.com/sponsors",
|
||||
"Patreon": "https://www.patreon.com/"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## API Documentation
|
||||
|
||||
See https://github.com/obsidianmd/obsidian-api
|
||||
This plugin is created to help users to easily view notes on Calendar using any YAML key with date value.
|
||||
|
|
|
|||
|
|
@ -1,43 +1,44 @@
|
|||
import esbuild from "esbuild";
|
||||
import process from "process";
|
||||
import builtins from "builtin-modules";
|
||||
import esbuild from 'esbuild';
|
||||
import process from 'process';
|
||||
import builtins from 'builtin-modules';
|
||||
|
||||
const banner =
|
||||
`/*
|
||||
const banner = `/*
|
||||
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
|
||||
if you want to view the source, please visit the github repository of this plugin
|
||||
https://github.com/ozntel/oz-calendar
|
||||
*/
|
||||
`;
|
||||
|
||||
const prod = (process.argv[2] === "production");
|
||||
const prod = process.argv[2] === 'production';
|
||||
|
||||
const context = await esbuild.context({
|
||||
banner: {
|
||||
js: banner,
|
||||
},
|
||||
entryPoints: ["main.ts"],
|
||||
entryPoints: ['src/main.ts'],
|
||||
bundle: true,
|
||||
external: [
|
||||
"obsidian",
|
||||
"electron",
|
||||
"@codemirror/autocomplete",
|
||||
"@codemirror/collab",
|
||||
"@codemirror/commands",
|
||||
"@codemirror/language",
|
||||
"@codemirror/lint",
|
||||
"@codemirror/search",
|
||||
"@codemirror/state",
|
||||
"@codemirror/view",
|
||||
"@lezer/common",
|
||||
"@lezer/highlight",
|
||||
"@lezer/lr",
|
||||
...builtins],
|
||||
format: "cjs",
|
||||
target: "es2018",
|
||||
logLevel: "info",
|
||||
sourcemap: prod ? false : "inline",
|
||||
'obsidian',
|
||||
'electron',
|
||||
'@codemirror/autocomplete',
|
||||
'@codemirror/collab',
|
||||
'@codemirror/commands',
|
||||
'@codemirror/language',
|
||||
'@codemirror/lint',
|
||||
'@codemirror/search',
|
||||
'@codemirror/state',
|
||||
'@codemirror/view',
|
||||
'@lezer/common',
|
||||
'@lezer/highlight',
|
||||
'@lezer/lr',
|
||||
...builtins,
|
||||
],
|
||||
format: 'cjs',
|
||||
target: 'es2018',
|
||||
logLevel: 'info',
|
||||
sourcemap: prod ? false : 'inline',
|
||||
treeShaking: true,
|
||||
outfile: "main.js",
|
||||
outfile: 'main.js',
|
||||
});
|
||||
|
||||
if (prod) {
|
||||
|
|
@ -45,4 +46,4 @@ if (prod) {
|
|||
process.exit(0);
|
||||
} else {
|
||||
await context.watch();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
137
main.ts
137
main.ts
|
|
@ -1,137 +0,0 @@
|
|||
import { App, Editor, MarkdownView, Modal, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
|
||||
|
||||
// Remember to rename these classes and interfaces!
|
||||
|
||||
interface MyPluginSettings {
|
||||
mySetting: string;
|
||||
}
|
||||
|
||||
const DEFAULT_SETTINGS: MyPluginSettings = {
|
||||
mySetting: 'default'
|
||||
}
|
||||
|
||||
export default class MyPlugin extends Plugin {
|
||||
settings: MyPluginSettings;
|
||||
|
||||
async onload() {
|
||||
await this.loadSettings();
|
||||
|
||||
// This creates an icon in the left ribbon.
|
||||
const ribbonIconEl = this.addRibbonIcon('dice', 'Sample Plugin', (evt: MouseEvent) => {
|
||||
// Called when the user clicks the icon.
|
||||
new Notice('This is a notice!');
|
||||
});
|
||||
// Perform additional things with the ribbon
|
||||
ribbonIconEl.addClass('my-plugin-ribbon-class');
|
||||
|
||||
// This adds a status bar item to the bottom of the app. Does not work on mobile apps.
|
||||
const statusBarItemEl = this.addStatusBarItem();
|
||||
statusBarItemEl.setText('Status Bar Text');
|
||||
|
||||
// This adds a simple command that can be triggered anywhere
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-simple',
|
||||
name: 'Open sample modal (simple)',
|
||||
callback: () => {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
});
|
||||
// This adds an editor command that can perform some operation on the current editor instance
|
||||
this.addCommand({
|
||||
id: 'sample-editor-command',
|
||||
name: 'Sample editor command',
|
||||
editorCallback: (editor: Editor, view: MarkdownView) => {
|
||||
console.log(editor.getSelection());
|
||||
editor.replaceSelection('Sample Editor Command');
|
||||
}
|
||||
});
|
||||
// This adds a complex command that can check whether the current state of the app allows execution of the command
|
||||
this.addCommand({
|
||||
id: 'open-sample-modal-complex',
|
||||
name: 'Open sample modal (complex)',
|
||||
checkCallback: (checking: boolean) => {
|
||||
// Conditions to check
|
||||
const markdownView = this.app.workspace.getActiveViewOfType(MarkdownView);
|
||||
if (markdownView) {
|
||||
// If checking is true, we're simply "checking" if the command can be run.
|
||||
// If checking is false, then we want to actually perform the operation.
|
||||
if (!checking) {
|
||||
new SampleModal(this.app).open();
|
||||
}
|
||||
|
||||
// This command will only show up in Command Palette when the check function returns true
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// This adds a settings tab so the user can configure various aspects of the plugin
|
||||
this.addSettingTab(new SampleSettingTab(this.app, this));
|
||||
|
||||
// If the plugin hooks up any global DOM events (on parts of the app that doesn't belong to this plugin)
|
||||
// Using this function will automatically remove the event listener when this plugin is disabled.
|
||||
this.registerDomEvent(document, 'click', (evt: MouseEvent) => {
|
||||
console.log('click', evt);
|
||||
});
|
||||
|
||||
// When registering intervals, this function will automatically clear the interval when the plugin is disabled.
|
||||
this.registerInterval(window.setInterval(() => console.log('setInterval'), 5 * 60 * 1000));
|
||||
}
|
||||
|
||||
onunload() {
|
||||
|
||||
}
|
||||
|
||||
async loadSettings() {
|
||||
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
|
||||
}
|
||||
|
||||
async saveSettings() {
|
||||
await this.saveData(this.settings);
|
||||
}
|
||||
}
|
||||
|
||||
class SampleModal extends Modal {
|
||||
constructor(app: App) {
|
||||
super(app);
|
||||
}
|
||||
|
||||
onOpen() {
|
||||
const {contentEl} = this;
|
||||
contentEl.setText('Woah!');
|
||||
}
|
||||
|
||||
onClose() {
|
||||
const {contentEl} = this;
|
||||
contentEl.empty();
|
||||
}
|
||||
}
|
||||
|
||||
class SampleSettingTab extends PluginSettingTab {
|
||||
plugin: MyPlugin;
|
||||
|
||||
constructor(app: App, plugin: MyPlugin) {
|
||||
super(app, plugin);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
display(): void {
|
||||
const {containerEl} = this;
|
||||
|
||||
containerEl.empty();
|
||||
|
||||
containerEl.createEl('h2', {text: 'Settings for my awesome plugin.'});
|
||||
|
||||
new Setting(containerEl)
|
||||
.setName('Setting #1')
|
||||
.setDesc('It\'s a secret')
|
||||
.addText(text => text
|
||||
.setPlaceholder('Enter your secret')
|
||||
.setValue(this.plugin.settings.mySetting)
|
||||
.onChange(async (value) => {
|
||||
console.log('Secret: ' + value);
|
||||
this.plugin.settings.mySetting = value;
|
||||
await this.plugin.saveSettings();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"id": "obsidian-sample-plugin",
|
||||
"name": "Sample Plugin",
|
||||
"version": "1.0.0",
|
||||
"id": "oz-calendar",
|
||||
"name": "OZ Calendar",
|
||||
"version": "0.0.0",
|
||||
"minAppVersion": "0.15.0",
|
||||
"description": "This is a sample plugin for Obsidian. This plugin demonstrates some of the capabilities of the Obsidian API.",
|
||||
"description": "View your notes in Calendar using any YAML key with date",
|
||||
"author": "Obsidian",
|
||||
"authorUrl": "https://obsidian.md",
|
||||
"fundingUrl": "https://obsidian.md/pricing",
|
||||
|
|
|
|||
3991
package-lock.json
generated
Normal file
3991
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
16
package.json
16
package.json
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"name": "obsidian-sample-plugin",
|
||||
"name": "oz-calendar",
|
||||
"version": "1.0.0",
|
||||
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
|
||||
"description": "View your notes in Calendar using any YAML key with date",
|
||||
"main": "main.js",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
|
|
@ -13,12 +13,24 @@
|
|||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.6",
|
||||
"@types/react": "17.0.2",
|
||||
"@types/react-calendar": "^3.9.0",
|
||||
"@types/react-dom": "17.0.2",
|
||||
"@typescript-eslint/eslint-plugin": "5.29.0",
|
||||
"@typescript-eslint/parser": "5.29.0",
|
||||
"builtin-modules": "3.3.0",
|
||||
"esbuild": "0.17.3",
|
||||
"obsidian": "latest",
|
||||
"prettier": "^2.8.4",
|
||||
"tslib": "2.4.0",
|
||||
"typescript": "4.7.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"dayjs": "1.11.7",
|
||||
"preact": "10",
|
||||
"react": "npm:@preact/compat@17.0.2",
|
||||
"react-calendar": "4.0.0",
|
||||
"react-dom": "npm:@preact/compat@17.0.2",
|
||||
"react-icons": "4.8.0"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
71
src/components/calendar.tsx
Normal file
71
src/components/calendar.tsx
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import React, { useEffect, useState } from 'react';
|
||||
import Calendar from 'react-calendar';
|
||||
import { RxDotFilled } from 'react-icons/rx';
|
||||
import OZCalendarPlugin from '../main';
|
||||
import useForceUpdate from '../hooks/forceUpdate';
|
||||
import NoteListComponent from './noteList';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export default function MyCalendar(params: { plugin: OZCalendarPlugin }) {
|
||||
const { plugin } = params;
|
||||
const forceUpdate = useForceUpdate();
|
||||
const [selectedDay, setSelectedDay] = useState<Date>(new Date());
|
||||
const [selectedDayNotes, setSelectedDayNotes] = useState<string[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const selectedDayIso = dayjs(selectedDay).format('YYYY-MM-DD');
|
||||
const notes =
|
||||
selectedDayIso in plugin.OZCALENDARDAYS_STATE ? plugin.OZCALENDARDAYS_STATE[selectedDayIso] : [];
|
||||
setSelectedDayNotes(notes);
|
||||
}, [selectedDay, plugin.OZCALENDARDAYS_STATE]);
|
||||
|
||||
useEffect(() => {
|
||||
window.addEventListener(plugin.EVENT_TYPES.forceUpdate, forceUpdate);
|
||||
return () => {
|
||||
window.removeEventListener(plugin.EVENT_TYPES.forceUpdate, forceUpdate);
|
||||
};
|
||||
}, []);
|
||||
|
||||
useEffect(() => forceUpdate(), [plugin.OZCALENDARDAYS_STATE]);
|
||||
|
||||
const customTileContent = ({ date, view }) => {
|
||||
if (view === 'month') {
|
||||
const dateString = dayjs(date).format('YYYY-MM-DD');
|
||||
|
||||
let dotsCount =
|
||||
dateString in plugin.OZCALENDARDAYS_STATE ? plugin.OZCALENDARDAYS_STATE[dateString].length : 0;
|
||||
return (
|
||||
<div className="dots-wrapper">
|
||||
{[...Array(Math.min(dotsCount, 2))].map((_, index) => (
|
||||
<RxDotFilled viewBox="0 0 15 15" />
|
||||
))}
|
||||
{dotsCount > 2 && <span>+{dotsCount - 2}</span>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Calendar
|
||||
onChange={setSelectedDay}
|
||||
value={selectedDay}
|
||||
maxDetail="month"
|
||||
minDetail="month"
|
||||
view="month"
|
||||
tileContent={customTileContent}
|
||||
formatMonthYear={(locale, date) => dayjs(date).format('MMM YYYY')}
|
||||
/>
|
||||
<>
|
||||
<div id="oz-calendar-divider"></div>
|
||||
<NoteListComponent
|
||||
selectedDay={selectedDay}
|
||||
setSelectedDay={setSelectedDay}
|
||||
selectedDayNotes={selectedDayNotes}
|
||||
plugin={plugin}
|
||||
/>
|
||||
</>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
73
src/components/noteList.tsx
Normal file
73
src/components/noteList.tsx
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import React from 'react';
|
||||
import { BsArrowRight, BsArrowLeft } from 'react-icons/bs';
|
||||
import { HiOutlineDocumentText } from 'react-icons/hi';
|
||||
import dayjs from 'dayjs';
|
||||
import OZCalendarPlugin from '../main';
|
||||
import { openFile } from '../utils';
|
||||
import { TFile } from 'obsidian';
|
||||
|
||||
interface NoteListComponentParams {
|
||||
selectedDay: Date;
|
||||
setSelectedDay: (selectedDay: Date) => void;
|
||||
selectedDayNotes: string[];
|
||||
plugin: OZCalendarPlugin;
|
||||
}
|
||||
|
||||
export default function NoteListComponent(params: NoteListComponentParams) {
|
||||
const { selectedDayNotes, setSelectedDay, selectedDay, plugin } = params;
|
||||
|
||||
const setNewSelectedDay = (nrChange: number) => {
|
||||
let newDate = new Date();
|
||||
newDate.setDate(selectedDay.getDate() + nrChange);
|
||||
setSelectedDay(newDate);
|
||||
};
|
||||
|
||||
const extractFileName = (filePath: string) => {
|
||||
let lastIndexOfSlash = filePath.lastIndexOf('/');
|
||||
let endIndex = filePath.lastIndexOf('.');
|
||||
if (lastIndexOfSlash === -1) {
|
||||
return filePath.substring(0, endIndex);
|
||||
} else {
|
||||
return filePath.substring(lastIndexOfSlash + 1, endIndex);
|
||||
}
|
||||
};
|
||||
|
||||
const openFilePath = (filePath: string) => {
|
||||
let abstractFile = plugin.app.metadataCache.getFirstLinkpathDest(filePath, '');
|
||||
if (abstractFile) {
|
||||
openFile({
|
||||
file: abstractFile as TFile,
|
||||
plugin: plugin,
|
||||
newLeaf: true,
|
||||
leafBySplit: false,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="oz-calendar-notelist-header-container">
|
||||
<div className="oz-calendar-nav-action-left">
|
||||
<BsArrowLeft size={22} onClick={() => setNewSelectedDay(-1)} />
|
||||
</div>
|
||||
<div className="oz-calendar-nav-action-middle">{dayjs(selectedDay).format('DD MMM YYYY')}</div>
|
||||
<div className="oz-calendar-nav-action-right">
|
||||
<BsArrowRight size={22} onClick={() => setNewSelectedDay(1)} />
|
||||
</div>
|
||||
</div>
|
||||
<div className="oz-calendar-notelist-container">
|
||||
{selectedDayNotes.map((notePath) => {
|
||||
return (
|
||||
<div
|
||||
className="oz-calendar-note-line"
|
||||
id={notePath}
|
||||
onClick={() => openFilePath(notePath)}>
|
||||
<HiOutlineDocumentText className="oz-calendar-note-line-icon" />
|
||||
<span>{extractFileName(notePath)}</span>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
6
src/hooks/forceUpdate.tsx
Normal file
6
src/hooks/forceUpdate.tsx
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import React, { useState } from 'react';
|
||||
|
||||
export default function useForceUpdate() {
|
||||
const [value, setValue] = useState(0);
|
||||
return () => setValue((value) => value + 1);
|
||||
}
|
||||
147
src/main.ts
Normal file
147
src/main.ts
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
import { CachedMetadata, Plugin, TFile } from 'obsidian';
|
||||
import { OZCalendarView, VIEW_TYPE } from './view';
|
||||
import dayjs from 'dayjs';
|
||||
import { OZCalendarDaysMap } from './types';
|
||||
|
||||
export default class OZCalendarPlugin extends Plugin {
|
||||
FM_KEY: string = 'created';
|
||||
FM_FORMAT: string = 'YYYY-MM-DD hh:mm:ss';
|
||||
OZCALENDARDAYS_STATE: OZCalendarDaysMap = {};
|
||||
EVENT_TYPES = {
|
||||
forceUpdate: 'ozCalendarForceUpdate',
|
||||
};
|
||||
|
||||
async onload() {
|
||||
this.registerView(VIEW_TYPE, (leaf) => {
|
||||
return new OZCalendarView(leaf, this);
|
||||
});
|
||||
|
||||
this.app.workspace.onLayoutReady(() => {
|
||||
this.openOZCalendarLeaf({ showAfterAttach: true });
|
||||
this.OZCALENDARDAYS_STATE = this.getNotesWithDates();
|
||||
});
|
||||
|
||||
this.app.metadataCache.on('changed', this.handleCacheChange);
|
||||
this.app.vault.on('rename', (file, oldPath) => {
|
||||
if (file instanceof TFile && file.extension === 'md') {
|
||||
this.removeFilePathFromState(oldPath);
|
||||
this.scanTFileDate(file);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
scanTFileDate = (file: TFile) => {
|
||||
let cache = this.app.metadataCache.getCache(file.path);
|
||||
if (cache && cache.frontmatter) {
|
||||
let fm = cache.frontmatter;
|
||||
for (let k of Object.keys(cache.frontmatter)) {
|
||||
if (k === this.FM_KEY) {
|
||||
let fmValue = fm[k];
|
||||
let parsedDayISOString = dayjs(fmValue, this.FM_FORMAT).format('YYYY-MM-DD');
|
||||
this.addFilePathToState(parsedDayISOString, file.path);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
addFilePathToState = (date: string, filePath: string) => {
|
||||
let newStateMap = this.OZCALENDARDAYS_STATE;
|
||||
// if exists, add the new file path
|
||||
if (date in newStateMap) {
|
||||
newStateMap[date] = [...newStateMap[date], filePath];
|
||||
} else {
|
||||
newStateMap[date] = [filePath];
|
||||
}
|
||||
this.OZCALENDARDAYS_STATE = newStateMap;
|
||||
};
|
||||
|
||||
removeFilePathFromState = (filePath: string) => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
this.OZCALENDARDAYS_STATE = newStateMap;
|
||||
};
|
||||
|
||||
onunload() {}
|
||||
|
||||
handleCacheChange = (file: TFile, data: string, cache: CachedMetadata) => {
|
||||
this.removeFilePathFromState(file.path);
|
||||
if (cache && cache.frontmatter) {
|
||||
let fm = cache.frontmatter;
|
||||
for (let k of Object.keys(cache.frontmatter)) {
|
||||
if (k === this.FM_KEY) {
|
||||
let fmValue = fm[k];
|
||||
let parsedDayISOString = dayjs(fmValue, this.FM_FORMAT).format('YYYY-MM-DD');
|
||||
// If date doesn't exist, create a new one
|
||||
if (!(parsedDayISOString in this.OZCALENDARDAYS_STATE)) {
|
||||
this.addFilePathToState(parsedDayISOString, file.path);
|
||||
} 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.calendarForceUpdate();
|
||||
};
|
||||
|
||||
openOZCalendarLeaf = async (params: { showAfterAttach: boolean }) => {
|
||||
const { showAfterAttach } = params;
|
||||
let leafs = this.app.workspace.getLeavesOfType(VIEW_TYPE);
|
||||
if (leafs.length === 0) {
|
||||
let leaf = this.app.workspace.getLeftLeaf(false);
|
||||
await leaf.setViewState({ type: VIEW_TYPE });
|
||||
if (showAfterAttach) this.app.workspace.revealLeaf(leaf);
|
||||
} else {
|
||||
if (showAfterAttach && leafs.length > 0) {
|
||||
this.app.workspace.revealLeaf(leafs[0]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
calendarForceUpdate = () => {
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(this.EVENT_TYPES.forceUpdate, {
|
||||
detail: {},
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
getNotesWithDates = (): OZCalendarDaysMap => {
|
||||
let mdFiles = this.app.vault.getMarkdownFiles();
|
||||
let OZCalendarDays: OZCalendarDaysMap = {};
|
||||
for (let mdFile of mdFiles) {
|
||||
// Get the file Cache
|
||||
let fileCache = app.metadataCache.getFileCache(mdFile);
|
||||
// Check if there is Frontmatter
|
||||
if (fileCache && fileCache.frontmatter) {
|
||||
let fm = fileCache.frontmatter;
|
||||
// Check the FM keys vs the provided key by the user in settings @todo
|
||||
for (let k of Object.keys(fm)) {
|
||||
if (k === this.FM_KEY) {
|
||||
let fmValue = fm[k];
|
||||
// Parse the date with provided date format
|
||||
let parsedDayJsDate = dayjs(fmValue, this.FM_FORMAT);
|
||||
// Take only YYYY-MM-DD part fromt the date as String
|
||||
let parsedDayISOString = parsedDayJsDate.format('YYYY-MM-DD');
|
||||
// Check if it already exists
|
||||
if (parsedDayISOString in OZCalendarDays) {
|
||||
OZCalendarDays[parsedDayISOString] = [
|
||||
...OZCalendarDays[parsedDayISOString],
|
||||
mdFile.path,
|
||||
];
|
||||
} else {
|
||||
OZCalendarDays[parsedDayISOString] = [mdFile.path];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return OZCalendarDays;
|
||||
};
|
||||
}
|
||||
3
src/types.ts
Normal file
3
src/types.ts
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
export interface OZCalendarDaysMap {
|
||||
[key: string]: string[];
|
||||
}
|
||||
19
src/utils.ts
Normal file
19
src/utils.ts
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import { TFile } from 'obsidian';
|
||||
import OZCalendarPlugin from './main';
|
||||
|
||||
/**
|
||||
* Helper to open a file passed in params within Obsidian (Tab/Separate)
|
||||
* @param params
|
||||
*/
|
||||
export const openFile = (params: {
|
||||
file: TFile;
|
||||
plugin: OZCalendarPlugin;
|
||||
newLeaf: boolean;
|
||||
leafBySplit?: boolean;
|
||||
}) => {
|
||||
const { file, plugin, newLeaf, leafBySplit } = params;
|
||||
let leaf = plugin.app.workspace.getLeaf(newLeaf);
|
||||
if (!newLeaf && leafBySplit) leaf = plugin.app.workspace.createLeafBySplit(leaf, 'vertical');
|
||||
plugin.app.workspace.setActiveLeaf(leaf, { focus: true });
|
||||
leaf.openFile(file, { eState: { focus: true } });
|
||||
};
|
||||
48
src/view.tsx
Normal file
48
src/view.tsx
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import { ItemView, WorkspaceLeaf } from 'obsidian';
|
||||
import React from 'react';
|
||||
import ReactDOM from 'react-dom';
|
||||
import OZCalendarPlugin from './main';
|
||||
import MyCalendar from './components/calendar';
|
||||
|
||||
export const VIEW_TYPE = 'oz-calendar';
|
||||
export const VIEW_DISPLAY_TEXT = 'OZ Calendar';
|
||||
export const ICON = 'sheets-in-box';
|
||||
|
||||
export class OZCalendarView extends ItemView {
|
||||
plugin: OZCalendarPlugin;
|
||||
|
||||
constructor(leaf: WorkspaceLeaf, plugin: OZCalendarPlugin) {
|
||||
super(leaf);
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
getViewType(): string {
|
||||
return VIEW_TYPE;
|
||||
}
|
||||
|
||||
getDisplayText(): string {
|
||||
return VIEW_DISPLAY_TEXT;
|
||||
}
|
||||
|
||||
getIcon(): string {
|
||||
return ICON;
|
||||
}
|
||||
|
||||
async onClose() {
|
||||
this.destroy();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
ReactDOM.unmountComponentAtNode(this.contentEl);
|
||||
}
|
||||
|
||||
async onOpen() {
|
||||
this.destroy();
|
||||
ReactDOM.render(
|
||||
<div className="oz-calendar-plugin-view">
|
||||
<MyCalendar plugin={this.plugin} />
|
||||
</div>,
|
||||
this.contentEl
|
||||
);
|
||||
}
|
||||
}
|
||||
143
styles.css
143
styles.css
|
|
@ -1,8 +1,141 @@
|
|||
/*
|
||||
.oz-calendar-plugin-view .react-calendar button:enabled:hover {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
This CSS file will be included with your plugin, and
|
||||
available in the app when your plugin is enabled.
|
||||
.oz-calendar-plugin-view .react-calendar__navigation {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
If your plugin does not need CSS, delete this file.
|
||||
.oz-calendar-plugin-view .react-calendar__month-view__weekdays {
|
||||
text-align: center;
|
||||
text-transform: uppercase;
|
||||
font-weight: bold;
|
||||
font-size: 0.75em;
|
||||
}
|
||||
|
||||
*/
|
||||
.oz-calendar-plugin-view .react-calendar__month-view__weekdays__weekday {
|
||||
padding: 0.5em;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .react-calendar__month-view__weekdays__weekday abbr {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .react-calendar__month-view__days__day--weekend {
|
||||
color: #d10000;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view
|
||||
.react-calendar__month-view__days__day--neighboringMonth {
|
||||
color: var(--text-muted);
|
||||
opacity: 0.5;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .react-calendar__tile {
|
||||
max-width: 100%;
|
||||
padding: 10px 6.6667px;
|
||||
background: none;
|
||||
text-align: center;
|
||||
line-height: 16px;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view
|
||||
button.react-calendar__tile.react-calendar__month-view__days__day,
|
||||
.oz-calendar-plugin-view button.react-calendar__navigation__arrow,
|
||||
.oz-calendar-plugin-view button.react-calendar__navigation__label {
|
||||
background-color: transparent !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view button.react-calendar__navigation__label {
|
||||
color: var(--interactive-accent);
|
||||
font-size: 1.1em;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view
|
||||
.react-calendar__tile.react-calendar__month-view__days__day {
|
||||
height: 40px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .dots-wrapper {
|
||||
font-size: 8px;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
vertical-align: middle !important;
|
||||
min-height: 3.5px !important;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .dots-wrapper svg {
|
||||
margin-top: 3.8px;
|
||||
}
|
||||
|
||||
.oz-calendar-plugin-view .react-calendar__tile--active {
|
||||
color: var(--interactive-accent);
|
||||
font-weight: bold;
|
||||
font-size: 1em;
|
||||
}
|
||||
|
||||
#oz-calendar-divider {
|
||||
height: 3.8px;
|
||||
opacity: 0.3;
|
||||
margin: 10px 0px 10px 0px;
|
||||
border-bottom: 3px solid var(--text-muted);
|
||||
}
|
||||
|
||||
.oz-calendar-notelist-container {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.oz-calendar-nav-action-left {
|
||||
display: inline-block;
|
||||
width: 20%;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.oz-calendar-nav-action-middle {
|
||||
display: inline-block;
|
||||
width: 60%;
|
||||
text-align: center;
|
||||
vertical-align: top;
|
||||
font-size: 1.1em;
|
||||
color: var(--interactive-accent);
|
||||
}
|
||||
|
||||
.oz-calendar-nav-action-right {
|
||||
display: inline-block;
|
||||
width: 20%;
|
||||
align-items: end;
|
||||
text-align: right;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.oz-calendar-nav-action-left svg:hover,
|
||||
.oz-calendar-nav-action-right svg:hover {
|
||||
cursor: pointer;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.oz-calendar-notelist-container {
|
||||
display: block;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.oz-calendar-note-line {
|
||||
padding-top: 3px;
|
||||
padding-bottom: 3px;
|
||||
font-size: var(--nav-item-size);
|
||||
}
|
||||
|
||||
.oz-calendar-note-line:hover {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.oz-calendar-note-line-icon {
|
||||
padding-right: 3px;
|
||||
padding-bottom: 2px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,24 +1,19 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"baseUrl": ".",
|
||||
"inlineSourceMap": true,
|
||||
"inlineSources": true,
|
||||
"module": "ESNext",
|
||||
"target": "ES6",
|
||||
"allowJs": true,
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"importHelpers": true,
|
||||
"isolatedModules": true,
|
||||
"strictNullChecks": true,
|
||||
"lib": [
|
||||
"DOM",
|
||||
"ES5",
|
||||
"ES6",
|
||||
"ES7"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"**/*.ts"
|
||||
]
|
||||
"compilerOptions": {
|
||||
"jsx": "react",
|
||||
"esModuleInterop": true,
|
||||
"baseUrl": "src",
|
||||
"inlineSourceMap": true,
|
||||
"inlineSources": true,
|
||||
"isolatedModules": true,
|
||||
"module": "ESNext",
|
||||
"target": "es6",
|
||||
"allowJs": true,
|
||||
"noImplicitAny": true,
|
||||
"moduleResolution": "node",
|
||||
"importHelpers": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"lib": ["dom", "es5", "scripthost", "es2015"]
|
||||
},
|
||||
"include": ["**/*.ts", "**/*.tsx", "src/view.tsx"]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
{
|
||||
"1.0.0": "0.15.0"
|
||||
"0.0.0": "0.15.0"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue