Skip to content

Commit

Permalink
feat(root page): Menu item that creates a PARA root page
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeguimaraes committed Aug 29, 2023
1 parent 5a5d829 commit b570e59
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 3 deletions.
29 changes: 27 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "@logseq/libs";
import { PageEntity } from "@logseq/libs/dist/LSPlugin.user";
import para_template from "./para_template";

async function add_property_to_page(name: string, value: string) {
const page = (await logseq.Editor.getCurrentPage()) as PageEntity;
Expand Down Expand Up @@ -64,6 +65,29 @@ function openAddParaPropertiesMenuBar(e) {
});
}

function createBlocksFromTemplate() {
return para_template;
}

async function createParaRootPage() {
const title = "PARA";
const obj = (await logseq.Editor.getPage(title)) as PageEntity | null;
if (obj === null) {
const page = await logseq.Editor.createPage(
title,
{},
{
createFirstBlock: false,
redirect: true,
}
);
await logseq.Editor.insertBatchBlock(page.uuid, createBlocksFromTemplate());
} else {
logseq.Editor.openInRightSidebar(title);
logseq.UI.showMsg("A page called 'PARA' already exists.", "warning");
}
}

logseq
.ready(() => {
logseq.Editor.registerSlashCommand(
Expand Down Expand Up @@ -106,6 +130,7 @@ logseq
logseq.provideModel({
createParaRootPage: () => {
console.log("createParaRootPage");
createParaRootPage();
},
openAddParaPropertiesMenuBar: (e) => {
console.log("openAddParaPropertiesMenuBar");
Expand All @@ -119,8 +144,8 @@ logseq
<button
class="button" id="add-para-properties"
data-on-click="openAddParaPropertiesMenuBar" data-rect>
<span id="add-para-properties-icon">
P
<span id="add-para-properties-icon" style="font-size: 10px">
PARA
</span>
</button>
`,
Expand Down
139 changes: 139 additions & 0 deletions src/para_template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const template = [
{
content: `This is a page to gather all your Projects, Areas, Resources and Archive together.`,
children: [
{
content: `To list all your Projects, we use any page that has its \`page-type\` set to \`project\`, or any page that is being linked to using \`project::\`. The same goes for Areas and Resources.`,
},
{
content: `Archive is any page that has \`archived\` set to \`true\`.`,
children: [{ content: "This functionality is not implemented yet" }],
},
],
},
{
content: `## Projects`,
children: [
{
content: `
#+BEGIN_QUERY
{
:query [:find (pull ?page [*])
:where
(or-join [?page]
(and [_ :block/properties ?prop]
[(get ?prop :project) ?page1]
[(str ?page1) ?page11]
[_ :block/refs ?page]
[?page :block/original-name ?page2]
[(str "#{\\"" ?page2 "\\"}") ?page22]
[(= ?page22 ?page11)]
)
(and
[?page :block/name]
[?page :block/properties ?prop3]
[?page :block/original-name ?page3-name]
[(get ?prop3 :page-type) ?v]
(or [(= ?v "project")] [(contains? ?v "project")])
)
)
]
:view (fn [result]
[:div.flex.flex-col
(for [page result]
[:a {:href (str "#/page/" (get page :block/original-name))} (get page :block/original-name) ]
)
[:hr]
]
)
}
#+END_QUERY`,
},
],
},
{
content: `## Areas`,
children: [
{
content: `#+BEGIN_QUERY
{
:query [:find (pull ?page [*])
:where
(or-join [?page]
(and [_ :block/properties ?prop]
[(get ?prop :area) ?page1]
[(str ?page1) ?page11]
[_ :block/refs ?page]
[?page :block/original-name ?page2]
[(str "#{\\"" ?page2 "\\"}") ?page22]
[(= ?page22 ?page11)]
)
(and
[?page :block/name]
[?page :block/properties ?prop3]
[?page :block/original-name ?page3-name]
[(get ?prop3 :page-type) ?v]
(or [(= ?v "area")] [(contains? ?v "area")])
)
)
]
:view (fn [result]
[:div.flex.flex-col
(for [page result]
[:a {:href (str "#/page/" (get page :block/original-name))} (get page :block/original-name) ]
)
[:hr]
]
)
}
#+END_QUERY`,
},
],
},
{
content: `## Resources`,
children: [
{
content: `#+BEGIN_QUERY
{
:query [:find (pull ?page [*])
:where
(or-join [?page]
(and [_ :block/properties ?prop]
[(get ?prop :resource) ?page1]
[(str ?page1) ?page11]
[_ :block/refs ?page]
[?page :block/original-name ?page2]
[(str "#{\\"" ?page2 "\\"}") ?page22]
[(= ?page22 ?page11)]
)
(and
[?page :block/name]
[?page :block/properties ?prop3]
[?page :block/original-name ?page3-name]
[(get ?prop3 :page-type) ?v]
(or [(= ?v "resource")] [(contains? ?v "resource")])
)
)
]
:view (fn [result]
[:div.flex.flex-col
(for [page result]
[:a {:href (str "#/page/" (get page :block/original-name))} (get page :block/original-name) ]
)
[:hr]
]
)
}
#+END_QUERY`,
},
],
},
];
export default template;
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"module": "ESNext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
"types": ["vite/client"],

/* Bundler mode */
"moduleResolution": "node",
Expand All @@ -14,7 +15,7 @@
"noEmit": true,

/* Linting */
"strict": true,
"strict": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
Expand Down

0 comments on commit b570e59

Please sign in to comment.