Skip to content

Commit

Permalink
feat: track page todos implicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
pengx17 committed Mar 28, 2022
1 parent cbe5e3e commit 2e29da5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"semantic-release": "19.0.2",
"typescript": "4.6.2",
"vite": "2.8.6",
"vite-plugin-logseq": "^1.0.0",
"vite-plugin-logseq": "^1.1.0",
"vite-plugin-windicss": "1.8.3"
},
"logseq": {
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

A simple plugin to render a progress bar to gather the overall progress of the current block or page.


https://user-images.githubusercontent.com/584378/143900107-75c9b32e-d808-4454-9268-2c675682ac75.mp4


## Usage

### Commands
- Use slash command `[TODO Master] Add Progress Bar for children blocks` to add a progress bar to the parent (aka the master) block.
- Use slash command `[TODO Master] Add Progress Bar for current page` to add a progress bar to the current page. Can be placed into page properties.

### Implicit Macros based on the rendering position
The plugin actually has an implicit mode for `{{renderer :todomaster}}` based on the rendering position:
- if it is at the root of the page (without a parent block) and have no children, then it will get the todos for the whole page
- otherwise, it will render the todos for the children blocks

This is useful if you want to add the plugin to your journals page template to track your daily completion.
10 changes: 7 additions & 3 deletions src/progress-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ function renderBar(num: number, marker: string) {

export function ProgressBar({
status,
mode,
}: {
status: {
status?: {
later: number;
now: number;
done: number;
} | null;
};
mode?: "page" | "block";
}) {
if (!status) {
return (
Expand All @@ -45,7 +47,9 @@ export function ProgressBar({
<div className="todo-master-progress-bar__percentage-label">
{percentage}%
</div>
<div className="todo-master-progress-bar__fraction-label">{`${done}/${total}`}</div>
<div className="todo-master-progress-bar__fraction-label">
{`${mode}:${done}/${total}`}
</div>
</div>
</div>
);
Expand Down
35 changes: 29 additions & 6 deletions src/register-command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,40 @@ const reduceToMap = (vals?: Marker[]) => {
};

async function getTODOStats(maybeUUID: string) {
const markers = await getBlockMarkers(maybeUUID);
return markers ? reduceToMap(markers) : null;
const result = await getBlockMarkers(maybeUUID);
return result
? { mapping: reduceToMap(result.markers), mode: result.mode }
: null;
}

function checkIsUUid(maybeUUID: string) {
return maybeUUID.length === 36 && maybeUUID.includes("-");
}

async function getBlockMarkers(maybeUUID: string): Promise<Marker[] | null> {
async function getBlockMarkers(
maybeUUID: string
): Promise<{ markers: Marker[]; mode: "page" | "block" } | null> {
let tree: any;
let pageMode = false;
if (checkIsUUid(maybeUUID)) {
tree = await logseq.Editor.getBlock(maybeUUID, { includeChildren: true });
} else {

// If this is the root node and have no children
if (
tree &&
tree.children &&
tree.children.length === 0 &&
tree.parent?.id &&
tree.parent?.id === tree.page?.id
) {
maybeUUID = tree.page?.originalName;
tree = null;
}
}

if (!tree) {
tree = { children: await logseq.Editor.getPageBlocksTree(maybeUUID) };
pageMode = true;
}

if (!tree || !tree.children) {
Expand All @@ -74,7 +94,10 @@ async function getBlockMarkers(maybeUUID: string): Promise<Marker[] | null> {
}
}
traverse(tree);
return res;
return {
markers: res,
mode: pageMode ? "page" : "block",
};
}
const pluginId = "todomaster";
const slotElId = (slot: string) => `${pluginId}-${slot}-${logseq.baseInfo.id}`;
Expand All @@ -96,7 +119,7 @@ async function render(maybeUUID: string, slot: string, counter: number) {
return;
}
const template = ReactDOMServer.renderToStaticMarkup(
<ProgressBar status={status} />
<ProgressBar status={status?.mapping} mode={status?.mode} />
);

// See https://github.com/logseq/logseq-plugin-samples/blob/master/logseq-pomodoro-timer/index.ts#L92
Expand Down
2 changes: 1 addition & 1 deletion src/style.tcss
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* make the extension with tcss to make sure vite does not transpile it by default */
.todo-master-progress-bar {
max-width: 100%;
height: 0.6rem;
height: 0.8rem;
cursor: default;
font-family: monospace;
display: inline-flex !important;
Expand Down
5 changes: 4 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ import reactRefresh from "@vitejs/plugin-react";
import { defineConfig } from "vite";
import logseqPlugin from "vite-plugin-logseq";

const reactRefreshPlugin = reactRefresh();
const reactRefreshPlugin = reactRefresh({
fastRefresh: false,
});

// https://vitejs.dev/config/
export default defineConfig({
plugins: [reactRefreshPlugin, logseqPlugin()],
clearScreen: false,
build: {
target: "esnext",
minify: "esbuild",
Expand Down

0 comments on commit 2e29da5

Please sign in to comment.