轻松实现文本标注:介绍 Annotation-React 库

文本标注功能在教育、数据分析、内容管理等领域中变得越来越重要。为了帮助开发者更轻松地实现这一功能,我写了这个 Annotation-React 库。这个库可以让你在 React 应用中轻松地对文章或段落中的单词或句子进行划线打标。

功能目前较简单,一张gif就能演示完成如下:

快速开始

安装

首先,你需要通过 npm 或 yarn 安装 Annotation-React 库:

bash 复制代码
npm install annotation-react

bash 复制代码
yarn add annotation-react

使用示例

以下是一个简单的使用示例,展示了如何在 React 应用中使用 Annotation-React 库对文本进行标注。

typescript 复制代码
import React from "react";
import { Annotation } from "annotation-react";
import { createRoot } from "react-dom/client";
import "annotation-react/dist/style.css";

createRoot(document.getElementById("app")!).render(
  <Annotation
    onChange={(annos) => {
      console.log({ annos });
    }}
    tags={[{ content: "error" }, { content: "success" }]}
    sentence="A story is a component with a set of arguments that define how the component should render."
  />
);
    
export default App;

参数说明

  • sentence:需要进行标注的文本内容。
  • onChange:标注完成后的回调函数,返回当前所有标注结果。
  • tags:自定义打标内容。

更多参数

typescript 复制代码
export interface PAnnotation {
    /** content to be showed */
    sentence?: string;
    tags?: TTag[];
    /**the annotations that already exist */
    annoations?: TAnnoDetail[];
    /**the fontSize of main content,now only support number, will use px unit */
    fontSize?: number;
    /** Ensure that there is enough space between the rows to fit a tag */
    lineHeight?: string;
    /** the color of marked text by annotation */
    hightlightColor?: string;
    hightlightBgColor?: string;
    /** can not mark */
    readonly?: boolean;
    /** notify when annotations add or remove */
    onChange?: (annoations: TAnnoDetail[]) => void;
}

目前项目刚发布第一个版本,有不足的地方希望大家多提pr

项目git地址

相关推荐
阿阳微客20 分钟前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro1 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom1 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...2 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡3 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜054 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx4 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9994 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
HarderCoder5 小时前
学习React的一些知识
react.js
后海 0_o5 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构