rich textarea
0.26.4
一个小的可自定义文本方面的反应,以使其着色,突出显示,装饰文本,提供自动完成等等。
笔记
您也可能喜欢https://github.com/inokawa/edix






https://inokawa.github.io/rich-textarea/
有时我们需要Web中的自定义文本编辑器。但是,用原始的满足来创建它是如此的正确做到,并且编辑框架通常太重了……也许您真的需要一个文本方面,带有突出显示和一些盘旋的菜单,但是本机Textarea和许多Textarea库远非如此,因为有限的自定义性。该库的目的是解决问题。
npm install rich-textarea如果您使用ESM和WebPack 5,请使用React> = 18来避免无法解析react/jsx-runtime错误。
import { useState } from "react" ;
import { RichTextarea } from "rich-textarea" ;
export const App = ( ) => {
const [ text , setText ] = useState ( "Lorem ipsum" ) ;
return (
< RichTextarea
value = { text }
style = { { width : "600px" , height : "400px" } }
onChange = { ( e ) => setText ( e . target . value ) }
>
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ; import { RichTextarea } from "rich-textarea" ;
export const App = ( ) => {
return (
< RichTextarea
defaultValue = "Lorem ipsum"
style = { { width : "600px" , height : "400px" } }
>
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;如果您不想创建自己的渲染功能,则可以将助手用于正则。
import { useState } from "react" ;
import { RichTextarea , createRegexRenderer } from "rich-textarea" ;
const renderer = createRegexRenderer ( [
[ / [A-Z][a-z]+ / g , { borderRadius : "3px" , backgroundColor : "#d0bfff" } ] ,
] ) ;
export const App = ( ) => {
const [ text , setText ] = useState ( "Lorem ipsum" ) ;
return (
< RichTextarea
value = { text }
style = { { width : "600px" , height : "400px" } }
onChange = { ( e ) => setText ( e . target . value ) }
>
{ renderer }
</ RichTextarea >
) ;
} ; // _components/Textarea.tsx
"use client" ;
import { RichTextarea , RichTextareaProps } from "rich-textarea" ;
export const Textarea = (
props : Omit < RichTextareaProps , "children" | "ref" >
) => {
return (
< RichTextarea { ... props } >
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;
// page.tsx in App Router of Next.js
import { Textarea } from "../_components/Textarea" ;
async function hello ( formData : FormData ) {
"use server" ;
console . log ( formData . get ( "hello" ) ) ;
}
export default ( ) => {
return (
< div >
< div > this is server! </ div >
< form action = { hello } >
< Textarea defaultValue = "Lorem ipsum" name = "hello" />
< button type = "submit" > submit </ button >
</ form >
</ div >
) ;
} ; import type { ActionFunction } from "@remix-run/node" ;
import { Form } from "@remix-run/react" ;
import { RichTextarea , RichTextareaProps } from "rich-textarea" ;
const Textarea = ( props : Omit < RichTextareaProps , "children" | "ref" > ) => {
return (
< RichTextarea { ... props } >
{ ( v ) => {
return v . split ( "" ) . map ( ( t , i ) => (
< span key = { i } style = { { color : i % 2 === 0 ? "red" : undefined } } >
{ t }
</ span >
) ) ;
} }
</ RichTextarea >
) ;
} ;
export const action : ActionFunction = async ( { request } ) => {
console . log ( ( await request . formData ( ) ) . get ( "hello" ) ) ;
} ;
export default ( ) => {
return (
< Form method = "post" >
< Textarea defaultValue = "Lorem ipsum" name = "hello" />
< button type = "submit" > submit </ button >
</ Form >
) ;
} ; 欢迎所有贡献。如果发现问题,请随时创建问题或PR。
npm install 。