Browse Source

优化复制日志时,内容挤在一起不会自动分行的bug

niujiuru 4 hours atrás
parent
commit
3d8c0711c4
1 changed files with 22 additions and 12 deletions
  1. 22 12
      web-ui.service/web/static/js/log-stream.js

+ 22 - 12
web-ui.service/web/static/js/log-stream.js

@@ -123,24 +123,34 @@ function clearLog(target) {
 
 function copyLog(target) {
     var el = typeof target === "string" ? document.getElementById(target) : target;
-    var text = el ? el.textContent : logLines.join("\n");
-    if (navigator.clipboard) {
+    // 每行日志是一个独立的 div,textContent 直接拼接不会带换行,需逐行 join("\n")
+    var text = el
+        ? Array.prototype.map
+              .call(el.children, function (c) {
+                  return c.textContent;
+              })
+              .join("\n")
+        : logLines.join("\n");
+    function fallbackCopy() {
+        var ta = document.createElement("textarea");
+        ta.value = text;
+        ta.style.position = "fixed";
+        ta.style.opacity = "0";
+        document.body.appendChild(ta);
+        ta.select();
+        var ok = document.execCommand("copy");
+        ta.remove();
+        alert(ok ? "复制成功" : "复制失败");
+    }
+    if (navigator.clipboard && navigator.clipboard.writeText) {
         navigator.clipboard
             .writeText(text)
             .then(function () {
                 return alert("复制成功");
             })
-            .catch(function () {
-                return alert("复制失败");
-            });
+            .catch(fallbackCopy);
     } else {
-        var ta = document.createElement("textarea");
-        ta.value = text;
-        document.body.appendChild(ta);
-        ta.select();
-        document.execCommand("copy");
-        ta.remove();
-        alert("复制成功");
+        fallbackCopy();
     }
 }
 ["beforeunload", "pagehide"].forEach(function (e) {