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 new file mode 100644 index 000000000..ec2316b2e --- /dev/null +++ b/apps/roam/src/utils/settingsUsingBlockProps.ts @@ -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)[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); +};