Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

Fix autocomplete vertical offset; Add autocomplete match highlight; Improve autocomplete UI style [WIP]

Tomas Valentaca8927bb80b1
diff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/HighlightedText.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/HighlightedText.tsx
new file mode 100644
index 000000000..7273f29bd
--- /dev/null
+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/HighlightedText.tsx
@@ -0,0 +1,31 @@
+import React from 'react'
+import Fuse from 'fuse.js'
+
+export interface Props {
+ text: string
+ ranges?: ReadonlyArray<Fuse.RangeTuple>
+}
+
+function HighlightedText({ text, ranges }: Props) {
+ const indices = ranges?.reduce<number[]>((acc, [start, end]) => {
+ for (let i = start; i <= end; i++) {
+ acc.push(i)
+ }
+ return acc
+ }, [])
+
+ return (
+ <div>
+ {text.split('').map((char, index) => (
+ <span
+ key={index}
+ className={indices?.includes(index) ? 'match' : ''}
+ >
+ {char}
+ </span>
+ ))}
+ </div>
+ )
+}
+
+export default HighlightedText
diff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx
index 13513f7f9..e78729b69 100644
--- a/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx
+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/Item.tsx
@@ -3,13 +3,13 @@ import cn from 'clsx'
import { Package, Server } from 'lucide-react'
import useElement from 'hooks/useElement'
-import Text from 'components/Text'
-import { Reference, ReferenceType } from 'editor/referenceType'
+import { Reference, ResultType, ReferenceType } from 'editor/referenceType'
+import HighlightedText from './HighlightedText'
export interface Props {
selectItem: () => void
isSelected: boolean
- item: Reference,
+ result: ResultType
}
function getReferenceIcon(reference: Reference) {
@@ -26,7 +26,7 @@ function getReferenceIcon(reference: Reference) {
function Item({
selectItem,
isSelected,
- item,
+ result,
}: Props) {
const [scrollEl, setScrollRef] = useElement<HTMLDivElement>()
@@ -47,23 +47,25 @@ function Item({
'space-x-2',
'shrink-0',
'flex',
+ 'text-sm',
'font-mono',
'items-center',
'group',
- 'hover:text-slate-800',
+ 'hover:bg-indigo-100',
'overflow-ellipsis',
'overflow-hidden',
+ 'text-slate-600',
'cursor-pointer',
{
- 'text-slate-500': !isSelected,
- 'bg-indigo-100 text-slate-800': isSelected,
+ 'bg-indigo-100': isSelected,
},
)}
onMouseDown={selectItem}
>
- {getReferenceIcon(item)}
- <Text
- text={item.value}
+ {getReferenceIcon(result.item)}
+ <HighlightedText
+ text={result.item.value}
+ ranges={result.matches?.flatMap(i => i.indices)}
/>
</div >
)
diff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx
index a8776bf30..f449dcbe3 100644
--- a/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx
+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper.tsx
@@ -11,14 +11,14 @@ import {
SuggestionProps,
SuggestionKeyDownProps,
} from 'editor/extensions/autocomplete/suggestion'
-import { Reference } from 'editor/referenceType'
+import { ResultType } from 'editor/referenceType'
type AutocompleteListRef = Partial<HTMLDivElement> & { onKeyDown: (props: SuggestionKeyDownProps) => boolean }
-export interface AutocompleteListProps extends SuggestionProps<Reference> { }
+export interface AutocompleteListProps extends SuggestionProps<ResultType> { }
export type AutocompleteList = ForwardRefExoticComponent<AutocompleteListProps & RefAttributes<AutocompleteListRef>>
-export interface AutocompleteListWrapperProps extends SuggestionProps<Reference> {
+export interface AutocompleteListWrapperProps extends SuggestionProps<ResultType> {
list: AutocompleteList
}
diff --git a/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsx b/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsx
index a2d453e36..df9016335 100644
--- a/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsx
+++ b/components/Editor/RouteEditor/PromptEditor/Autocomplete/index.tsx
@@ -57,16 +57,16 @@ const Autocomplete: AutocompleteList = forwardRef(({
overflow-y-auto
overscroll-none
rounded
+ border-slate-200
border
- border-slate-300
bg-slate-50
shadow-md
"
>
{items.map((item, index) => (
<Item
- key={item.value}
- item={item}
+ key={item.refIndex}
+ result={item}
isSelected={index === selectedIndex}
selectItem={() => selectItem(index)}
/>
diff --git a/components/Editor/RouteEditor/PromptEditor/index.tsx b/components/Editor/RouteEditor/PromptEditor/index.tsx
index 580c705a9..505352c61 100644
--- a/components/Editor/RouteEditor/PromptEditor/index.tsx
+++ b/components/Editor/RouteEditor/PromptEditor/index.tsx
@@ -50,7 +50,6 @@ function PromptEditor({
className="
flex-1
flex
- max-w-[65ch]
flex-col
"
/>
diff --git a/editor/extensions/autocomplete/index.ts b/editor/extensions/autocomplete/index.ts
index b265b5eba..3b7c5d4cd 100644
--- a/editor/extensions/autocomplete/index.ts
+++ b/editor/extensions/autocomplete/index.ts
@@ -8,7 +8,7 @@ import tippy, {
Props,
} from 'tippy.js'
-import { destroyOnBackspace, destroyOnEsc } from 'editor/tippyPlugins'
+import { destroyOnEsc } from 'editor/tippyPlugins'
import AutocompleteListWrapper, { AutocompleteList } from 'components/Editor/RouteEditor/PromptEditor/Autocomplete/ListWrapper'
import Autocomplete from 'components/Editor/RouteEditor/PromptEditor/Autocomplete'
@@ -30,6 +30,7 @@ function getReferenceClientRect(props: SuggestionProps): () => DOMRect {
const rect = range.getBoundingClientRect()
rect.x = coords.left
+ rect.y = coords.top - rect.height + coords.bottom - coords.top
return rect
}
@@ -73,10 +74,10 @@ const autocomplete = Node.create<AutocompleteOptions>({
showOnCreate: true,
interactive: true,
hideOnClick: true,
- delay: 160,
+ delay: 150,
maxWidth: 'none',
trigger: 'manual',
- offset: [1, 2],
+ offset: [0, 1],
animation: false,
placement: 'bottom-start',
onHide() {
@@ -87,7 +88,7 @@ const autocomplete = Node.create<AutocompleteOptions>({
disabled = true
reactRenderer.destroy()
},
- plugins: [destroyOnEsc, destroyOnBackspace],
+ plugins: [destroyOnEsc],
})
},
onUpdate(props) {
diff --git a/editor/extensions/autocomplete/suggestion.ts b/editor/extensions/autocomplete/suggestion.ts
index d6ec40454..b5d890f4a 100644
--- a/editor/extensions/autocomplete/suggestion.ts
+++ b/editor/extensions/autocomplete/suggestion.ts
@@ -2,9 +2,11 @@ import { Editor, Range } from '@tiptap/core'
import { Plugin, PluginKey } from 'prosemirror-state'
import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'
-import type { Reference } from 'editor/referenceType'
+import type { ResultType } from 'editor/referenceType'
-export interface SuggestionOptions<Item = Reference> {
+
+
+export interface SuggestionOptions<Item = ResultType> {
editor: Editor,
char?: string,
startOfLine?: boolean,
@@ -28,7 +30,7 @@ export interface SuggestionOptions<Item = Reference> {
}) => boolean,
}
-export interface SuggestionProps<Item = Reference> {
+export interface SuggestionProps<Item = ResultType> {
editor: Editor
range: Range
query: string
@@ -57,7 +59,7 @@ interface SuggestionPluginState {
wasCharTyped: boolean
}
-export function Suggestion<Item = Reference>({
+export function Suggestion<Item = ResultType>({
editor,
char = '/',
startOfLine = false,
diff --git a/editor/extensions/reference.ts b/editor/extensions/reference.ts
index 3a8479931..5a89e7601 100644
--- a/editor/extensions/reference.ts
+++ b/editor/extensions/reference.ts
@@ -57,8 +57,8 @@ export default Node.create<ReferenceOptions>({
{
type: this.name,
attrs: {
- [REFERENCE_VALUE_ATTRIBUTE_NAME]: props.value,
- [REFERENCE_TYPE_ATTRIBUTE_NAME]: props.type,
+ [REFERENCE_VALUE_ATTRIBUTE_NAME]: props.item.value,
+ [REFERENCE_TYPE_ATTRIBUTE_NAME]: props.item.type,
},
},
{
diff --git a/editor/referenceType.ts b/editor/referenceType.ts
index 1eda7fbdf..d68ef5824 100644
--- a/editor/referenceType.ts
+++ b/editor/referenceType.ts
@@ -1,3 +1,5 @@
+import Fuse from 'fuse.js'
+
export enum ReferenceType {
NPMPackage = 'NPM_PACKAGE',
DEPLOYMENT = 'DEPLOYMENT_SERVICE',
@@ -7,3 +9,5 @@ export interface Reference {
type: ReferenceType
value: string
}
+
+export type ResultType = Fuse.FuseResult<Reference>
diff --git a/hooks/useDocEditor.ts b/hooks/useDocEditor.ts
index 214cd9af6..c19475078 100644
--- a/hooks/useDocEditor.ts
+++ b/hooks/useDocEditor.ts
@@ -32,7 +32,7 @@ function useDocEditor({
...extensions,
AutocompleteExtension.configure({
suggestion: {
- items: query => referenceSearch.search(query).map(r => r.item),
+ items: query => referenceSearch.search(query),
},
}),
Placeholder.configure({
diff --git a/hooks/useReferences.ts b/hooks/useReferences.ts
index a8ebac280..3e75608ce 100644
--- a/hooks/useReferences.ts
+++ b/hooks/useReferences.ts
@@ -17,7 +17,12 @@ function useReferences(): [Fuse<Reference>, Reference[]] {
},
], [])
- const engine = useMemo(() => new Fuse(references, { keys: ['value'], threshold: 0.3 }), [references])
+ const engine = useMemo(() => new Fuse(references, {
+ keys: ['value'],
+ threshold: 0.3,
+ includeMatches: true,
+ useExtendedSearch: true,
+ }), [references])
return [engine, references]
}
diff --git a/styles/global.css b/styles/global.css
index 1f56f682c..0d79cb039 100644
--- a/styles/global.css
+++ b/styles/global.css
@@ -45,6 +45,15 @@
@apply rounded;
color-scheme: dark;
}
+
+ .match {
+ @apply text-indigo-400 font-bold;
+ }
+
+ .reference {
+ @apply text-slate-600 font-mono font-semibold border-b border-slate-600;
+ box-decoration-break: clone;
+ }
}
.no-scroller::-webkit-scrollbar {
diff --git a/styles/prosemirror.css b/styles/prosemirror.css
index d3458ecfd..d9facac17 100644
--- a/styles/prosemirror.css
+++ b/styles/prosemirror.css
@@ -1,5 +1,5 @@
.ProseMirror {
- @apply prose prose-base text-slate-600 whitespace-pre-wrap;
+ @apply prose prose-base text-slate-600 break-words whitespace-pre-wrap;
/*
* NOTE: Disabling overflow anchor fixes a code cell scrolling bug
* that happens in Chrome:
@@ -23,7 +23,7 @@
}
.ProseMirror p {
- @apply mx-0 my-0 py-0 leading-6;
+ @apply mx-0 my-0 py-0 leading-6 whitespace-pre-wrap break-words;
}
/* Placeholder (on every new line) */
@@ -37,8 +37,3 @@
visibility: hidden;
}
}
-
-.reference {
- @apply text-slate-600 font-mono font-semibold;
- box-decoration-break: clone;
-}