Skip to content

ISSUE: 复制时换行丢失 #81

Open

Description

@Fimallory

复制时会丢失换行。

Activity

  1. Fimallory commented on May 29, 2025

    @Fimallory
    Author

    #82
    看这个

  2. wuhaoworld commented on May 30, 2025

    @wuhaoworld
    Contributor

    @ItsFimall 没太明白,使用什么模型会遇到上面的情况?或者补充个截图?

  3. Fimallory commented on May 30, 2025

    @Fimallory
    Author
    Image

    这样的

  4. Fimallory commented on May 30, 2025

    @Fimallory
    Author

    我不太会表达,直接 ai 解释吧

    原始代码解释:

    if (Array.isArray(props.item.content) && props.item.content.length > 0) {
      const images = props.item.content.filter((item: any) => item.type === 'image').map((item: any) => item.data);
      setImages(images);
      const plainText = props.item.content.filter((i) => i.type === 'text').map((it) => it.text).join('')
      setPlainText(plainText);
    } else {
      setPlainText(props.item.content as string);
    }
    1. 条件判断:首先检查 props.item.content 是否是一个非空数组(Array.isArray + length > 0)。
      • 如果是:
        • 提取图片:
          • 过滤出 type 为 'image' 的项(filter)。
          • 提取这些项的 data 字段(map)。
          • 将结果存入 images 并调用 setImages 更新状态。
        • 提取纯文本:
          • 过滤出 type 为 'text' 的项(filter)。
          • 提取这些项的 text 字段(map)。
          • 将所有文本拼接成一个字符串(join(''),无分隔符)。
          • 调用 setPlainText 更新状态。
      • 如果不是数组:
        • 直接将 props.item.content 作为字符串传给 setPlainText(假设它是字符串,as string 是类型断言)。

    修改后的代码区别:

    if (Array.isArray(props.item.content) && props.item.content.length > 0) {
      const images = props.item.content.filter((item: any) => item.type === 'image').map((item: any) => item.data);
      setImages(images);
      const plainText = props.item.content.filter((i) => i.type === 'text').map((it) => it.text).join('\n\n') // <- 关键变化
      setPlainText(plainText);
    } else {
      setPlainText(props.item.content as string);
    }

    唯一变化在于 join('\n\n') 替代了 join('')。

    具体区别:

    1. 文本拼接方式:
      • 原代码:直接用空字符串拼接(join('')),导致所有文本块无分隔地连在一起。
        • 例如:["a", "b"] → "ab"。
      • 修改后:用 \n\n(两个换行符)拼接,使每个文本块之间用空行分隔。
        • 例如:["a", "b"] → "a\n\nb",渲染时会显示为:
          a
          
          b
          

    作用:

    • 改善文本的可读性,尤其在渲染多段文本时,空行能更清晰地区分段落。
  5. wuhaoworld commented on Jun 1, 2025

    @wuhaoworld
    Contributor

    我用相同的 prompt 测试了多次,复制出来的都是正常带换行的,详细沟通可以加一下我的微信 wuhaoworld

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions