contentEditable 实现可编辑区域

背景

最近看豆包的时候,发现他的代码写AI编程模块 如下

借助的是contentEditable 来实现的,操作比较友好,可以随意输入可编辑数据,有类似于 placeholder等属性

如果我们开发大模型,免不了东西借鉴一下,下面基于react 来封装 一个 使用contentEditable 来实现的可编辑区域组件

实现重点

  1. 开启 contentEditable 后 如何模拟 placeholder
  2. 如何判断何时展示 placeholder 何时展示真实内容
  3. 调整优化以及受控组件封装

代码如下

js 复制代码
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)

  // 更新内容并处理空状态
  const updateContent = (newValue: string) => {
    if (!contentEditableRef.current) return

    // 更新内部状态
    setInternalValue(newValue)

    // 调用父组件的onChange回调
    if (onChange) {
      onChange(newValue)
    }

    // 更新空状态
    const isNowEmpty = newValue.trim() === ''
    setIsEmpty(isNowEmpty)

    // 处理空状态下的placeholder显示
    if (contentEditableRef.current) {
      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)

    // 确保DOM内容与内部状态一致
    if (contentEditableRef.current.textContent !== internalValue) {
      contentEditableRef.current.textContent = internalValue
    }
  }

  // 初始化内容和响应外部value变化
  useEffect(() => {
    if (!contentEditableRef.current) return

    // 只有当外部value与内部值不同时才更新
    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

使用如下:

js 复制代码
<EditableArea placeholder='预设提取项' value='' onChange={onChangeValue}></EditableArea>

结束

相关推荐
im_AMBER6 分钟前
React 01
前端·javascript·笔记·react.js·前端框架·web
@大迁世界13 分钟前
React 19.2.0 有哪些新变化
前端·javascript·react.js·前端框架·ecmascript
华仔啊1 小时前
用 Vue3 + Canvas 做了个超实用的水印工具,同事都在抢着用
前端·vue.js·canvas
Bacon2 小时前
前端:从0-1实现一个脚手架
前端
Bacon2 小时前
前端项目部署实战 nginx+docker持续集成
前端
beckyye2 小时前
阿里云智能语音简单使用:语音识别
前端·语音识别·录音
东东2332 小时前
前端规范工具之husky与lint-staged
前端·javascript·eslint
jump6802 小时前
手写事件总线、事件总线可能带来的内存泄露问题
前端
岁月宁静2 小时前
在 Vue 3.5 中优雅地集成 wangEditor,并定制“AI 工具”下拉菜单(总结/润色/翻译)
前端·javascript·vue.js
执沐2 小时前
基于HTML 使用星辰拼出爱心,并附带闪烁+流星+点击生成流星
前端·html