499 lines
27 KiB
HTML
499 lines
27 KiB
HTML
<!doctype html>
|
||
<html lang="zh-CN">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="color-scheme" content="light dark">
|
||
<title>见物 · 位置布局</title>
|
||
</head>
|
||
<body>
|
||
<div id="jianwu-layout-study" class="viz-dotted-background">
|
||
<main class="layout-phone" aria-label="见物位置布局原型">
|
||
<header class="layout-header">
|
||
<button type="button" class="layout-link layout-back" hidden>〈 返回</button>
|
||
<div><div class="layout-context"></div><h2 class="layout-title">位置</h2></div>
|
||
<button type="button" class="layout-link layout-action">编辑布局</button>
|
||
</header>
|
||
<div class="layout-modes" role="group" aria-label="位置浏览方式">
|
||
<button type="button" data-mode="plan" aria-pressed="true">俯视图</button>
|
||
<button type="button" data-mode="list" aria-pressed="false">平铺</button>
|
||
</div>
|
||
<section class="layout-plan" aria-label="俯视图">
|
||
<div class="layout-plan-head"><strong class="layout-level">一级位置</strong><span class="layout-note">点位置查看内部</span></div>
|
||
<div class="layout-canvas" aria-label="位置平面图"></div>
|
||
<div class="layout-plan-foot">
|
||
<button type="button" class="layout-link layout-unplaced"></button>
|
||
<button type="button" class="layout-link layout-ai">AI 建模</button>
|
||
</div>
|
||
<div class="layout-editor" hidden>
|
||
<div class="layout-fields">
|
||
<span class="layout-selection"></span>
|
||
<label>宽度 <input class="layout-width" type="number" inputmode="numeric" min="10" max="100"></label>
|
||
<label>深度 <input class="layout-depth" type="number" inputmode="numeric" min="10" max="100"></label>
|
||
<button type="button" class="layout-link layout-rotate">旋转</button>
|
||
</div>
|
||
<div class="layout-template-row">
|
||
<label>模板 <select class="layout-template" aria-label="位置模板"></select></label>
|
||
<button type="button" class="layout-link layout-add">添加</button>
|
||
<button type="button" class="layout-link layout-cancel">取消</button>
|
||
</div>
|
||
</div>
|
||
</section>
|
||
<section class="layout-list" aria-label="平铺位置" hidden>
|
||
<div class="layout-plan-head"><strong class="layout-list-level"></strong><span class="layout-list-note"></span></div>
|
||
<ol class="layout-rows"></ol>
|
||
<button type="button" class="layout-link layout-cancel-order" hidden>取消排序</button>
|
||
</section>
|
||
<section class="layout-assistant" aria-labelledby="jianwu-layout-ai-title" hidden>
|
||
<div class="layout-plan-head"><strong id="jianwu-layout-ai-title">AI 建模</strong><button type="button" class="layout-link layout-ai-close">关闭</button></div>
|
||
<label for="jianwu-layout-description">描述房间或家具的位置</label>
|
||
<textarea id="jianwu-layout-description" rows="3"></textarea>
|
||
<div class="layout-assistant-actions">
|
||
<button type="button" class="layout-link layout-fill">填入语音示例</button>
|
||
<button type="button" class="layout-blue layout-generate">预览示例草稿</button>
|
||
<button type="button" class="layout-blue layout-apply" hidden>应用到布局</button>
|
||
</div>
|
||
<p class="layout-draft-note" hidden>虚线是示例草稿,大小待确认。应用后可拖动和调整尺寸。</p>
|
||
</section>
|
||
<div class="layout-live" role="status"></div>
|
||
</main>
|
||
</div>
|
||
<style>
|
||
#jianwu-layout-study {
|
||
color-scheme: light dark;
|
||
--jw-bg: light-dark(#f4f6fa,#171b24);
|
||
--jw-paper: light-dark(#fff,#232a38);
|
||
--jw-text: light-dark(#17233b,#eef3ff);
|
||
--jw-muted: light-dark(#536680,#adbcd6);
|
||
--jw-blue: light-dark(#304fc7,#a4b6ff);
|
||
--jw-blue-fill: light-dark(#304fc7,#a4b6ff);
|
||
--jw-on-blue: light-dark(#fff,#17264f);
|
||
--jw-rule: light-dark(#d6dfed,#41516c);
|
||
--jw-grid: light-dark(#dbe5f4,#344462);
|
||
--jw-room: light-dark(#e9efff,#263657);
|
||
--jw-zone: light-dark(#eff2f7,#282f40);
|
||
--jw-furniture: light-dark(#cce1dc,#28483f);
|
||
--jw-furniture-line: light-dark(#51756e,#97c9bb);
|
||
--jw-draft: light-dark(#fff0d6,#47371d);
|
||
--jw-draft-line: light-dark(#8a5a11,#ebba74);
|
||
font-size: 15px;
|
||
font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", system-ui, sans-serif;
|
||
padding: 16px 0;
|
||
}
|
||
#jianwu-layout-study * { box-sizing: border-box; }
|
||
#jianwu-layout-study [hidden] { display: none !important; }
|
||
#jianwu-layout-study .layout-phone {
|
||
margin: 0 auto; width: min(100%, 500px); color: var(--jw-text); background: var(--jw-bg);
|
||
border: 1px solid var(--jw-rule); border-radius: 25px; padding: 17px 16px 13px;
|
||
display: grid; gap: 13px;
|
||
}
|
||
#jianwu-layout-study button, #jianwu-layout-study input, #jianwu-layout-study select,
|
||
#jianwu-layout-study textarea { font: inherit; color: inherit; }
|
||
#jianwu-layout-study button { border: 0; border-radius: 10px; background: none; }
|
||
#jianwu-layout-study button:focus-visible, #jianwu-layout-study input:focus-visible {
|
||
outline: 2px solid var(--jw-blue); outline-offset: 2px;
|
||
}
|
||
#jianwu-layout-study .layout-header { display: flex; gap: 10px; align-items: flex-end; }
|
||
#jianwu-layout-study .layout-header > div { flex: 1; min-width: 0; }
|
||
#jianwu-layout-study .layout-context { color: var(--jw-muted); font-size: .83em; overflow-wrap: anywhere; }
|
||
#jianwu-layout-study h2 { color: var(--jw-text); margin: 2px 0 0; font-size: 26px; letter-spacing: -.04em; font-weight: 680; }
|
||
#jianwu-layout-study .layout-link { color: var(--jw-blue); min-height: 42px; font-weight: 600; padding: 5px 8px; }
|
||
#jianwu-layout-study .layout-back { margin-left: -8px; flex-shrink: 0; }
|
||
#jianwu-layout-study .layout-modes {
|
||
display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 3px; border-radius: 12px;
|
||
padding: 3px; background: var(--jw-rule);
|
||
}
|
||
#jianwu-layout-study .layout-modes button { min-height: 39px; color: var(--jw-muted); }
|
||
#jianwu-layout-study .layout-modes [aria-pressed="true"] { background: var(--jw-paper); color: var(--jw-blue); font-weight: 650; }
|
||
#jianwu-layout-study .layout-plan-head, #jianwu-layout-study .layout-plan-foot {
|
||
display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 3px 12px;
|
||
}
|
||
#jianwu-layout-study .layout-plan-head { min-height: 25px; margin-bottom: 8px; }
|
||
#jianwu-layout-study .layout-note, #jianwu-layout-study .layout-list-note { color: var(--jw-muted); font-size: .83em; }
|
||
#jianwu-layout-study .layout-canvas {
|
||
aspect-ratio: 4/3; position: relative; overflow: hidden; border: 1px solid var(--jw-rule); border-radius: 14px;
|
||
background: linear-gradient(var(--jw-grid) 1px,transparent 1px),linear-gradient(90deg,var(--jw-grid) 1px,transparent 1px),var(--jw-paper);
|
||
background-size: 5% 5%; background-position: center center;
|
||
}
|
||
#jianwu-layout-study .layout-model {
|
||
position: absolute; display: grid; place-items: center; border: 1.5px solid var(--jw-rule);
|
||
font-weight: 650; border-radius: 5px; background: var(--jw-room); color: var(--jw-text);
|
||
padding: 3px; font-size: clamp(11px, 3vw, 14px); letter-spacing: .015em; overflow: visible;
|
||
}
|
||
#jianwu-layout-study .layout-model > span { position: relative; max-width: 100%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
|
||
#jianwu-layout-study .layout-model[data-kind="room"] { border-color: var(--jw-muted); align-items: start; padding-top: 7px; }
|
||
#jianwu-layout-study .layout-model[data-kind="zone"] { background: var(--jw-zone); border-style: dashed; }
|
||
#jianwu-layout-study .layout-model[data-kind="furniture"] {
|
||
background: var(--jw-furniture); border-color: var(--jw-furniture-line);
|
||
}
|
||
#jianwu-layout-study .layout-model[data-mold="sofa"]::before {
|
||
content: ""; position: absolute; inset: 5% 6%; border: solid var(--jw-furniture-line);
|
||
border-width: 4px 3px 0; border-radius: 5px;
|
||
}
|
||
#jianwu-layout-study .layout-model:is([data-mold="wardrobe"],[data-mold="cabinet"],[data-mold="shelf"])::before {
|
||
content: ""; position: absolute; inset: 10% 8%; border: 1px solid var(--jw-furniture-line);
|
||
background: linear-gradient(90deg,transparent 48%,var(--jw-furniture-line) 49% 51%,transparent 52%);
|
||
opacity: .55;
|
||
}
|
||
#jianwu-layout-study .layout-model[data-mold="bed"]::before {
|
||
content: ""; position: absolute; inset: 9% 8%; border: 1px solid var(--jw-furniture-line);
|
||
background: linear-gradient(var(--jw-furniture-line) 22%,transparent 23%);
|
||
opacity: .55; border-radius: 3px;
|
||
}
|
||
#jianwu-layout-study .layout-model[aria-pressed="true"] { outline: 2px solid var(--jw-blue); outline-offset: 1px; }
|
||
#jianwu-layout-study .layout-model[data-draft="true"] { background: var(--jw-draft); border: 2px dashed var(--jw-draft-line); }
|
||
#jianwu-layout-study .layout-canvas.is-editing .layout-model { cursor: grab; touch-action: none; }
|
||
#jianwu-layout-study .layout-handle {
|
||
position: absolute; right: -12px; bottom: -12px; width: 24px; height: 24px; cursor: nwse-resize;
|
||
display: grid; place-items: center;
|
||
}
|
||
#jianwu-layout-study .layout-handle::after {
|
||
content: ""; width: 12px; height: 12px; border-radius: 3px; background: var(--jw-blue-fill);
|
||
border: 2px solid var(--jw-paper);
|
||
}
|
||
#jianwu-layout-study .layout-editor, #jianwu-layout-study .layout-assistant {
|
||
padding: 11px; border-radius: 13px; border: 1px solid var(--jw-rule); background: var(--jw-paper); display: grid; gap: 9px;
|
||
}
|
||
#jianwu-layout-study .layout-fields, #jianwu-layout-study .layout-template-row, #jianwu-layout-study .layout-assistant-actions {
|
||
display: flex; flex-wrap: wrap; gap: 7px 10px; align-items: center;
|
||
}
|
||
#jianwu-layout-study .layout-selection { flex-basis: 100%; font-weight: 650; }
|
||
#jianwu-layout-study label { color: var(--jw-muted); }
|
||
#jianwu-layout-study input, #jianwu-layout-study select, #jianwu-layout-study textarea {
|
||
border: 1px solid var(--jw-rule); border-radius: 8px; background: var(--jw-bg); padding: 6px;
|
||
}
|
||
#jianwu-layout-study input[type="number"] { width: 4.2em; }
|
||
#jianwu-layout-study textarea { width: 100%; resize: vertical; }
|
||
#jianwu-layout-study .layout-blue { background: var(--jw-blue-fill); color: var(--jw-on-blue); padding: 9px 12px; min-height: 42px; }
|
||
#jianwu-layout-study .layout-rows { padding: 0 11px; margin: 0; list-style: none; background: var(--jw-paper); border-radius: 14px; }
|
||
#jianwu-layout-study .layout-row { display: flex; align-items: center; gap: 5px; padding: 4px 0; min-height: 51px; }
|
||
#jianwu-layout-study .layout-row + .layout-row { border-top: 1px solid var(--jw-rule); }
|
||
#jianwu-layout-study .layout-row > button:first-child { text-align: left; flex: 1; min-width: 0; min-height: 44px; overflow-wrap: anywhere; }
|
||
#jianwu-layout-study .layout-order { min-width: 44px; min-height: 44px; color: var(--jw-blue); font-weight: 700; }
|
||
#jianwu-layout-study .layout-order:disabled { opacity: .35; }
|
||
#jianwu-layout-study .layout-row-detail { font-size: .82em; color: var(--jw-muted); }
|
||
#jianwu-layout-study .layout-draft-note, #jianwu-layout-study .layout-live { font-size: .84em; color: var(--jw-muted); margin: 0; }
|
||
#jianwu-layout-study .layout-live:empty { display: none; }
|
||
#jianwu-layout-study .layout-draft-note { color: var(--jw-draft-line); }
|
||
@media (max-width: 370px) {
|
||
#jianwu-layout-study .layout-phone { padding: 12px 10px; }
|
||
#jianwu-layout-study .layout-model[data-kind="room"] { padding-top: 4px; }
|
||
#jianwu-layout-study input, #jianwu-layout-study select, #jianwu-layout-study textarea { font-size: 16px; }
|
||
}
|
||
</style>
|
||
<script>
|
||
(() => {
|
||
const root = document.getElementById("jianwu-layout-study");
|
||
const part = (name) => root.querySelector(".layout-" + name);
|
||
const canvas = part("canvas");
|
||
const template = part("template");
|
||
const width = part("width");
|
||
const depth = part("depth");
|
||
const description = root.querySelector("textarea");
|
||
const molds = {
|
||
livingRoom: ["客厅", "room", 41, 34], bedroom: ["卧室", "room", 34, 32],
|
||
study: ["书房", "room", 31, 27], kitchen: ["厨房", "room", 27, 29],
|
||
bathroom: ["卫生间", "room", 21, 21], storage: ["储藏室", "room", 26, 24],
|
||
entry: ["玄关", "zone", 28, 16], dining: ["餐区", "zone", 33, 23],
|
||
balcony: ["阳台", "zone", 31, 16], hallway: ["走廊", "zone", 39, 12],
|
||
zone: ["开放区域", "zone", 27, 25],
|
||
sofa: ["沙发", "furniture", 25, 13], wardrobe: ["衣柜", "furniture", 30, 19],
|
||
cabinet: ["电视柜", "furniture", 40, 14], shelf: ["书架", "furniture", 20, 26],
|
||
bed: ["床", "furniture", 29, 36], desk: ["桌子", "furniture", 32, 19],
|
||
rack: ["层架", "furniture", 24, 20],
|
||
box: ["收纳箱", "furniture", 16, 20], table: ["边几", "furniture", 18, 19],
|
||
generic: ["其他", "zone", 20, 20]
|
||
};
|
||
const node = (id, label, mold, x, y, w, h) => ({id, label, mold, x, y, w, h});
|
||
let boards = {
|
||
root: [
|
||
node("living","客厅","livingRoom",6,7,53,56), node("study","书房","study",62,7,32,45),
|
||
node("hall","玄关","entry",6,66,53,15), node("balcony","阳台","balcony",62,55,32,22),
|
||
node("sofa","沙发","sofa",18,34,27,15), node("store","储藏室","storage",null,null,null,null)
|
||
],
|
||
living: [
|
||
node("tv","电视柜","cabinet",25,10,50,15), node("shelf","书架","shelf",77,28,17,48),
|
||
node("table","窗边边几","table",10,51,24,20)
|
||
],
|
||
study: [node("book","书架","shelf",12,12,22,48),node("desk","书桌","desk",43,12,45,25)],
|
||
sofa: [node("under","沙发下方","box",25,33,50,34)],
|
||
tv: [node("drawer","左侧抽屉","box",null,null,null,null),node("rack","右侧开放格","box",null,null,null,null)]
|
||
};
|
||
const saved = window.openai?.widgetState?.privateContent;
|
||
if (saved?.version === 1) boards = saved.boards;
|
||
const trail = [];
|
||
let mode = "plan", editing = false, sorting = false, selected = null, snapshot = null, suggestion = null;
|
||
let drag = null, suppressClick = false, nextID = Date.now();
|
||
const level = () => trail.length + 1;
|
||
const key = () => trail.at(-1)?.id || "root";
|
||
const items = () => boards[key()] ?? (boards[key()] = []);
|
||
const located = (item) => item.x !== null;
|
||
const current = () => items().find((item) => item.id === selected);
|
||
const suggestionLabel = () => level() === 1 ? "阅读区" : "窗边收纳箱";
|
||
const sampleText = () => level() === 1 ? "右下角加一块阅读区,大概两米宽。" : "房间左下角靠墙放一个收纳箱。";
|
||
const clamp = (number, min, max) => Math.min(Math.max(number, min), max);
|
||
const tell = (text) => { part("live").textContent = text; };
|
||
const remember = () => window.openai?.setWidgetState?.({
|
||
modelContent: { prototype: "见物位置俯视图", mode, level: level() },
|
||
privateContent: { version: 1, boards }
|
||
})?.catch(() => {});
|
||
window.addEventListener("openai:set_globals", (event) => {
|
||
const state = event.detail?.globals?.widgetState?.privateContent;
|
||
if (state?.version === 1 && !editing && !sorting && !drag) {
|
||
boards = state.boards;
|
||
render();
|
||
}
|
||
});
|
||
const button = (text, action, className = "layout-order") => {
|
||
const el = document.createElement("button");
|
||
el.type = "button"; el.className = className; el.textContent = text;
|
||
el.addEventListener("click", action);
|
||
return el;
|
||
};
|
||
function position(el, item) {
|
||
Object.assign(el.style, { left: item.x + "%", top: item.y + "%", width: item.w + "%", height: item.h + "%" });
|
||
}
|
||
function open(item) {
|
||
trail.push(item); selected = null; suggestion = null;
|
||
mode = level() > 2 ? "list" : "plan";
|
||
part("assistant").hidden = true; tell(""); render();
|
||
}
|
||
function moveRow(index, next) {
|
||
const rows = items();
|
||
if (next < 0 || next >= rows.length || next === index) return;
|
||
rows.splice(next, 0, rows.splice(index, 1)[0]); render();
|
||
part("rows").children[next].querySelector("button").focus();
|
||
}
|
||
function render() {
|
||
const rows = items();
|
||
const place = current();
|
||
const canPlan = level() <= 2;
|
||
part("title").textContent = trail.at(-1)?.label || "位置";
|
||
part("context").textContent = ["我的空间", ...trail.slice(0,-1).map((item) => item.label)].join(" / ");
|
||
part("back").hidden = !trail.length || editing || sorting;
|
||
part("action").textContent = editing || sorting ? "完成" : mode === "plan" ? "编辑布局" : "排序";
|
||
part("action").hidden = mode === "list" && !sorting && rows.length < 2;
|
||
part("modes").hidden = !canPlan;
|
||
root.querySelectorAll("[data-mode]").forEach((el) => {
|
||
el.setAttribute("aria-pressed", String(mode === el.dataset.mode));
|
||
el.disabled = editing || sorting;
|
||
});
|
||
part("plan").hidden = mode !== "plan";
|
||
part("list").hidden = mode !== "list";
|
||
part("level").textContent = level() === 1 ? "一级位置" : "二级位置";
|
||
part("note").textContent = editing ? "拖动位置 · 拉角缩放" : "点位置查看内部";
|
||
part("list-level").textContent = level() === 1 ? "一级位置" : level() === 2 ? "二级位置" : "下级位置";
|
||
part("list-note").textContent = sorting ? "拖动位置,或用箭头调整" : "";
|
||
part("editor").hidden = !editing;
|
||
part("cancel-order").hidden = !sorting;
|
||
part("selection").textContent = place?.label || "选择位置以调整";
|
||
part("rotate").disabled = !place || !located(place);
|
||
width.disabled = depth.disabled = !place || !located(place);
|
||
width.value = place?.w ?? ""; depth.value = place?.h ?? "";
|
||
if (template.dataset.level !== String(level())) {
|
||
const groups = level() === 1
|
||
? [["房间",["livingRoom","bedroom","study","kitchen","bathroom","storage"]],
|
||
["区域",["entry","dining","balcony","hallway","zone"]],
|
||
["独立家具",["sofa","bed","desk","cabinet","wardrobe","shelf"]]]
|
||
: [["家具与收纳",["wardrobe","cabinet","shelf","rack","box","table","desk","sofa","bed"]],["其他",["generic"]]];
|
||
template.replaceChildren(...groups.map(([group,keys]) => {
|
||
const options = document.createElement("optgroup");
|
||
options.label = group; options.append(...keys.map((mold) => new Option(molds[mold][0], mold)));
|
||
return options;
|
||
}));
|
||
template.dataset.level = String(level());
|
||
}
|
||
const unplaced = rows.filter((item) => !located(item));
|
||
const unplacedButton = part("unplaced");
|
||
unplacedButton.textContent = "未摆放 " + unplaced.length + " 处";
|
||
unplacedButton.disabled = unplaced.length === 0 || editing;
|
||
part("apply").hidden = !suggestion;
|
||
part("generate").hidden = Boolean(suggestion);
|
||
part("draft-note").hidden = !suggestion;
|
||
canvas.classList.toggle("is-editing", editing);
|
||
canvas.replaceChildren();
|
||
[...rows, ...(suggestion ? [suggestion] : [])].filter(located)
|
||
.sort((a,b) => Number(molds[a.mold][1] === "furniture") - Number(molds[b.mold][1] === "furniture"))
|
||
.forEach((item) => {
|
||
const el = button("", () => {
|
||
if (suppressClick || item === suggestion) return;
|
||
if (editing) { selected = item.id; render(); }
|
||
else open(item);
|
||
}, "layout-model");
|
||
const label = document.createElement("span");
|
||
label.textContent = item.label;
|
||
el.append(label);
|
||
el.dataset.id = item.id; el.dataset.mold = item.mold; el.dataset.kind = molds[item.mold][1];
|
||
if (item === suggestion) { el.dataset.draft = "true"; el.disabled = true; }
|
||
if (editing) el.setAttribute("aria-pressed", String(item.id === selected));
|
||
if (editing && item.id === selected) {
|
||
const handle = document.createElement("i");
|
||
handle.className = "layout-handle"; handle.setAttribute("aria-hidden","true"); el.append(handle);
|
||
}
|
||
position(el, item); canvas.append(el);
|
||
});
|
||
const list = part("rows");
|
||
list.replaceChildren();
|
||
rows.forEach((item,index) => {
|
||
const row = document.createElement("li");
|
||
row.className = "layout-row";
|
||
const location = button(item.label, () => { if (!sorting) open(item); }, "layout-place");
|
||
row.append(location);
|
||
if (sorting) {
|
||
row.draggable = true;
|
||
row.addEventListener("dragstart", (event) => event.dataTransfer.setData("text/plain", String(index)));
|
||
row.addEventListener("dragover", (event) => event.preventDefault());
|
||
row.addEventListener("drop", (event) => {
|
||
event.preventDefault(); const original = event.dataTransfer.getData("text/plain");
|
||
if (/^\d+$/.test(original)) moveRow(Number(original), index);
|
||
});
|
||
const up = button("↑", () => moveRow(index,index - 1));
|
||
const down = button("↓", () => moveRow(index,index + 1));
|
||
up.setAttribute("aria-label","上移" + item.label);
|
||
down.setAttribute("aria-label","下移" + item.label);
|
||
up.disabled = index === 0; down.disabled = index === rows.length - 1;
|
||
row.append(up,down);
|
||
} else if (canPlan && !located(item)) {
|
||
const placeIt = button("摆放", () => {
|
||
startEdit(); mode = "plan"; selected = item.id;
|
||
placeInCanvas(item); render();
|
||
}, "layout-link");
|
||
row.append(placeIt);
|
||
} else {
|
||
const type = document.createElement("span"); type.className = "layout-row-detail";
|
||
type.textContent = molds[item.mold][0] + " ›"; row.append(type);
|
||
}
|
||
list.append(row);
|
||
});
|
||
if (rows.length === 0) {
|
||
const empty = document.createElement("li"); empty.className = "layout-row layout-row-detail";
|
||
empty.textContent = "暂无下级位置"; list.append(empty);
|
||
}
|
||
}
|
||
function startEdit() { snapshot = structuredClone(boards); editing = true; }
|
||
function cancel() {
|
||
if (snapshot) boards = snapshot;
|
||
editing = sorting = false; selected = null; snapshot = null; suggestion = null;
|
||
part("assistant").hidden = true; tell(""); render();
|
||
}
|
||
function placeInCanvas(item) {
|
||
const mold = molds[item.mold];
|
||
const visible = items().filter((other) => located(other) && other.id !== item.id);
|
||
function findSpace(w,h) {
|
||
for (let y = 4; y + h <= 97; y += 3) {
|
||
for (let x = 4; x + w <= 97; x += 3) {
|
||
if (visible.every((other) => x + w <= other.x || x >= other.x + other.w ||
|
||
y + h <= other.y || y >= other.y + other.h)) return {x,y,w,h};
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
const position = findSpace(mold[2],mold[3]) || findSpace(Math.max(10,Math.round(mold[2] / 2)),Math.max(10,Math.round(mold[3] / 2)));
|
||
Object.assign(item,position || {x:8,y:8,w:mold[2],h:mold[3]});
|
||
}
|
||
part("back").addEventListener("click", () => {
|
||
trail.pop(); selected = null; suggestion = null; mode = level() <= 2 ? "plan" : "list";
|
||
part("assistant").hidden = true; tell(""); render();
|
||
});
|
||
root.querySelectorAll("[data-mode]").forEach((el) => el.addEventListener("click", () => {
|
||
mode = el.dataset.mode; suggestion = null; part("assistant").hidden = true; tell(""); render();
|
||
}));
|
||
part("action").addEventListener("click", () => {
|
||
if (editing || sorting) {
|
||
tell(editing ? "布局已保存" : "同级位置顺序已保存");
|
||
editing = sorting = false; selected = null; snapshot = null; remember();
|
||
} else if (mode === "plan") { startEdit(); tell(""); }
|
||
else { snapshot = structuredClone(boards); sorting = true; tell(""); }
|
||
render();
|
||
});
|
||
part("cancel").addEventListener("click",cancel);
|
||
part("cancel-order").addEventListener("click",cancel);
|
||
part("unplaced").addEventListener("click", () => { mode = "list"; render(); });
|
||
part("add").addEventListener("click", () => {
|
||
const mold = template.value;
|
||
const name = molds[mold][0];
|
||
let suffix = 1, label = name;
|
||
while (items().some((item) => item.label === label)) label = name + " " + (++suffix);
|
||
const item = node("node-" + (++nextID),label,mold,null,null,null,null);
|
||
placeInCanvas(item); items().push(item); selected = item.id; render();
|
||
});
|
||
function setSize() {
|
||
const place = current();
|
||
if (!place) return;
|
||
const w = width.valueAsNumber, h = depth.valueAsNumber;
|
||
if (Number.isFinite(w)) place.w = clamp(w,10,100 - place.x);
|
||
if (Number.isFinite(h)) place.h = clamp(h,10,100 - place.y);
|
||
render();
|
||
}
|
||
width.addEventListener("change",setSize);
|
||
depth.addEventListener("change",setSize);
|
||
part("rotate").addEventListener("click", () => {
|
||
const place = current();
|
||
if (!place) return;
|
||
[place.w,place.h] = [place.h,place.w];
|
||
place.x = Math.min(place.x,100 - place.w); place.y = Math.min(place.y,100 - place.h); render();
|
||
});
|
||
canvas.addEventListener("pointerdown", (event) => {
|
||
const model = event.target.closest(".layout-model");
|
||
if (!editing || !model || event.button !== 0) return;
|
||
const item = items().find((candidate) => candidate.id === model.dataset.id);
|
||
if (!item) return;
|
||
drag = { id:event.pointerId, initial:{ ...item }, startX:event.clientX, startY:event.clientY,
|
||
scale:canvas.getBoundingClientRect(), resize:Boolean(event.target.closest(".layout-handle")), moved:false };
|
||
if (item.id !== selected) { selected = item.id; render(); }
|
||
canvas.setPointerCapture(event.pointerId);
|
||
});
|
||
canvas.addEventListener("pointermove", (event) => {
|
||
if (!drag || event.pointerId !== drag.id) return;
|
||
const place = current();
|
||
const dx = (event.clientX - drag.startX) * 100 / drag.scale.width;
|
||
const dy = (event.clientY - drag.startY) * 100 / drag.scale.height;
|
||
drag.moved ||= Math.abs(dx) + Math.abs(dy) > 0.8;
|
||
if (!drag.moved || !place) return;
|
||
const first = drag.initial;
|
||
if (drag.resize) {
|
||
place.w = clamp(Math.round(first.w + dx),10,100 - place.x);
|
||
place.h = clamp(Math.round(first.h + dy),10,100 - place.y);
|
||
} else {
|
||
place.x = clamp(Math.round(first.x + dx),0,100 - place.w);
|
||
place.y = clamp(Math.round(first.y + dy),0,100 - place.h);
|
||
}
|
||
position(canvas.querySelector('[aria-pressed="true"]'),place);
|
||
width.value = place.w; depth.value = place.h;
|
||
});
|
||
for (const eventType of ["pointerup","pointercancel","lostpointercapture"]) {
|
||
canvas.addEventListener(eventType, (event) => {
|
||
if (drag?.id !== event.pointerId) return;
|
||
suppressClick = drag.moved; drag = null;
|
||
if (suppressClick) setTimeout(() => { suppressClick = false; },0);
|
||
});
|
||
}
|
||
part("ai").addEventListener("click", () => {
|
||
description.placeholder = sampleText(); part("assistant").hidden = false; description.focus();
|
||
});
|
||
part("fill").addEventListener("click", () => { description.value = sampleText(); });
|
||
part("generate").addEventListener("click", () => {
|
||
if (!description.value.trim()) { description.focus(); tell("可以先填入语音示例。"); return; }
|
||
if (!description.value.includes(level() === 1 ? "阅读区" : "收纳箱")) {
|
||
tell("此原型用阅读区或收纳箱的示例展示草稿。"); return;
|
||
}
|
||
suggestion = level() === 1
|
||
? node("node-" + (++nextID),suggestionLabel(),"zone",65,80,25,17)
|
||
: node("node-" + (++nextID),suggestionLabel(),"box",12,76,23,17);
|
||
tell("示例草稿已在图中标出"); render();
|
||
});
|
||
part("apply").addEventListener("click", () => {
|
||
if (!suggestion) return;
|
||
if (!editing) startEdit();
|
||
items().push(suggestion); selected = suggestion.id; suggestion = null;
|
||
part("assistant").hidden = true; description.value = ""; tell("可调整位置及大小,再点完成。"); render();
|
||
});
|
||
part("ai-close").addEventListener("click", () => {
|
||
suggestion = null; part("assistant").hidden = true; tell(""); render();
|
||
});
|
||
render();
|
||
})();
|
||
</script>
|
||
</body>
|
||
</html>
|