From ba2dbc660c229d3e86662d35513dfa7c904d9870 Mon Sep 17 00:00:00 2001 From: polwex Date: Sun, 23 Nov 2025 13:29:28 +0700 Subject: wew --- packages/prosody-ui/src/components/Colors.tsx | 198 +++++++++++++++++++++ packages/prosody-ui/src/components/Sentence.tsx | 173 +++++++++++++++--- .../src/components/word/FullWordData.tsx | 156 ++++++++++++++++ .../prosody-ui/src/components/word/Phonetic.tsx | 92 ++++++++++ .../prosody-ui/src/components/word/Semantic.tsx | 184 +++++++++++++++++++ 5 files changed, 781 insertions(+), 22 deletions(-) create mode 100644 packages/prosody-ui/src/components/Colors.tsx create mode 100644 packages/prosody-ui/src/components/word/FullWordData.tsx create mode 100644 packages/prosody-ui/src/components/word/Phonetic.tsx create mode 100644 packages/prosody-ui/src/components/word/Semantic.tsx (limited to 'packages/prosody-ui/src/components') diff --git a/packages/prosody-ui/src/components/Colors.tsx b/packages/prosody-ui/src/components/Colors.tsx new file mode 100644 index 0000000..d98838f --- /dev/null +++ b/packages/prosody-ui/src/components/Colors.tsx @@ -0,0 +1,198 @@ +import React from "react"; +import { notRandomFromArray, randomFromArrayMany } from "@sortug/lib"; +import "./sentence.css"; +import type { AnalyzeRes, ColorTheme, LangToColor } from "../logic/types"; +import type { POS_CODE } from "../thai/logic/thainlp"; + +export function assignColors(keys: string[], theme?: ColorTheme): string[] { + const background = theme ? theme : "light"; + const colors = colorPalette[background]; + const reduced = randomFromArrayMany(colors, keys.length, false); + const assigned: string[] = []; + for (const key of keys) { + const color = notRandomFromArray(key, reduced); + assigned.push(color); + } + return assigned; +} + +export function ColoredText({ + frags, + fn, + lang, + theme, +}: { + frags: LangToColor[]; + fn?: (s: any) => void; + lang?: string; + theme: ColorTheme; +}) { + const colors = colorPalette[theme]; + console.log("coloredText", theme); + + // function getStyle(frags: AnalyzeRes[], i: number) { + // const prev = frags[i - 1]; + // const prevC = prev ? notRandomFromArray(prev.word, colors) : "lol"; + // const color = notRandomFromArray(s, colors); + // const opacity = prev && prevC === color ? 0.8 : 1; + // const style = { color, opacity }; + // return style; + // } + + return ( + <> + {frags.map((s, i) => { + // old code + const prev = frags[i - 1]; + const prevC = prev ? notRandomFromArray(prev.colorBy, colors) : "lol"; + const color = notRandomFromArray(s.colorBy, colors); + const style = !prev ? { color } : { color }; + return ( + + ); + })} + + ); +} + +export function CTInner({ + s, + style, + fn, + lang, +}: { + s: LangToColor; + style: any; + fn?: (s: any) => void; + lang?: string; +}) { + function handleClick(e: React.MouseEvent) { + if (fn) { + e.stopPropagation(); + fn(s.data); + } + } + return ( + + {s.display} + + ); +} + +export const colorPalette: Record = { + light: [ + // Black Standard high contrast + "#000000", + // Charcoal Softer than pure black + "#36454F", + // Slate Grey Cool, dark grey-green + "#2F4F4F", + // Navy Blue Classic professional blue + "#000080", + // Midnight Blue Very deep, rich blue + "#191970", + // Cobalt Vivid, highly legible blue + "#0047AB", + // Teal Distinct blue-green + "#008080", + // Forest Green Nature-inspired dark green + "#006400", + // Pine Green Cooler, bluish green + "#01796F", + // Olive Drab Dark brownish-green + "#4B5320", + // Bronze Metallic brown-orange + "#CD7F32", + // Saddle Brown Robust earthy tone + "#8B4513", + // Chocolate Warm, readable orange-brown + "#D2691E", + // Burnt Sienna Reddish-orange earth tone + "#E97451", + // Firebrick Muted dark red + "#B22222", + // Crimson Vivid, alarming red + "#DC143C", + // Maroon Deep, serious red + "#800000", + // Burgundy Purple-leaning red + "#800020", + // Deep Pink High contrast magenta-pink + "#C71585", + // Dark Violet Vivid purple + "#9400D3", + // Indigo Deep blue-purple + "#4B0082", + // Purple Standard distinct purple + "#800080", + // Rebecca Purple Web-standard bluish purple + "#663399", + // Dim Gray Neutral, medium-dark gray + "#696969", + ], + dark: [ + // White Standard high contrast + "#FFFFFF", + // Silver Soft readable grey + "#C0C0C0", + // Cream Warm white, easier on eyes + "#FFFDD0", + // Cyan The standard terminal blue-green + "#00FFFF", + // Sky Blue Pleasant, airy blue + "#87CEEB", + // Powder Blue Very pale, soft blue + "#B0E0E6", + // Aquamarine Bright neon blue-green + "#7FFFD4", + // Mint Green Soft, pastel green + "#98FB98", + // Lime Classic high-vis terminal green + "#00FF00", + // Chartreuse Yellow-green neon + "#7FFF00", + // Gold Bright yellow-orange + "#FFD700", + // Yellow Standard high-vis yellow + "#FFFF00", + // Khaki Muted, sandy yellow + "#F0E68C", + // Wheat Soft beige/earth tone + "#F5DEB3", + // Orange Standard distinctive orange + "#FFA500", + // Coral Pinkish-orange + "#FF7F50", + // Salmon Soft reddish-pink + "#FA8072", + // Hot Pink Vivid, high-energy pink + "#FF69B4", + // Magenta Pure, digital pink-purple + "#FF00FF", + // Plum Muted, readable purple + "#DDA0DD", + // Violet Bright, distinct purple + "#EE82EE", + // Lavender Very light purple-blue + "#E6E6FA", + // Periwinkle Soft indigo-blue + "#CCCCFF", + // Thistle Desaturated light purple + "#D8BFD8", + ], +}; + +// export const colors = [ +// "#8c2c2c", +// "#000000", +// "#ffd400", +// "#1513a0", +// "#7e7e7e", +// "1eb52d", +// ]; diff --git a/packages/prosody-ui/src/components/Sentence.tsx b/packages/prosody-ui/src/components/Sentence.tsx index 33144ac..1986ba8 100644 --- a/packages/prosody-ui/src/components/Sentence.tsx +++ b/packages/prosody-ui/src/components/Sentence.tsx @@ -1,26 +1,49 @@ import React from "react"; -import { notRandomFromArray } from "sortug"; +import { notRandomFromArray } from "@sortug/lib"; import "./sentence.css"; +import type { AnalyzeRes, ColorTheme, LangToColor } from "../logic/types"; +import type { POS_CODE } from "../thai/logic/thainlp"; export function ColoredText({ frags, fn, lang, + theme, }: { - frags: string[]; - fn?: (s: string) => void; + frags: LangToColor[]; + fn?: (s: any) => void; lang?: string; + theme: ColorTheme; }) { + const colors = colorPalette[theme]; + console.log("coloredText", theme); + + // function getStyle(frags: AnalyzeRes[], i: number) { + // const prev = frags[i - 1]; + // const prevC = prev ? notRandomFromArray(prev.word, colors) : "lol"; + // const color = notRandomFromArray(s, colors); + // const opacity = prev && prevC === color ? 0.8 : 1; + // const style = { color, opacity }; + // return style; + // } + return ( <> {frags.map((s, i) => { + // old code const prev = frags[i - 1]; - const prevC = prev ? notRandomFromArray(prev, colors) : "lol"; - const color = notRandomFromArray(s, colors); - const opacity = prev && prevC === color ? 0.8 : 1; - const style = { color, opacity }; - console.log({ style }); - return ; + const prevC = prev ? notRandomFromArray(prev.colorBy, colors) : "lol"; + const color = notRandomFromArray(s.colorBy, colors); + const style = !prev ? { color } : { color }; + return ( + + ); })} ); @@ -32,26 +55,132 @@ export function CTInner({ fn, lang, }: { - s: string; + s: LangToColor; style: any; - fn?: (s: string) => void; + fn?: (s: any) => void; lang?: string; }) { function handleClick(e: React.MouseEvent) { - console.log(!!fn, "fn"); - if (fn) fn(e.currentTarget.innerText.trim()); + if (fn) { + e.stopPropagation(); + fn(s.data); + } } return ( - {s} + {s.display} ); } -export const colors = [ - "#8c2c2c", - "#000000", - "#ffd400", - "#1513a0", - "#7e7e7e", - "1eb52d", -]; + +export const colorPalette: Record = { + light: [ + // Black Standard high contrast + "#000000", + // Charcoal Softer than pure black + "#36454F", + // Slate Grey Cool, dark grey-green + "#2F4F4F", + // Navy Blue Classic professional blue + "#000080", + // Midnight Blue Very deep, rich blue + "#191970", + // Cobalt Vivid, highly legible blue + "#0047AB", + // Teal Distinct blue-green + "#008080", + // Forest Green Nature-inspired dark green + "#006400", + // Pine Green Cooler, bluish green + "#01796F", + // Olive Drab Dark brownish-green + "#4B5320", + // Bronze Metallic brown-orange + "#CD7F32", + // Saddle Brown Robust earthy tone + "#8B4513", + // Chocolate Warm, readable orange-brown + "#D2691E", + // Burnt Sienna Reddish-orange earth tone + "#E97451", + // Firebrick Muted dark red + "#B22222", + // Crimson Vivid, alarming red + "#DC143C", + // Maroon Deep, serious red + "#800000", + // Burgundy Purple-leaning red + "#800020", + // Deep Pink High contrast magenta-pink + "#C71585", + // Dark Violet Vivid purple + "#9400D3", + // Indigo Deep blue-purple + "#4B0082", + // Purple Standard distinct purple + "#800080", + // Rebecca Purple Web-standard bluish purple + "#663399", + // Dim Gray Neutral, medium-dark gray + "#696969", + ], + dark: [ + // White Standard high contrast + "#FFFFFF", + // Silver Soft readable grey + "#C0C0C0", + // Cream Warm white, easier on eyes + "#FFFDD0", + // Cyan The standard terminal blue-green + "#00FFFF", + // Sky Blue Pleasant, airy blue + "#87CEEB", + // Powder Blue Very pale, soft blue + "#B0E0E6", + // Aquamarine Bright neon blue-green + "#7FFFD4", + // Mint Green Soft, pastel green + "#98FB98", + // Lime Classic high-vis terminal green + "#00FF00", + // Chartreuse Yellow-green neon + "#7FFF00", + // Gold Bright yellow-orange + "#FFD700", + // Yellow Standard high-vis yellow + "#FFFF00", + // Khaki Muted, sandy yellow + "#F0E68C", + // Wheat Soft beige/earth tone + "#F5DEB3", + // Orange Standard distinctive orange + "#FFA500", + // Coral Pinkish-orange + "#FF7F50", + // Salmon Soft reddish-pink + "#FA8072", + // Hot Pink Vivid, high-energy pink + "#FF69B4", + // Magenta Pure, digital pink-purple + "#FF00FF", + // Plum Muted, readable purple + "#DDA0DD", + // Violet Bright, distinct purple + "#EE82EE", + // Lavender Very light purple-blue + "#E6E6FA", + // Periwinkle Soft indigo-blue + "#CCCCFF", + // Thistle Desaturated light purple + "#D8BFD8", + ], +}; + +// export const colors = [ +// "#8c2c2c", +// "#000000", +// "#ffd400", +// "#1513a0", +// "#7e7e7e", +// "1eb52d", +// ]; diff --git a/packages/prosody-ui/src/components/word/FullWordData.tsx b/packages/prosody-ui/src/components/word/FullWordData.tsx new file mode 100644 index 0000000..9b1fc69 --- /dev/null +++ b/packages/prosody-ui/src/components/word/FullWordData.tsx @@ -0,0 +1,156 @@ +import React, { useCallback, useEffect, useState } from "react"; +import spinner from "../assets/icons/spinner.svg"; +import likeIcon from "../assets/icons/heart.svg"; +import commentsIcon from "../assets/icons/quote.svg"; +import shareIcon from "../assets/icons/share.svg"; +import fontIcon from "../assets/icons/font.svg"; +import bookmarkIcon from "@/assets/icons/bookmark.svg"; +import speakerIcon from "@/assets/icons/speaker.svg"; +import type { AnalyzeRes, ColorTheme, Meaning } from "@/logic/types"; +import { ColoredText } from "../Sentence.tsx"; +import { P, Span, useSpeechSynthesis } from "@/hooks/useLang.tsx"; +import type { FullWordData } from "@sortug/langlib"; +import { cycleNext } from "@sortug/lib"; +import FontChanger from "@/fonts/FontChanger.tsx"; +import Phonetic from "./Phonetic.tsx"; + +function Word({ + data, + lang, + theme, +}: { + data: FullWordData; + lang: string; + theme: ColorTheme; +}) { + async function load() { + // const wiki = await fetchWiki(data.word); + // console.log(wiki, "wiki res"); + // if ("ok" in wiki) setM(wiki.ok.meanings); + // else setError(wiki.error); + // setLoading(false); + } + useEffect(() => { + load(); + }, []); + const [error, setError] = useState(""); + const [loading, setLoading] = useState(false); + const [meanings, setM] = useState([]); + const [fontIdx, setFont] = useState(0); + + const { voices, speaking, speak, stop } = useSpeechSynthesis(); + function playAudio() { + console.log({ voices, speaking }); + console.log("word", data); + speak(data.spelling); + } + console.log({ data }); + + async function saveW() {} + + return ( +
+ + +
+ ({ + data: s, + display: s.spelling, + colorBy: s.tone.name, + }))} + theme={theme} + /> +
+ +
+

