Explorer

html-ui.snapshot-script

description
The JS of the edit page: shows the diff from the part as it was (html-ui.line-diff-script), checks the change with the type checker without taking a snapshot (check: true, also Ctrl/Cmd+Enter) and takes a snapshot of it with POST /api/snapshot
type
String
expression
@core.string-concat(
  left: @html-ui.cbor-script,
  right: "const out = document.getElementById(\"out\");\nconst show = (text, error) => {\n  out.textContent = text;\n  out.className = error ? \"error\" : \"\";\n};\nconst form = document.getElementById(\"edit-form\");\nconst field = (name) => form.elements.namedItem(name);\nconst languages = [\"en\", \"ja\", \"eo\"];\nconst snapshot = () =>\n  [\n    \"visibility: \" + field(\"visibility\").value,\n    ...languages.map((l) => \"description (\" + l + \"): \" + field(\"description_\" + l).value.trim()),\n    \"\",\n    \"type:\",\n    field(\"type\").value,\n    \"\",\n    \"expression:\",\n    field(\"expression\").value,\n  ].join(\"\\n\");\nconst original = form.dataset.exists === \"1\" ? snapshot() : \"\";\nconst diff = document.getElementById(\"diff\");\nconst updateDiff = () => renderLineDiff(diff, original, snapshot());\nlet timer;\nform.addEventListener(\"input\", () => {\n  clearTimeout(timer);\n  timer = setTimeout(updateDiff, 300);\n});\nupdateDiff();\nconst send = async (check, button) => {\n  button.disabled = true;\n  try {\n    const d = form.dataset;\n    const name = field(\"part_name\").value.trim() || d.part;\n    const renamed = name !== d.part && d.exists === \"1\";\n    const descriptions = {};\n    for (const l of languages) descriptions[l] = field(\"description_\" + l).value.trim();\n    const change = {\n      part: name,\n      type: field(\"type\").value,\n      expression: field(\"expression\").value,\n      visibility: field(\"visibility\").value,\n      descriptions,\n    };\n    const changes = renamed ? [change, { part: d.part, delete: true }] : [change];\n    show(document.documentElement.dataset.busy);\n    const text = await definyCbor.api(\"/api/snapshot\", {\n      project: d.project,\n      branch: d.branch,\n      message: field(\"message\").value || (renamed ? \"Rename \" + d.part + \" to \" + name : \"Change \" + name),\n      changes,\n      check,\n    });\n    if (check) {\n      show(d.checkOk);\n      return;\n    }\n    show(text);\n    location.href = \"/p/\" + encodeURIComponent(d.project) + \"/\" + encodeURIComponent(d.branch) + \"/\" + encodeURIComponent(name);\n  } catch (err) {\n    show(String(err.message ?? err), true);\n  } finally {\n    button.disabled = false;\n  }\n};\nform.addEventListener(\"submit\", (e) => {\n  e.preventDefault();\n  send(false, form.querySelector(\"button[type=submit]\"));\n});\ndocument.getElementById(\"check-button\").addEventListener(\"click\", (e) => send(true, e.currentTarget));\ndocument.addEventListener(\"keydown\", (e) => {\n  if (e.key === \"Enter\" && (e.ctrlKey || e.metaKey) && form.contains(document.activeElement)) {\n    e.preventDefault();\n    send(true, document.getElementById(\"check-button\"));\n  }\n});\nfor (const button of form.querySelectorAll(\"[data-template-type]\")) {\n  button.addEventListener(\"click\", () => {\n    field(\"type\").value = button.dataset.templateType;\n    field(\"expression\").value = button.dataset.templateExpression;\n    updateDiff();\n  });\n}\n"
)
visibility
public
html-ui.snapshot-script (project html-ui, branch main, snapshot Daaw3vbJhY15cDW6JsA4JuXvffjj_qFyxoZQMUM8hvQ)
id: KKxOh3NV6wS7FsKsGvWrGzveahwVB0A_yNxRgrGQPR8
content hash: 0ZV3cprAmOFgBBFILbx_0CD1UqzaCZEJgDcSTurkljw