From 6deb6247f63f5aaa25550dc0398d50221c5383c1 Mon Sep 17 00:00:00 2001 From: "Thomas F. K. Jorna" Date: Sat, 9 Oct 2021 18:35:39 +0200 Subject: fix: images in preview --- components/Sidebar/Link.tsx | 46 ++++++++++++++++++++++++++++++++++----------- next.config.js | 3 +++ org-roam-ui.el | 8 +++++++- util/processOrg.tsx | 15 +++++++++++++-- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/components/Sidebar/Link.tsx b/components/Sidebar/Link.tsx index 1211cb4..57fb723 100644 --- a/components/Sidebar/Link.tsx +++ b/components/Sidebar/Link.tsx @@ -47,6 +47,7 @@ export interface NormalLinkProps { import { hexToRGBA, getThemeColor } from '../../pages/index' import noteStyle from './noteStyle' +import { OrgImage } from './OrgImage' export const NormalLink = (props: NormalLinkProps) => { const { setSidebarHighlightedNode, setPreviewNode, nodeById, href, children } = props @@ -66,6 +67,7 @@ export const NormalLink = (props: NormalLinkProps) => { color={highlightColor} textDecoration="underline" onClick={() => setPreviewNode(nodeById[uri])} + // TODO don't hardcode the opacitycolor _hover={{ textDecoration: 'none', cursor: 'pointer', bgColor: coolHighlightColor + '22' }} _focus={{ outlineColor: highlightColor }} > @@ -86,8 +88,9 @@ export const PreviewLink = (props: LinkProps) => { } = props // TODO figure out how to properly type this // see https://github.com/rehypejs/rehype-react/issues/25 - const [orgText, setOrgText] = useState() + const [orgText, setOrgText] = useState(null) const [whatever, type, uri] = [...href.matchAll(/(.*?)\:(.*)/g)][0] + const [hover, setHover] = useState(false) const getId = (type: string, uri: string) => { if (type === 'id') { @@ -108,6 +111,7 @@ export const PreviewLink = (props: LinkProps) => { } const id = getId(type, uri) + const file = encodeURIComponent(encodeURIComponent(nodeById[id]?.file as string)) const processor = unified() .use(uniorgParse) @@ -118,19 +122,24 @@ export const PreviewLink = (props: LinkProps) => { components: { // eslint-disable-next-line react/display-name a: ({ children, href }) => ( - + > + {nodeById[id as string]?.title} + ), + img: ({ src }) => { + return + }, }, }) - const file = encodeURIComponent(encodeURIComponent(nodeById[id]?.file as string)) const getText = () => { + console.log('gettin text') fetch(`http://localhost:35901/file/${file}`) .then((res) => { return res.text() @@ -148,15 +157,26 @@ export const PreviewLink = (props: LinkProps) => { }) } - useMemo(() => { + useEffect(() => { + if (!!orgText) { + return + } + if (!hover) { + return + } getText() - }, [id]) + }, [hover, orgText]) + if (id) { return ( <> - + setHover(true)} + onMouseLeave={() => setHover(false)} + > { boxShadow="xl" position="relative" zIndex="tooltip" - onMouseEnter={() => setSidebarHighlightedNode(nodeById[id] ?? {})} - onMouseLeave={() => setSidebarHighlightedNode({})} + onMouseEnter={() => { + setSidebarHighlightedNode(nodeById[id] ?? {}) + }} + onMouseLeave={() => { + setSidebarHighlightedNode({}) + }} > { - const { nodeById, setSidebarHighlightedNode, setPreviewNode, previewText, nodeByCite } = props + const { + nodeById, + setSidebarHighlightedNode, + setPreviewNode, + previewText, + nodeByCite, + previewNode, + } = props const processor = unified() .use(uniorgParse) @@ -48,10 +56,13 @@ export const ProcessedOrg = (props: ProcessedOrgProps) => { nodeById={nodeById} setPreviewNode={setPreviewNode} > - {typeof children === 'string' ? children : null} + {children} ) }, + img: ({ src }) => { + return + }, }, }) -- cgit v1.2.3