背景
最近看豆包的时候,发现他的代码写AI编程模块 如下
借助的是
contentEditable 来实现的,操作比较友好,可以随意输入可编辑数据,有类似于 placeholder等属性
如果我们开发大模型,免不了东西借鉴一下,下面基于react 来封装 一个 使用contentEditable 来实现的可编辑区域组件
实现重点
- 开启
contentEditable后 如何模拟placeholder - 如何判断何时展示
placeholder何时展示真实内容 - 调整优化以及受控组件封装
代码如下
import React, { useState, useRef, useEffect } from 'react'
interface ContentEditablePromptProps {
placeholder?: string
className?: string
value?: string
onChange?: (value: string) => void
}
const ContentEditablePrompt: React.FC<ContentEditablePromptProps> = ({
placeholder = '请输入',
className = '',
value = '',
onChange
}) => {
const contentEditableRef = useRef<HTMLDivElement>(null)
const [isEmpty, setIsEmpty] = useState(true)
const [internalValue, setInternalValue] = useState(value)
useEffect(() => {
if (contentEditableRef.current) {
contentEditableRef.current.textContent = value
setIsEmpty(value.trim() === '')
}
}, [])
const updateContent = (newValue: string) => {
if (!contentEditableRef.current) return
setInternalValue(newValue)
if (onChange) {
onChange(newValue)
}
const isNowEmpty = newValue.trim() === ''
setIsEmpty(isNowEmpty)
if (isNowEmpty && contentEditableRef.current.innerHTML === '<br>') {
contentEditableRef.current.innerHTML = ''
}
}
const handleInput = () => {
if (!contentEditableRef.current) return
updateContent(contentEditableRef.current.textContent || '')
}
const handleBlur = () => {
if (!contentEditableRef.current) return
const newValue = contentEditableRef.current.textContent || ''
updateContent(newValue)
if (contentEditableRef.current.textContent !== internalValue) {
contentEditableRef.current.textContent = internalValue
}
}
useEffect(() => {
if (!contentEditableRef.current) return
if (value !== internalValue) {
setInternalValue(value)
contentEditableRef.current.textContent = value
setIsEmpty(value.trim() === '')
}
}, [value])
return (
<div
ref={contentEditableRef}
className={`p-2 px-3 w-fit rounded-2xl flex items-center ${
isEmpty
? 'after:content-[attr(data-placeholder)] after:tracking-[1px] bg-[#EEF6FF] font-bold text-[#007DFA] word-spacing-2'
: 'bg-[#EEF6FF] font-bold text-[#007DFA] word-spacing-2'
} ${className}`}
contentEditable
data-placeholder={placeholder}
onInput={handleInput}
onBlur={handleBlur}
suppressContentEditableWarning
/>
)
}
export default ContentEditablePrompt
使用如下:
<EditableArea placeholder='预设提取项' value='' onChange={onChangeValue}></EditableArea>