Update mosaic tree to remove and add the console.

This commit is contained in:
Kurt Hutten
2021-10-11 22:23:23 +11:00
parent c6e46d1475
commit 0fda7ebbbb
3 changed files with 80 additions and 16 deletions

View File

@@ -110,7 +110,7 @@ const IdeContainer = () => {
}} }}
value={state.layout} value={state.layout}
onChange={(newLayout) => onChange={(newLayout) =>
thunkDispatch({ type: 'setLayout', payload: { message: newLayout } }) thunkDispatch({ type: 'setLayout', payload: newLayout })
} }
onRelease={handleViewerSizeUpdate} onRelease={handleViewerSizeUpdate}
/> />

View File

@@ -1,7 +1,8 @@
import React, { ReactNode } from 'react' import { ReactNode } from 'react'
import { SvgNames } from 'src/components/Svg/Svg' import { SvgNames } from 'src/components/Svg/Svg'
import { useIdeContext } from 'src/helpers/hooks/useIdeContext' import { useIdeContext } from 'src/helpers/hooks/useIdeContext'
import { createRemoveUpdate, updateTree } from 'react-mosaic-component'
import type { MosaicPath } from 'react-mosaic-component'
interface SidebarConfigType { interface SidebarConfigType {
name: string name: string
icon: SvgNames icon: SvgNames
@@ -9,6 +10,39 @@ interface SidebarConfigType {
panel: ReactNode | null panel: ReactNode | null
} }
interface MosaicTree {
first: string | MosaicTree
second: string | MosaicTree
}
const getPathById = (
tree: MosaicTree,
id: string,
path: MosaicPath = []
): MosaicPath => {
if (tree.first === id || tree.second === id) {
return [...path, tree.first === id ? 'first' : 'second']
}
if (typeof tree.first !== 'string' && typeof tree.second !== 'string') {
throw new Error('id not found')
}
try {
if (typeof tree.first !== 'string') {
return getPathById(tree.first, id, [...path, 'first'])
}
} catch (error) {
throw new Error('id not found')
}
try {
if (typeof tree.second !== 'string') {
return getPathById(tree.second, id, [...path, 'second'])
}
} catch (error) {
throw new Error('id not found')
}
throw new Error('id not found')
}
export const sidebarTopConfig: SidebarConfigType[] = [ export const sidebarTopConfig: SidebarConfigType[] = [
{ {
name: 'Files', name: 'Files',
@@ -107,19 +141,51 @@ const settingsConfig: settingsConfig[] = [
name: 'console', name: 'console',
Content: () => { Content: () => {
const { state, thunkDispatch } = useIdeContext() const { state, thunkDispatch } = useIdeContext()
const consolePath = React.useMemo<MosaicPath | null>(() => {
try {
const path = getPathById(state.layout, 'Console')
return path
} catch (error) {
return null
}
}, [state.layout])
return ( return (
<div className="p-2"> <div className="p-2">
<li className="grid items-center my-2" style={{ gridTemplateColumns: 'auto 4rem' }}> <li
className="grid items-center my-2"
style={{ gridTemplateColumns: 'auto 4rem' }}
>
<div className="text-sm">Visible</div> <div className="text-sm">Visible</div>
<input type="checkbox" <input
type="checkbox"
onChange={(newValue) => { onChange={(newValue) => {
state.consoleVisible = !state.consoleVisible if (consolePath) {
if (state.consoleVisible) const newTree = updateTree(state.layout, [
thunkDispatch({ type: 'resetLayout'}) createRemoveUpdate(state.layout, consolePath),
else ])
thunkDispatch({ type: 'setLayout', payload:{ message:{ direction: 'row', first: 'Editor', second: 'Viewer'}}}) thunkDispatch({ type: 'setLayout', payload: newTree })
}} } else {
checked={state.consoleVisible}/> // Split 'Viewer' panel to add console back in
const viewerPath = getPathById(state.layout, 'Viewer')
const newTree = { ...state.layout }
let temp = newTree
viewerPath.forEach((name) => {
if (newTree[name] === 'Viewer') {
newTree[name] = {
direction: 'column',
first: 'Viewer',
second: 'Console',
splitPercentage: 70,
}
return
}
temp = { ...newTree[name] }
})
thunkDispatch({ type: 'setLayout', payload: newTree })
}
}}
checked={!!consolePath}
/>
</li> </li>
</div> </div>
) )

View File

@@ -57,7 +57,6 @@ export interface State {
isLoading: boolean isLoading: boolean
threeInstance: RootState threeInstance: RootState
sideTray: string[] // could probably be an array of a union type sideTray: string[] // could probably be an array of a union type
consoleVisible: boolean
} }
const code = '' const code = ''
@@ -94,7 +93,6 @@ export const initialState: State = {
isLoading: false, isLoading: false,
threeInstance: null, threeInstance: null,
sideTray: [], sideTray: [],
consoleVisible: true,
} }
const reducer = (state: State, { type, payload }): State => { const reducer = (state: State, { type, payload }): State => {
@@ -170,7 +168,7 @@ const reducer = (state: State, { type, payload }): State => {
case 'setLayout': case 'setLayout':
return { return {
...state, ...state,
layout: payload.message, layout: payload,
} }
case 'updateCamera': case 'updateCamera':
return { return {