UNPKG

ze-react-component-library

Version:
106 lines (96 loc) 2.78 kB
import "antd/es/tag/style"; import _Tag from "antd/es/tag"; import "antd/es/space/style"; import _Space from "antd/es/space"; import "antd/es/typography/style"; import _Typography from "antd/es/typography"; import React, { useState, useRef, useEffect } from "react"; import { useRequest } from "@umijs/hooks"; import { requestKeywords } from "../request"; import "./index.less"; var Title = _Typography.Title; export var useInputCursor = function useInputCursor(_a) { var _b = _a.text, text = _b === void 0 ? "" : _b; var _c = useState({ start: 0, end: 0 }), _d = _c[0], start = _d.start, end = _d.end, setPosition = _c[1]; var inputRef = useRef(null); var cursorText = text ? text.slice(0, end) : undefined; var onCursorChange = function onCursorChange(e) { var el = e.target; setPosition({ start: el.selectionStart, end: el.selectionEnd }); }; var getNewWord = function getNewWord(w) { var rest = text.slice(end); var prefix = cursorText.slice(0, cursorText.length - w.replace.length); var newWord = prefix + w.word + rest; return newWord; }; useEffect(function () { setPosition({ start: 0, end: text.length }); }, [text]); return { onCursorChange: onCursorChange, cursorText: cursorText, start: start, end: end, inputRef: inputRef, getNewWord: getNewWord }; }; var AutoKeywords = function AutoKeywords(_a) { var _b; var _onClick = _a.onClick, question = _a.question; var result = useRequest(function () { return question ? requestKeywords(question) : Promise.resolve({ data: [] }); }, { refreshDeps: [question], throttleInterval: 200 }).data; var keywords = ((_b = result === null || result === void 0 ? void 0 : result.data) === null || _b === void 0 ? void 0 : _b.filter(function (f) { return !["func"].includes(f.t); })) || []; if (keywords.length === 0) { return null; } return /*#__PURE__*/React.createElement("div", { className: "ze-auto-keywords" }, /*#__PURE__*/React.createElement(Title, { level: 5, style: { marginBottom: 16, fontSize: 14 } }, "\u731C\u60A8\u60F3\u95EE\uFF1A"), /*#__PURE__*/React.createElement(_Space, { wrap: true }, keywords.map(function (i, index) { return /*#__PURE__*/React.createElement(_Tag, { className: "ze-tag", key: i.n + index, onClick: function onClick(e) { e.preventDefault(); e.stopPropagation(); _onClick === null || _onClick === void 0 ? void 0 : _onClick({ word: i.n, replace: result === null || result === void 0 ? void 0 : result.replace }); } }, i.n); }))); }; export default AutoKeywords;