-
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.
Adds highlight lines feature to the code block. (#1233)
* Updated the CodeBlock component to render the highlight button when there's a text selection. * Implemented the handle highlight function to set the selected line numbers as data attributes. * Created a plugin that can add highlight style to the selected lines of code. * Added the hightlight plugin and addAttributes to set the attributes based on the line selection. * Fixed editor content ignoring existing highlight information. * Implemented the code highlight preview feature in EditorContent. * Modify the handle selection callback to properly toggle the highlight based on the selection. * Added tooltip text to the highlight button. * Added highlightLine feature to the codeBlockhight.js * Added a filter to prevent setting line number zeros to highlight js. * Added css style to style the highlighted code in the code block component. * Minor improvements to the code while reviewing. * Added before and after to the higlighted lines to extend it beyond the container and hide padding.
- Loading branch information
1 parent
7c03a33
commit 7e5300f
Showing
10 changed files
with
295 additions
and
11 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
73 changes: 73 additions & 0 deletions
73
src/components/Editor/CustomExtensions/CodeBlock/plugins.js
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* eslint-disable @bigbinary/neeto/file-name-and-export-name-standards */ | ||
import { Plugin, PluginKey } from "prosemirror-state"; | ||
import { Decoration, DecorationSet } from "prosemirror-view"; | ||
|
||
const codeBlockHighlightKey = new PluginKey("codeBlockHighlight"); | ||
|
||
function createLineDecoration(from, lineNumber) { | ||
return Decoration.widget(from, view => { | ||
const line = document.createElement("div"); | ||
line.className = "highlighted-line"; | ||
line.setAttribute("data-line-number", lineNumber.toString()); | ||
|
||
// Set the height of the highlight to match the line height | ||
const lineElement = view.domAtPos(from).node; | ||
if (lineElement) { | ||
const lineHeight = window.getComputedStyle(lineElement).lineHeight; | ||
line.style.height = lineHeight; | ||
} | ||
|
||
return line; | ||
}); | ||
} | ||
|
||
function getLineRanges(node, pos) { | ||
const lines = node.textContent.split("\n"); | ||
let currentPos = pos + 1; // +1 to skip the opening tag of the code block | ||
|
||
return lines.map((line, index) => { | ||
const from = currentPos; | ||
const to = from + line.length; // +1 for newline, except last line | ||
currentPos = to + (index < lines.length - 1 ? 1 : 0); | ||
|
||
return { from, to, lineNumber: index + 1 }; | ||
}); | ||
} | ||
|
||
const codeBlockHighlightPlugin = new Plugin({ | ||
key: codeBlockHighlightKey, | ||
state: { | ||
init() { | ||
return DecorationSet.empty; | ||
}, | ||
apply(tr, set) { | ||
set = set.map(tr.mapping, tr.doc); | ||
|
||
if (tr.getMeta(codeBlockHighlightKey)) { | ||
const decorations = []; | ||
tr.doc.descendants((node, pos) => { | ||
if (!(node.type.name === "codeBlock")) return; | ||
const highlightedLines = node.attrs.highlightedLines || []; | ||
const lineRanges = getLineRanges(node, pos); | ||
|
||
lineRanges.forEach(({ from, lineNumber }) => { | ||
if (highlightedLines.includes(lineNumber)) { | ||
decorations.push(createLineDecoration(from, lineNumber)); | ||
} | ||
}); | ||
}); | ||
|
||
return DecorationSet.create(tr.doc, decorations); | ||
} | ||
|
||
return set; | ||
}, | ||
}, | ||
props: { | ||
decorations(state) { | ||
return this.getState(state); | ||
}, | ||
}, | ||
}); | ||
|
||
export { codeBlockHighlightPlugin, codeBlockHighlightKey }; |
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 |
---|---|---|
@@ -1,7 +1,33 @@ | ||
/* eslint-disable @bigbinary/neeto/file-name-and-export-name-standards */ | ||
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 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); | ||
} | ||
} | ||
|
||
document.addEventListener("DOMContentLoaded", () => { | ||
document | ||
.querySelectorAll("pre code") | ||
.forEach(element => hljs.highlightElement(element)); | ||
const script = document.createElement("script"); | ||
script.src = highlightLinesScriptCDNPath; | ||
script.async = true; | ||
document.head.appendChild(script); | ||
script.addEventListener("load", () => { | ||
document.querySelectorAll("pre code").forEach(applyLineHighlighting); | ||
}); | ||
}); |
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.ne-codeblock-nodeview-wrapper { | ||
.highlighted-line { | ||
background-color: rgba(255, 255, 0, 0.2); | ||
position: absolute; | ||
left: 0; | ||
right: 0; | ||
z-index: 0; | ||
pointer-events: none; | ||
} | ||
} |
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
Oops, something went wrong.