1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* eslint-disable react/display-name */
import {
Box,
Button,
Link,
Popover,
PopoverArrow,
PopoverBody,
PopoverCloseButton,
PopoverContent,
PopoverHeader,
PopoverTrigger,
Portal,
Text,
useTheme,
} from '@chakra-ui/react'
import React, { ReactElement, useContext, useEffect, useMemo, useState } from 'react'
import unified from 'unified'
//import createStream from 'unified-stream'
import uniorgParse from 'uniorg-parse'
import uniorg2rehype from 'uniorg-rehype'
//import highlight from 'rehype-highlight'
import katex from 'rehype-katex'
import 'katex/dist/katex.css'
import rehype2react from 'rehype-react'
import { ThemeContext } from '../../util/themecontext'
import { NodeByCite, NodeById } from '../../pages'
export interface LinkProps {
href: any
children: any
previewNode?: any
setPreviewNode: any
setSidebarHighlightedNode: any
nodeByCite: NodeByCite
nodeById: NodeById
openContextMenu: any
}
export interface NodeLinkProps {
setPreviewNode: any
nodeById: NodeById
nodeByCite: NodeByCite
href: any
children: any
setSidebarHighlightedNode: any
openContextMenu: any
}
export interface NormalLinkProps {
href: string
children: string
}
import { hexToRGBA, getThemeColor } from '../../pages/index'
import noteStyle from './noteStyle'
import { OrgImage } from './OrgImage'
import { Scrollbars } from 'react-custom-scrollbars-2'
import { ExternalLinkIcon } from '@chakra-ui/icons'
export const NodeLink = (props: NodeLinkProps) => {
const { setSidebarHighlightedNode, setPreviewNode, nodeById, openContextMenu, href, children } =
props
const { highlightColor } = useContext(ThemeContext)
const theme = useTheme()
const coolHighlightColor = getThemeColor(highlightColor, theme)
const [whatever, type, uri] = [...href.matchAll(/(.*?)\:(.*)/g)][0]
return (
<Text
onMouseEnter={() => setSidebarHighlightedNode(nodeById[uri])}
onMouseLeave={() => setSidebarHighlightedNode({})}
tabIndex={0}
display="inline"
overflow="hidden"
fontWeight={500}
color={highlightColor}
textDecoration="underline"
onContextMenu={(e) => {
e.preventDefault()
openContextMenu(nodeById[uri], e)
}}
onClick={() => setPreviewNode(nodeById[uri])}
// TODO don't hardcode the opacitycolor
_hover={{ textDecoration: 'none', cursor: 'pointer', bgColor: coolHighlightColor + '22' }}
_focus={{ outlineColor: highlightColor }}
>
{children}
</Text>
)
}
export const NormalLink = (props: NormalLinkProps) => {
const { href, children } = props
const { highlightColor } = useContext(ThemeContext)
return (
<Link color={highlightColor} isExternal href={href}>
{children}
<ExternalLinkIcon mx="1px" pb="2px" />
</Link>
)
}
export const PreviewLink = (props: LinkProps) => {
const {
href,
children,
nodeById,
setSidebarHighlightedNode,
previewNode,
setPreviewNode,
nodeByCite,
openContextMenu,
} = props
// TODO figure out how to properly type this
// see https://github.com/rehypejs/rehype-react/issues/25
const [orgText, setOrgText] = useState<any>(null)
const [whatever, type, uri] = [...href.matchAll(/(.*?)\:(.*)/g)][0]
const [hover, setHover] = useState(false)
const getText = () => {
fetch(`http://localhost:35901/file/${file}`)
.then((res) => {
return res.text()
})
.then((res) => {
if (res !== 'error') {
const text = processor.processSync(res).result
setOrgText(text)
return
}
})
.catch((e) => {
console.log(e)
return 'Could not fetch the text for some reason, sorry!\n\n This can happen because you have an id with forward slashes (/) in it.'
})
}
useEffect(() => {
if (type.replaceAll(/(http)?.*/g, '$1')) {
return
}
if (!!orgText) {
return
}
if (!hover) {
return
}
getText()
}, [hover, orgText])
if (type.replaceAll(/(http)?.*/g, '$1')) {
return <NormalLink href={href}>{children}</NormalLink>
}
const getId = (type: string, uri: string) => {
if (type === 'id') {
return uri
}
if (type.includes('cite')) {
const node = nodeByCite[uri] ?? false
if (!node) {
return ''
}
if (node?.properties.FILELESS) {
return ''
}
return node?.id
}
return ''
}
const id = getId(type, uri)
const file = encodeURIComponent(encodeURIComponent(nodeById[id]?.file as string))
const processor = unified()
.use(uniorgParse)
.use(uniorg2rehype)
.use(katex)
.use(rehype2react, {
createElement: React.createElement,
components: {
// eslint-disable-next-line react/display-name
a: ({ children, href }) => (
<PreviewLink
nodeByCite={nodeByCite}
setSidebarHighlightedNode={setSidebarHighlightedNode}
href={href}
nodeById={nodeById}
setPreviewNode={setPreviewNode}
openContextMenu={openContextMenu}
>
{children}
</PreviewLink>
),
img: ({ src }) => {
return <OrgImage src={src as string} file={nodeById[id]?.file as string} />
},
},
})
if (id) {
return (
<>
<Popover gutter={12} trigger="hover" placement="top-start">
<PopoverTrigger>
<Box
display="inline"
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
<NodeLink
key={nodeById[id]?.title ?? id}
{...{
setSidebarHighlightedNode,
setPreviewNode,
nodeById,
href,
children,
nodeByCite,
openContextMenu,
}}
/>
</Box>
</PopoverTrigger>
<Portal>
<PopoverContent
transform="scale(1)"
key={nodeById[id]?.title ?? id}
boxShadow="xl"
position="relative"
zIndex="tooltip"
onMouseEnter={() => {
setSidebarHighlightedNode(nodeById[id] ?? {})
}}
onMouseLeave={() => {
setSidebarHighlightedNode({})
}}
>
<PopoverArrow />
<PopoverBody
pb={5}
fontSize="xs"
position="relative"
zIndex="tooltip"
transform="scale(1)"
width="100%"
>
<Scrollbars
autoHeight
autoHeightMax={300}
autoHide
renderThumbVertical={({ style, ...props }) => (
<Box
style={{
...style,
borderRadius: 0,
// backgroundColor: highlightColor,
}}
//color="alt.100"
{...props}
/>
)}
>
<Box
w="100%"
color="black"
px={3}
sx={noteStyle}
//overflowY="scroll"
>
{orgText}
</Box>
</Scrollbars>
</PopoverBody>
</PopoverContent>
</Portal>
</Popover>
</>
)
}
return (
<Text display="inline" color="base.700" cursor="not-allowed">
{children}
</Text>
)
}
|