From 46771fe06650c2488dc068ebb3fde6b0f0a646b9 Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 22 Dec 2025 11:02:24 +0530 Subject: [PATCH 1/3] get and set block prop based settings --- .../roam/src/utils/settingsUsingBlockProps.ts | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 apps/roam/src/utils/settingsUsingBlockProps.ts diff --git a/apps/roam/src/utils/settingsUsingBlockProps.ts b/apps/roam/src/utils/settingsUsingBlockProps.ts new file mode 100644 index 000000000..bb9a0a830 --- /dev/null +++ b/apps/roam/src/utils/settingsUsingBlockProps.ts @@ -0,0 +1,99 @@ +import getBlockProps, { type json } from "./getBlockProps"; +import getBlockUidByTextOnPage from "roamjs-components/queries/getBlockUidByTextOnPage"; +import setBlockProps from "./setBlockProps"; + +export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE = + "roam/js/discourse-graph/block-prop-settings"; + +export const getBlockPropBasedSettings = ({ + keys, +}: { + keys: string[]; +}): { blockProps: json | undefined; blockUid: string } => { + 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)[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, 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>( + (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; + }, + updatedProps, + ); + + setBlockProps(blockUid, updatedProps, true); +}; From bd4bc522ea236052c4d9b06f6938d4f940cf8e2d Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 22 Dec 2025 11:13:07 +0530 Subject: [PATCH 2/3] address coderabbit --- apps/roam/src/utils/settingsUsingBlockProps.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/apps/roam/src/utils/settingsUsingBlockProps.ts b/apps/roam/src/utils/settingsUsingBlockProps.ts index bb9a0a830..e17101688 100644 --- a/apps/roam/src/utils/settingsUsingBlockProps.ts +++ b/apps/roam/src/utils/settingsUsingBlockProps.ts @@ -10,6 +10,11 @@ export const getBlockPropBasedSettings = ({ }: { 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({ From 6d0429aea1e59e2279689014775426d31bec9a58 Mon Sep 17 00:00:00 2001 From: sid597 Date: Mon, 22 Dec 2025 11:23:27 +0530 Subject: [PATCH 3/3] new const file --- apps/roam/src/data/blockPropsSettingsConfig.ts | 1 + apps/roam/src/utils/settingsUsingBlockProps.ts | 4 +--- 2 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 apps/roam/src/data/blockPropsSettingsConfig.ts diff --git a/apps/roam/src/data/blockPropsSettingsConfig.ts b/apps/roam/src/data/blockPropsSettingsConfig.ts new file mode 100644 index 000000000..9b6b10987 --- /dev/null +++ b/apps/roam/src/data/blockPropsSettingsConfig.ts @@ -0,0 +1 @@ +export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE ="roam/js/discourse-graph/block-prop-settings"; \ No newline at end of file diff --git a/apps/roam/src/utils/settingsUsingBlockProps.ts b/apps/roam/src/utils/settingsUsingBlockProps.ts index e17101688..ec2316b2e 100644 --- a/apps/roam/src/utils/settingsUsingBlockProps.ts +++ b/apps/roam/src/utils/settingsUsingBlockProps.ts @@ -1,9 +1,7 @@ import getBlockProps, { type json } from "./getBlockProps"; import getBlockUidByTextOnPage from "roamjs-components/queries/getBlockUidByTextOnPage"; import setBlockProps from "./setBlockProps"; - -export const DG_BLOCK_PROP_SETTINGS_PAGE_TITLE = - "roam/js/discourse-graph/block-prop-settings"; +import { DG_BLOCK_PROP_SETTINGS_PAGE_TITLE } from "~/data/blockPropsSettingsConfig"; export const getBlockPropBasedSettings = ({ keys,