From 4c6c9fe9dd24f17e07efca9c98b49734dc6d3d35 Mon Sep 17 00:00:00 2001 From: Jing Xu Date: Thu, 22 Aug 2024 06:21:16 +0900 Subject: [PATCH] add command copy button (#3210) --- _css/custom.css | 19 +++++++++++++++++++ _scripts/actions.js | 28 ++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/_css/custom.css b/_css/custom.css index 0ce7a1002..64b2705d5 100644 --- a/_css/custom.css +++ b/_css/custom.css @@ -137,6 +137,25 @@ padding-top: 50px; } + + +.code-div { + display: flex; + flex-direction: row; + flex-wrap: nowrap; +} + +.highlight-bash { + flex: 1; +} + +.code-svg { + width: 20px; + height: 20px; + margin: 10px 5px 0px 8px; +} + + /* Footer */ a#wap_dns { display: none; diff --git a/_scripts/actions.js b/_scripts/actions.js index 517010c4c..5669c38d8 100644 --- a/_scripts/actions.js +++ b/_scripts/actions.js @@ -1,4 +1,7 @@ $(document).ready(function() { + var svg_copy = ""; + var svg_done = ""; + var search_fields = []; if(window.location.hash.startsWith("#installation")) { var hash_list = decodeURIComponent(window.location.hash).split("?"); @@ -143,7 +146,10 @@ $(document).ready(function() { } $.code_gen = function(data) { - return "
" + data.join("
") + "
"; + return "
" + + "
" + data.join("\n") + "
" + + "
" + svg_copy + "
" + + "
"; } $.secid_gen = function(indice_main, index_sub = -1) { @@ -433,9 +439,9 @@ $(document).ready(function() { ret += "
"; $.each(value.commands, function(index, value) { if(index == 0) - ret += $.code_gen(value).replace("notranslate\"", "notranslate\" style=\"display: block\""); + ret += $.code_gen(value).replace("code-div\"", "code-div\" style=\"display: flex\""); else - ret += $.code_gen(value).replace("notranslate\"", "notranslate\" style=\"display: none\""); + ret += $.code_gen(value).replace("code-div\"", "code-div\" style=\"display: none\""); }); ret += "
"; } @@ -800,10 +806,24 @@ $(document).ready(function() { var i = 0; $("#code-" + category).children().each(function () { if(i == index) - $(this).css("display", "block"); + $(this).css("display", "flex"); else $(this).css("display", "none"); i += 1; }); }); + + $("#install-instructions").on("click", "svg", function() { + try { + var text = $(this).parent().parent().children("div:first").children("div:first").text(); + navigator.clipboard.writeText(text); + var div_svg = $(this).parent(); + div_svg.html(svg_done); + setTimeout(function(div) { + div.html(svg_copy); + }, 2000, div_svg); + } catch(err) { + window.alert("Failed to copy commands to clipboard.\n" + err.message); + } + }); });