set cursor on geo click
This commit is contained in:
@ -13,6 +13,7 @@ import { isOverlapping } from '../lib/utils'
|
|||||||
import { LineGeos } from '../lang/engine'
|
import { LineGeos } from '../lang/engine'
|
||||||
import { Vector3, DoubleSide, Quaternion, Vector2 } from 'three'
|
import { Vector3, DoubleSide, Quaternion, Vector2 } from 'three'
|
||||||
import { combineTransformsAlt } from '../lang/sketch'
|
import { combineTransformsAlt } from '../lang/sketch'
|
||||||
|
import { useSetCursor } from '../hooks/useSetCursor'
|
||||||
|
|
||||||
function SketchLine({
|
function SketchLine({
|
||||||
geo,
|
geo,
|
||||||
@ -23,15 +24,10 @@ function SketchLine({
|
|||||||
sourceRange: [number, number]
|
sourceRange: [number, number]
|
||||||
forceHighlight?: boolean
|
forceHighlight?: boolean
|
||||||
}) {
|
}) {
|
||||||
const { setHighlightRange } = useStore(
|
const { setHighlightRange } = useStore(({ setHighlightRange }) => ({
|
||||||
({ setHighlightRange, selectionRange, guiMode, setGuiMode, ast }) => ({
|
setHighlightRange,
|
||||||
setHighlightRange,
|
}))
|
||||||
selectionRange,
|
const onClick = useSetCursor(sourceRange)
|
||||||
guiMode,
|
|
||||||
setGuiMode,
|
|
||||||
ast,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
// This reference will give us direct access to the mesh
|
// This reference will give us direct access to the mesh
|
||||||
const ref = useRef<BufferGeometry | undefined>() as any
|
const ref = useRef<BufferGeometry | undefined>() as any
|
||||||
const [hovered, setHover] = useState(false)
|
const [hovered, setHover] = useState(false)
|
||||||
@ -48,6 +44,7 @@ function SketchLine({
|
|||||||
setHover(false)
|
setHover(false)
|
||||||
setHighlightRange([0, 0])
|
setHighlightRange([0, 0])
|
||||||
}}
|
}}
|
||||||
|
onClick={onClick}
|
||||||
>
|
>
|
||||||
<primitive object={geo.line} />
|
<primitive object={geo.line} />
|
||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
@ -81,6 +78,7 @@ function ExtrudeWall({
|
|||||||
ast,
|
ast,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
const onClick = useSetCursor(sourceRange)
|
||||||
// This reference will give us direct access to the mesh
|
// This reference will give us direct access to the mesh
|
||||||
const ref = useRef<BufferGeometry | undefined>() as any
|
const ref = useRef<BufferGeometry | undefined>() as any
|
||||||
const [hovered, setHover] = useState(false)
|
const [hovered, setHover] = useState(false)
|
||||||
@ -97,6 +95,7 @@ function ExtrudeWall({
|
|||||||
setHover(false)
|
setHover(false)
|
||||||
setHighlightRange([0, 0])
|
setHighlightRange([0, 0])
|
||||||
}}
|
}}
|
||||||
|
onClick={onClick}
|
||||||
>
|
>
|
||||||
<primitive object={geo} />
|
<primitive object={geo} />
|
||||||
<meshStandardMaterial
|
<meshStandardMaterial
|
||||||
|
12
src/hooks/useSetCursor.ts
Normal file
12
src/hooks/useSetCursor.ts
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import { useStore } from '../useStore'
|
||||||
|
|
||||||
|
export function useSetCursor(sourceRange: [number, number]) {
|
||||||
|
const setCursor = useStore((state) => state.setCursor)
|
||||||
|
return () => {
|
||||||
|
setCursor(sourceRange[1])
|
||||||
|
const element: HTMLDivElement | null = document.querySelector('.cm-content')
|
||||||
|
if (element) {
|
||||||
|
element.focus()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -46,6 +46,7 @@ interface StoreState {
|
|||||||
setEditorView: (editorView: EditorView) => void
|
setEditorView: (editorView: EditorView) => void
|
||||||
highlightRange: [number, number]
|
highlightRange: [number, number]
|
||||||
setHighlightRange: (range: Range) => void
|
setHighlightRange: (range: Range) => void
|
||||||
|
setCursor: (cursor: number) => void
|
||||||
selectionRange: [number, number]
|
selectionRange: [number, number]
|
||||||
setSelectionRange: (range: Range) => void
|
setSelectionRange: (range: Range) => void
|
||||||
guiMode: GuiModes
|
guiMode: GuiModes
|
||||||
@ -83,6 +84,13 @@ export const useStore = create<StoreState>()((set, get) => ({
|
|||||||
editorView.dispatch({ effects: addLineHighlight.of(highlightRange) })
|
editorView.dispatch({ effects: addLineHighlight.of(highlightRange) })
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
setCursor: (cursor: number) => {
|
||||||
|
const editorView = get().editorView
|
||||||
|
if (!editorView) return
|
||||||
|
editorView.dispatch({
|
||||||
|
selection: { anchor: cursor, head: cursor },
|
||||||
|
})
|
||||||
|
},
|
||||||
selectionRange: [0, 0],
|
selectionRange: [0, 0],
|
||||||
setSelectionRange: (selectionRange) => {
|
setSelectionRange: (selectionRange) => {
|
||||||
set({ selectionRange })
|
set({ selectionRange })
|
||||||
|
Reference in New Issue
Block a user