feat: advanced prompt (#1330)

Co-authored-by: Joel <iamjoel007@gmail.com>
Co-authored-by: JzoNg <jzongcode@gmail.com>
Co-authored-by: Gillian97 <jinling.sunshine@gmail.com>
This commit is contained in:
zxhlyh
2023-10-12 23:14:28 +08:00
committed by GitHub
parent 42a5b3ec17
commit 5b9858a8a3
131 changed files with 5944 additions and 451 deletions

View File

@@ -0,0 +1,33 @@
import type { FC } from 'react'
import { useTranslation } from 'react-i18next'
import { useSelectOrDelete } from '../../hooks'
import { DELETE_QUERY_BLOCK_COMMAND } from './index'
import { UserEdit02 } from '@/app/components/base/icons/src/vender/solid/users'
type QueryBlockComponentProps = {
nodeKey: string
}
const QueryBlockComponent: FC<QueryBlockComponentProps> = ({
nodeKey,
}) => {
const { t } = useTranslation()
const [ref, isSelected] = useSelectOrDelete(nodeKey, DELETE_QUERY_BLOCK_COMMAND)
return (
<div
className={`
inline-flex items-center pl-1 pr-0.5 h-6 bg-[#FFF6ED] border border-transparent rounded-[5px] hover:bg-[#FFEAD5]
${isSelected && '!border-[#FD853A]'}
`}
ref={ref}
>
<UserEdit02 className='mr-1 w-[14px] h-[14px] text-[#FD853A]' />
<div className='text-xs font-medium text-[#EC4A0A] opacity-60'>{'{{'}</div>
<div className='text-xs font-medium text-[#EC4A0A]'>{t('common.promptEditor.query.item.title')}</div>
<div className='text-xs font-medium text-[#EC4A0A] opacity-60'>{'}}'}</div>
</div>
)
}
export default QueryBlockComponent

View File

@@ -0,0 +1,62 @@
import type { FC } from 'react'
import { useEffect } from 'react'
import {
$insertNodes,
COMMAND_PRIORITY_EDITOR,
createCommand,
} from 'lexical'
import { mergeRegister } from '@lexical/utils'
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext'
import {
$createQueryBlockNode,
QueryBlockNode,
} from './node'
export const INSERT_QUERY_BLOCK_COMMAND = createCommand('INSERT_QUERY_BLOCK_COMMAND')
export const DELETE_QUERY_BLOCK_COMMAND = createCommand('DELETE_QUERY_BLOCK_COMMAND')
export type QueryBlockProps = {
onInsert?: () => void
onDelete?: () => void
}
const QueryBlock: FC<QueryBlockProps> = ({
onInsert,
onDelete,
}) => {
const [editor] = useLexicalComposerContext()
useEffect(() => {
if (!editor.hasNodes([QueryBlockNode]))
throw new Error('QueryBlockPlugin: QueryBlock not registered on editor')
return mergeRegister(
editor.registerCommand(
INSERT_QUERY_BLOCK_COMMAND,
() => {
const contextBlockNode = $createQueryBlockNode()
$insertNodes([contextBlockNode])
if (onInsert)
onInsert()
return true
},
COMMAND_PRIORITY_EDITOR,
),
editor.registerCommand(
DELETE_QUERY_BLOCK_COMMAND,
() => {
if (onDelete)
onDelete()
return true
},
COMMAND_PRIORITY_EDITOR,
),
)
}, [editor, onInsert, onDelete])
return null
}
export default QueryBlock

View File

@@ -0,0 +1,59 @@
import type { LexicalNode, SerializedLexicalNode } from 'lexical'
import { DecoratorNode } from 'lexical'
import QueryBlockComponent from './component'
export type SerializedNode = SerializedLexicalNode
export class QueryBlockNode extends DecoratorNode<JSX.Element> {
static getType(): string {
return 'query-block'
}
static clone(): QueryBlockNode {
return new QueryBlockNode()
}
isInline(): boolean {
return true
}
createDOM(): HTMLElement {
const div = document.createElement('div')
div.classList.add('inline-flex', 'items-center', 'align-middle')
return div
}
updateDOM(): false {
return false
}
decorate(): JSX.Element {
return <QueryBlockComponent nodeKey={this.getKey()} />
}
static importJSON(): QueryBlockNode {
const node = $createQueryBlockNode()
return node
}
exportJSON(): SerializedNode {
return {
type: 'query-block',
version: 1,
}
}
getTextContent(): string {
return '{{#query#}}'
}
}
export function $createQueryBlockNode(): QueryBlockNode {
return new QueryBlockNode()
}
export function $isQueryBlockNode(
node: QueryBlockNode | LexicalNode | null | undefined,
): node is QueryBlockNode {
return node instanceof QueryBlockNode
}