-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditorPreview.tsx
145 lines (128 loc) · 4.35 KB
/
EditorPreview.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
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
// different import path!
import React, { forwardRef, useState } from 'react'
import closeCircleLine from '@iconify/icons-ri/close-circle-line'
import fileList2Line from '@iconify/icons-ri/file-list-2-line'
import { Icon } from '@iconify/react'
import Tippy from '@tippyjs/react/headless'
import { NodeEditorContent, generateTempId, mog, MEXIT_FRONTEND_URL_BASE, getNameFromPath } from '@mexit/core'
import {
EditorPreviewControls,
EditorPreviewEditorWrapper,
EditorPreviewNoteName,
EditorPreviewWrapper
} from '@mexit/shared'
import { Button } from '@mexit/shared'
import { generateEditorPluginsWithComponents } from '../../Editor/plugins'
// import useLoad from '../../../Hooks/useLoad'
// import { useRouting, ROUTE_PATHS, NavigationType } from '../../../Hooks/useRouting'
// import { useTags } from '../../Hooks/useTags'
import { useLinks } from '../../Hooks/useLinks'
import { useContentStore } from '../../Stores/useContentStore'
import { getElementById } from '../../contentScript'
import EditorPreviewRenderer from '../EditorPreviewRenderer'
import components from './Components'
export interface EditorPreviewProps {
nodeid: string
children: React.ReactElement
placement?: string
delay?: number
preview?: boolean
previewRef?: any
content?: NodeEditorContent
allowClosePreview?: boolean
closePreview?: () => void
}
export const LazyTippy = forwardRef(function LT(props: any, ref) {
const [mounted, setMounted] = useState(false)
const lazyPlugin = {
fn: () => ({
onMount: () => {
setMounted(true)
},
onHidden: () => {
setMounted(false)
}
})
}
const computedProps = { ...props }
computedProps.plugins = [lazyPlugin, ...(props.plugins || [])]
if (props.render) {
computedProps.render = (...args) => (mounted ? props.render(...args) : '')
} else {
computedProps.content = mounted ? props.content : ''
}
return <Tippy {...computedProps} ref={ref} />
})
const EditorPreview = ({
nodeid,
placement,
allowClosePreview,
closePreview,
preview,
children,
delay,
content,
...props
}: EditorPreviewProps) => {
const { getILinkFromNodeid } = useLinks()
const getContent = useContentStore((store) => store.getContent)
const nodeContent = getContent(nodeid)
const cc = content ?? (nodeContent && nodeContent.content)
// const { hasTags } = useTags()
// const { loadNode } = useLoad()
// const { goTo } = useRouting()
const ilink = getILinkFromNodeid(nodeid)
const editorId = `__preview__${nodeid}_${generateTempId()}`
const onClickNavigate = (e) => {
e.preventDefault()
mog('OnClickNavigate', { e })
// loadNode(nodeid)
window.open(`${MEXIT_FRONTEND_URL_BASE}/editor/${nodeid}`)
// goTo(ROUTE_PATHS.node, NavigationType.push, nodeid)
}
const plugins = generateEditorPluginsWithComponents(components, { exclude: { dnd: true } })
if (cc) {
return (
<LazyTippy
interactive
delay={delay ?? 250}
interactiveDebounce={100}
placement={placement ?? 'bottom'}
visible={preview}
appendTo={() => getElementById('sputlit-main')}
render={(attrs) => (
<EditorPreviewWrapper className="__editor__preview" tabIndex={-1} {...attrs}>
{(allowClosePreview ||
// TODO: look into adding useTags later
// hasTags(nodeid)
false ||
ilink?.path) && (
<EditorPreviewControls
// hasTags={
// hasTags(nodeid)
// }
>
{ilink?.path && (
<EditorPreviewNoteName onClick={onClickNavigate}>
<Icon icon={ilink?.icon ?? fileList2Line} />
{getNameFromPath(ilink.path)}
</EditorPreviewNoteName>
)}
{/* <TagsRelatedTiny nodeid={nodeid} /> */}
<Button transparent onClick={() => closePreview && closePreview()}>
<Icon icon={closeCircleLine} />
</Button>
</EditorPreviewControls>
)}
<EditorPreviewEditorWrapper>
<EditorPreviewRenderer content={cc} editorId={editorId} plugins={plugins} />
</EditorPreviewEditorWrapper>
</EditorPreviewWrapper>
)}
>
{children}
</LazyTippy>
)
} else return children
}
export default EditorPreview