|
1 | | -import * as React from 'react' |
2 | | -import styles from './styles.module.css' |
| 1 | +import React, { useMemo, useState, useEffect } from "react"; |
3 | 2 |
|
4 | | -interface Props { |
5 | | - text: string |
6 | | -} |
| 3 | +const joinWbr2StringArray = (ary: string[]) => { |
| 4 | + const res: React.ReactNode[] = ary |
| 5 | + .filter((e) => !!e) |
| 6 | + .flatMap((e, i) => [e, <wbr key={i} />]); |
| 7 | + return res; |
| 8 | +}; |
| 9 | + |
| 10 | +type TextSegmenterCoreProps = { |
| 11 | + children: string; |
| 12 | + locale: string; |
| 13 | +}; |
| 14 | + |
| 15 | +const TextSegmenterCore: React.FC<TextSegmenterCoreProps> = ({ |
| 16 | + locale, |
| 17 | + children |
| 18 | +}) => { |
| 19 | + const segmenter = useMemo( |
| 20 | + () => new Intl.Segmenter(locale, { granularity: "word" }), |
| 21 | + [locale] |
| 22 | + ); |
| 23 | + const splittedText = useMemo(() => { |
| 24 | + return Array.from(segmenter.segment(children)).map((s) => s.segment); |
| 25 | + }, [segmenter, children]); |
| 26 | + |
| 27 | + return <>{joinWbr2StringArray(splittedText)}</>; |
| 28 | +}; |
| 29 | + |
| 30 | +type Props = { |
| 31 | + children: React.ReactNode; |
| 32 | + locale?: string; |
| 33 | +}; |
| 34 | + |
| 35 | +const TextSegmenterInner: React.FC<Required<Props>> = ({ |
| 36 | + locale, |
| 37 | + children |
| 38 | +}) => { |
| 39 | + return ( |
| 40 | + <> |
| 41 | + {React.Children.map(children, (child) => { |
| 42 | + if (typeof child === "string") |
| 43 | + return <TextSegmenterCore locale={locale}>{child}</TextSegmenterCore>; |
| 44 | + if (!React.isValidElement(child)) return child; |
| 45 | + if (child.props.children) { |
| 46 | + return React.cloneElement( |
| 47 | + child, |
| 48 | + {}, |
| 49 | + <TextSegmenterInner locale={locale}> |
| 50 | + {child.props.children} |
| 51 | + </TextSegmenterInner> |
| 52 | + ); |
| 53 | + } |
| 54 | + return child; |
| 55 | + })} |
| 56 | + </> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +/** |
| 61 | + * A component to split a word by semantics for non-separated sentences like Japanese. |
| 62 | + * @param children [ReactNode] target node |
| 63 | + * @param locale [string] target locale. default is "ja-JP" |
| 64 | + * @return sentences separated by semantics and wrapped with span |
| 65 | + */ |
| 66 | +export const TextSegmenter: React.FC<Props> = ({ |
| 67 | + locale = "ja-JP", |
| 68 | + children |
| 69 | +}) => { |
| 70 | + const [isClient, setIsClient] = useState(false); |
| 71 | + useEffect(() => setIsClient(true), []); |
| 72 | + |
| 73 | + if (!isClient) { |
| 74 | + // two-pass rendering. |
| 75 | + return <>{children}</>; |
| 76 | + } |
| 77 | + if (Intl.Segmenter === undefined) { |
| 78 | + // Intl.Segmenter is unavailable. |
| 79 | + return <>{children}</>; |
| 80 | + } |
| 81 | + if (!Intl.Segmenter.supportedLocalesOf(locale)) { |
| 82 | + // the locale is not supported. |
| 83 | + return <>{children}</>; |
| 84 | + } |
| 85 | + |
| 86 | + return ( |
| 87 | + <span style={{ wordBreak: "keep-all" }}> |
| 88 | + <TextSegmenterInner locale={locale}>{children}</TextSegmenterInner> |
| 89 | + </span> |
| 90 | + ); |
| 91 | +}; |
7 | 92 |
|
8 | | -export const ExampleComponent = ({ text }: Props) => { |
9 | | - return <div className={styles.test}>Example Component: {text}</div> |
10 | | -} |
|
0 commit comments