Files
plugins/packages/output-strategy/page/app.js
T
Mañjūṣaka 006e96ef03 output-strategy 0.2.1: finish #814's review follow-ups (#815)
Follow-up to #814 (081c67ac). The core request injection is unchanged.

- Page: static text goes back to English when magpie.theme.lang changes
  from zh to en. Unsaved style picks survive a language change, and the
  title follows the language. zh / zh-CN / zh-Hans use the Simplified
  strings. zh-TW and other languages fall back to English on purpose,
  with <html lang> kept as zh-TW. With no host theme, the page loads in
  English without throwing.
- apply.js: an empty system message gets the style block with no
  leading blank lines. The unused media detector and addedBytes are
  removed.
- style.css: obsolete stats/lang/workspace rules are removed, and the
  720px media block is closed again (52 { / 52 }).
- Tests: lowercase "delete" in the cross-turn cache case;
  empty/null/object system; zh -> en restore; unsaved picks kept;
  zh-TW; a missing theme; style.css brace balance and the version.
- PROVENANCE names ANTISLOP_BOUNDARIES (OmniRoute v3.8.52), checked
  byte for byte against the tag. The page/stats.js ignore is gone.
- Version 0.2.1. dist had only 0.2.0, and npm has no version yet.

Run at 2f971ab9 (mote, Bun 1.4.2, fresh HOME):
- package bun test: 55 pass, 0 fail;
- repo bun test: 6561 pass, 20 skip, 0 fail;
- scripts/check.mjs output-strategy: ok;
- CI run 42320776930 at this head, attempt 1: test and on-npm green;
- page render, main vs head, in Chromium and WebKit at 400/700/1000px:
  identical.
Broken by hand, each turning exactly one test red: the dropped }, the
version at 0.2.0, an unguarded theme.lang, the old conditional
applyTheme, a case-sensitive delete skip, the old appendInstruction,
one-way translate, unkept checkbox state, the old zh* normalize, and
addedBytes put back.
Not tried: a live theme switch in the real magpie window, and real
Claude Code/Codex sessions.
2026-11-07 09:10:31 +08:00

175 lines
5.7 KiB
JavaScript

(function () {
"use strict"
var i18n = globalThis.OutputStrategyI18n
var t = function (text) { return i18n ? i18n.t(text) : text }
var byId = function (id) { return document.getElementById(id) }
var form = byId("stylesForm")
var fieldset = byId("stylesFieldset")
var list = byId("stylesList")
var saveButton = byId("saveButton")
var message = byId("formMessage")
var options = {}
var inputs = Object.create(null)
var loaded = false
var saving = false
var dirty = false
var lastMessage = ["Reading settings…", false]
var catalog = readCatalog(globalThis.OUTPUT_STRATEGY_CATALOG)
function readCatalog(value) {
var entries = Array.isArray(value) ? value : value && Array.isArray(value.styles) ? value.styles : []
return entries.filter(function (entry) {
return entry && typeof entry.id === "string" && entry.id.length > 0
}).map(function (entry) {
return {
id: entry.id,
label: typeof entry.label === "string" && entry.label ? entry.label : entry.id,
description: typeof entry.description === "string" ? entry.description : "",
}
})
}
function element(tag, className, text) {
var node = document.createElement(tag)
if (className) node.className = className
if (text != null) node.textContent = text
return node
}
function clear(node) {
while (node.firstChild) node.removeChild(node.firstChild)
}
function setMessage(text, isError) {
lastMessage = [text, isError === true]
message.textContent = t(text)
message.className = "form-message" + (isError ? " error" : "")
}
function selectedTotal() {
return catalog.reduce(function (total, style) {
return total + (inputs[style.id] && inputs[style.id].checked ? 1 : 0)
}, 0)
}
function updateSelection() {
byId("selectedCount").textContent = selectedTotal() + " / " + catalog.length
}
function updateControls() {
var unavailable = !loaded || saving || catalog.length === 0
fieldset.disabled = unavailable
saveButton.disabled = unavailable || !dirty
saveButton.textContent = t(saving ? "Saving…" : "Save changes")
}
function renderCatalogState() {
byId("catalogState").textContent = catalog.length
? t("{n} styles; injected in catalog order.").replace("{n}", String(catalog.length))
: t("Catalog not loaded")
}
function renderCatalog() {
var checked = Object.create(null)
catalog.forEach(function (style) {
if (inputs[style.id]) checked[style.id] = inputs[style.id].checked === true
})
clear(list)
inputs = Object.create(null)
renderCatalogState()
if (!catalog.length) {
list.append(element("p", "empty-state", t("Catalog not loaded; style toggles unavailable.")))
updateSelection()
updateControls()
return
}
catalog.forEach(function (style) {
var input = element("input")
var copy = element("span", "style-copy")
var row = element("label", "style-row")
input.type = "checkbox"
input.id = "style-" + style.id
input.name = style.id
input.checked = checked[style.id] === true
input.setAttribute("data-style-id", style.id)
input.addEventListener("change", function () {
dirty = true
setMessage("Unsaved changes.")
updateSelection()
updateControls()
})
copy.append(element("span", "style-name", t(style.label)), element("span", "style-description", t(style.description)))
row.setAttribute("for", input.id)
row.append(copy, input)
list.append(row)
inputs[style.id] = input
})
updateSelection()
updateControls()
}
function applyOptions(next) {
options = next && typeof next === "object" ? next : {}
var styles = options.styles && typeof options.styles === "object" ? options.styles : {}
catalog.forEach(function (style) {
if (inputs[style.id]) inputs[style.id].checked = styles[style.id] === true
})
dirty = false
updateSelection()
updateControls()
}
function save(event) {
event.preventDefault()
if (!loaded || saving || !catalog.length) return
var styles = Object.assign({}, options.styles && typeof options.styles === "object" ? options.styles : {})
catalog.forEach(function (style) { styles[style.id] = inputs[style.id].checked === true })
var next = Object.assign({}, options, { styles: styles })
saving = true
setMessage("Saving…")
updateControls()
Promise.resolve()
.then(function () { return globalThis.magpie.options.set(next) })
.then(function (saved) {
applyOptions(saved && typeof saved === "object" ? saved : next)
setMessage("Saved.")
})
.catch(function () { setMessage("Could not save. Nothing was written; try again.", true) })
.then(function () {
saving = false
updateControls()
})
}
// The sandboxed frame has CSP form-action 'none'; call save directly.
saveButton.addEventListener("click", save)
form.addEventListener("submit", save)
renderCatalog()
if (i18n) i18n.onChange(function () {
renderCatalog()
setMessage(lastMessage[0], lastMessage[1])
})
var host = globalThis.magpie
if (!host || !host.options || typeof host.options.get !== "function" || typeof host.options.set !== "function") {
setMessage("Configuration bridge unavailable; view only.", true)
updateControls()
return
}
Promise.resolve()
.then(function () { return host.options.get() })
.then(function (current) {
loaded = true
applyOptions(current)
setMessage(catalog.length ? "Pick styles and save." : "Catalog not loaded; styles cannot be changed.", !catalog.length)
})
.catch(function () {
loaded = false
setMessage("Could not read settings; reopen the page.", true)
updateControls()
})
})()