-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
BlockLink.tsx
71 lines (64 loc) · 1.82 KB
/
BlockLink.tsx
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
import { validUUID } from '@tldraw/core'
import React from 'react'
import { LogseqContext } from '../../lib/logseq-context'
import { TablerIcon } from '../icons'
export const BlockLink = ({
id,
showReferenceContent = false,
}: {
id: string
showReferenceContent?: boolean
}) => {
const {
handlers: { isWhiteboardPage, redirectToPage, sidebarAddBlock, queryBlockByUUID },
renderers: { Breadcrumb, PageName },
} = React.useContext(LogseqContext)
let iconName = ''
let linkType = validUUID(id) ? 'B' : 'P'
let blockContent = ''
if (validUUID(id)) {
const block = queryBlockByUUID(id)
if (!block) {
return <span className="p-2">Invalid reference. Did you remove it?</span>
}
blockContent = block.content
if (block.properties?.['ls-type'] === 'whiteboard-shape') {
iconName = 'link-to-whiteboard'
} else {
iconName = 'link-to-block'
}
} else {
if (isWhiteboardPage(id)) {
iconName = 'link-to-whiteboard'
} else {
iconName = 'link-to-page'
}
}
const slicedContent =
blockContent && blockContent.length > 23 ? blockContent.slice(0, 20) + '...' : blockContent
return (
<button
className="inline-flex gap-1 items-center w-full"
onPointerDown={e => {
e.stopPropagation()
if (e.shiftKey) {
sidebarAddBlock(id, linkType === 'B' ? 'block' : 'page')
} else {
redirectToPage(id)
}
}}
>
<TablerIcon name={iconName} />
<span className="pointer-events-none block-link-reference-row">
{linkType === 'P' ? (
<PageName pageName={id} />
) : (
<>
<Breadcrumb levelLimit={1} blockId={id} endSeparator={showReferenceContent} />
{showReferenceContent && slicedContent}
</>
)}
</span>
</button>
)
}