Skip to content

Commit

Permalink
feat: 1.任务切换完成状态 2.任务跳转
Browse files Browse the repository at this point in the history
  • Loading branch information
JuckZ committed Apr 25, 2023
1 parent 91ecf46 commit 68a403f
Show file tree
Hide file tree
Showing 4 changed files with 413 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,16 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
}

maybeRefresh = () => {
// TODO
// If the index revision has changed recently, then queue a reload.
// But only if we're mounted in the DOM and auto-refreshing is active.
// if (this.lastReload != this.index.revision && this.container.isShown() && this.settings.refreshEnabled) {
// this.lastReload = this.index.revision;
// this.render();
// }
};

private watchVault() {
// https://github.com/kepano/obsidian-system-dark-mode/blob/master/main.ts
// Watch for system changes to color theme
Expand Down Expand Up @@ -1267,6 +1277,7 @@ export default class AwesomeBrainManagerPlugin extends Plugin {
});
window.addEventListener(eventTypes.openBrowser, this.openBrowserHandle.bind(this));
[
this.app.workspace.on('dataview:refresh-views', this.maybeRefresh),
this.app.workspace.on('codemirror', this.codemirrorFunction),
// this.app.workspace.on('click', this.clickFunction),
this.app.workspace.on('resize', this.resizeFunction),
Expand Down
2 changes: 2 additions & 0 deletions src/types/obsidian.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ declare module 'obsidian' {
activeTime: number;
}
interface Workspace {
/** Sent to rendered dataview components to tell them to possibly refresh */
on(name: 'dataview:refresh-views', callback: () => void, ctx?: any): EventRef;
recordHistory(leaf: WorkspaceLeaf, pushHistory: boolean): void;
iterateLeaves(
callback: (item: WorkspaceLeaf) => boolean | void,
Expand Down
125 changes: 107 additions & 18 deletions src/ui/DataViewTimeLine.vue
Original file line number Diff line number Diff line change
@@ -1,50 +1,139 @@
<template>
<div>
<button @click="searchHandle">查询</button>
<div id="resultContainer"></div>
<!-- <Teleport to="body" disable="false">
<div v-html="mdText"></div>
</Teleport> -->
<div v-for="task in taskList" :key="task.blockId" @mouseenter="previewTask(task)" @click="completeTask(task)">
{{ task.text }}
<div class="taskListContainer">
<div
v-for="task in taskList"
:key="task.blockId"
@mouseenter="previewTask($event, task)"
@click="onClicked($event, task)"
>
<input type="checkbox" :checked="task.status !== ' '" @click="onChecked($event, task)" />
{{ task.text }}
</div>
</div>
</div>
</template>

<script setup lang="ts">
import { Component, MarkdownPreviewView, MarkdownView } from 'obsidian';
import { STask, getAPI as getDataviewApi } from 'obsidian-dataview';
import { Component, MarkdownPreviewView, MarkdownView, Platform } from 'obsidian';
import { type SListItem, STask, getAPI as getDataviewApi } from 'obsidian-dataview';
import { type Ref, ref } from 'vue';
import { listTasks, queryAll, queryImportantTasks } from '@/utils/dataview';
import {
concentrateTasks,
importantTasks,
listTasks,
queryAll,
queryImportantTasks,
rewriteTask,
setTaskCompletion,
} from '@/utils/dataview';
const keyword = ref('');
let mdText = ref('');
const taskList: Ref<STask[]> = ref([]);
const DataviewAPI = getDataviewApi();
const searchHandle = async () => {
const DataviewAPI = getDataviewApi();
// query
mdText.value = (await DataviewAPI.queryMarkdown(listTasks)).value;
// mdText.value = (await DataviewAPI.queryMarkdown(listTasks)).value;
// taskList.value
const res = await DataviewAPI.query(listTasks);
const res = await DataviewAPI.query(concentrateTasks);
taskList.value = res.value.values;
console.log(taskList.value);
const component = new Component();
const container = document.querySelector('#resultContainer');
// console.log(taskList.value);
// const component = new Component();
// const container = document.querySelector('#resultContainer');
// executeJs
// const res = await DataviewAPI.execute(queryImportantTasks, container, component, '');
// console.log(res);
};
const previewTask = async (task: STask) => {
// xxx
console.log('previewTask', task);
searchHandle();
// DataviewAPI.index.revision
const refreshOperation = async () => {
searchHandle();
};
// DataviewAPI.index.onChange(refreshOperation);
app.workspace.on('dataview:refresh-views', refreshOperation);
const previewTask = async (event, item: STask) => {
// 延迟触发,鼠标移出后取消触发
// console.log('previewTask', item);
};
const completeTask = async (task: STask) => {
console.log('completeTask', task);
const onClicked = async (evt, item: STask) => {
const selectionState = {
eState: {
cursor: {
from: { line: item.line, ch: item.position.start.col },
to: { line: item.line + item.lineCount - 1, ch: item.position.end.col },
},
line: item.line,
},
};
app.workspace.openLinkText(
item.link.toFile().obsidianLink(),
item.path,
evt.ctrlKey || (evt.metaKey && Platform.isMacOS),
selectionState as any,
);
};
const onChecked = async (evt, item: STask) => {
evt.stopPropagation();
const completed = evt.currentTarget.checked;
const status = completed ? 'x' : ' ';
// Update data-task on the parent element (css style)
const parent = evt.currentTarget.parentElement;
parent?.setAttribute('data-task', status);
let flatted: STask[] = [item];
const recursiveSubTaskCompletion = true;
const taskCompletionTracking = true;
const taskCompletionUseEmojiShorthand = true;
const taskCompletionText = 'completion';
const taskCompletionDateFormat = 'yyyy-MM-dd';
function flatter(iitem: STask | SListItem) {
flatted.push(iitem as STask);
iitem.children.forEach(flatter);
}
if (recursiveSubTaskCompletion) {
item.children.forEach(flatter);
flatted = flatted.flat(Infinity);
}
async function effectFn() {
for (let i = 0; i < flatted.length; i++) {
const _item = flatted[i];
let updatedText: string = _item.text;
if (taskCompletionTracking) {
updatedText = setTaskCompletion(
_item.text,
taskCompletionUseEmojiShorthand,
taskCompletionText,
taskCompletionDateFormat,
completed,
);
}
await rewriteTask(app.vault, _item, status, updatedText);
}
app.workspace.trigger('dataview:refresh-views');
}
effectFn();
};
</script>

<style lang="scss" scoped></style>
<style lang="scss" scoped>
.taskListContainer {
margin: 8px 0;
}
</style>
Loading

0 comments on commit 68a403f

Please sign in to comment.