Pārlūkot izejas kodu

Add copy code button (#963)

* Add copy code button

* External package not needed

* Improve alignement

* Update button style + add feedback on click

Signed-off-by: hossainemruz <hossainemruz@gmail.com>

* Update dependencies

Signed-off-by: hossainemruz <hossainemruz@gmail.com>

---------

Signed-off-by: hossainemruz <hossainemruz@gmail.com>
Co-authored-by: Emruz Hossain <hossainemruz@gmail.com>
Bernat Borràs Civil 7 mēneši atpakaļ
vecāks
revīzija
ff5d237dcf

+ 22 - 0
assets/scripts/features/copyCode/copyCode.js

@@ -0,0 +1,22 @@
+addCopyButtons(navigator.clipboard)
+
+function addCopyButtons(clipboard) {
+  document.querySelectorAll('pre > code').forEach(function (codeBlock) {
+    const button = document.createElement('button')
+    button.title = "Copy"
+    button.className = 'copy-code-button btn btn-sm'
+    button.innerHTML = "<i class='fa-regular fa-copy'></i>"
+
+    button.addEventListener('click', function () {
+      clipboard.writeText(codeBlock.innerText)
+    })
+
+    const pre = codeBlock.parentNode
+    if (pre.parentNode.classList.contains('highlight')) {
+      const highlight = pre.parentNode
+      highlight.parentNode.insertBefore(button, highlight)
+    } else {
+      pre.parentNode.insertBefore(button, pre)
+    }
+  })
+}

+ 1 - 0
assets/scripts/features/copyCode/index.js

@@ -0,0 +1 @@
+import './copyCode'

+ 4 - 0
assets/scripts/features/index.js

@@ -29,3 +29,7 @@ if (process.env.FEATURE_MATH === '1') {
 if (process.env.FEATURE_EMBEDPDF === '1') {
   import('./embedpdf')
 }
+
+if (process.env.FEATURE_COPYCODEBUTTON === '1') {
+  import('./copyCode')
+}

+ 45 - 0
assets/styles/components/buttons.scss

@@ -102,6 +102,40 @@
   }
 }
 
+.copy-code-button {
+  float: right;
+  margin-top: 0.5em;
+  margin-left: -2.6em;
+  margin-right: 3em;
+
+  background-color: get-light-color('text-color') !important;
+  color: get-light-color('inverse-text-color') !important;
+  padding: 0.25rem 0.5rem;
+  line-height: 1.5;
+  border-radius: 0.2rem;
+  border: none;
+
+  &:hover,
+  &:focus {
+    background-color: get-light-color('accent-color') !important;
+    color: get-light-color('text-over-accent-color') !important;
+    @include transition();
+  }
+  &:focus {
+    &::before {
+      content: 'Copied!';
+      position: absolute;
+      padding: 0.3em;
+      border-radius: 0.2em;
+      margin-left: -5em;
+      margin-top: -0.2em;
+      background-color: get-light-color('accent-color') !important;
+      color: get-light-color('text-over-accent-color') !important;
+      @include transition();
+    }
+  }
+}
+
 html[data-theme='dark'] {
   .btn-dark {
     background-color: get-dark-color('accent-color') !important;
@@ -169,4 +203,15 @@ html[data-theme='dark'] {
       background-color: get-dark-color('hover-over-accent-color') !important;
     }
   }
+  .copy-code-button {
+    background-color: get-dark-color('bg-primary') !important;
+    color: get-dark-color('muted-text-color') !important;
+
+    &:hover,
+    &:focus,
+    &::before {
+      background-color: get-dark-color('accent-color') !important;
+      color: get-dark-color('text-over-accent-color') !important;
+    }
+  }
 }

+ 7 - 0
exampleSite/hugo.yaml

@@ -91,6 +91,9 @@ params:
   # Configure various features of this theme
   features:
 
+    # Enable dark theme
+    darkMode:
+    
     # [Deprecated] Enable dark theme
     # This is a deprecated setting, but has not been removed to maintain backward compatibility
     # If `theme` is set, the `darkMode` setting will be discarded.
@@ -285,6 +288,10 @@ params:
           # options doc: https://github.com/sampotts/plyr#options
           # fullscreen: true
     
+    # Enables copy code button
+    copyCodeButton:
+      enable: true
+
     # Enable reading time support in post cards and in post pages
     readingTime:
       enable: true

Failā izmaiņas netiks attēlotas, jo tās ir par lielu
+ 440 - 91
exampleSite/package-lock.json


+ 1 - 2
exampleSite/package.json

@@ -30,7 +30,6 @@
       "postcss-cli": "github.com/hugo-toha/toha/v4"
     }
   },
-  "dependencies": {},
   "devDependencies": {
     "@fontsource/mulish": "4.5.13",
     "@fortawesome/fontawesome-free": "^6.6.0",
@@ -53,7 +52,7 @@
     "ityped": "^1.0.3",
     "katex": "^0.16.11",
     "mark.js": "^8.11.1",
-    "mermaid": "^9.2.1",
+    "mermaid": "^11.2.1",
     "plyr": "^3.7.2",
     "popper.js": "^1.16.1",
     "postcss": "^8.4.41",

+ 1 - 1
package.json

@@ -58,7 +58,7 @@
     "ityped": "^1.0.3",
     "katex": "^0.16.11",
     "mark.js": "^8.11.1",
-    "mermaid": "^11.2.1",
+    "mermaid": "^9.2.1",
     "plyr": "^3.7.2",
     "popper.js": "^1.16.1",
     "postcss": "^8.4.41",

Daži faili netika attēloti, jo izmaiņu fails ir pārāk liels