Files
Jyotisha/frontend/src/lib/answer-heading-focus.ts
T
ClaudeandClaude Opus 5.5 903c17dcaa
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m7s
Independent Staging Quality Gate / validate-python (push) Successful in 5m41s
Independent Staging Quality Gate / publish (push) Successful in 2m45s
feat(chat): a heading that names houses or planets lights them on the chat board; chart icons follow the board style
Product request 2026-11-06 (prototype approved): a consultation answer's
subheading such as 「对上级(太阳、9 宫、10 宫)」 gets a small chart icon.
Clicking it washes and outlines the named houses, lights the named planets
(their houses outlined), fades the rest, switches to a named varga, and
brings the board into view (panel on desktop, sheet on phones). A second
click, 清除, another heading or a board pick clears or replaces it; a
conversation change drops it. Headings only, consultations only.
The heading icon and the phone header's 看本命盘 button draw the North or
South Indian board, following the reader's board style.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyeZrYn6ryNcSe5Y2Y
2026-11-06 14:44:25 +08:00

116 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { BoardFocus } from "@/lib/chart-board-focus";
import { bodyHouse, type BoardModel } from "@/lib/chart-board-model";
import { CHART_TYPE_ORDER, type ChartTypeId } from "@/lib/chart-view-labels";
import type { PlanetGlyphKey } from "@/lib/planet-glyphs";
/**
* What an answer subheading points at on the board (2026-11-06, product: the icon
* beside 「对上级(太阳、9 宫、10 宫)」 lights those houses and that planet in the
* chat's chart panel). Read from the heading text only; nothing is computed.
*/
export type AnswerChartFocus = Readonly<{
/** Identifies the heading that set it, so a second click clears it. */
key: string;
/** The heading without its parenthesis: 「对上级」. */
title: string;
houses: readonly number[];
bodies: readonly PlanetGlyphKey[];
/** A divisional chart named in the heading (D10, D12 …); null = the natal board. */
chartType: ChartTypeId | null;
/** 「太阳、9 宫、10 宫」, in the heading's order. */
items: string;
}>;
/** The names an answer uses; 罗睺 / 计都 are the product's words (PLAIN_TERMS). */
const BODY_NAMES: readonly (readonly [PlanetGlyphKey, string])[] = [
["sun", "太阳"],
["moon", "月亮"],
["mars", "火星"],
["mercury", "水星"],
["jupiter", "木星"],
["venus", "金星"],
["saturn", "土星"],
["rahu", "罗睺"],
["ketu", "计都"],
];
const CN_DIGIT: Readonly<Record<string, number>> = { 一: 1, 二: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9, 十: 10 };
function houseNumber(token: string): number | null {
const trimmed = token.trim();
if (/^\d{1,2}$/.test(trimmed)) return Number(trimmed);
if (!/^[一二三四五六七八九十]{1,3}$/.test(trimmed)) return null;
if (trimmed === "十") return 10;
if (trimmed.startsWith("十")) return 10 + (CN_DIGIT[trimmed[1]!] ?? 0);
if (trimmed.length === 1) return CN_DIGIT[trimmed] ?? null;
return (CN_DIGIT[trimmed[0]!] ?? 0) * 10 + (trimmed[2] ? CN_DIGIT[trimmed[2]] ?? 0 : 0);
}
const NUM = "(?:\\d{1,2}|[一二三四五六七八九十]{1,3})";
/** 「9 宫」「第10宫」「2、4、9 宫」「9和10宫」; never 「宫主」 alone. */
const HOUSE_RUN = new RegExp(`第?\\s*(${NUM}(?:\\s*[、,,和与及/]\\s*第?\\s*${NUM})*)\\s*宫`, "gu");
/** The heading's chart pointers, or null when it names no house and no planet. */
export function parseHeadingFocus(text: string): Omit<AnswerChartFocus, "key"> | null {
const clean = text.replace(/\s+/g, " ").trim();
if (!clean) return null;
const houses: number[] = [];
const order: { at: number; label: string }[] = [];
for (const match of clean.matchAll(HOUSE_RUN)) {
const at = match.index ?? 0;
for (const token of match[1]!.split(/[、,,和与及/]|第/u)) {
const house = houseNumber(token);
if (house !== null && house >= 1 && house <= 12 && !houses.includes(house)) {
houses.push(house);
order.push({ at, label: `${house} 宫` });
}
}
}
const bodies: PlanetGlyphKey[] = [];
for (const [key, name] of BODY_NAMES) {
const at = clean.indexOf(name);
if (at >= 0) {
bodies.push(key);
order.push({ at, label: name });
}
}
if (houses.length === 0 && bodies.length === 0) return null;
const varga = clean.match(/\bD(\d{1,2})\b/u);
const named = varga ? (`D${varga[1]}` as ChartTypeId) : null;
const chartType = named && named !== "D1" && (CHART_TYPE_ORDER as readonly string[]).includes(named) ? named : null;
const title = clean.replace(/[((].*$/u, "").replace(/[::]\s*$/u, "").trim() || clean;
const items = order.sort((a, b) => a.at - b.at).map((item) => item.label).join("、");
return { title, houses, bodies, chartType, items };
}
/**
* The board drawing for an answer focus: the named houses washed and outlined, the
* named planets lit and their own houses outlined, everything else faint.
*/
export function answerBoardFocus(model: BoardModel, focus: AnswerChartFocus): BoardFocus {
const houses = new Set(focus.houses);
const lit = new Set(focus.bodies);
const outlined = new Set(focus.houses);
for (const key of focus.bodies) {
const house = bodyHouse(model, key);
if (house !== null) outlined.add(house);
}
const dimmed = new Set<PlanetGlyphKey>();
for (const house of model.houses) {
for (const body of house.bodies) {
if (!lit.has(body.key) && !houses.has(house.house)) dimmed.add(body.key);
}
}
return {
lit,
dimmed,
marks: new Map(),
houses,
outlined,
links: [],
numberFrom: null,
chips: [focus.title, focus.items],
fadeOthers: true,
};
}