Skip to content

Commit

Permalink
feat: support change start day of week
Browse files Browse the repository at this point in the history
  • Loading branch information
linyibing committed May 12, 2024
1 parent 26c13f6 commit 23dfd3b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/component/CreateNote/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ import { AutoComplete } from '../AutoComplete';

import weekOfYear from 'dayjs/plugin/isoWeek';
import quarterOfYear from 'dayjs/plugin/quarterOfYear';
import updateLocale from 'dayjs/plugin/updateLocale';
import { useDocumentEvent } from '../../hooks/useDocumentEvent';

dayjs.extend(weekOfYear);
dayjs.extend(quarterOfYear);
dayjs.extend(updateLocale);

export const CreateNote = (props: { width: number }) => {
const { app, settings, locale } = useApp() || {};
const { app, settings: initialSettings, locale } = useApp() || {};
const [settings, setSettings] = useState<PluginSettings | undefined>(
initialSettings
);
const { width } = props;
const [periodicActiveTab, setPeriodicActiveTab] = useState(DAILY);
const [paraActiveTab, setParaActiveTab] = useState(PROJECT);
Expand Down Expand Up @@ -73,6 +80,11 @@ export const CreateNote = (props: { width: number }) => {
)
.map((file) => (file as { basename?: string }).basename) || []
);

useDocumentEvent('settingUpdate', (event) => {
setSettings(event.detail);
});

app?.vault.on('create', (file) => {
if (file instanceof TFile) {
setExistsDates([file.basename, ...existsDates]);
Expand All @@ -91,6 +103,16 @@ export const CreateNote = (props: { width: number }) => {
}
});

// TODO: 监听设置变动
dayjs.updateLocale(localeKey, {
weekStart:
settings?.weekStart === -1
? locale?.locale === 'zh-cn'
? 1
: 0
: settings?.weekStart,
});

const cellRender: (value: dayjs.Dayjs, picker: string) => JSX.Element = (
value,
picker
Expand Down
28 changes: 26 additions & 2 deletions src/component/SettingTab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ export const SettingTab = (props: {
settings: PluginSettings;
saveSettings: (settings: PluginSettings) => void;
}) => {
const { app } = useApp() || {};
const { app, locale } = useApp() || {};
const { settings, saveSettings } = props;
const [formValues, setFormValues] = useState(settings);
const [form] = Form.useForm();

const folders =
app?.vault
.getAllLoadedFiles()
Expand Down Expand Up @@ -169,6 +168,31 @@ export const SettingTab = (props: {
</Form.Item>
</>
)}
<Form.Item
help="The start day of the week"
name="weekStart"
label="Week Start:"
>
<Select
options={[
{
value: -1,
label:
'Auto' +
(locale?.locale === 'zh-cn'
? '(Monday)'
: '(Sunday)'),
},
{ value: 1, label: 'Monday' },
{ value: 2, label: 'Tuesday' },
{ value: 3, label: 'Wednesday' },
{ value: 4, label: 'Thursday' },
{ value: 5, label: 'Friday' },
{ value: 6, label: 'Saturday' },
{ value: 0, label: 'Sunday' },
]}
/>
</Form.Item>
</>
)}
</>
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useDocumentEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react';

export const useDocumentEvent = (eventName: string, handler: (event: CustomEvent) => void) => {
React.useEffect(() => {
document.addEventListener(eventName, handler);

return () => {
document.removeEventListener(eventName, handler);
};
}, [eventName, handler]);
};
1 change: 1 addition & 0 deletions src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface PluginSettings {
usePARANotes: boolean;
usePARAAdvanced: boolean;
paraIndexFilename: 'readme' | 'foldername';
weekStart: number;
}

export type DateType = {
Expand Down
2 changes: 1 addition & 1 deletion src/view/CreateNote.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ItemView, WorkspaceLeaf, debounce } from 'obsidian';
import React from 'react';
import { ItemView, WorkspaceLeaf, debounce } from 'obsidian';
import { type Root, createRoot } from 'react-dom/client';
import type { Locale } from 'antd/es/locale';
import { CreateNote } from '../component/CreateNote';
Expand Down
5 changes: 5 additions & 0 deletions src/view/SettingTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const DEFAULT_SETTINGS: PluginSettings = {
usePARANotes: true,
usePARAAdvanced: false,
paraIndexFilename: 'readme',
weekStart: -1,
};

export class SettingTabView extends PluginSettingTab {
Expand Down Expand Up @@ -54,6 +55,10 @@ export class SettingTabView extends PluginSettingTab {
const saveSettings = (newSettings: PluginSettings) => {
this.settings = { ...this.settings, ...newSettings };
this.plugin.saveSettings(this.settings);
const event = new CustomEvent('settingUpdate', {
detail: this.settings,
});
document.dispatchEvent(event);
};

this.root.render(
Expand Down

0 comments on commit 23dfd3b

Please sign in to comment.