fix vertex selection (#869)

This commit is contained in:
Kurt Hutten
2023-10-16 15:29:02 +11:00
committed by GitHub
parent b257b202c3
commit 8fad9ef3c2
5 changed files with 38 additions and 14 deletions

View File

@ -282,7 +282,7 @@ export const ModelingMachineProvider = ({
} }
// This DOES NOT set the `selectionRanges` in xstate context // This DOES NOT set the `selectionRanges` in xstate context
// same as comment above // same as comment above
const selectionRangeTypeMap = dispatchCodeMirrorCursor({ const { selectionRangeTypeMap } = dispatchCodeMirrorCursor({
selections: setSelections.selection, selections: setSelections.selection,
editorView, editorView,
}) })

View File

@ -160,15 +160,24 @@ export const TextEditor = ({
) )
const idBasedSelections = codeBasedSelections const idBasedSelections = codeBasedSelections
.map(({ type, range }) => { .map(({ type, range }) => {
const hasOverlap = Object.entries( // TODO #868: loops over all artifacts will become inefficient at a large scale
const entriesWithOverlap = Object.entries(
engineCommandManager.artifactMap || {} engineCommandManager.artifactMap || {}
).filter(([_, { range: artifactRange }]) => { ).filter(([_, artifact]) => {
return artifactRange && isOverlap(artifactRange, range) return artifact.range && isOverlap(artifact.range, range)
? artifact
: false
}) })
if (hasOverlap.length) { if (entriesWithOverlap.length) {
const [id, artifact] = entriesWithOverlap?.[0]
return { return {
type, type,
id: hasOverlap?.[0]?.[0], id:
type === 'line-end' &&
artifact.type === 'result' &&
artifact.headVertexId
? artifact.headVertexId
: id,
} }
} }
return null return null

View File

@ -3,6 +3,7 @@ import { useStore } from 'useStore'
import { engineCommandManager } from '../lang/std/engineConnection' import { engineCommandManager } from '../lang/std/engineConnection'
import { useModelingContext } from './useModelingContext' import { useModelingContext } from './useModelingContext'
import { v4 as uuidv4 } from 'uuid' import { v4 as uuidv4 } from 'uuid'
import { SourceRange } from 'lang/wasm'
export function useEngineConnectionSubscriptions() { export function useEngineConnectionSubscriptions() {
const { setHighlightRange, highlightRange } = useStore((s) => ({ const { setHighlightRange, highlightRange } = useStore((s) => ({
@ -10,6 +11,12 @@ export function useEngineConnectionSubscriptions() {
highlightRange: s.highlightRange, highlightRange: s.highlightRange,
})) }))
const { send, context } = useModelingContext() const { send, context } = useModelingContext()
interface RangeAndId {
id: string
range: SourceRange
}
useEffect(() => { useEffect(() => {
if (!engineCommandManager) return if (!engineCommandManager) return
@ -62,19 +69,26 @@ export function useEngineConnectionSubscriptions() {
}) })
.then((res) => { .then((res) => {
const curveIds = res?.data?.data?.curve_ids const curveIds = res?.data?.data?.curve_ids
const ranges = curveIds const ranges: RangeAndId[] = curveIds
.map( .map(
(id: string) => engineCommandManager.artifactMap[id]?.range (id: string): RangeAndId => ({
id,
range: engineCommandManager.artifactMap[id].range,
})
) )
.sort((a: [number, number], b: [number, number]) => a[0] - b[0]) .sort((a: RangeAndId, b: RangeAndId) => a.range[0] - b.range[0])
// default to the head of the curve selected // default to the head of the curve selected
const _sourceRange = ranges?.[0] const _sourceRange = ranges?.[0].range
// TODO, we telling the engine that the line is selected, becasue we don't store const artifact = engineCommandManager.artifactMap[ranges?.[0]?.id]
// vertex ranges in the artifact map, needs some thought. if (artifact.type === 'result') {
artifact.headVertexId = data.entity_id
}
send({ send({
type: 'Set selection', type: 'Set selection',
data: { data: {
selectionType: 'singleCodeCursor', selectionType: 'singleCodeCursor',
// line-end is used to indicate that headVertexId should be sent to the engine as "selected"
// not the whole curve
selection: { range: _sourceRange, type: 'line-end' }, selection: { range: _sourceRange, type: 'line-end' },
}, },
}) })

View File

@ -21,6 +21,7 @@ interface ResultCommand extends CommandInfo {
type: 'result' type: 'result'
data: any data: any
raw: WebSocketResponse raw: WebSocketResponse
headVertexId?: string
} }
interface FailedCommand extends CommandInfo { interface FailedCommand extends CommandInfo {
type: 'failed' type: 'failed'

View File

@ -392,7 +392,7 @@ export function setCodeMirrorCursor({
// because we want to respect the user manually placing the cursor too. // because we want to respect the user manually placing the cursor too.
const code = kclManager.code const code = kclManager.code
if (!codeSelection) { if (!codeSelection) {
const selectionRangeTypeMap = dispatchCodeMirrorCursor({ const { selectionRangeTypeMap } = dispatchCodeMirrorCursor({
editorView, editorView,
selections: { selections: {
otherSelections: currestSelections.otherSelections, otherSelections: currestSelections.otherSelections,
@ -412,7 +412,7 @@ export function setCodeMirrorCursor({
? [...currestSelections.codeBasedSelections, codeSelection] ? [...currestSelections.codeBasedSelections, codeSelection]
: [codeSelection], : [codeSelection],
} }
const selectionRangeTypeMap = dispatchCodeMirrorCursor({ const { selectionRangeTypeMap } = dispatchCodeMirrorCursor({
editorView, editorView,
selections, selections,
}) })