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
17 changes: 14 additions & 3 deletions apps/roam/src/components/DiscourseNodeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ import { getNewDiscourseNodeText } from "~/utils/formatUtils";
import { OnloadArgs } from "roamjs-components/types";
import { formatHexColor } from "./settings/DiscourseNodeCanvasSettings";
import posthog from "posthog-js";
import { setPersonalSetting } from "~/components/settings/utils/accessors";
import {
setPersonalSetting,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";
import { PERSONAL_KEYS } from "~/components/settings/utils/settingKeys";
import type { PersonalSettings } from "~/components/settings/utils/zodSchema";

Expand All @@ -38,6 +41,7 @@ type Props = {
trigger?: JSX.Element;
isShift?: boolean;
menuMaxHeight?: number;
settingsSnapshot?: SettingsSnapshot;
};

const NodeMenu = ({
Expand All @@ -48,6 +52,7 @@ const NodeMenu = ({
trigger,
isShift,
menuMaxHeight,
settingsSnapshot,
}: { onClose: () => void } & Props) => {
const isInitialTextSelected =
!!textarea && textarea.selectionStart !== textarea.selectionEnd;
Expand All @@ -56,8 +61,11 @@ const NodeMenu = ({
isInitialTextSelected || (isShift ?? false),
);
const userDiscourseNodes = useMemo(
() => getDiscourseNodes().filter((n) => n.backedBy === "user"),
[],
() =>
getDiscourseNodes(undefined, settingsSnapshot).filter(
(n) => n.backedBy === "user",
),
[settingsSnapshot],
);
const discourseNodes = userDiscourseNodes.filter(
(n) => showNodeTypes || n.tag,
Expand Down Expand Up @@ -359,10 +367,12 @@ export const TextSelectionNodeMenu = ({
textarea,
extensionAPI,
onClose,
settingsSnapshot,
}: {
textarea: HTMLTextAreaElement;
extensionAPI: OnloadArgs["extensionAPI"];
onClose: () => void;
settingsSnapshot?: SettingsSnapshot;
}) => {
const trigger = (
<Button
Expand Down Expand Up @@ -403,6 +413,7 @@ export const TextSelectionNodeMenu = ({
onClose={onClose}
isShift
menuMaxHeight={menuMaxHeight}
settingsSnapshot={settingsSnapshot}
/>
);
};
Expand Down
11 changes: 9 additions & 2 deletions apps/roam/src/utils/getExportTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import {
pullBlockToTreeNode,
collectUids,
} from "./exportUtils";
import {
bulkReadSettings,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";

export const updateExportProgress = (detail: {
progress: number;
Expand All @@ -36,6 +40,7 @@ type getExportTypesProps = {
results?: ExportDialogProps["results"];
exportId: string;
isExportDiscourseGraph: boolean;
settingsSnapshot?: SettingsSnapshot;
};

type RoamImportUser = {
Expand Down Expand Up @@ -66,9 +71,11 @@ const getExportTypes = ({
results,
exportId,
isExportDiscourseGraph,
settingsSnapshot,
}: getExportTypesProps): ExportTypes => {
const allRelations = getDiscourseRelations();
const allNodes = getDiscourseNodes(allRelations);
const settings = settingsSnapshot ?? bulkReadSettings();
const allRelations = getDiscourseRelations(settings);
const allNodes = getDiscourseNodes(allRelations, settings);
const nodeLabelByType = Object.fromEntries(
allNodes.map((a) => [a.type, a.text]),
);
Expand Down
3 changes: 2 additions & 1 deletion apps/roam/src/utils/initializeObserversAndListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export const initObservers = ({
className: "rm-inline-img",
callback: (img: HTMLElement) => {
if (img instanceof HTMLImageElement) {
renderImageToolsMenu(img, onloadArgs.extensionAPI);
renderImageToolsMenu(img, onloadArgs.extensionAPI, settings);
}
},
});
Expand Down Expand Up @@ -443,6 +443,7 @@ export const initObservers = ({
extensionAPI: onloadArgs.extensionAPI,
blockElement,
textarea,
settingsSnapshot: settings,
});
} else {
removeTextSelectionPopup();
Expand Down
19 changes: 18 additions & 1 deletion apps/roam/src/utils/pageRefObserverHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { OnloadArgs } from "roamjs-components/types";
import { renderSuggestive as renderSuggestiveOverlay } from "~/components/SuggestiveModeOverlay";
import getDiscourseNodes, { type DiscourseNode } from "./getDiscourseNodes";
import findDiscourseNode from "./findDiscourseNode";
import {
bulkReadSettings,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";

const PAGE_REF_SELECTOR = "span.rm-page-ref";
const DISCOURSE_OVERLAY_CLASS = "roamjs-discourse-context-overlay";
Expand All @@ -24,11 +28,13 @@ type PageRefDiscourseNodeStatus = {
};

let batchDiscourseNodes: DiscourseNode[] | null = null;
let batchSettingsSnapshot: SettingsSnapshot | null = null;
let clearBatchCacheQueued = false;
const pageRefDiscourseNodeCache = new Map<string, PageRefDiscourseNodeStatus>();

const clearBatchCache = (): void => {
batchDiscourseNodes = null;
batchSettingsSnapshot = null;
pageRefDiscourseNodeCache.clear();
clearBatchCacheQueued = false;
};
Expand All @@ -39,10 +45,21 @@ const queueBatchCacheClear = (): void => {
queueMicrotask(clearBatchCache);
};

const getBatchSettingsSnapshot = (): SettingsSnapshot => {
if (batchSettingsSnapshot) return batchSettingsSnapshot;

batchSettingsSnapshot = bulkReadSettings();
queueBatchCacheClear();
return batchSettingsSnapshot;
};

const getBatchDiscourseNodes = (): DiscourseNode[] => {
if (batchDiscourseNodes) return batchDiscourseNodes;

batchDiscourseNodes = getDiscourseNodes();
batchDiscourseNodes = getDiscourseNodes(
undefined,
getBatchSettingsSnapshot(),
);
queueBatchCacheClear();
return batchDiscourseNodes;
};
Expand Down
27 changes: 25 additions & 2 deletions apps/roam/src/utils/renderImageToolsMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,24 @@ import getUids from "roamjs-components/dom/getUids";
import NodeMenu from "~/components/DiscourseNodeMenu";
import { OnloadArgs } from "roamjs-components/types";
import posthog from "posthog-js";
import {
bulkReadSettings,
type SettingsSnapshot,
} from "~/components/settings/utils/accessors";

type ImageToolsMenuProps = {
blockUid: string;
extensionAPI: OnloadArgs["extensionAPI"];
initialSettings: SettingsSnapshot;
};

const ImageToolsMenu = ({
blockUid,
extensionAPI,
initialSettings,
}: ImageToolsMenuProps): JSX.Element => {
const [menuKey, setMenuKey] = useState(0);
const [settings, setSettings] = useState(initialSettings);

const handleEditBlock = useCallback((): void => {
posthog.capture("Image Tools Menu: Edit Block Clicked");
Expand All @@ -24,12 +31,22 @@ const ImageToolsMenu = ({
});
}, [blockUid]);

const refreshSettings = useCallback((): void => {
setSettings(bulkReadSettings());
}, []);

const handleMenuClose = useCallback(() => {
setMenuKey((prev) => prev + 1);
}, []);

const trigger = (
<Button icon={"label" as IconName} minimal small title="Add Node Tag" />
<Button
icon={"label" as IconName}
minimal
small
title="Add Node Tag"
onMouseDown={refreshSettings}
/>
);

return (
Expand All @@ -45,6 +62,7 @@ const ImageToolsMenu = ({
extensionAPI={extensionAPI}
trigger={trigger}
isShift={false}
settingsSnapshot={settings}
/>

<Button
Expand Down Expand Up @@ -125,6 +143,7 @@ const attachHoverListeners = (
export const renderImageToolsMenu = (
imageElement: HTMLImageElement,
extensionAPI: OnloadArgs["extensionAPI"],
initialSettings: SettingsSnapshot,
): void => {
const wrapper = getImageWrapper(imageElement);
if (!wrapper) return;
Expand All @@ -142,7 +161,11 @@ export const renderImageToolsMenu = (

// eslint-disable-next-line react/no-deprecated
ReactDOM.render(
<ImageToolsMenu blockUid={blockUid} extensionAPI={extensionAPI} />,
<ImageToolsMenu
blockUid={blockUid}
extensionAPI={extensionAPI}
initialSettings={initialSettings}
/>,
menuContainer,
);
};
4 changes: 4 additions & 0 deletions apps/roam/src/utils/renderTextSelectionPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TextSelectionNodeMenu } from "~/components/DiscourseNodeMenu";
import { getCoordsFromTextarea } from "roamjs-components/components/CursorMenu";
import { OnloadArgs } from "roamjs-components/types";
import posthog from "posthog-js";
import type { SettingsSnapshot } from "~/components/settings/utils/accessors";

let currentPopupContainer: HTMLDivElement | null = null;

Expand Down Expand Up @@ -44,10 +45,12 @@ export const renderTextSelectionPopup = ({
extensionAPI,
blockElement,
textarea,
settingsSnapshot,
}: {
extensionAPI: OnloadArgs["extensionAPI"];
blockElement: Element;
textarea: HTMLTextAreaElement;
settingsSnapshot: SettingsSnapshot;
}) => {
posthog.capture("Text Selection Popup: Opened");
removeTextSelectionPopup();
Expand All @@ -66,6 +69,7 @@ export const renderTextSelectionPopup = ({
textarea={textarea}
extensionAPI={extensionAPI}
onClose={removeTextSelectionPopup}
settingsSnapshot={settingsSnapshot}
/>,
currentPopupContainer,
);
Expand Down
Loading