(() => {
"use strict";
const SAMPLE_QUESTION = "我是否要接受这次工作调整?怎样做能更稳妥?";
const SAMPLE_LINES = [9, 8, 8, 8, 8, 8];
const HEXAGRAM_NAMES = [
"", "乾", "坤", "屯", "蒙", "需", "讼", "师", "比", "小畜", "履", "泰", "否",
"同人", "大有", "谦", "豫", "随", "蛊", "临", "观", "噬嗑", "贲", "剥", "复",
"无妄", "大畜", "颐", "大过", "坎", "离", "咸", "恒", "遁", "大壮", "晋",
"明夷", "家人", "睽", "蹇", "解", "损", "益", "夬", "姤", "萃", "升", "困",
"井", "革", "鼎", "震", "艮", "渐", "归妹", "丰", "旅", "巽", "兑", "涣",
"节", "中孚", "小过", "既济", "未济"
];
// 行为下卦、列为上卦;三位编码均按自下而上排列,1 为阳、0 为阴。
const KING_WEN = {
"111": { "111": 1, "100": 34, "010": 5, "001": 26, "000": 11, "011": 9, "101": 14, "110": 43 },
"100": { "111": 25, "100": 51, "010": 3, "001": 27, "000": 24, "011": 42, "101": 21, "110": 17 },
"010": { "111": 6, "100": 40, "010": 29, "001": 4, "000": 7, "011": 59, "101": 64, "110": 47 },
"001": { "111": 33, "100": 62, "010": 39, "001": 52, "000": 15, "011": 53, "101": 56, "110": 31 },
"000": { "111": 12, "100": 16, "010": 8, "001": 23, "000": 2, "011": 20, "101": 35, "110": 45 },
"011": { "111": 44, "100": 32, "010": 48, "001": 18, "000": 46, "011": 57, "101": 50, "110": 28 },
"101": { "111": 13, "100": 55, "010": 63, "001": 22, "000": 36, "011": 37, "101": 30, "110": 49 },
"110": { "111": 10, "100": 54, "010": 60, "001": 41, "000": 19, "011": 61, "101": 38, "110": 58 }
};
const icons = {
lock: '',
arrow: '',
chevron: '',
shield: ''
};
const screen = document.querySelector("#screen");
const modalRoot = document.querySelector("#modal-root");
const toastRegion = document.querySelector("#toast-region");
const backButton = document.querySelector("[data-action='back']");
const state = {
screen: "welcome",
question: "",
lines: [],
coins: [null, null, null],
modal: null,
consent: false,
explanationSource: "ai",
aiError: false
};
function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
}
function lineIsYang(value) {
return value === 7 || value === 9;
}
function lineIsMoving(value) {
return value === 6 || value === 9;
}
function transformLines(lines) {
return lines.map((value) => {
if (value === 6) return 7;
if (value === 9) return 8;
return value;
});
}
function lookupHexagram(lines) {
const bits = lines.map((value) => lineIsYang(value) ? "1" : "0");
const lower = bits.slice(0, 3).join("");
const upper = bits.slice(3, 6).join("");
const number = KING_WEN[lower][upper];
return { number, name: HEXAGRAM_NAMES[number] };
}
function lineName(value, index) {
const position = ["初", "二", "三", "四", "五", "上"][index];
const polarity = lineIsYang(value) ? "九" : "六";
if (index === 0) return `${position}${polarity}`;
if (index === 5) return `${position}${polarity}`;
return `${polarity}${position}`;
}
function resultData() {
const sourceLines = state.lines.length === 6 ? state.lines : SAMPLE_LINES;
const changedLines = transformLines(sourceLines);
return {
sourceLines,
changedLines,
primary: lookupHexagram(sourceLines),
changed: lookupHexagram(changedLines),
movingIndexes: sourceLines
.map((value, index) => lineIsMoving(value) ? index : -1)
.filter((index) => index >= 0)
};
}
function stepper(current, total) {
const progress = Math.round((current / total) * 100);
return `
${String(current).padStart(2, "0")}
${String(total).padStart(2, "0")}
`;
}
function welcomeView() {
return `
慢一点,看清当下
把一个犹豫,安静地放在这里。
用你手中的三枚硬币,依次记录六次结果。这里提供文化文本与反思线索,不替你预测,也不替你决定。
观
工作名“一问”仅用于本轮视觉评审
`;
}
function questionView() {
return `
${stepper(1, 2)}
起念
你想看清什么?
写成一个开放的问题,比只问“会不会”更容易照见可行动的部分。
只保存在本次原型会话中
${state.question.length}/120
${icons.lock}
起卦计算在本地完成。只有你主动选择 AI 解读并再次确认后,所需内容才会被发送。
`;
}
function lineStack(lines) {
return [5, 4, 3, 2, 1, 0].map((index) => {
const value = lines[index];
if (!value) {
return '';
}
const kind = lineIsYang(value) ? "yang" : "yin";
const moving = lineIsMoving(value) ? " line-slot--moving" : "";
return `
${lineIsMoving(value) ? `${value}` : ""}
`;
}).join("");
}
function coinButton(value, index) {
const face = value === 2 ? "字" : value === 3 ? "背" : "点选";
const score = value ? String(value) : "字 ↔ 背";
const valueAttr = value ? `data-value="${value}"` : "";
const next = value === null ? "字,2 分" : value === 2 ? "背,3 分" : "字,2 分";
return `
硬币 ${index + 1}
`;
}
function castingView() {
const round = state.lines.length + 1;
const selected = state.coins.every((value) => value !== null);
const sum = selected ? state.coins.reduce((total, value) => total + value, 0) : null;
const currentType = sum
? ({ 6: "老阴 · 动", 7: "少阳", 8: "少阴", 9: "老阳 · 动" })[sum]
: "";
return `
${stepper(2, 2)}
${escapeHtml(state.question)}
${state.lines.length} / 6
从下往上记录
${state.lines.length ? '
' : ""}
${lineStack(state.lines)}
第 ${round} 爻
依照手中硬币,逐枚点选“字”或“背”
${state.coins.map(coinButton).join("")}
${sum ? `本次合计 ${sum} · ${currentType}` : "点按硬币可在“字 2”与“背 3”间切换"}
`;
}
function hexLine(value, index, showMoving) {
const kind = lineIsYang(value) ? "yang" : "yin";
const moving = showMoving && lineIsMoving(value) ? " hex-line--moving" : "";
return `
`;
}
function hexagramFigure(lines, hexagram, showMoving) {
const rendered = [5, 4, 3, 2, 1, 0]
.map((index) => hexLine(lines[index], index, showMoving))
.join("");
return `
${rendered}
${hexagram.name}
第 ${hexagram.number} 卦
`;
}
function resultCopy(data) {
if (data.primary.number === 24) {
return {
classic: "复:亨。出入无疾,朋来无咎。反复其道,七日来复。利有攸往。",
plain: "“复”提醒人把注意力放回可返回、可重新开始之处。它不是替你判断成败,而是邀请你看见:此刻是否有一条更朴素、更合乎本心的路。",
moving: "初九:不远复,无祇悔,元吉。",
movingPlain: "偏离尚不远时就察觉并回转,代价通常较小。先修正一个最近的动作,比一次解决所有问题更可行。"
};
}
return {
classic: `${data.primary.name}卦 · 第 ${data.primary.number} 卦。本原型仅演示排盘与阅读层级,正式经典文本将从经校勘的本地内容包读取。`,
plain: "先观察卦象呈现的张力与变化,再回到你能核实的事实、感受和资源。不要把符号当成替你下结论的证据。",
moving: data.movingIndexes.length ? `${data.movingIndexes.map((index) => lineName(data.sourceLines[index], index)).join("、")}发生变化。` : "",
movingPlain: "变化的位置可作为反思入口:哪里正在松动,哪里仍需要保留余地?"
};
}
function errorCard() {
return `
AI 解读暂时没有返回
起卦结果仍完整保存在本页。你可以改看本地解读,或稍后重试。
`;
}
function resultView() {
const data = resultData();
const copy = resultCopy(data);
const hasMoving = data.movingIndexes.length > 0;
const question = state.question || "未记录具体问题";
return `
起卦结果 · 本地计算
${hasMoving ? `${data.movingIndexes.length} 个动爻` : "无动爻"}
${hexagramFigure(data.sourceLines, data.primary, true)}
${hasMoving ? `
之
${hexagramFigure(data.changedLines, data.changed, false)}` : ""}
${hasMoving ? "" : '
卦象稳定 · 不显示之卦
'}
你所问
本次记录
${escapeHtml(question)}
${state.aiError ? errorCard() : ""}
经典原文
经典文本
${copy.classic}
${hasMoving ? `
动爻
变化处
${data.movingIndexes.map((index) => lineName(data.sourceLines[index], index)).join(" · ")}
${copy.moving}
${copy.movingPlain}
` : ""}
`;
}
function loadingView() {
return `
正在整理反思线索
结果已经生成。AI 只在此处协助组织文字,不参与起卦。
`;
}
function explanationView() {
const data = resultData();
const ai = state.explanationSource === "ai";
return `
眼前更重要的,也许不是立刻选定一条路,而是辨认哪一步能让你重新获得稳定与判断力。
可以先观察
- 你对“调整”的担心里,哪些是已经发生的事实,哪些是尚未验证的推测?
- 复卦的动处在最初一爻,适合把注意力放在能尽早修正的小偏差。
- 之卦为坤,提示行动时给现实条件、协作关系与节奏留出空间。
问问自己
- 如果暂时不追求一次做对,我最想先确认的一个事实是什么?
- 哪一种选择更容易保留回转余地,而不是把自己锁死?
- 我可以向谁说明顾虑,并获得一条具体信息?
可以试的一小步
在今天结束前,写下“已知事实、尚待确认、我的底线”各一条,再约一次不超过 20 分钟的信息沟通。
这是一种文化反思与文字整理,不是预测、诊断或替代你的判断。涉及医疗、法律、财务或人身安全时,请寻求相应专业支持。
`;
}
function interpretationModal() {
return `
`;
}
function consentModal() {
return `
${icons.shield}
将发送以下内容;不会发送设备标识、历史记录或其他本地数据。
- 你本次写下的问题
- 本卦、之卦与动爻结果
- 用于约束语气和安全边界的提示
`;
}
function methodModal() {
return `
- 准备三枚硬币,在现实中投掷一次。
- 在页面逐枚记录字面或背面,确认后形成一爻。
- 从下往上重复六次;程序只换算,不替你随机生成。
合计 6 为老阴、7 为少阳、8 为少阴、9 为老阳;6 与 9 是动爻。
`;
}
function renderModal() {
if (state.modal === "interpretation") modalRoot.innerHTML = interpretationModal();
else if (state.modal === "consent") modalRoot.innerHTML = consentModal();
else if (state.modal === "method") modalRoot.innerHTML = methodModal();
else modalRoot.innerHTML = "";
document.body.style.overflow = state.modal ? "hidden" : "";
if (state.modal) {
window.setTimeout(() => {
modalRoot.querySelector("button, input")?.focus();
}, 20);
}
}
function render(options = {}) {
if (state.screen === "welcome") screen.innerHTML = welcomeView();
else if (state.screen === "question") screen.innerHTML = questionView();
else if (state.screen === "casting") screen.innerHTML = castingView();
else if (state.screen === "result") screen.innerHTML = resultView();
else if (state.screen === "loading") screen.innerHTML = loadingView();
else if (state.screen === "explanation") screen.innerHTML = explanationView();
backButton.hidden = state.screen === "welcome";
renderModal();
if (!options.preserveFocus) {
screen.focus({ preventScroll: true });
window.scrollTo({ top: 0, behavior: "auto" });
}
}
function showToast(message) {
toastRegion.innerHTML = `${escapeHtml(message)}
`;
window.setTimeout(() => {
toastRegion.innerHTML = "";
}, 1800);
}
function closeModal() {
state.modal = null;
state.consent = false;
renderModal();
}
function goBack() {
if (state.modal) {
closeModal();
return;
}
if (state.screen === "question") state.screen = "welcome";
else if (state.screen === "casting") state.screen = "question";
else if (state.screen === "result") state.screen = "question";
else if (state.screen === "loading" || state.screen === "explanation") state.screen = "result";
render();
}
function beginAiExplanation() {
state.modal = null;
state.screen = "loading";
state.explanationSource = "ai";
render();
window.setTimeout(() => {
if (state.screen !== "loading") return;
state.screen = "explanation";
render();
}, 900);
}
document.addEventListener("input", (event) => {
if (!event.target.matches("[data-question-input]")) return;
state.question = event.target.value;
document.querySelector("[data-char-count]").textContent = `${state.question.length}/120`;
const startButton = document.querySelector("[data-action='start-casting']");
if (startButton) startButton.disabled = !state.question.trim();
});
document.addEventListener("change", (event) => {
if (!event.target.matches("[data-action='consent-toggle']")) return;
state.consent = event.target.checked;
const confirm = document.querySelector("[data-action='confirm-ai']");
if (confirm) confirm.disabled = !state.consent;
});
document.addEventListener("click", (event) => {
const actionTarget = event.target.closest("[data-action]");
if (!actionTarget) return;
const action = actionTarget.dataset.action;
if (action === "dismiss-modal" && event.target.closest("[data-modal-sheet]")) return;
if (action === "back") goBack();
else if (action === "begin") {
state.screen = "question";
render();
} else if (action === "skip-question") {
state.question = "未记录具体问题(仅作自我观察)";
state.lines = [];
state.coins = [null, null, null];
state.screen = "casting";
render();
} else if (action === "start-casting") {
if (!state.question.trim()) return;
state.lines = [];
state.coins = [null, null, null];
state.screen = "casting";
render();
} else if (action === "toggle-coin") {
const index = Number(actionTarget.dataset.index);
const current = state.coins[index];
state.coins[index] = current === null || current === 3 ? 2 : 3;
render({ preserveFocus: true });
document.querySelector(`[data-action='toggle-coin'][data-index='${index}']`)?.focus();
} else if (action === "confirm-line") {
if (!state.coins.every((value) => value !== null)) return;
state.lines.push(state.coins.reduce((total, value) => total + value, 0));
state.coins = [null, null, null];
if (state.lines.length === 6) {
state.screen = "result";
}
render();
} else if (action === "undo-line") {
state.lines.pop();
state.coins = [null, null, null];
render();
showToast("已撤销上一爻");
} else if (action === "open-interpretation") {
state.modal = "interpretation";
renderModal();
} else if (action === "choose-ai") {
state.modal = "consent";
state.consent = false;
renderModal();
} else if (action === "choose-local") {
state.modal = null;
state.explanationSource = "local";
state.screen = "explanation";
render();
} else if (action === "confirm-ai") {
if (!state.consent) return;
beginAiExplanation();
} else if (action === "retry-ai") {
state.aiError = false;
state.modal = "consent";
state.consent = false;
renderModal();
} else if (action === "restart") {
state.question = "";
state.lines = [];
state.coins = [null, null, null];
state.aiError = false;
state.screen = "welcome";
render();
} else if (action === "back-to-result") {
state.screen = "result";
render();
} else if (action === "open-method") {
state.modal = "method";
renderModal();
} else if (action === "close-modal" || action === "dismiss-modal") {
closeModal();
}
});
document.addEventListener("keydown", (event) => {
if (event.key === "Escape" && state.modal) closeModal();
});
function seedPreview() {
const view = new URLSearchParams(window.location.search).get("view");
if (!view || view === "welcome") return;
if (view === "question") {
state.screen = "question";
state.question = SAMPLE_QUESTION;
return;
}
state.question = SAMPLE_QUESTION;
if (view === "casting") {
state.screen = "casting";
state.lines = [9, 8];
state.coins = [2, 3, null];
return;
}
state.lines = view === "static" ? [7, 7, 7, 7, 7, 7] : [...SAMPLE_LINES];
state.screen = "result";
if (view === "consent") state.modal = "consent";
else if (view === "explanation") {
state.screen = "explanation";
state.explanationSource = "ai";
} else if (view === "local") {
state.screen = "explanation";
state.explanationSource = "local";
} else if (view === "error") {
state.aiError = true;
}
}
seedPreview();
render();
})();