Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions apps/roam/src/data/blockPropsSettingsConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ="roam/js/discourse-graph/block-prop-settings";
102 changes: 102 additions & 0 deletions apps/roam/src/utils/settingsUsingBlockProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import getBlockProps, { type json } from "./getBlockProps";
import getBlockUidByTextOnPage from "roamjs-components/queries/getBlockUidByTextOnPage";
import setBlockProps from "./setBlockProps";
import { DG_BLOCK_PROP_SETTINGS_PAGE_TITLE } from "~/data/blockPropsSettingsConfig";

export const getBlockPropBasedSettings = ({
keys,
}: {
keys: string[];
}): { blockProps: json | undefined; blockUid: string } => {
if (keys.length === 0) {
console.warn("Attempting to get block prop with no keys");
return { blockProps: undefined, blockUid: "" };
}

const sectionKey = keys[0];

const blockUid = getBlockUidByTextOnPage({
text: sectionKey,
title: DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
});

const allBlockPropsForSection = getBlockProps(blockUid);

if (keys.length > 1) {
const propertyPath = keys.slice(1);

const targetValue = propertyPath.reduce(
(currentContext: json, currentKey) => {
if (
currentContext &&
typeof currentContext === "object" &&
!Array.isArray(currentContext)
) {
const value = (currentContext as Record<string, json>)[currentKey];
return value === undefined ? null : value;
}
return null;
},
allBlockPropsForSection,
);
return {
blockProps: targetValue === null ? undefined : targetValue,
blockUid,
};
}
return { blockProps: allBlockPropsForSection, blockUid };
};

export const setBlockPropBasedSettings = ({
keys,
value,
}: {
keys: string[];
value: json;
}) => {
if (keys.length === 0) {
console.warn("Attempting to set block prop with no keys");
return;
}

const blockUid = getBlockUidByTextOnPage({
text: keys[0],
title: DG_BLOCK_PROP_SETTINGS_PAGE_TITLE,
});

if (keys.length === 1) {
setBlockProps(blockUid, value as Record<string, json>, true);
return;
}

const currentProps = getBlockProps(blockUid);
const updatedProps = JSON.parse(JSON.stringify(currentProps || {})) as Record<
string,
json
>;

const propertyPath = keys.slice(1);
const lastKeyIndex = propertyPath.length - 1;

propertyPath.reduce<Record<string, json>>(
(currentContext, currentKey, index) => {
if (index === lastKeyIndex) {
currentContext[currentKey] = value;
return currentContext;
}

if (
!currentContext[currentKey] ||
typeof currentContext[currentKey] !== "object" ||
Array.isArray(currentContext[currentKey])
) {
currentContext[currentKey] = {};
}

return currentContext[currentKey] as Record<string, json>;
},
updatedProps,
);

setBlockProps(blockUid, updatedProps, true);
};