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 。