-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): add action bar in code browser (#1719)
* feat: action bar mock * rename * [autofix.ci] apply automated fixes * logo-transparent-logo * [autofix.ci] apply automated fixes * reverse * update * emitter event * meta * isChatEnabled * rename * displayName * fix lint * update * delay * [autofix.ci] apply automated fixes * update * Update ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx Co-authored-by: Meng Zhang <[email protected]> * mv event-emttier * renaem * Update ee/tabby-ui/app/files/lib/event-emitter.ts Co-authored-by: Meng Zhang <[email protected]> * Update ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget.tsx Co-authored-by: Meng Zhang <[email protected]> * Update ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget.tsx Co-authored-by: Meng Zhang <[email protected]> * explain --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Meng Zhang <[email protected]>
- Loading branch information
1 parent
e45e509
commit df3fbcb
Showing
11 changed files
with
285 additions
and
45 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget-extension.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import type { EditorState, Extension, Transaction } from '@codemirror/state' | ||
import { StateField } from '@codemirror/state' | ||
import type { Tooltip } from '@codemirror/view' | ||
import { showTooltip } from '@codemirror/view' | ||
import ReactDOM from 'react-dom/client' | ||
|
||
import { ActionBarWidget } from './action-bar-widget' | ||
|
||
let delayTimer: number | ||
|
||
function ActionBarWidgetExtension(): Extension { | ||
return StateField.define<Tooltip | null>({ | ||
create() { | ||
return null | ||
}, | ||
update(value, transaction) { | ||
if (transaction.newSelection.main.empty) { | ||
clearTimeout(delayTimer) | ||
return null | ||
} | ||
if (transaction.selection) { | ||
if (shouldShowActionBarWidget(transaction)) { | ||
const tooltip = createActionBarWidget(transaction.state) | ||
// avoid flickering | ||
// return tooltip?.pos !== value?.pos ? tooltip : value | ||
return tooltip | ||
} | ||
|
||
clearTimeout(delayTimer) | ||
return null | ||
} | ||
return value | ||
}, | ||
provide: field => showTooltip.compute([field], state => state.field(field)) | ||
}) | ||
} | ||
|
||
function createActionBarWidget(state: EditorState): Tooltip { | ||
const { selection } = state | ||
const lineFrom = state.doc.lineAt(selection.main.from) | ||
const lineTo = state.doc.lineAt(selection.main.to) | ||
const isMultiline = lineFrom.number !== lineTo.number | ||
const pos = isMultiline ? lineTo.from : selection.main.from | ||
|
||
return { | ||
pos, | ||
above: false, | ||
strictSide: true, | ||
arrow: false, | ||
create() { | ||
const dom = document.createElement('div') | ||
dom.style.background = 'transparent' | ||
dom.style.border = 'none' | ||
const root = ReactDOM.createRoot(dom) | ||
dom.onclick = e => e.stopImmediatePropagation() | ||
// delay popup | ||
if (delayTimer) clearTimeout(delayTimer) | ||
delayTimer = window.setTimeout(() => { | ||
root.render(<ActionBarWidget />) | ||
}, 1000) | ||
|
||
return { dom } | ||
} | ||
} | ||
} | ||
|
||
function shouldShowActionBarWidget(transaction: Transaction): boolean { | ||
const isTextSelected = | ||
!!transaction.selection && !transaction.selection.main.empty | ||
return isTextSelected && transaction.isUserEvent('select') | ||
} | ||
|
||
export { ActionBarWidgetExtension } |
66 changes: 66 additions & 0 deletions
66
ee/tabby-ui/app/files/components/action-bar-widget/action-bar-widget.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import Image from 'next/image' | ||
import tabbyLogo from '@/assets/tabby.png' | ||
|
||
import { cn } from '@/lib/utils' | ||
import { Button } from '@/components/ui/button' | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger | ||
} from '@/components/ui/dropdown-menu' | ||
import { IconChevronUpDown } from '@/components/ui/icons' | ||
|
||
import { CodeBrowserQuickAction, emitter } from '../../lib/event-emitter' | ||
|
||
interface ActionBarWidgetProps extends React.HTMLAttributes<HTMLDivElement> {} | ||
|
||
export const ActionBarWidget: React.FC<ActionBarWidgetProps> = ({ | ||
className, | ||
...props | ||
}) => { | ||
const handleAction = (action: CodeBrowserQuickAction) => { | ||
emitter.emit('code_browser_quick_action', action) | ||
} | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'mt-2 flex items-center gap-2 rounded-md border bg-background px-2 py-1', | ||
className | ||
)} | ||
{...props} | ||
> | ||
<Image src={tabbyLogo} width={32} alt="logo" /> | ||
<Button | ||
size="sm" | ||
variant="outline" | ||
onClick={e => handleAction('explain')} | ||
> | ||
Explain | ||
</Button> | ||
<DropdownMenu modal={false}> | ||
<DropdownMenuTrigger asChild> | ||
<Button size="sm" variant="outline"> | ||
Generate | ||
<IconChevronUpDown className="ml-1" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="start"> | ||
<DropdownMenuItem | ||
className="cursor-pointer" | ||
onSelect={() => handleAction('generate_unittest')} | ||
> | ||
Unit Test | ||
</DropdownMenuItem> | ||
<DropdownMenuItem | ||
className="cursor-pointer" | ||
onSelect={() => handleAction('generate_doc')} | ||
> | ||
Documentation | ||
</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import mitt from 'mitt' | ||
|
||
type CodeBrowserQuickAction = 'explain' | 'generate_unittest' | 'generate_doc' | ||
|
||
type CodeBrowserQuickActionEvents = { | ||
code_browser_quick_action: CodeBrowserQuickAction | ||
} | ||
|
||
const emitter = mitt<CodeBrowserQuickActionEvents>() | ||
|
||
export type { CodeBrowserQuickAction } | ||
export { emitter } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.