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
53 changes: 53 additions & 0 deletions apps/roam/src/components/LeftSidebarView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { DISCOURSE_CONFIG_PAGE_TITLE } from "~/utils/renderNodeConfigPage";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
import { migrateLeftSidebarSettings } from "~/utils/migrateLeftSidebarSettings";
import posthog from "posthog-js";
import { isSmartBlockUid } from "~/utils/getLeftSidebarSettings";

const parseReference = (text: string) => {
const extracted = extractRef(text);
Expand Down Expand Up @@ -120,6 +121,30 @@ const toggleFoldedState = ({
}
};

const RoamRenderedBlock = ({ uid }: { uid: string }) => {
// @ts-expect-error - .react.block is available but not in the type definitions yet
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
const Block = window.roamAlphaAPI.ui.components.react.block;

const handleClick = useCallback(
(e: React.MouseEvent) => {
const target = e.target as HTMLElement;
if (target.closest("[data-roamjs-smartblock-button]")) {
void window.roamAlphaAPI.ui.mainWindow.openBlock({
block: { uid },
});
}
},
[uid],
);

return (
<div className="dg-sidebar-rendered-block" onClick={handleClick}>
<Block uid={uid} open={false} />
</div>
);
};

const SectionChildren = ({
childrenNodes,
truncateAt,
Expand All @@ -133,6 +158,18 @@ const SectionChildren = ({
{childrenNodes.map((child) => {
const ref = parseReference(child.text);
const alias = child.alias?.value;
const isSmartBlock = ref.type === "block" && isSmartBlockUid(ref.uid);

if (isSmartBlock) {
return (
<div key={child.uid} className="pl-8 pr-2.5">
<div className="section-child-item rounded-sm leading-normal text-gray-600">
<RoamRenderedBlock uid={ref.uid} />
</div>
</div>
);
}

const display =
ref.type === "page"
? getPageTitleByPageUid(ref.display)
Expand Down Expand Up @@ -519,6 +556,22 @@ export const mountLeftSidebar = async (
): Promise<void> => {
if (!wrapper) return;

const styleId = "dg-sidebar-rendered-block-styles";
if (!document.getElementById(styleId)) {
const style = document.createElement("style");
style.id = styleId;
style.textContent = `
.dg-sidebar-rendered-block .rm-bullet { display: none; }
.dg-sidebar-rendered-block .rm-block-separator { display: none; }
.dg-sidebar-rendered-block .controls { display: none; }
.dg-sidebar-rendered-block .block-expand { display: none; }
.dg-sidebar-rendered-block .block-border-left { display: none; }
.dg-sidebar-rendered-block .block-ref-count-button { display: none; }
.dg-sidebar-rendered-block .rm-block-main { min-height: unset; padding: 0; }
`;
document.head.appendChild(style);
}

const id = "dg-left-sidebar-root";
let root = wrapper.querySelector(`#${id}`) as HTMLDivElement;
if (!root) {
Expand Down
25 changes: 17 additions & 8 deletions apps/roam/src/components/settings/LeftSidebarPersonalSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ import { refreshAndNotify } from "~/components/LeftSidebarView";
import { memo, Dispatch, SetStateAction } from "react";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
import posthog from "posthog-js";
import { isSmartBlockUid } from "~/utils/getLeftSidebarSettings";

const isSmartBlockRef = (text: string): boolean => {
if (!text.startsWith("((") || !text.endsWith("))")) return false;
return isSmartBlockUid(extractRef(text));
};

/* eslint-disable @typescript-eslint/naming-convention */
export const sectionsToBlockProps = (
Expand Down Expand Up @@ -439,6 +445,7 @@ const SectionItem = memo(
{(section.children || []).map((child, index) => {
const childAlias = child.alias?.value;
const isSettingsOpen = childSettingsUid === child.uid;
const childIsSmartBlock = isSmartBlockRef(child.text);
const childDisplayTitle =
getPageTitleByPageUid(child.text) ||
getTextByBlockUid(extractRef(child.text)) ||
Expand All @@ -450,7 +457,7 @@ const SectionItem = memo(
className="mr-2 min-w-0 flex-1 truncate"
title={childDisplayTitle}
>
{childAlias ? (
{childAlias && !childIsSmartBlock ? (
<span>
<span className="font-medium">
{childAlias}
Expand All @@ -464,13 +471,15 @@ const SectionItem = memo(
)}
</div>
<ButtonGroup minimal className="flex-shrink-0">
<Button
icon="settings"
small
onClick={() => setChildSettingsUid(child.uid)}
title="Child settings"
className="opacity-0 transition-opacity group-hover:opacity-100"
/>
{!childIsSmartBlock && (
<Button
icon="settings"
small
onClick={() => setChildSettingsUid(child.uid)}
title="Child settings"
className="opacity-0 transition-opacity group-hover:opacity-100"
/>
)}
<Button
icon="arrow-up"
small
Expand Down
2 changes: 1 addition & 1 deletion apps/roam/src/utils/createDiscourseNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const createDiscourseNode = async ({
await handleImageCreation(pageUid);
};

const hasSmartBlockSyntax = (node: RoamBasicNode) => {
const hasSmartBlockSyntax = (node: RoamBasicNode): boolean => {
if (node.text.includes("<%")) return true;
if (node.children) return node.children.some(hasSmartBlockSyntax);
return false;
Expand Down
7 changes: 7 additions & 0 deletions apps/roam/src/utils/getLeftSidebarSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import {
getUidAndStringSetting,
} from "./getExportSettings";
import { getSubTree } from "roamjs-components/util";
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";

export const isSmartBlockUid = (uid: string): boolean => {
const text = getTextByBlockUid(uid);
if (!text) return false;
return text.includes(":SmartBlock:");
};

type LeftSidebarPersonalSectionSettings = {
uid: string;
Expand Down
61 changes: 59 additions & 2 deletions apps/roam/src/utils/registerCommandPaletteCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ import { HIDE_METADATA_KEY } from "~/data/userSettings";
import posthog from "posthog-js";
import { extractRef } from "roamjs-components/util";
import discourseConfigRef from "~/utils/discourseConfigRef";
import { getLeftSidebarPersonalSectionConfig } from "~/utils/getLeftSidebarSettings";
import {
getLeftSidebarPersonalSectionConfig,
getLeftSidebarGlobalSectionConfig,
} from "~/utils/getLeftSidebarSettings";
import { getUidAndBooleanSetting } from "~/utils/getExportSettings";
import refreshConfigTree from "~/utils/refreshConfigTree";
import { refreshAndNotify } from "~/components/LeftSidebarView";
import { setPersonalSetting } from "~/components/settings/utils/accessors";
import {
setPersonalSetting,
setGlobalSetting,
} from "~/components/settings/utils/accessors";
import { sectionsToBlockProps } from "~/components/settings/LeftSidebarPersonalSettings";

export const registerCommandPaletteCommands = (onloadArgs: OnloadArgs) => {
Expand Down Expand Up @@ -341,6 +347,22 @@ export const registerCommandPaletteCommands = (onloadArgs: OnloadArgs) => {
},
});
}

const globalSection = getLeftSidebarGlobalSectionConfig(
leftSidebarNode?.children || [],
);
if (globalSection.childrenUid) {
window.roamAlphaAPI.ui.blockContextMenu.addCommand({
label: "DG: Favorites - Add to Global section",
// eslint-disable-next-line @typescript-eslint/naming-convention
callback: (props: { "block-uid": string }) => {
void addBlockToGlobalSection({
blockUid: props["block-uid"],
globalChildrenUid: globalSection.childrenUid,
});
},
});
}
}
};

Expand Down Expand Up @@ -404,3 +426,38 @@ const addBlockToPersonalSection = async ({
});
}
};

const addBlockToGlobalSection = async ({
blockUid,
globalChildrenUid,
}: {
blockUid: string;
globalChildrenUid: string;
}) => {
const blockRef = `((${blockUid}))`;

try {
await createBlock({
parentUid: globalChildrenUid,
order: "last",
node: { text: blockRef },
});

refreshConfigTree();
const updatedChildren = getLeftSidebarGlobalSectionConfig(
discourseConfigRef.tree.find((n) => n.text === "Left Sidebar")
?.children || [],
).children;
setGlobalSetting(
["Left sidebar", "Children"],
updatedChildren.map((c) => c.text),
);
refreshAndNotify();
} catch {
renderToast({
content: "Failed to add block to global section",
intent: "danger",
id: "add-block-to-global-section-error",
});
}
};
Loading