mirror of
https://github.com/logseq/logseq.git
synced 2026-04-30 00:46:23 +00:00
feat: add dev tools for renderer
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import { useRendererContext } from '@tldraw/react'
|
||||
import { observer } from 'mobx-react-lite'
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
|
||||
const printPoint = (point: number[]) => {
|
||||
return `[${point.map(d => d.toFixed(2)).join(', ')}]`
|
||||
}
|
||||
|
||||
export const DevTools = observer(() => {
|
||||
const {
|
||||
viewport: {
|
||||
camera: { point, zoom },
|
||||
},
|
||||
inputs,
|
||||
} = useRendererContext()
|
||||
|
||||
const canvasAnchorRef = React.useRef<HTMLElement | null>()
|
||||
const statusbarAnchorRef = React.useRef<HTMLElement | null>()
|
||||
|
||||
React.useEffect(() => {
|
||||
const canvasAnchor = document.getElementById('tl-dev-tools-canvas-anchor')
|
||||
canvasAnchorRef.current = canvasAnchor
|
||||
|
||||
const statusbarAnchor = document.getElementById('tl-statusbar-anchor')
|
||||
statusbarAnchorRef.current = statusbarAnchor
|
||||
}, [])
|
||||
|
||||
const originPoint = canvasAnchorRef.current
|
||||
? ReactDOM.createPortal(
|
||||
<svg className="tl-renderer-dev-tools tl-grid">
|
||||
<circle cx={point[0] * zoom} cy={point[1] * zoom} r="4" fill="red" />
|
||||
</svg>,
|
||||
canvasAnchorRef.current
|
||||
)
|
||||
: null
|
||||
|
||||
const rendererStatusText = [
|
||||
['Z', zoom.toFixed(2)],
|
||||
['MP', printPoint(inputs.currentPoint)],
|
||||
['MS', printPoint(inputs.currentScreenPoint)],
|
||||
['VP', printPoint(point)],
|
||||
]
|
||||
.map(p => p.join(''))
|
||||
.join('|')
|
||||
|
||||
const rendererStatus = statusbarAnchorRef.current
|
||||
? ReactDOM.createPortal(
|
||||
<div
|
||||
style={{
|
||||
flex: 1,
|
||||
}}
|
||||
>
|
||||
{rendererStatusText}
|
||||
</div>,
|
||||
statusbarAnchorRef.current
|
||||
)
|
||||
: null
|
||||
|
||||
return (
|
||||
<>
|
||||
{originPoint}
|
||||
{rendererStatus}
|
||||
</>
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1 @@
|
||||
export * from './Devtools'
|
||||
Reference in New Issue
Block a user