|
|
@@ -1,5 +1,5 @@
|
|
|
import { Button, Dropdown, Input, List } from "antd";
|
|
|
-import { useEffect, useState } from "react";
|
|
|
+import { useEffect, useMemo, useState } from "react";
|
|
|
import {
|
|
|
ArrowLeftOutlined,
|
|
|
FieldTimeOutlined,
|
|
|
@@ -24,6 +24,7 @@ import { fetchPublicChannels, type Channel } from "../../api/channel";
|
|
|
import { grammarTermFetch } from "../../load";
|
|
|
import TermModal from "./TermModal";
|
|
|
import { popRecent, pushRecent } from "./utils";
|
|
|
+import type { IGrammarBookState } from "./grammarBookState";
|
|
|
|
|
|
const { Search } = Input;
|
|
|
|
|
|
@@ -31,12 +32,55 @@ interface IGrammarList {
|
|
|
term: ITerm;
|
|
|
weight: number;
|
|
|
}
|
|
|
-const GrammarBookWidget = () => {
|
|
|
+interface IWidget {
|
|
|
+ /** 初始状态(如从地址栏恢复),只在挂载时读一次 */
|
|
|
+ defaultState?: IGrammarBookState;
|
|
|
+ /** 状态变化时回报,供外部写进地址栏或拼「在新标签页打开」的链接 */
|
|
|
+ onStateChange?: (state: IGrammarBookState) => void;
|
|
|
+}
|
|
|
+
|
|
|
+/** 按检索词给系统语法术语打分排序 */
|
|
|
+function searchGrammar(
|
|
|
+ sysGrammar: ITerm[] | undefined,
|
|
|
+ keyWord: string,
|
|
|
+): IGrammarList[] | undefined {
|
|
|
+ if (keyWord.trim().length === 0) return undefined;
|
|
|
+ /**
|
|
|
+ * 权重算法
|
|
|
+ * 约靠近头,分数约高
|
|
|
+ * 剩余尾巴约短,分数越高
|
|
|
+ */
|
|
|
+ return sysGrammar
|
|
|
+ ?.map((item) => {
|
|
|
+ let weight = 0;
|
|
|
+ const wordBegin = item.word
|
|
|
+ .toLocaleLowerCase()
|
|
|
+ .indexOf(keyWord.toLocaleLowerCase());
|
|
|
+ if (wordBegin >= 0) {
|
|
|
+ weight += (1 / (wordBegin + 1)) * 1000;
|
|
|
+ const wordRemain = item.word.length - keyWord.length - wordBegin;
|
|
|
+ weight += (1 / (wordRemain + 1)) * 100;
|
|
|
+ }
|
|
|
+ const meaningBegin = (item.meaning + item.other_meaning)
|
|
|
+ .toLocaleLowerCase()
|
|
|
+ .indexOf(keyWord);
|
|
|
+ if (meaningBegin >= 0) {
|
|
|
+ weight += (1 / (meaningBegin + 1)) * 1000;
|
|
|
+ const meaningRemain = item.meaning.length - keyWord.length - wordBegin;
|
|
|
+ weight += (1 / (meaningRemain + 1)) * 100;
|
|
|
+ }
|
|
|
+ return { term: item, weight: weight };
|
|
|
+ })
|
|
|
+ .filter((value) => value.weight > 0)
|
|
|
+ .sort((a, b) => b.weight - a.weight);
|
|
|
+}
|
|
|
+
|
|
|
+const GrammarBookWidget = ({ defaultState, onStateChange }: IWidget) => {
|
|
|
const intl = useIntl();
|
|
|
|
|
|
- const [result, setResult] = useState<IGrammarList[]>();
|
|
|
- const [localTermId, setLocalTermId] = useState<string>();
|
|
|
- const [localTermSearch, setLocalTermSearch] = useState<string>();
|
|
|
+ const [keyword, setKeyword] = useState(defaultState?.q ?? "");
|
|
|
+ const [localTermId, setLocalTermId] = useState(defaultState?.id);
|
|
|
+ const [localTermSearch, setLocalTermSearch] = useState(defaultState?.word);
|
|
|
const [showRecent, setShowRecent] = useState(false);
|
|
|
const [create, setCreate] = useState(false);
|
|
|
const [grammarChannel, setGrammarChannel] = useState<Channel>();
|
|
|
@@ -47,6 +91,20 @@ const GrammarBookWidget = () => {
|
|
|
const termId = searchWordId || localTermId;
|
|
|
const termSearch = searchWord || localTermSearch;
|
|
|
|
|
|
+ // 术语表是异步加载的:从地址栏带着检索词进来时,要等它到了再算结果,所以派生而不是在 onChange 里算
|
|
|
+ const result = useMemo(
|
|
|
+ () => searchGrammar(sysGrammar, keyword),
|
|
|
+ [sysGrammar, keyword],
|
|
|
+ );
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ onStateChange?.({
|
|
|
+ q: keyword || undefined,
|
|
|
+ id: localTermId || undefined,
|
|
|
+ word: localTermSearch || undefined,
|
|
|
+ });
|
|
|
+ }, [keyword, localTermId, localTermSearch, onStateChange]);
|
|
|
+
|
|
|
useEffect(() => {
|
|
|
const name = "_System_Grammar_Term_zh-hans_";
|
|
|
fetchPublicChannels({ system: true, search: name, per_page: 20 }).then(
|
|
|
@@ -95,48 +153,14 @@ const GrammarBookWidget = () => {
|
|
|
/>
|
|
|
<Search
|
|
|
placeholder="input search text"
|
|
|
+ value={keyword}
|
|
|
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
console.debug("on change", event.target.value);
|
|
|
setLocalTermId(undefined);
|
|
|
setLocalTermSearch(undefined);
|
|
|
const keyWord = event.target.value;
|
|
|
- if (keyWord.trim().length === 0) {
|
|
|
- setShowRecent(true);
|
|
|
- } else {
|
|
|
- setShowRecent(false);
|
|
|
- }
|
|
|
- /**
|
|
|
- * 权重算法
|
|
|
- * 约靠近头,分数约高
|
|
|
- * 剩余尾巴约短,分数越高
|
|
|
- */
|
|
|
- const search = sysGrammar
|
|
|
- ?.map((item) => {
|
|
|
- let weight = 0;
|
|
|
- const wordBegin = item.word
|
|
|
- .toLocaleLowerCase()
|
|
|
- .indexOf(keyWord.toLocaleLowerCase());
|
|
|
- if (wordBegin >= 0) {
|
|
|
- weight += (1 / (wordBegin + 1)) * 1000;
|
|
|
- const wordRemain =
|
|
|
- item.word.length - keyWord.length - wordBegin;
|
|
|
- weight += (1 / (wordRemain + 1)) * 100;
|
|
|
- }
|
|
|
- const meaningBegin = (item.meaning + item.other_meaning)
|
|
|
- .toLocaleLowerCase()
|
|
|
- .indexOf(keyWord);
|
|
|
- if (meaningBegin >= 0) {
|
|
|
- weight += (1 / (meaningBegin + 1)) * 1000;
|
|
|
- const meaningRemain =
|
|
|
- item.meaning.length - keyWord.length - wordBegin;
|
|
|
- weight += (1 / (meaningRemain + 1)) * 100;
|
|
|
- }
|
|
|
- return { term: item, weight: weight };
|
|
|
- })
|
|
|
- .filter((value) => value.weight > 0)
|
|
|
- .sort((a, b) => b.weight - a.weight);
|
|
|
-
|
|
|
- setResult(search);
|
|
|
+ setKeyword(keyWord);
|
|
|
+ setShowRecent(keyWord.trim().length === 0);
|
|
|
}}
|
|
|
style={{ width: "100%" }}
|
|
|
/>
|