{data.phonetic.ipa}

+ +
+
+ {loading ? ( + + ) : ( + data.senses.map((m) => ( +
+
+ {m.pos} +
+
    + {m.glosses.map((t, i) => ( +
  1. +

    {t}

    +
  2. + ))} +
+
+ )) + )} + {error &&
{error}
} +
+
+
+ ); +} + +export default Word; + + +
+ +
+ + Meanings + Grammar + Examples + +
+ +
+ + + + + +
+
+

+ Tone Analysis +

+
+ {tones.map((tone, idx) => ( + + Syl {idx + 1}:{" "} + + {tone} + + + ))} +
+
+ +
+

+ Word Structure +

+

+ This word consists of {syls.length} syllable + {syls.length > 1 ? "s" : ""}. The tone pattern is essential for + conveying the correct meaning. +

+
+
+
+ + + s.examples || [])} + /> + +
+
+
+
; diff --git a/packages/prosody-ui/src/components/word/Phonetic.tsx b/packages/prosody-ui/src/components/word/Phonetic.tsx new file mode 100644 index 0000000..db3d0cb --- /dev/null +++ b/packages/prosody-ui/src/components/word/Phonetic.tsx @@ -0,0 +1,92 @@ +import React, { useCallback, useEffect, useState } from "react"; +import spinner from "../assets/icons/spinner.svg"; +import likeIcon from "../assets/icons/heart.svg"; +import commentsIcon from "../assets/icons/quote.svg"; +import shareIcon from "../assets/icons/share.svg"; +import fontIcon from "../assets/icons/font.svg"; +import bookmarkIcon from "../assets/icons/bookmark.svg"; +import type { AnalyzeRes, ColorTheme, Meaning } from "@/logic/types"; +import { P, Span, useSpeechSynthesis } from "@/hooks/useLang.tsx"; +import type { FullWordData, Syllable, Tone } from "@sortug/langlib"; +import { cycleNext } from "@sortug/lib"; +import FontChanger from "../fonts/FontChanger.tsx"; +import { assignColors } from "../Colors.tsx"; +import { IconBadgeFilled, IconSpeakerphone } from "@tabler/icons-react"; + +function Phonetic({ + data, + lang, + theme, +}: { + data: FullWordData; + lang: string; + theme: ColorTheme; +}) { + async function load() { + // const wiki = await fetchWiki(data.word); + // console.log(wiki, "wiki res"); + // if ("ok" in wiki) setM(wiki.ok.meanings); + // else setError(wiki.error); + // setLoading(false); + } + useEffect(() => { + load(); + }, []); + const [loading, setLoading] = useState(false); + + const { voices, speaking, speak, stop } = useSpeechSynthesis(); + function playAudio() { + setLoading(true); + console.log({ voices, speaking }); + console.log("word", data); + speak(data.spelling); + setLoading(false); + } + console.log({ data }); + + async function saveW() {} + + return ( +
+
+

{data.phonetic.ipa}

+ {loading ? ( + + ) : ( + + )} +
+ +
+ ); +} + +export default Phonetic; + +function Syllables({ data }: { data: FullWordData }) { + const syllables = data.phonetic.syllables; + + console.log(data.phonetic.tone_sequence); + const isTonal = !!data.phonetic.tone_sequence; + const colorMap = isTonal + ? (s: Syllable) => s.tone.name + : (s: Syllable) => (s.stressed ? "stressed" : "neuter"); + const colors = assignColors(syllables.map(colorMap)); + return ( +
+ {data.phonetic.syllables.map((syl) => ( +
+ {syl.tone.letters && } + {syl.spelling} +
+ ))} +
+ ); +} +function Tone({ tone }: { tone: Tone }) { + return ( +
+ {tone.letters} +
+ ); +} diff --git a/packages/prosody-ui/src/components/word/Semantic.tsx b/packages/prosody-ui/src/components/word/Semantic.tsx new file mode 100644 index 0000000..059194c --- /dev/null +++ b/packages/prosody-ui/src/components/word/Semantic.tsx @@ -0,0 +1,184 @@ +import { useEffect, useState } from "react"; +import type { Example, FullWordData } from "@sortug/langlib"; +import { IconBadgeFilled, IconSparkles } from "@tabler/icons-react"; + +type Tab = "meanings" | "grammar" | "examples"; +function Semantic({ data }: { data: FullWordData }) { + return ( +
+
+
+ {data.senses.map((sense, i) => ( +
+
+ {sense.pos &&
{sense.pos}
} + +
    + {sense.glosses.map((gloss, idx: number) => ( +
  • + {gloss} +
  • + ))} +
+ + {sense.etymology && ( +
+ Etymology: {sense.etymology} +
+ )} + + {sense.categories.length > 0 && ( +
+ Categories: {sense.categories.join(", ")} +
+ )} + {sense.derivation.length > 0 && ( +
+ Derived forms: + {sense.derivation.map((dr, i) => ( +
+ {dr.type}: {dr.text} - {dr.tags} +
+ ))} +
+ )} + {sense.examples.length > 0 && ( + + )} +
+
+ ))} +
+
+
+ ); +} + +export default Semantic; + +function ExamplesTab({ + data, + examples, +}: { + data: FullWordData; + examples: Example[]; +}) { + const [isGenerating, setIsGenerating] = useState(false); + const [generatedExamples, setGeneratedExamples] = useState([]); + + const generateExamples = async () => { + setIsGenerating(true); + + try { + // Get the primary meaning from the first sense + const primaryMeaning = + data.senses?.[0]?.glosses?.[0] || "unknown meaning"; + + const response = await fetch("/api/generate-examples", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + word: data.spelling, + meaning: primaryMeaning, + examples, + }), + }); + + if (!response.ok) { + throw new Error("Failed to generate examples"); + } + + const j = await response.json(); + setGeneratedExamples(j.examples || []); + } catch (err) { + console.error("Error generating examples:", err); + } finally { + setIsGenerating(false); + } + }; + return ( +
+
+

Usage Examples

+ + {/* Generate More Button */} +
+ +
+ + {/* Examples Display */} +
+ {examples.map((example, idx) => ( +
+

+ {example?.text || ""} +

+ {example.ref && ( +

+ Source: {example.ref} +

+ )} +
+ ))} + + {generatedExamples.length > 0 && ( + <> +
+ AI-Generated Examples: +
+ {generatedExamples.map((example, idx) => ( +
+

+ {example.thai} +

+

+ {example.english} +

+ {example.context && ( +

+ Context: {example.context} +

+ )} +
+ ))} + + )} + + {/* No Examples */} + {!moreExamples?.length && !generatedExamples.length && ( +
+

+ No examples available for this word. Click "Generate More + Example Sentences" to get AI-generated examples. +

+
+ )} +
+
+
+ ); +} -- cgit v1.2.3