var React = require('react'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } var React__default = /*#__PURE__*/_interopDefaultLegacy(React); var joinWbr2StringArray = function joinWbr2StringArray(ary) { var res = ary.filter(function (e) { return !!e; }).flatMap(function (e, i) { return [e, React__default["default"].createElement("wbr", { key: i })]; }); return res; }; var TextSegmenterCore = function TextSegmenterCore(_ref) { var locale = _ref.locale, children = _ref.children; var segmenter = React.useMemo(function () { return new Intl.Segmenter(locale, { granularity: "word" }); }, [locale]); var splittedText = React.useMemo(function () { return Array.from(segmenter.segment(children)).map(function (s) { return s.segment; }); }, [segmenter, children]); return React__default["default"].createElement(React__default["default"].Fragment, null, joinWbr2StringArray(splittedText)); }; var TextSegmenterInner = function TextSegmenterInner(_ref2) { var locale = _ref2.locale, children = _ref2.children; return React__default["default"].createElement(React__default["default"].Fragment, null, React__default["default"].Children.map(children, function (child) { if (typeof child === "string") return React__default["default"].createElement(TextSegmenterCore, { locale: locale }, child); if (!React__default["default"].isValidElement(child)) return child; if (child.props.children) { return React__default["default"].cloneElement(child, {}, React__default["default"].createElement(TextSegmenterInner, { locale: locale }, child.props.children)); } return child; })); }; /** * A component to split a word by semantics for non-separated sentences like Japanese. * @param children [ReactNode] target node * @param locale [string] target locale. default is "ja-JP" * @return sentences separated by semantics and wrapped with span */ var TextSegmenter = function TextSegmenter(_ref3) { var _ref3$locale = _ref3.locale, locale = _ref3$locale === void 0 ? "ja-JP" : _ref3$locale, children = _ref3.children; var _useState = React.useState(false), isClient = _useState[0], setIsClient = _useState[1]; React.useEffect(function () { return setIsClient(true); }, []); if (!isClient) { // two-pass rendering. return React__default["default"].createElement(React__default["default"].Fragment, null, children); } if (Intl.Segmenter === undefined) { // Intl.Segmenter is unavailable. return React__default["default"].createElement(React__default["default"].Fragment, null, children); } if (!Intl.Segmenter.supportedLocalesOf(locale)) { // the locale is not supported. return React__default["default"].createElement(React__default["default"].Fragment, null, children); } return React__default["default"].createElement("span", { style: { wordBreak: "keep-all" } }, React__default["default"].createElement(TextSegmenterInner, { locale: locale }, children)); }; exports.TextSegmenter = TextSegmenter; //# sourceMappingURL=index.cjs.map