Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/roam/src/data/blockPropsSettingsConfig.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ="roam/js/discourse-graph/block-prop-settings";
export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ="roam/js/discourse-graph/block-prop-settings";
export const TOP_LEVEL_BLOCK_PROP_KEYS = { featureFlags: "Feature Flags" };
47 changes: 47 additions & 0 deletions apps/roam/src/utils/initBlockPropSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
TOP_LEVEL_BLOCK_PROP_KEYS,
DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
} from "~/data/blockPropsSettingsConfig";
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
import getShallowTreeByParentUid from "roamjs-components/queries/getShallowTreeByParentUid";
import { createPage, createBlock } from "roamjs-components/writes";

const ensurePageExists = async (pageTitle: string): Promise<string> => {
let pageUid = getPageUidByPageTitle(pageTitle);

if (!pageUid) {
pageUid = window.roamAlphaAPI.util.generateUID();
await createPage({
title: pageTitle,
uid: pageUid,
});
}

return pageUid;
};

const ensureBlocksExist = async (pageUid: string): Promise<void> => {
const blockTexts = Object.values(TOP_LEVEL_BLOCK_PROP_KEYS);
const existingChildren = getShallowTreeByParentUid(pageUid);
const existingTexts = new Set(existingChildren.map((child) => child.text));

const missingBlocks = blockTexts.filter(
(blockText) => !existingTexts.has(blockText),
);

if (missingBlocks.length > 0) {
await Promise.all(
missingBlocks.map((blockText) =>
createBlock({
parentUid: pageUid,
node: { text: blockText },
}),
),
);
}
};

export const initSchema = async () => {
const pageUid = await ensurePageExists(DG_BLOCK_PROP_SETTINGS_PAGE_TITLE);
await ensureBlocksExist(pageUid);
};