propose: exact paragraph + apply-ticket guardrails

This commit is contained in:
2026-01-20 12:42:30 +01:00
parent ec42c4b2f4
commit 3b8376d6a9
4 changed files with 290 additions and 187 deletions

View File

@@ -95,12 +95,14 @@
/** @type {"correction"|"fact"|""} */
let kind = "";
// Doit rester cohérent avec EditionLayout
const URL_HARD_LIMIT = 6500;
const esc = (s) => String(s).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const upsertLine = (text, key, value) => {
const re = new RegExp(`^\\s*${esc(key)}\\s*:\\s*.*$`, "mi");
// value vide => supprimer la ligne si elle existe
if (!value) {
if (!re.test(text)) return text;
return (
@@ -111,10 +113,8 @@
);
}
// remplace si existe
if (re.test(text)) return text.replace(re, `${key}: ${value}`);
// sinon append
const sep = text && !text.endsWith("\n") ? "\n" : "";
return text + sep + `${key}: ${value}\n`;
};
@@ -125,6 +125,40 @@
u.searchParams.set("title", `[${prefix}] ${cleaned}`.trim());
};
const tryUpgradeBodyWithFull = (u, full) => {
if (!full) return;
let body = u.searchParams.get("body") || "";
if (!body) return;
// Si déjà en "copie exacte", rien à faire
if (body.includes("Texte actuel (copie exacte du paragraphe)")) return;
// On ne tente que si le body est en mode extrait
if (!body.includes("Texte actuel (extrait):")) return;
const quoted = full.split(/\r?\n/).map(l => `> ${l}`.trimEnd()).join("\n");
// Remplace le bloc "Texte actuel (extrait)" jusqu'à "Proposition (remplacer par):"
const re = /Texte actuel \(extrait\):[\s\S]*?\n\nProposition \(remplacer par\):/m;
const next = body.replace(
re,
`Texte actuel (copie exacte du paragraphe):\n${quoted}\n\nProposition (remplacer par):`
);
if (next === body) return;
const prev = u.toString();
u.searchParams.set("body", next);
// garde-fou URL
if (u.toString().length > URL_HARD_LIMIT) {
// revert
u.searchParams.set("body", body);
return;
}
};
const openWith = (url) => {
pending = url;
kind = "";
@@ -134,18 +168,28 @@
};
// Intercepte UNIQUEMENT les liens marqués data-propose
document.addEventListener("click", (e) => {
document.addEventListener("click", async (e) => {
const a = e.target?.closest?.("a[data-propose]");
if (!a) return;
e.preventDefault();
// L'URL réelle est dans data-url (préparée côté EditionLayout)
const rawUrl = a.dataset.url || a.getAttribute("href") || "";
if (!rawUrl || rawUrl === "#") return;
const full = (a.dataset.full || "").trim();
try {
openWith(new URL(rawUrl));
const u = new URL(rawUrl);
// Option B.1 : copie presse-papier du texte complet (si dispo)
if (full) {
try { await navigator.clipboard.writeText(full); } catch {}
// Option B.2 : upgrade du body (si l'URL reste raisonnable)
tryUpgradeBodyWithFull(u, full);
}
openWith(u);
} catch {
window.open(rawUrl, "_blank", "noopener,noreferrer");
}
@@ -157,7 +201,6 @@
if (!btn || !pending) return;
kind = btn.getAttribute("data-kind") || "";
let body = pending.searchParams.get("body") || "";
if (kind === "fact") {
@@ -190,7 +233,6 @@
let body = pending.searchParams.get("body") || "";
body = upsertLine(body, "Category", cat);
pending.searchParams.set("body", body);
const u = pending.toString();