-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements missing features of the Codeblock component in NeetoWebsit…
…e custom codeblock component. (#1278) * Updated the codeblock highlight script to inject a copy button to the codeblock. * Updated the codeblock component to render line numbers along with the line. * Cleanedup the codeblock line and code spacing. * Fixed the codeblock usability issues in the editor. * Fixed code block line higlighting not working in the bigbinary website. * Fixed missing line numbers when there was no line highlighting in the EditorContent component. --------- Co-authored-by: Praveen Murali <[email protected]>
- Loading branch information
1 parent
f60cfb4
commit e501e7b
Showing
6 changed files
with
146 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,22 +4,72 @@ import hljs from "highlight.js/lib/common"; | |
const highlightLinesScriptCDNPath = | ||
"//cdn.jsdelivr.net/gh/TRSasasusu/[email protected]/highlightjs-highlight-lines.min.js"; | ||
|
||
function applyLineHighlighting(codeElement) { | ||
hljs.highlightElement(codeElement); | ||
const preElement = codeElement.closest("pre"); | ||
const getHighlightedLines = (preNode, codeNode) => { | ||
const highlightedLines = | ||
preNode?.getAttribute("data-highlighted-lines") ?? | ||
codeNode?.getAttribute("data-highlighted-lines"); | ||
|
||
return highlightedLines?.split(",")?.map(Number) ?? [0]; | ||
}; | ||
|
||
const copyToClipboard = text => { | ||
try { | ||
if (navigator.clipboard && window.isSecureContext) { | ||
navigator.clipboard.writeText(text); | ||
} else { | ||
const textArea = document.createElement("textarea"); | ||
textArea.value = text; | ||
|
||
const highlightedLines = preElement.getAttribute("data-highlighted-lines"); | ||
if (highlightedLines) { | ||
const linesToHighlight = highlightedLines.split(",").map(Number); | ||
const highlightLinesOptions = linesToHighlight | ||
.filter(line => line > 0) | ||
.map(line => ({ | ||
start: line, | ||
end: line, | ||
color: "rgba(255, 255, 0, 0.2)", | ||
})); | ||
hljs.highlightLinesElement(codeElement, highlightLinesOptions); | ||
textArea.style.top = "0"; | ||
textArea.style.left = "0"; | ||
textArea.style.position = "fixed"; | ||
|
||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
|
||
document.execCommand("copy"); | ||
document.body.removeChild(textArea); | ||
} | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log(error); | ||
} | ||
}; | ||
|
||
const addCopyToClipboardButton = codeElement => { | ||
const copyButton = document.createElement("button"); | ||
copyButton.setAttribute( | ||
"class", | ||
"text-primary-grey absolute right-3 top-3 text-xs copy-button" | ||
); | ||
copyButton.innerText = "Copy"; | ||
|
||
copyButton.addEventListener("click", () => { | ||
copyToClipboard(codeElement.textContent?.trim()); | ||
copyButton.innerText = "Copied"; | ||
setTimeout(() => { | ||
copyButton.innerText = "Copy"; | ||
}, 2500); | ||
}); | ||
|
||
codeElement.parentNode.appendChild(copyButton); | ||
}; | ||
|
||
function applyCodeblockDecorations(codeElement) { | ||
const preElement = codeElement.closest("pre"); | ||
hljs.highlightElement(codeElement); | ||
addCopyToClipboardButton(codeElement); | ||
|
||
const linesToHighlight = getHighlightedLines(preElement, codeElement); | ||
const highlightLinesOptions = linesToHighlight | ||
.filter(line => line > 0) | ||
.map(line => ({ | ||
start: line, | ||
end: line, | ||
color: "rgba(255, 255, 0, 0.2)", | ||
})); | ||
hljs.highlightLinesElement(codeElement, highlightLinesOptions); | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
|
@@ -28,6 +78,6 @@ document.addEventListener("DOMContentLoaded", () => { | |
script.async = true; | ||
document.head.appendChild(script); | ||
script.addEventListener("load", () => { | ||
document.querySelectorAll("pre code").forEach(applyLineHighlighting); | ||
document.querySelectorAll("pre code").forEach(applyCodeblockDecorations); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters