Skip to content
2 changes: 1 addition & 1 deletion apps/roam/src/components/LeftSidebarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ const PersonalSections = ({ config }: { config: LeftSidebarConfig }) => {
return (
<div className="personal-left-sidebar-sections">
{sections.map((section, index) => (
<div key={section.uid}>
<div key={section.uid || `${section.text}-${index}`}>
<PersonalSectionItem section={section} sectionIndex={index} />
</div>
))}
Expand Down
21 changes: 16 additions & 5 deletions apps/roam/src/components/settings/DiscourseNodeAttributes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,28 @@ const toRecord = (attrs: Attribute[]): Record<string, string> =>
const NodeAttributes = ({
uid,
nodeType,
defaultValue,
}: {
uid: string;
nodeType: string;
defaultValue?: Record<string, string>;
}) => {
const [attributes, setAttributes] = useState<Attribute[]>(() =>
getBasicTreeByParentUid(uid).map((t) => ({
const [attributes, setAttributes] = useState<Attribute[]>(() => {
const tree = getBasicTreeByParentUid(uid);
if (defaultValue && Object.keys(defaultValue).length > 0) {
const treeByLabel = new Map(tree.map((t) => [t.text, t]));
return Object.entries(defaultValue).map(([label, value]) => ({
uid: treeByLabel.get(label)?.uid ?? "",
label,
value,
}));
Comment thread
sid597 marked this conversation as resolved.
}
Comment thread
sid597 marked this conversation as resolved.
return tree.map((t) => ({
uid: t.uid,
label: t.text,
value: t.children[0]?.text,
})),
);
}));
});
const attributesRef = useRef(attributes);
attributesRef.current = attributes;
const syncToBlockProps = () => {
Expand All @@ -93,7 +104,7 @@ const NodeAttributes = ({
<div style={{ marginBottom: 32 }}>
{attributes.map((a) => (
<NodeAttribute
key={a.uid}
key={a.label}
{...a}
onChange={(v) =>
setAttributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const DiscourseNodeSuggestiveRules = ({
description={`The template that auto fills ${node.text} page when generated.`}
settingKeys={TEMPLATE_SETTING_KEYS}
uid={templateUid}
defaultValue={node.template}
/>

<DiscourseNodeTextPanel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const LeftSidebarGlobalSectionsContent = ({
} else {
const merged = mergeGlobalSectionWithAccessor(config, globalValues);
setChildrenUid(merged.childrenUid || null);
setPages(merged.children || []);
setPages(merged.children);
setGlobalSection(merged);
}
setIsInitializing(false);
Expand Down
5 changes: 5 additions & 0 deletions apps/roam/src/components/settings/NodeConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ const NodeConfig = ({
description={`The template that auto fills ${node.text} page when generated.`}
settingKeys={TEMPLATE_SETTING_KEYS}
uid={templateUid}
defaultValue={node.template}
/>
</div>
}
Expand All @@ -343,6 +344,10 @@ const NodeConfig = ({
<DiscourseNodeAttributes
uid={attributeNode.uid}
nodeType={node.type}
defaultValue={getDiscourseNodeSetting<Record<string, string>>(
node.type,
[DISCOURSE_NODE_KEYS.attributes],
)}
/>
<DiscourseNodeSelectPanel
nodeType={node.type}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Description from "roamjs-components/components/Description";
import createBlock from "roamjs-components/writes/createBlock";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import getFirstChildUidByBlockUid from "roamjs-components/queries/getFirstChildUidByBlockUid";
import type { TreeNode } from "roamjs-components/types";
import type { InputTextNode, TreeNode } from "roamjs-components/types";
import type { RoamNodeType } from "~/components/settings/utils/zodSchema";
import { setDiscourseNodeSetting } from "~/components/settings/utils/accessors";
import type { DiscourseNodeBaseProps } from "./BlockPropSettingPanels";
Expand All @@ -13,6 +13,7 @@ const DEBOUNCE_MS = 250;

type DualWriteBlocksPanelProps = DiscourseNodeBaseProps & {
uid: string;
defaultValue?: InputTextNode[];
};

const serializeBlockTree = (children: TreeNode[]): RoamNodeType[] =>
Expand All @@ -33,9 +34,12 @@ const DualWriteBlocksPanel = ({
title,
description,
uid,
defaultValue,
}: DualWriteBlocksPanelProps) => {
const containerRef = useRef<HTMLDivElement>(null);
const debounceRef = useRef(0);
const defaultValueRef = useRef(defaultValue);
defaultValueRef.current = defaultValue;
const pullWatchArgsRef = useRef<
[string, string, (before: unknown, after: unknown) => void] | null
>(null);
Expand All @@ -62,17 +66,23 @@ const DualWriteBlocksPanel = ({
window.roamAlphaAPI.data.addPullWatch(pattern, entityId, callback);
};

if (!getFirstChildUidByBlockUid(uid)) {
void createBlock({ node: { text: " " }, parentUid: uid }).then(() => {
el.innerHTML = "";
void window.roamAlphaAPI.ui.components.renderBlock({ uid, el });
registerPullWatch();
});
} else {
const dv = defaultValueRef.current;
const ensureChildren = getFirstChildUidByBlockUid(uid)
? Promise.resolve()
: (dv && dv.length > 0
? Promise.all(
dv.map((node, i) =>
createBlock({ node, parentUid: uid, order: i }),
),
)
: createBlock({ node: { text: " " }, parentUid: uid })
).then(() => {});
Comment thread
sid597 marked this conversation as resolved.

void ensureChildren.then(() => {
el.innerHTML = "";
void window.roamAlphaAPI.ui.components.renderBlock({ uid, el });
registerPullWatch();
}
});

return () => {
window.clearTimeout(debounceRef.current);
Expand Down
1 change: 1 addition & 0 deletions apps/roam/src/components/settings/utils/settingKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const DISCOURSE_NODE_KEYS = {
attributes: "attributes",
specification: "specification",
index: "index",
template: "template",
description: "description",
shortcut: "shortcut",
tag: "tag",
Expand Down
40 changes: 40 additions & 0 deletions apps/roam/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { initPostHog } from "./utils/posthog";
import { initSchema } from "./components/settings/utils/init";
import {
bulkReadSettings,
isNewSettingsStoreEnabled,
isSyncEnabled,
} from "./components/settings/utils/accessors";
import { PERSONAL_KEYS } from "./components/settings/utils/settingKeys";
Expand Down Expand Up @@ -128,6 +129,45 @@ export default runExtension(async (onloadArgs) => {
}

const { extensionAPI } = onloadArgs;
if (isNewSettingsStoreEnabled()) {
const personalLegacyKeys = [
"discourse-context-overlay",
"text-selection-popup",
"disable-sidebar-open",
"page-preview",
"hide-feedback-button",
"auto-canvas-relations",
"discourse-context-overlay-in-canvas",
"streamline-styling",
"disallow-diagnostics",
"discourse-tool-shortcut",
"personal-node-menu-trigger",
"node-search-trigger",
"hide-metadata",
"default-page-size",
"query-pages",
"default-filters",
"use-reified-relations",
"canvas-page-format",
];
void extensionAPI.ui.commandPalette.addCommand({
label: "DG: Dev - Nuke personal legacy settings",
callback: () => {
const before = Object.fromEntries(
personalLegacyKeys.map((k) => [k, extensionAPI.settings.get(k)]),
);
console.log("[dg] personal legacy BEFORE nuke:", before);
void Promise.all(
personalLegacyKeys.map((k) => extensionAPI.settings.set(k, null)),
).then(() => {
const after = Object.fromEntries(
personalLegacyKeys.map((k) => [k, extensionAPI.settings.get(k)]),
);
console.log("[dg] personal legacy AFTER nuke:", after);
});
},
});
}
Comment thread
sid597 marked this conversation as resolved.
window.roamjs.extension.queryBuilder = {
runQuery: (parentUid: string) =>
runQuery({ parentUid, extensionAPI }).then(
Expand Down
55 changes: 40 additions & 15 deletions apps/roam/src/utils/createDiscourseNode.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { render as renderToast } from "roamjs-components/components/Toast";
import createBlock from "roamjs-components/writes/createBlock";
import stripUid from "roamjs-components/util/stripUid";
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
import createPage from "roamjs-components/writes/createPage";
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
import getSubTree from "roamjs-components/util/getSubTree";
import openBlockInSidebar from "roamjs-components/writes/openBlockInSidebar";
import getDiscourseNodes from "./getDiscourseNodes";
import resolveQueryBuilderRef from "./resolveQueryBuilderRef";
import { OnloadArgs, RoamBasicNode } from "roamjs-components/types";
import { InputTextNode, OnloadArgs } from "roamjs-components/types";
import runQuery from "./runQuery";
import updateBlock from "roamjs-components/writes/updateBlock";
import posthog from "posthog-js";
import { getPersonalSetting } from "~/components/settings/utils/accessors";
import { PERSONAL_KEYS } from "~/components/settings/utils/settingKeys";
import {
getDiscourseNodeSetting,
getPersonalSetting,
} from "~/components/settings/utils/accessors";
import {
DISCOURSE_NODE_KEYS,
PERSONAL_KEYS,
} from "~/components/settings/utils/settingKeys";

type Props = {
text: string;
Expand All @@ -23,6 +28,18 @@ type Props = {
extensionAPI?: OnloadArgs["extensionAPI"];
};

const stripTemplateUids = (nodes: InputTextNode[]): InputTextNode[] =>
nodes.map((node) => {
const nodeWithoutUid = { ...node };
const { children } = nodeWithoutUid;
delete nodeWithoutUid.uid;
delete nodeWithoutUid.children;
return {
...nodeWithoutUid,
...(children?.length ? { children: stripTemplateUids(children) } : {}),
};
});

const createDiscourseNode = async ({
text,
configPageUid,
Expand Down Expand Up @@ -141,15 +158,14 @@ const createDiscourseNode = async ({
return pageUid;
}

const nodeTree = getFullTreeByParentUid(configPageUid).children;
const templateNode = getSubTree({
tree: nodeTree,
key: "template",
});
const templateChildren =
getDiscourseNodeSetting<InputTextNode[]>(configPageUid, [
DISCOURSE_NODE_KEYS.template,
]) ?? [];

const createBlocksFromTemplate = async () => {
await Promise.all(
stripUid(templateNode.children).map(({ uid, ...node }, order) =>
stripTemplateUids(templateChildren).map((node, order) =>
createBlock({
node,
order,
Expand All @@ -162,12 +178,17 @@ const createDiscourseNode = async ({
await handleImageCreation(pageUid);
};

const hasSmartBlockSyntax = (node: RoamBasicNode) => {
const hasSmartBlockSyntax = (node: InputTextNode) => {
if (node.text.includes("<%")) return true;
if (node.children) return node.children.some(hasSmartBlockSyntax);
return false;
};
const useSmartBlocks = hasSmartBlockSyntax(templateNode);
const useSmartBlocks = templateChildren.some(hasSmartBlockSyntax);
const legacyTemplateNode = getSubTree({
tree: getFullTreeByParentUid(configPageUid).children,
key: "template",
});
const canUseLegacySmartBlock = !!legacyTemplateNode.uid;

if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) {
renderToast({
Expand All @@ -177,9 +198,13 @@ const createDiscourseNode = async ({
intent: "warning",
});
await createBlocksFromTemplate();
} else if (useSmartBlocks && window.roamjs?.extension?.smartblocks) {
window.roamjs.extension.smartblocks?.triggerSmartblock({
srcUid: templateNode.uid,
} else if (
useSmartBlocks &&
canUseLegacySmartBlock &&
window.roamjs?.extension?.smartblocks
) {
void window.roamjs.extension.smartblocks?.triggerSmartblock({
srcUid: legacyTemplateNode.uid,
targetUid: pageUid,
});
await handleImageCreation(pageUid);
Expand Down
23 changes: 13 additions & 10 deletions apps/roam/src/utils/getExportSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTit
import { RoamBasicNode } from "roamjs-components/types";
import { getSubTree } from "roamjs-components/util";
import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/data/constants";
import { bulkReadSettings } from "~/components/settings/utils/accessors";
import { EXPORT_KEYS } from "~/components/settings/utils/settingKeys";

type UidPair<T> = {
uid?: string;
Expand Down Expand Up @@ -110,16 +112,17 @@ export const getExportSettingsAndUids = (): ExportConfigWithUids => {
};

export const getExportSettings = (): Omit<ExportConfig, "exportUid"> => {
const settings = getExportSettingsAndUids();
const exportValues = bulkReadSettings().globalSettings.Export;
const legacy = getExportSettingsAndUids();
return {
maxFilenameLength: settings.maxFilenameLength.value,
openSidebar: settings.openSidebar.value,
removeSpecialCharacters: settings.removeSpecialCharacters.value,
simplifiedFilename: settings.simplifiedFilename.value,
optsEmbeds: settings.optsEmbeds.value,
optsRefs: settings.optsRefs.value,
linkType: settings.linkType.value,
appendRefNodeContext: settings.appendRefNodeContext.value,
frontmatter: settings.frontmatter.values,
maxFilenameLength: exportValues[EXPORT_KEYS.maxFilenameLength],
removeSpecialCharacters: exportValues[EXPORT_KEYS.removeSpecialCharacters],
optsEmbeds: exportValues[EXPORT_KEYS.resolveBlockEmbeds],
optsRefs: exportValues[EXPORT_KEYS.resolveBlockReferences],
linkType: exportValues[EXPORT_KEYS.linkType],
appendRefNodeContext: exportValues[EXPORT_KEYS.appendReferencedNode],
frontmatter: exportValues[EXPORT_KEYS.frontmatter],
openSidebar: legacy.openSidebar.value,
simplifiedFilename: legacy.simplifiedFilename.value,
};
};
Loading
Loading