Skip to content
Merged
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
16 changes: 13 additions & 3 deletions apps/roam/src/components/DiscourseContextOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import getDiscourseNodes from "~/utils/getDiscourseNodes";
import getDiscourseRelations from "~/utils/getDiscourseRelations";
import ExtensionApiContextProvider from "roamjs-components/components/ExtensionApiContext";
import { OnloadArgs } from "roamjs-components/types/native";
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";

type DiscourseData = {
results: Awaited<ReturnType<typeof getDiscourseContextResults>>;
Expand Down Expand Up @@ -57,15 +58,24 @@ const getOverlayInfo = async (tag: string): Promise<DiscourseData> => {
}
};

const DiscourseContextOverlay = ({ tag, id }: { tag: string; id: string }) => {
const tagUid = useMemo(() => getPageUidByPageTitle(tag), [tag]);
type DiscourseContextOverlayProps =
| { id: string; tag: string; uid?: never }
| { id: string; uid: string; tag?: never }
| { id: string; tag: string; uid: string };
const DiscourseContextOverlay = ({
tag,
id,
uid,
}: DiscourseContextOverlayProps) => {
const newTag = tag ?? (uid ? (getPageTitleByPageUid(uid) ?? "") : "");
const tagUid = uid || getPageUidByPageTitle(newTag);
const [loading, setLoading] = useState(true);
const [results, setResults] = useState<DiscourseData["results"]>([]);
const [refs, setRefs] = useState(0);
const [score, setScore] = useState<number | string>(0);
const getInfo = useCallback(
() =>
getOverlayInfo(tag)
getOverlayInfo(newTag)
.then(({ refs, results }) => {
const discourseNode = findDiscourseNode(tagUid);
if (discourseNode) {
Expand Down
32 changes: 29 additions & 3 deletions apps/roam/src/components/canvas/DiscourseNodeUtil.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
TLDefaultFontStyle,
DefaultFontStyle,
} from "tldraw";
import React, { useState, useEffect, useRef } from "react";
import React, { useState, useEffect, useRef, useMemo } from "react";
import { useExtensionAPI } from "roamjs-components/components/ExtensionApiContext";
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
import isLiveBlock from "roamjs-components/queries/isLiveBlock";
Expand All @@ -38,8 +38,12 @@ import calcCanvasNodeSizeAndImg from "~/utils/calcCanvasNodeSizeAndImg";
import { createTextJsxFromSpans } from "./DiscourseRelationShape/helpers";
import { loadImage } from "~/utils/loadImage";
import { getRelationColor } from "./DiscourseRelationShape/DiscourseRelationUtil";
import { AUTO_CANVAS_RELATIONS_KEY } from "~/data/userSettings";
import {
AUTO_CANVAS_RELATIONS_KEY,
DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY,
} from "~/data/userSettings";
import { getSetting } from "~/utils/extensionSettings";
import DiscourseContextOverlay from "~/components/DiscourseContextOverlay";

// TODO REPLACE WITH TLDRAW DEFAULTS
// https://github.com/tldraw/tldraw/pull/1580/files
Expand Down Expand Up @@ -450,13 +454,20 @@ export class BaseDiscourseNodeUtil extends ShapeUtil<DiscourseNodeShape> {
const {
canvasSettings: { alias = "", "key-image": isKeyImage = "" } = {},
} = discourseContext.nodes[shape.type] || {};
// eslint-disable-next-line react-hooks/rules-of-hooks
const isOverlayEnabled = useMemo(
() => getSetting(DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY, false),
[],
);

const isEditing = this.editor.getEditingShapeId() === shape.id;
// eslint-disable-next-line react-hooks/rules-of-hooks
const contentRef = useRef<HTMLDivElement>(null);
// eslint-disable-next-line react-hooks/rules-of-hooks
const [loaded, setLoaded] = useState("");
// eslint-disable-next-line react-hooks/rules-of-hooks
const [overlayMounted, setOverlayMounted] = useState(false);
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
if (
shape.props.uid !== loaded &&
Expand Down Expand Up @@ -498,7 +509,11 @@ export class BaseDiscourseNodeUtil extends ShapeUtil<DiscourseNodeShape> {
<HTMLContainer
id={shape.id}
className="roamjs-tldraw-node pointer-events-auto flex items-center justify-center overflow-hidden rounded-2xl"
style={{ background: backgroundColor, color: textColor }}
style={{
background: backgroundColor,
color: textColor,
}}
onPointerEnter={() => setOverlayMounted(true)}
>
<div style={{ pointerEvents: "all" }}>
{shape.props.imageUrl && isKeyImage ? (
Expand All @@ -519,6 +534,17 @@ export class BaseDiscourseNodeUtil extends ShapeUtil<DiscourseNodeShape> {
fontSize: FONT_SIZES[shape.props.size],
}}
>
{overlayMounted && isOverlayEnabled && (
<div
className="roamjs-discourse-context-overlay-container absolute right-0 top-0"
onPointerDown={(e) => e.stopPropagation()}
>
<DiscourseContextOverlay
uid={shape.props.uid}
id={`${shape.id}-overlay`}
/>
</div>
)}
{alias
? new RegExp(alias).exec(shape.props.title)?.[1] ||
shape.props.title
Expand Down
22 changes: 22 additions & 0 deletions apps/roam/src/components/settings/HomePersonalSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import {
import { NodeSearchMenuTriggerSetting } from "../DiscourseNodeSearchMenu";
import {
AUTO_CANVAS_RELATIONS_KEY,
DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY,
DISCOURSE_TOOL_SHORTCUT_KEY,
} from "~/data/userSettings";
import KeyboardShortcutInput from "./KeyboardShortcutInput";
import { getSetting, setSetting } from "~/utils/extensionSettings";

const HomePersonalSettings = ({ onloadArgs }: { onloadArgs: OnloadArgs }) => {
const extensionAPI = onloadArgs.extensionAPI;
Expand Down Expand Up @@ -177,6 +179,26 @@ const HomePersonalSettings = ({ onloadArgs }: { onloadArgs: OnloadArgs }) => {
</>
}
/>
<Checkbox
defaultChecked={getSetting(
DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY,
false,
)}
onChange={(e) => {
const target = e.target as HTMLInputElement;
setSetting(DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY, target.checked);
}}
labelElement={
<>
(BETA) Overlay in Canvas
<Description
description={
"Whether or not to overlay Discourse Context information over Canvas Nodes."
}
/>
</>
}
/>
</div>
);
};
Expand Down
2 changes: 2 additions & 0 deletions apps/roam/src/data/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const DEFAULT_FILTERS_KEY = "default-filters";
export const QUERY_BUILDER_SETTINGS_KEY = "query-builder-settings";
export const AUTO_CANVAS_RELATIONS_KEY = "auto-canvas-relations";
export const DISCOURSE_TOOL_SHORTCUT_KEY = "discourse-tool-shortcut";
export const DISCOURSE_CONTEXT_OVERLAY_IN_CANVAS_KEY =
"discourse-context-overlay-in-canvas";