feat(editor): the canvas draws the view the Rust core composed; denylist and anatomy dependency removed
This commit is contained in:
1 parent
43a370aed1
commit
09a6cf7def
11 files changed
+165
-158
No files matched your search
@@ -1,8 +1,11 @@
|
||||
import { useCallback, useEffect, useLayoutEffect, useRef } from 'react'
|
||||
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import { buildViewSvg, type ViewStates } from '@/anatomy/drawing'
|
||||
import type { SheetIndex } from '@/services/sheetIndex'
|
||||
import { useLocaleStore } from '@/state/localeStore'
|
||||
import { pickLocaleLabel } from '@/i18n/pickLocaleLabel'
|
||||
import Button from '@/components/ui/Button'
|
||||
import Notice from '@/components/ui/Notice'
|
||||
import { useT } from '@/i18n/useT'
|
||||
|
||||
export interface AnatomyCanvasProps {
|
||||
index: SheetIndex
|
||||
@@ -20,9 +23,11 @@ function annotatedTarget(event: Event): Element | null {
|
||||
}
|
||||
|
||||
/**
|
||||
* One view of the sheet: the drawing plus the annotation layer generated
|
||||
* from the document, rebuilt when the view changes and restyled in place
|
||||
* when the selection changes. Clicks are delegated on `data-anno-id`.
|
||||
* One view of the sheet: the drawing plus the annotation layer, composed by
|
||||
* the core (R3), rebuilt when the view changes and restyled in place when the
|
||||
* selection changes (the restyle runs right after every build). Clicks are
|
||||
* delegated on `data-anno-id`. When the core cannot compose the view, the
|
||||
* canvas shows the degraded notice with a retry.
|
||||
*/
|
||||
export default function AnatomyCanvas({
|
||||
index,
|
||||
@@ -35,17 +40,18 @@ export default function AnatomyCanvas({
|
||||
const svgRef = useRef<HTMLDivElement | null>(null)
|
||||
const tooltipRef = useRef<HTMLDivElement | null>(null)
|
||||
const locale = useLocaleStore((s) => s.locale)
|
||||
// Latest states for the build effect, which must only re-run on a view change.
|
||||
const statesRef = useRef(states)
|
||||
useLayoutEffect(() => {
|
||||
statesRef.current = states
|
||||
}, [states])
|
||||
const t = useT()
|
||||
// Each retry builds again; a failed build leaves the canvas empty behind the notice.
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
const [failed, setFailed] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const host = svgRef.current
|
||||
if (!host) return
|
||||
host.replaceChildren(buildViewSvg(index, viewId, statesRef.current))
|
||||
}, [index, viewId])
|
||||
const svg = buildViewSvg(index, viewId)
|
||||
setFailed(svg === null)
|
||||
host.replaceChildren(...(svg ? [svg] : []))
|
||||
}, [index, viewId, attempt])
|
||||
|
||||
useEffect(() => {
|
||||
const host = svgRef.current
|
||||
@@ -55,7 +61,7 @@ export default function AnatomyCanvas({
|
||||
if (state) el.setAttribute('data-state', state)
|
||||
else el.removeAttribute('data-state')
|
||||
})
|
||||
}, [states, index, viewId])
|
||||
}, [states, index, viewId, attempt])
|
||||
|
||||
useEffect(() => {
|
||||
const host = svgRef.current
|
||||
@@ -123,6 +129,16 @@ export default function AnatomyCanvas({
|
||||
className="w-full h-full [&>svg]:max-w-full [&>svg]:max-h-full [&>svg]:mx-auto"
|
||||
/>
|
||||
<div ref={tooltipRef} className="zone-tooltip" style={{ opacity: 0 }} />
|
||||
{failed && (
|
||||
<div className="absolute inset-0 grid place-items-center p-4">
|
||||
<div className="space-y-3 max-w-sm">
|
||||
<Notice tone="error">{t('sheet.core_unavailable')}</Notice>
|
||||
<Button onClick={() => setAttempt((n) => n + 1)}>
|
||||
{t('sheet.core_retry')}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user