diff --git a/src/core/loadingCards.js b/src/core/loadingCards.js index 4fd8346..683b665 100644 --- a/src/core/loadingCards.js +++ b/src/core/loadingCards.js @@ -82,24 +82,24 @@ const srPageTagsToClause = (tags) => "(or " + tags.map((tag) => `[?srPage :node/ //cards with the flag-tag or the "query"-tag are not permissible const createQueryForAllPermissibleCards = (settings) => `[ :find (pull ?card [ - :block/string - :block/uid - {:block/refs [:node/title]} - {:block/_refs - [:block/uid :block/string - {:block/_children - [:block/uid {:block/refs [:node/title]}]} - {:block/refs [:node/title]} + :block/string + :block/uid + {:block/refs [:node/title]} + {:block/_refs + [:block/uid :block/string + {:block/_children + [:block/uid {:block/refs [:node/title]}]} + {:block/refs [:node/title]} {:block/page [:block/uid]}]} {:block/_children ...} ]) - :where + :where ${srPageTagsToClause(settings.mainTags)} - [?card :block/refs ?srPage] - (not-join [?card] + [?card :block/refs ?srPage] + (not-join [?card] [?flagPage :node/title "${settings.flagTag}"] [?card :block/refs ?flagPage]) - (not-join [?card] + (not-join [?card] [?queryPage :node/title "query"] [?card :block/refs ?queryPage]) ]`; @@ -129,25 +129,25 @@ const queryDueCards = async (settings, dateBasis, asyncQueryFunction) => { }; const getTodayQuery = (settings, todayUid) => `[ - :find (pull ?card - [:block/uid - {:block/refs [:node/title]} - {:block/_refs + :find (pull ?card + [:block/uid + {:block/refs [:node/title]} + {:block/_refs [ {:block/page [:block/uid]} - {:block/_children + {:block/_children [:block/uid {:block/refs [:node/title]}]} - ]}]) + ]}]) (pull ?review [:block/refs]) - :where + :where ${srPageTagsToClause(settings.mainTags)} - [?card :block/refs ?srPage] - [?review :block/refs ?card] - [?reviewPage :node/title "roam/sr/review"] - [?reviewParent :block/refs ?reviewPage] - [?reviewParent :block/children ?review] - [?todayPage :block/uid "${todayUid}"] - [?reviewParent :block/page ?todayPage] + [?card :block/refs ?srPage] + [?review :block/refs ?card] + [?reviewPage :node/title "roam/sr/review"] + [?reviewParent :block/refs ?reviewPage] + [?reviewParent :block/children ?review] + [?todayPage :block/uid "${todayUid}"] + [?reviewParent :block/page ?todayPage] ]`; const queryTodayReviewedCards = async (settings, asyncQueryFunction) => { @@ -289,3 +289,13 @@ export const loadCards = async (hasLimits, settings, asyncQueryFunction, dateBas return { extraCards: extraCardsResult, cards }; }; + +export const getReviewBlocks = () => { + return window.roamAlphaAPI.q(`[:find ?puid ?u :where + [?r :block/uid ?u] + [?r :block/refs ?b] + [?b :node/title "roam/sr/review"] + [?parent :block/children ?r] + [?parent :block/uid ?puid] + ]`); +} diff --git a/src/core/mainFunctions.js b/src/core/mainFunctions.js index 772caef..bd188e2 100644 --- a/src/core/mainFunctions.js +++ b/src/core/mainFunctions.js @@ -24,39 +24,34 @@ export const scheduleCardIn = async (card, interval) => { var nextRoamDate = getRoamDate(nextDate); - // Create daily note if it doesn't exist yet - await window.roamAlphaAPI.createPage({ - page: { - title: nextRoamDate.title, - }, - }); - - await sleep(); - - // Query for the [[roam/sr/review]] block - var queryReviewBlock = window.roamAlphaAPI.q( - '[:find (pull ?reviewBlock [:block/uid]) :in $ ?dailyNoteUID :where [?reviewBlock :block/refs ?reviewPage] [?reviewPage :node/title "roam/sr/review"] [?dailyNote :block/children ?reviewBlock] [?dailyNote :block/uid ?dailyNoteUID]]', - nextRoamDate.uid - ); - - // Check if it's there; if not, create it - var topLevelUid; - if (queryReviewBlock.length == 0) { - topLevelUid = createUid(); - await window.roamAlphaAPI.createBlock({ - location: { - "parent-uid": nextRoamDate.uid, - order: 0, - }, - block: { - string: "[[roam/sr/review]]", - uid: topLevelUid, - }, - }); - await sleep(); - } else { - topLevelUid = queryReviewBlock[0][0].uid; - } + var cachedNextRoamDate = roamsr.state.reviewBlocks.find(e => e[0] === nextRoamDate.uid); + var topLevelUid; + + if (cachedNextRoamDate) { + topLevelUid = cachedNextRoamDate[1]; + } else { + await window.roamAlphaAPI.createPage({ + page: { + title: nextRoamDate.title, + }, + }); + await sleep(); + + topLevelUid = createUid(); + await window.roamAlphaAPI.createBlock({ + location: { + "parent-uid": nextRoamDate.uid, + order: 0, + }, + block: { + string: "[[roam/sr/review]]", + uid: topLevelUid, + }, + }); + await sleep(); + + roamsr.state.reviewBlocks.push([nextRoamDate.uid, topLevelUid]); + } // Generate the block var block = { diff --git a/src/core/sessions.js b/src/core/sessions.js index 005e655..1df95fd 100644 --- a/src/core/sessions.js +++ b/src/core/sessions.js @@ -1,6 +1,6 @@ import { removeSelector, goToUid, sleep } from "./helperFunctions"; import { addKeyListener, removeKeyListener } from "./keybindings"; -import { loadCards } from "./loadingCards"; +import { getReviewBlocks, loadCards } from "./loadingCards"; import { goToCurrentCard } from "./mainFunctions"; import { setCards, setCurrentCardIndex, setLimitActivation, standbyState } from "./state"; import { setCustomStyle, removeCustomStyle, removeRoamsrMainviewCSS } from "../ui/styles"; @@ -33,6 +33,7 @@ export const loadState = async (i) => { setLimitActivation(true); setCurrentCardIndex(i); const { cards, extraCards } = await loadCards(roamsr.state.limits, roamsr.settings, window.roamAlphaAPI.q); + roamsr.state.reviewBlocks = getReviewBlocks(); setCards(cards, extraCards); updateCounters(roamsr.state); return;