Skip to content

Commit

Permalink
Compress repeated messages with different context.
Browse files Browse the repository at this point in the history
  • Loading branch information
kohler committed Sep 12, 2024
1 parent 6203cb6 commit ff77c72
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
32 changes: 27 additions & 5 deletions lib/messageset.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,14 @@ static function list_status($message_list) {
static function feedback_html_items($message_list) {
$ts = [];
$t = "";
$last_landmark = null;
$last_mi = $last_landmark = null;
foreach ($message_list as $mi) {
if ($mi->message === ""
&& ($mi->pos1 === null || $mi->context === null)) {
continue;
}

// render message
$s = $mi->message_as(5);
$pstart = $pstartclass = "";
if (str_starts_with($s, "<p")) {
Expand All @@ -744,6 +746,23 @@ static function feedback_html_items($message_list) {
$s = substr($s, strlen($m[0]));
}
}

// close previous message
// (special case: avoid duplicate messages if adding context)
if ($last_mi
&& $last_mi->status === $mi->status
&& $last_mi->message === $mi->message
&& ($last_mi->landmark ?? "") === ""
&& ($mi->landmark ?? "") === ""
&& $mi->pos1 !== null
&& $mi->context !== null) {
$s = "";
} else if ($mi->status !== self::INFORM && $t !== "") {
$ts[] = $t;
$t = "";
}

// render landmark
if ($mi->landmark !== null
&& $mi->landmark !== ""
&& ($mi->status !== self::INFORM || $mi->landmark !== $last_landmark)) {
Expand All @@ -763,10 +782,8 @@ static function feedback_html_items($message_list) {
} else {
$lm = "";
}
if ($mi->status !== self::INFORM && $t !== "") {
$ts[] = $t;
$t = "";
}

// add message
if ($s === "") {
// Do not report message
} else if ($mi->status !== self::INFORM) {
Expand All @@ -780,11 +797,16 @@ static function feedback_html_items($message_list) {
} else {
$t .= "<div class=\"msg-inform\">{$pstart}{$lm}{$s}</div>";
}

// add context
if ($mi->pos1 !== null && $mi->context !== null) {
$mark = Ht::mark_substring($mi->context, $mi->pos1, $mi->pos2, $mi->status);
$lmx = $s === "" ? $lm : "";
$t .= "<div class=\"msg-context\">{$lmx}{$mark}</div>";
}

// cleanup
$last_mi = $mi;
if ($mi->status !== self::INFORM) {
$last_landmark = $mi->landmark;
}
Expand Down
25 changes: 20 additions & 5 deletions scripts/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -1789,9 +1789,18 @@ function render_alert(ml) {
return div;
}

function redundant_item(mi, ul) {
let xm;
return mi.message == null
|| mi.message === ""
|| (!mi.landmark
&& mi.context
&& ul.getAttribute("data-last-mi") === mi.status + " " + mi.message);
}

function append_item(ul, mi) {
let li, div;
if (mi.message != null && mi.message !== "") {
if (!redundant_item(mi, ul)) {
let sklass = "";
if (mi.status != null && mi.status >= -4 /*MessageSet::MARKED_NOTE*/ && mi.status <= 3)
sklass = ["note", "success", "warning-note", "urgent-note", "", "warning", "error", "error"][mi.status + 4];
Expand All @@ -1806,6 +1815,11 @@ function append_item(ul, mi) {
}
li.appendChild(div);
render_text.ftext_onto(div, mi.message, 5);
if (!mi.landmark) {
ul.setAttribute("data-last-mi", mi.status + " " + mi.message);
} else {
ul.removeAttribute("data-last-mi");
}
}
if (mi.context) {
const s = mi.context[0],
Expand Down Expand Up @@ -1844,11 +1858,12 @@ function append_item_near(elt, mi) {
while (fl && (fl.tagName === "LABEL" || fl.tagName === "LEGEND" || hasClass(fl, "feedback"))) {
fl = fl.nextElementSibling;
}
if (fl && hasClass(fl, "feedback-list")) {
append_item(fl, mi);
} else {
owner.insertBefore(render_list([mi]), fl);
if (!fl || !hasClass(fl, "feedback-list")) {
let nfl = render_list();
owner.insertBefore(nfl, fl);
fl = nfl;
}
append_item(fl, mi);
return true;
}

Expand Down

0 comments on commit ff77c72

Please sign in to comment.