Skip to content

Commit

Permalink
feat: implement conditional region selection mode
Browse files Browse the repository at this point in the history
  • Loading branch information
heypoom committed Dec 19, 2023
1 parent a416840 commit df3ebe2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
5 changes: 4 additions & 1 deletion canvas/src/blocks/components/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface SettingsConfig {
className?: string
onUpdate?: () => void
children?: ReactNode
footer?: () => ReactNode
}

type Props<T extends BlockTypes, F extends Field<T, BlockKeys<T>>> = {
Expand All @@ -27,7 +28,7 @@ export const Settings = <
>(
props: Props<T, F>,
) => {
const { node, schema, className } = props
const { node, schema, className, footer: Footer } = props
const { id } = node.data

const [inputs, setInputs] = useState<Inputs>({})
Expand Down Expand Up @@ -159,6 +160,8 @@ export const Settings = <
})}

{props.children}

{Footer && <Footer />}
</div>
)
}
Expand Down
26 changes: 14 additions & 12 deletions canvas/src/blocks/machine/components/PaginatedMemoryViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
setMemPage,
} from "@/store/memory"
import { $nodes } from "@/store/nodes"
import { $memoryRegions, updateValueViewers } from "@/store/remote-values"
import {
$memoryRegions,
$selectingRegionViewerId,
updateValueViewers,
} from "@/store/remote-values"
import { createDragAction } from "@/types/drag-action"

interface Props {
Expand Down Expand Up @@ -66,20 +70,18 @@ export const PaginatedMemoryViewer = (props: Props) => {
}, [isEditOffset, memStart, show])

function onConfirm(start: number, end: number) {
const viewer = $nodes
.get()
.find((n) => n.selected && n.type === "ValueView")
const viewerId = $selectingRegionViewerId.get()

// Update remote value viewer if it is selected
// TODO: re-implement this by adding selection states (similar to Google Sheets)
if (viewer) {
// engine.setBlock(viewer.data.id, "ValueView", {
// target: id,
// size: end - start + 1,
// offset: memStart + start,
// })
//
// updateValueViewers()
if (viewerId !== null) {
engine.setBlock(viewerId, "ValueView", {
target: id,
size: end - start + 1,
offset: memStart + start,
})

updateValueViewers()
}
}

Expand Down
2 changes: 1 addition & 1 deletion canvas/src/blocks/osc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const OscBlock = (props: OscProps) => {
targets={1}
sources={1}
schema={schema}
settingsConfig={{ children: settings() }}
settingsConfig={{ footer: settings }}
className="px-4 py-2 font-mono text-center"
>
{getOscLog()}
Expand Down
41 changes: 38 additions & 3 deletions canvas/src/blocks/value-view/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { Icon } from "@iconify/react"
import { useStore } from "@nanostores/react"
import { Tooltip } from "@radix-ui/themes"
import cn from "classnames"
import { memo } from "react"

import { BaseBlock, createSchema } from "@/blocks"
import { ValueRenderer } from "@/blocks/value-view/components/ValueRenderer"
import { $remoteValues, updateValueViewers } from "@/store/remote-values"
import {
$remoteValues,
$selectingRegionViewerId,
updateValueViewers,
} from "@/store/remote-values"
import { BlockPropsOf } from "@/types/Node"

type Props = BlockPropsOf<"ValueView">
Expand All @@ -16,12 +23,40 @@ export const ValueViewBlock = memo((props: Props) => {
const valueMap = useStore($remoteValues)
const values = valueMap[id] ?? []

const selectingViewerId = useStore($selectingRegionViewerId)

const footer = () => {
return (
<div className="flex items-center justify-end w-full">
<Tooltip content="select row regions">
<Icon
icon="material-symbols:select-all"
className={cn(
"text-2 text-white cursor-pointer hover:text-pink-9",
selectingViewerId === id && "text-pink-11",
)}
onClick={() => {
$selectingRegionViewerId.set(selectingViewerId === id ? null : id)
}}
/>
</Tooltip>
</div>
)
}

return (
<BaseBlock
node={props}
className="relative font-mono"
className={cn(
"relative font-mono",
id === selectingViewerId && "!border-pink-10",
)}
schema={schema}
settingsConfig={{ onUpdate: updateValueViewers, className: "px-3 pb-2" }}
settingsConfig={{
onUpdate: updateValueViewers,
className: "px-3 pb-2",
footer,
}}
>
<ValueRenderer {...{ values, visual, target, offset }} />

Expand Down
5 changes: 4 additions & 1 deletion canvas/src/store/remote-values.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { computed, map } from "nanostores"
import { atom, computed, map } from "nanostores"

import { isBlock } from "@/blocks"
import { engine } from "@/engine"
Expand Down Expand Up @@ -37,3 +37,6 @@ export const $memoryRegions = computed($nodes, (nodes) => {

return viewers
})

// Which memory viewer block is in the region selection mode?
export const $selectingRegionViewerId = atom<number | null>(null)

0 comments on commit df3ebe2

Please sign in to comment.