mirror of
https://github.com/patarapolw/cjdic2.git
synced 2026-11-09 17:39:35 +00:00
(feat) add infinite scroll
This commit is contained in:
1 parent
e3ccd517a8
commit
5dcda78b05
4 files changed
+107
-28
No files matched your search
@@ -39,3 +39,8 @@ body {
|
|||||||
padding-left: env(safe-area-inset-left);
|
padding-left: env(safe-area-inset-left);
|
||||||
padding-right: env(safe-area-inset-right);
|
padding-right: env(safe-area-inset-right);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
html {
|
||||||
|
height: 100vh;
|
||||||
|
overflow-y: hidden;
|
||||||
|
}
|
||||||
@@ -7,8 +7,10 @@ import SearchPage from "./pages/SearchPage";
|
|||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<Provider>
|
<Provider>
|
||||||
|
<>
|
||||||
<SearchPage />
|
<SearchPage />
|
||||||
<LoadingDialog />
|
<LoadingDialog />
|
||||||
|
</>
|
||||||
</Provider>
|
</Provider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,19 +2,29 @@ import { createElement as h } from "react";
|
|||||||
|
|
||||||
export default function Glossary({
|
export default function Glossary({
|
||||||
glossary,
|
glossary,
|
||||||
|
maxLength,
|
||||||
onTermClicked,
|
onTermClicked,
|
||||||
}: {
|
}: {
|
||||||
glossary: any;
|
glossary: any;
|
||||||
|
maxLength?: number;
|
||||||
onTermClicked: (t: string) => void;
|
onTermClicked: (t: string) => void;
|
||||||
}) {
|
}) {
|
||||||
|
function trimString(s: any) {
|
||||||
|
if (!maxLength) return s;
|
||||||
|
if (typeof s === "string") {
|
||||||
|
return s.substring(0, maxLength);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
// Plain string
|
// Plain string
|
||||||
if (typeof glossary === "string") {
|
if (typeof glossary === "string") {
|
||||||
return glossary;
|
return trimString(glossary);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlossaryText
|
// GlossaryText
|
||||||
if ("type" in glossary && glossary.type === "text") {
|
if ("type" in glossary && glossary.type === "text") {
|
||||||
return glossary.text;
|
return trimString(glossary.text);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GlossaryImage
|
// GlossaryImage
|
||||||
@@ -26,7 +36,11 @@ export default function Glossary({
|
|||||||
if ("type" in glossary && glossary.type === "structured-content") {
|
if ("type" in glossary && glossary.type === "structured-content") {
|
||||||
const sc = glossary;
|
const sc = glossary;
|
||||||
return (
|
return (
|
||||||
<StructuredContent node={sc.content} onTermClicked={onTermClicked} />
|
<StructuredContent
|
||||||
|
node={sc.content}
|
||||||
|
maxLength={maxLength}
|
||||||
|
onTermClicked={onTermClicked}
|
||||||
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -58,22 +72,37 @@ function GlossaryImage(img: any) {
|
|||||||
|
|
||||||
function StructuredContent({
|
function StructuredContent({
|
||||||
node,
|
node,
|
||||||
|
maxLength,
|
||||||
onTermClicked,
|
onTermClicked,
|
||||||
}: {
|
}: {
|
||||||
node: any;
|
node: any;
|
||||||
|
maxLength?: number;
|
||||||
onTermClicked: (t: string) => void;
|
onTermClicked: (t: string) => void;
|
||||||
}): any {
|
}): any {
|
||||||
if (!node) return null;
|
if (!node) return null;
|
||||||
|
|
||||||
|
function trimString(s: any) {
|
||||||
|
if (!maxLength) return s;
|
||||||
|
if (typeof s === "string") {
|
||||||
|
return s.substring(0, maxLength);
|
||||||
|
}
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof node === "string") {
|
if (typeof node === "string") {
|
||||||
return node;
|
return trimString(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(node)) {
|
if (Array.isArray(node)) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{...node.map((n, i) => (
|
{...node.map((n, i) => (
|
||||||
<StructuredContent key={i} node={n} onTermClicked={onTermClicked} />
|
<StructuredContent
|
||||||
|
key={i}
|
||||||
|
node={n}
|
||||||
|
maxLength={maxLength}
|
||||||
|
onTermClicked={onTermClicked}
|
||||||
|
/>
|
||||||
))}
|
))}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
@@ -114,7 +143,11 @@ function StructuredContent({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{content ? (
|
{content ? (
|
||||||
<StructuredContent node={content} onTermClicked={onTermClicked} />
|
<StructuredContent
|
||||||
|
node={content}
|
||||||
|
maxLength={maxLength}
|
||||||
|
onTermClicked={onTermClicked}
|
||||||
|
/>
|
||||||
) : null}
|
) : null}
|
||||||
</a>
|
</a>
|
||||||
);
|
);
|
||||||
@@ -126,6 +159,6 @@ function StructuredContent({
|
|||||||
return h(
|
return h(
|
||||||
tag,
|
tag,
|
||||||
{ style: block.style, title: block.title, open: block.open },
|
{ style: block.style, title: block.title, open: block.open },
|
||||||
StructuredContent({ node: block.content, onTermClicked }),
|
StructuredContent({ node: block.content, maxLength, onTermClicked }),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,10 @@
|
|||||||
import { CompositionEventHandler, useEffect, useRef, useState } from "react";
|
import {
|
||||||
|
CompositionEventHandler,
|
||||||
|
UIEventHandler,
|
||||||
|
useEffect,
|
||||||
|
useRef,
|
||||||
|
useState,
|
||||||
|
} from "react";
|
||||||
import { toHiragana } from "wanakana";
|
import { toHiragana } from "wanakana";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -65,7 +71,7 @@ function SearchPage() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runSearch() {
|
async function runSearch(isNew = true) {
|
||||||
const ender = /\p{Z}$/u.test(q) ? "" : "*";
|
const ender = /\p{Z}$/u.test(q) ? "" : "*";
|
||||||
|
|
||||||
const re = /\<(.+?)\>\[(.+?)\]/g;
|
const re = /\<(.+?)\>\[(.+?)\]/g;
|
||||||
@@ -73,9 +79,12 @@ function SearchPage() {
|
|||||||
let qTerm = "";
|
let qTerm = "";
|
||||||
let qReading = "";
|
let qReading = "";
|
||||||
|
|
||||||
q.trim()
|
let norm_q = q.trim();
|
||||||
.split(re)
|
if (isAutoKana) {
|
||||||
.map((s, i) => {
|
norm_q = norm_q.replace(/n$/i, "ん").replace(/[a-z]$/i, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
norm_q.split(re).map((s, i) => {
|
||||||
switch (i % 3) {
|
switch (i % 3) {
|
||||||
case 1:
|
case 1:
|
||||||
qTerm += s;
|
qTerm += s;
|
||||||
@@ -92,15 +101,33 @@ function SearchPage() {
|
|||||||
qTerm += ender;
|
qTerm += ender;
|
||||||
qReading += ender;
|
qReading += ender;
|
||||||
|
|
||||||
setEntries(
|
const result = await invoke<Entry[]>("search_yomitan", {
|
||||||
await invoke("search_yomitan", {
|
|
||||||
qTerm,
|
qTerm,
|
||||||
qReading,
|
qReading,
|
||||||
limit: 10,
|
limit: 10,
|
||||||
offset: 0,
|
offset: isNew ? 0 : entries.length,
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
|
if (isNew) {
|
||||||
|
setEntries(result);
|
||||||
|
} else {
|
||||||
|
setEntries([...entries, ...result]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const onEntriesScrollEnd: UIEventHandler = async ({ target }) => {
|
||||||
|
if (!(target instanceof HTMLElement)) return;
|
||||||
|
const { scrollHeight, scrollTop } = target;
|
||||||
|
|
||||||
|
if (scrollHeight - 20 > scrollTop + target.getBoundingClientRect().height)
|
||||||
|
return;
|
||||||
|
|
||||||
|
await runSearch(false);
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
target.scrollTop = scrollTop + 100;
|
||||||
|
}, 100);
|
||||||
|
};
|
||||||
|
|
||||||
function onSearchboxChange(q: string) {
|
function onSearchboxChange(q: string) {
|
||||||
if (isAutoKana) {
|
if (isAutoKana) {
|
||||||
@@ -167,7 +194,16 @@ function SearchPage() {
|
|||||||
) : null;
|
) : null;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack maxW={"1000px"} margin={"0.5em auto"}>
|
<Stack
|
||||||
|
style={{
|
||||||
|
margin: "0.5em auto",
|
||||||
|
maxWidth: "1000px",
|
||||||
|
maxHeight: "100vh",
|
||||||
|
display: "flex",
|
||||||
|
flexDirection: "column",
|
||||||
|
overflowY: "hidden",
|
||||||
|
}}
|
||||||
|
>
|
||||||
<form
|
<form
|
||||||
lang={lang}
|
lang={lang}
|
||||||
onSubmit={(e) => {
|
onSubmit={(e) => {
|
||||||
@@ -201,18 +237,20 @@ function SearchPage() {
|
|||||||
</Switch.Root>
|
</Switch.Root>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<Box as={"ol"} listStyleType={"number"} style={{ margin: "1em" }}>
|
<Box
|
||||||
|
as={"ol"}
|
||||||
|
listStyleType={"number"}
|
||||||
|
style={{ overflowY: "scroll", scrollbarWidth: "none" }}
|
||||||
|
onScrollEnd={onEntriesScrollEnd}
|
||||||
|
>
|
||||||
{entries.map(
|
{entries.map(
|
||||||
({ term, reading, dict_title, glossary_json, ...it }, i) => (
|
({ term, reading, dict_title, glossary_json, ...it }, i) => (
|
||||||
<li key={i} lang={lang}>
|
<li key={i} lang={lang} style={{ marginLeft: "1.5em" }}>
|
||||||
<details>
|
<div>
|
||||||
<summary>
|
|
||||||
{term}
|
|
||||||
{reading ? ` (${reading})` : ""} 【{dict_title}】
|
|
||||||
</summary>
|
|
||||||
|
|
||||||
<Card.Root>
|
<Card.Root>
|
||||||
<Card.Header>
|
<Card.Header>
|
||||||
|
{term}
|
||||||
|
{reading ? ` (${reading})` : ""} 【{dict_title}】
|
||||||
{JSON.stringify(it, (_, v) => v || undefined)}
|
{JSON.stringify(it, (_, v) => v || undefined)}
|
||||||
</Card.Header>
|
</Card.Header>
|
||||||
<Card.Body>
|
<Card.Body>
|
||||||
@@ -223,8 +261,9 @@ function SearchPage() {
|
|||||||
whiteSpace: "pre-wrap",
|
whiteSpace: "pre-wrap",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{JSON.parse(glossary_json).map((g: any) => (
|
{JSON.parse(glossary_json).map((g: any, i: number) => (
|
||||||
<Glossary
|
<Glossary
|
||||||
|
key={i}
|
||||||
glossary={g}
|
glossary={g}
|
||||||
onTermClicked={(t) => setQ(t + " ")}
|
onTermClicked={(t) => setQ(t + " ")}
|
||||||
/>
|
/>
|
||||||
@@ -232,7 +271,7 @@ function SearchPage() {
|
|||||||
</div>
|
</div>
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
</Card.Root>
|
</Card.Root>
|
||||||
</details>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
),
|
),
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in new issue
Block a user