Implement undo and redo

This commit is contained in:
Misode
2024-10-22 22:55:04 +02:00
parent ea37eb168f
commit c358c871da
5 changed files with 140 additions and 56 deletions

View File

@@ -1,8 +1,9 @@
import type { DocAndNode } from '@spyglassmc/core'
import { JsonFileNode } from '@spyglassmc/json'
import { useErrorBoundary } from 'preact/hooks'
import { useLocale } from '../../contexts/index.js'
import { useDocAndNode } from '../../contexts/Spyglass.jsx'
import { useCallback, useErrorBoundary } from 'preact/hooks'
import { useLocale, useVersion } from '../../contexts/index.js'
import { useDocAndNode, useSpyglass } from '../../contexts/Spyglass.jsx'
import type { Edit } from '../../services/Spyglass.js'
import { McdocRoot } from './McdocRenderer.jsx'
type TreePanelProps = {
@@ -11,6 +12,9 @@ type TreePanelProps = {
}
export function Tree({ docAndNode, onError }: TreePanelProps) {
const { lang } = useLocale()
const { version } = useVersion()
const { spyglass } = useSpyglass()
if (lang === 'none') return <></>
const fileChild = useDocAndNode(docAndNode).node.children[0]
@@ -24,7 +28,11 @@ export function Tree({ docAndNode, onError }: TreePanelProps) {
})
if (error) return <></>
const makeEdits = useCallback((edits: Edit[]) => {
spyglass.applyEdits(version, docAndNode.doc.uri, edits)
}, [spyglass, version, docAndNode])
return <div class="tree node-root" data-cy="tree">
<McdocRoot node={fileChild.children[0]} />
<McdocRoot node={fileChild.children[0]} makeEdits={makeEdits} />
</div>
}