Skip to content

Commit

Permalink
feat: add whereToPlaceNewTask setting item, #24
Browse files Browse the repository at this point in the history
  • Loading branch information
ahonn committed Oct 19, 2022
1 parent eab6c37 commit a42ed45
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 45 deletions.
34 changes: 9 additions & 25 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { themeModeState, themeStyleState } from './state/theme';
import getTodayTaskQuery from './querys/today';
import getScheduledTaskQuery from './querys/scheduled';
import getAnytimeTaskQuery from './querys/anytime';
import './style.css';
import { settingsState } from './state/settings';
import * as api from './api';
import Mousetrap from 'mousetrap';
import 'mousetrap-global-bind';
import './style.css';

dayjs.extend(advancedFormat);

Expand All @@ -40,7 +41,7 @@ function App() {
const userConfigs = useRecoilValue(userConfigsState);
const themeStyle = useRecoilValue(themeStyleState);
const themeMode = useRecoilValue(themeModeState);
const settings = useRecoilValue(settingsState);
const { hotkey, whereToPlaceNewTask } = useRecoilValue(settingsState);

const refreshAll = useRecoilCallback(
({ snapshot, refresh }) =>
Expand All @@ -53,23 +54,17 @@ function App() {
);

useEffect(() => {
if (!settings.hotkey) {
if (!hotkey) {
return;
}

// @ts-ignore
Mousetrap.bindGlobal(
settings.hotkey,
() => {
window.logseq.hideMainUI();
},
'keydown',
);
Mousetrap.bindGlobal(hotkey, () => window.logseq.hideMainUI(), 'keydown');
return () => {
// @ts-ignore
Mousetrap.unbindGlobal(settings.hotkey, 'keydown');
Mousetrap.unbindGlobal(hotkey, 'keydown');
};
}, [settings.hotkey]);
}, [hotkey]);

useEffect(() => {
if (visible) {
Expand Down Expand Up @@ -102,26 +97,15 @@ function App() {
}, [themeMode]);

const handleClickOutside = (e: React.MouseEvent) => {
if (!innerRef.current?.contains(e.target as any)) {
if (!innerRef.current?.contains(e.target as unknown as Node)) {
window.logseq.hideMainUI();
}
};

const createNewTask = async (content: string) => {
const { preferredDateFormat, preferredTodo } = userConfigs!;
const date = dayjs().format(preferredDateFormat);
let page = await window.logseq.Editor.getPage(date);
if (page === null) {
page = await window.logseq.Editor.createPage(date, {
journal: true,
redirect: false,
});
}
await window.logseq.Editor.insertBlock(
page!.name,
`${preferredTodo} ${content}`,
{ isPageBlock: true, before: false },
);
await api.createNewTask(date, content, { preferredTodo, whereToPlaceNewTask });
refreshAll();
};

Expand Down
78 changes: 65 additions & 13 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { BlockEntity } from '@logseq/libs/dist/LSPlugin.user';
import dayjs from 'dayjs';
import { TaskEntityObject, TaskMarker } from './models/TaskEntity';

export const MARKER_GROUPS: Record<string, TaskMarker[]> = {
[TaskMarker.TODO]: [TaskMarker.TODO, TaskMarker.DOING, TaskMarker.DONE],
[TaskMarker.LATER]: [TaskMarker.LATER, TaskMarker.NOW, TaskMarker.DONE],
};

export interface ITaskOptions {
preferredTodo: string;
whereToPlaceNewTask?: string;
}

export function isTodayTask(task: TaskEntityObject) {
Expand All @@ -12,13 +18,48 @@ export function isTodayTask(task: TaskEntityObject) {
return dayjs(new Date()).format('YYYYMMDD') === scheduled.toString();
}

export interface IToggleOptions {
preferredTodo: string;
export async function createNewTask(
date: string,
content: string,
opts: ITaskOptions,
) {
const { preferredTodo, whereToPlaceNewTask } = opts;
let page = await window.logseq.Editor.getPage(date);
if (page === null) {
page = await window.logseq.Editor.createPage(date, {
journal: true,
redirect: false,
});
}
const blocksTree = await window.logseq.Editor.getPageBlocksTree(date);

if (whereToPlaceNewTask) {
let parentBlock = blocksTree.find((block: BlockEntity) => block.content === whereToPlaceNewTask);
if (parentBlock === undefined) {
parentBlock = (await window.logseq.Editor.appendBlockInPage(
page!.name,
whereToPlaceNewTask,
)) as BlockEntity;
}
await window.logseq.Editor.insertBlock(
parentBlock!.uuid,
`${preferredTodo} ${content}`,
);
} else {
await window.logseq.Editor.appendBlockInPage(
page!.name,
`${preferredTodo} ${content}`,
);
}

if (blocksTree.length === 1 && blocksTree[0].content === '') {
await window.logseq.Editor.removeBlock(blocksTree[0].uuid);
}
}

export async function toggleTaskStatus(
task: TaskEntityObject,
options: IToggleOptions,
options: ITaskOptions,
) {
const { uuid, completed, marker } = task;
const nextMarker = completed ? options.preferredTodo : TaskMarker.DONE;
Expand All @@ -40,30 +81,42 @@ export function openTask(task: TaskEntityObject, opts?: IOpenTaskOptions) {
return window.logseq.Editor.scrollToBlockInPage(task.page.name, uuid);
}

export function openTaskPage(page: TaskEntityObject['page'], opts?: IOpenTaskOptions) {
export function openTaskPage(
page: TaskEntityObject['page'],
opts?: IOpenTaskOptions,
) {
if (opts?.openInRightSidebar) {
return window.logseq.Editor.openInRightSidebar(page.uuid);
}
return window.logseq.Editor.scrollToBlockInPage(page.name, page.uuid);
}

export async function toggleTaskMarker(task: TaskEntityObject, options: IToggleOptions) {
export async function toggleTaskMarker(
task: TaskEntityObject,
options: ITaskOptions,
) {
const { uuid, rawContent, marker } = task;

let newMarker = marker;
if (marker === TaskMarker.WAITING) {
newMarker = options.preferredTodo === TaskMarker.LATER ? TaskMarker.LATER : TaskMarker.TODO;
newMarker =
options.preferredTodo === TaskMarker.LATER
? TaskMarker.LATER
: TaskMarker.TODO;
} else {
const markerGroup = MARKER_GROUPS[options.preferredTodo];
const currentMarkIndex = markerGroup.findIndex(m => m === marker);
const currentMarkIndex = markerGroup.findIndex((m) => m === marker);
newMarker = markerGroup[(currentMarkIndex + 1) % markerGroup.length];
}

const newRawContent = rawContent.replace(new RegExp(`^${marker}`), newMarker);
await window.logseq.Editor.updateBlock(uuid, newRawContent);
}

export async function setTaskScheduled(task: TaskEntityObject, date: Date | null) {
export async function setTaskScheduled(
task: TaskEntityObject,
date: Date | null,
) {
const { uuid, rawContent } = task;
let newRawContent = task.rawContent;
if (date === null) {
Expand All @@ -72,12 +125,11 @@ export async function setTaskScheduled(task: TaskEntityObject, date: Date | null
return;
}

const scheduledString = `SCHEDULED: <${dayjs(date).format('YYYY-MM-DD ddd')}>`;
const scheduledString = `SCHEDULED: <${dayjs(date).format(
'YYYY-MM-DD ddd',
)}>`;
if (rawContent.includes('SCHEDULED')) {
newRawContent = rawContent.replace(
/SCHEDULED: <[^>]+>/,
scheduledString,
);
newRawContent = rawContent.replace(/SCHEDULED: <[^>]+>/, scheduledString);
} else {
const lines = rawContent.split('\n');
lines.splice(1, 0, scheduledString);
Expand Down
21 changes: 14 additions & 7 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ const settings: SettingSchemaDesc[] = [
description: 'quick open task panel',
default: 'mod+shift+t',
},
{
key: 'whereToPlaceNewTask',
type: 'string',
title: 'where to place new task',
description: 'Where to place new task on journal page',
default: '',
},
{
key: 'openInRightSidebar',
type: 'boolean',
title: 'Open task in right sidebar',
description: '👉 open task in the right sidebar',
default: false,
},
{
key: 'sectionTitleColor',
type: 'string',
Expand Down Expand Up @@ -48,13 +62,6 @@ const settings: SettingSchemaDesc[] = [
default: '#002B37',
inputAs: 'color'
},
{
key: 'openInRightSidebar',
type: 'boolean',
title: 'Open task in right sidebar',
description: '👉 open task in the right sidebar',
default: false,
},
];

export default settings;
2 changes: 2 additions & 0 deletions src/state/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface IPluginSettings {
darkSecondaryBackgroundColor: string;
sectionTitleColor: string;
openInRightSidebar: boolean;
whereToPlaceNewTask: string;
}

const DEFAULT_SETTINGS = {
Expand All @@ -18,6 +19,7 @@ const DEFAULT_SETTINGS = {
darkPrimaryBackgroundColor: '#002B37',
darkSecondaryBackgroundColor: '#106ba3',
openInRightSidebar: false,
whereToPlaceNewTask: '',
};

const settingsChangedEffect: AtomEffect<IPluginSettings> = ({ setSelf }) => {
Expand Down

0 comments on commit a42ed45

Please sign in to comment.