Fix strategy checklist print closing too early.
Stop auto-closing the print tab on afterprint (short checklist pages were especially fragile), load print HTML via fetch into a blank window, and use CSS checkboxes instead of Unicode ballot boxes. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+40
-18
@@ -218,16 +218,22 @@ def strategy_meta_payload() -> dict[str, Any]:
|
||||
return {"ok": True, "exchanges": tabs}
|
||||
|
||||
|
||||
def _checklist_html(checklist: dict[str, Any]) -> str:
|
||||
def _checklist_html(checklist: dict[str, Any], *, include_title: bool = True) -> str:
|
||||
groups = checklist.get("groups") or []
|
||||
parts = [f"<h2>{escape_html(str(checklist.get('title') or '开仓检查清单'))}</h2>"]
|
||||
parts: list[str] = []
|
||||
if include_title:
|
||||
parts.append(f"<h2>{escape_html(str(checklist.get('title') or '开仓检查清单'))}</h2>")
|
||||
for grp in groups:
|
||||
if not isinstance(grp, dict):
|
||||
continue
|
||||
gtitle = escape_html(str(grp.get("title") or ""))
|
||||
parts.append(f"<h3>{gtitle}</h3><ul class=\"checklist\">")
|
||||
for item in grp.get("items") or []:
|
||||
parts.append(f"<li><span class=\"box\" aria-hidden=\"true\">☐</span> {escape_html(str(item))}</li>")
|
||||
# 用 CSS 方框代替 ☐,避免部分打印机/预览吞掉 Unicode 勾选符
|
||||
parts.append(
|
||||
f"<li><span class=\"box\" aria-hidden=\"true\"></span>"
|
||||
f"<span class=\"item-text\">{escape_html(str(item))}</span></li>"
|
||||
)
|
||||
parts.append("</ul>")
|
||||
footnotes = checklist.get("footnotes") or []
|
||||
if footnotes:
|
||||
@@ -330,15 +336,28 @@ body {
|
||||
padding: 0;
|
||||
}
|
||||
.checklist li {
|
||||
margin: 7px 0;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 8px;
|
||||
margin: 8px 0;
|
||||
padding: 0;
|
||||
}
|
||||
.box {
|
||||
flex: 0 0 auto;
|
||||
display: inline-block;
|
||||
width: 1.05em;
|
||||
margin-right: 6px;
|
||||
font-size: 1.05em;
|
||||
line-height: 1.2;
|
||||
width: 0.95em;
|
||||
height: 0.95em;
|
||||
margin-top: 0.2em;
|
||||
border: 1.5px solid #333;
|
||||
border-radius: 2px;
|
||||
background: #fff;
|
||||
box-sizing: border-box;
|
||||
-webkit-print-color-adjust: exact;
|
||||
print-color-adjust: exact;
|
||||
}
|
||||
.item-text {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
.footnotes {
|
||||
margin: 18px 0 0;
|
||||
@@ -364,16 +383,18 @@ body {
|
||||
|
||||
|
||||
def _print_auto_script() -> str:
|
||||
# 不在 afterprint 里 window.close(): Firefox 会在打开打印框时就触发 afterprint,
|
||||
# 短页(检查清单)更容易被立刻关掉,表现为「打不开/打不出来」;策略长文相对不易复现.
|
||||
return """<script>
|
||||
(function () {
|
||||
function done() {
|
||||
try { window.close(); } catch (e) {}
|
||||
function go() {
|
||||
try { window.focus(); } catch (e) {}
|
||||
window.setTimeout(function () {
|
||||
try { window.print(); } catch (e) {}
|
||||
}, 250);
|
||||
}
|
||||
window.addEventListener("afterprint", done, { once: true });
|
||||
window.addEventListener("load", function () {
|
||||
window.setTimeout(function () { window.print(); }, 120);
|
||||
window.setTimeout(done, 60000);
|
||||
});
|
||||
if (document.readyState === "complete") go();
|
||||
else window.addEventListener("load", go, { once: true });
|
||||
})();
|
||||
</script>"""
|
||||
|
||||
@@ -398,7 +419,7 @@ def build_print_html(exchange_key: str, part: str = "doc") -> str:
|
||||
<h1>{escape_html(cl_title)}</h1>
|
||||
<p class="doc-meta">{escape_html(label)} · {escape_html(version)} · 打印 {escape_html(now)}</p>
|
||||
</header>
|
||||
<div class="doc-body">{_checklist_html(checklist)}</div>
|
||||
<div class="doc-body">{_checklist_html(checklist, include_title=False)}</div>
|
||||
</article>"""
|
||||
elif part == "doc":
|
||||
page_title = f"{label} · 策略说明"
|
||||
@@ -453,8 +474,9 @@ h1{{font-size:1.35rem;margin:0 0 8px}}
|
||||
.strategy-doc table{{border-collapse:collapse;width:100%;font-size:.88rem}}
|
||||
.strategy-doc th,.strategy-doc td{{border:1px solid #ccc;padding:6px 8px;text-align:left}}
|
||||
.checklist ul{{list-style:none;padding:0;margin:0}}
|
||||
.checklist li{{margin:8px 0;padding-left:0}}
|
||||
.box{{display:inline-block;width:1em;margin-right:6px}}
|
||||
.checklist li{{display:flex;align-items:flex-start;gap:8px;margin:8px 0}}
|
||||
.box{{flex:0 0 auto;display:inline-block;width:.95em;height:.95em;margin-top:.2em;border:1.5px solid #333;border-radius:2px;background:#fff;box-sizing:border-box}}
|
||||
.item-text{{flex:1 1 auto}}
|
||||
@media print{{body{{margin:12mm}}}}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
Reference in New Issue
Block a user