Skip to content

Commit

Permalink
focus system refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
kholmogorov27 committed Mar 11, 2023
1 parent 20cadd2 commit 628273e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 22 deletions.
4 changes: 1 addition & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ function App() {
const resetStore = useReset()
// ---

const inputRef = useRef(null)

const [showSettings, setShowSettings] = useState(false)
const [showReset, setShowReset] = useState(false)

Expand Down Expand Up @@ -132,7 +130,7 @@ function App() {
animate={{ opacity: 1 }}
exit={redirected || { opacity: 0 }}>
<ActiveElements/>
<QueryField ref={inputRef}/>
<QueryField/>
<LayoutButton
id='settings'
style={{ right: 0, top: 0 }}
Expand Down
4 changes: 1 addition & 3 deletions src/chatGPT/createCompletion.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ const PARAMS = {

function createCompletion(stateSetter, messages, temperature, key, endCallback) {
const controller = new AbortController()

console.log(messages);


fetch(API_URL, {
signal: controller.signal,
method: 'POST',
Expand Down
4 changes: 2 additions & 2 deletions src/components/AIcompletion/AIcompletion.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function AIcompletion({ query, className }) {
return null

return <>
<Icon className={classes['icon']}/>
<div className={className}>
<Icon className={classes['icon']} onClick={e => e.stopPropagation()}/>
<div className={className} onClick={e => e.stopPropagation()}>
<div className={classes['md-container']}>
<ReactMarkdown children={completion}/>
</div>
Expand Down
4 changes: 1 addition & 3 deletions src/components/AIcompletion/AIcompletion.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
margin-right: 1em;
}
.md-container * {
/*
user-select: text !important;
*/
user-select: text !important;
}
.md-container code {
backdrop-filter: contrast(0.8);
Expand Down
38 changes: 27 additions & 11 deletions src/components/QueryField/QueryField.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useCallback, useEffect, memo, forwardRef, useRef } from 'react'
import { useContext, useCallback, useEffect, memo, useRef } from 'react'
import useSuggestions from '../../hooks/useSuggestions'
import useParseQuery from '../../hooks/useParseQuery'
import useRedirect from '../../hooks/useRedirect'
Expand All @@ -15,7 +15,7 @@ import { useState } from 'react'

const DOUBLE_PRESS_THRESHOLD = 300

const QueryField = forwardRef((props, inputRef) => {
function QueryField () {
// settings
const settings = useContext(SettingsContext)

Expand All @@ -24,6 +24,8 @@ const QueryField = forwardRef((props, inputRef) => {
const suggestionsFontSize = settings.query.suggestions.fontSize
const enableCarret = settings.query.field.caret

const inputRef = useRef(null)

/* store */
const mode = useStateSelector(store => store.mode)
const query = useStateSelector(store => store.query)
Expand Down Expand Up @@ -106,24 +108,40 @@ const QueryField = forwardRef((props, inputRef) => {

const spacebarLastPressRef = useRef(-1)
const onKeyPress = useCallback((e) => {
if (e.code === 'Space') {
if (Date.now() - spacebarLastPressRef.current < DOUBLE_PRESS_THRESHOLD)
setAiQuery(query)
if (allowedModes.get('QueryField').has(mode)) {
if (e.code === 'Space') {
if (Date.now() - spacebarLastPressRef.current < DOUBLE_PRESS_THRESHOLD)
setAiQuery(query)

spacebarLastPressRef.current = Date.now()
spacebarLastPressRef.current = Date.now()
}

if (document.activeElement !== inputRef.current)
inputRef.current.focus()
}
}, [query])
}, [query, mode])
// onKeyPress listener
useEffect(() => {
window.addEventListener('keypress', onKeyPress)
return () => window.removeEventListener('keypress', onKeyPress)
}, [onKeyPress])

// focus grabber
useEffect(() => {
const grabFocus = () => {
if (document.activeElement !== inputRef.current)
inputRef.current.focus()
}

document.addEventListener('click', grabFocus)
return () => document.removeEventListener('click', grabFocus)
}, [])

// re-focusing the input inputField to focus on the caret
useEffect(() => {
inputRef.current.blur()
inputRef.current.focus()
}, [inputRef])
}, [])

// css variables
const variables = {
Expand All @@ -134,7 +152,6 @@ const QueryField = forwardRef((props, inputRef) => {
const input = <input
ref={inputRef}
value={parsedQuery.value}
onBlur={e => setTimeout(() => e.target.focus(), 0)}
className={gC(classes['field'], !selectedSuggestion && classes['selected'])}
onChange={e => handleQueryChange(e.target.value)}
style={{
Expand All @@ -157,8 +174,7 @@ const QueryField = forwardRef((props, inputRef) => {
setSelected={suggestion => updateStore({ selectedSuggestion: suggestion })}/> }
</div>
)
})
QueryField.displayName = 'QueryField'
}

function getSuggestion(suggestions, selectedSuggestion, option) {
// current index
Expand Down

0 comments on commit 628273e

Please sign in to comment.