-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add snippet includes feature. Introduce new snippet UX/UI. #13217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,128 @@ | ||
package dotty.tools.scaladoc | ||
|
||
import org.scalajs.dom._ | ||
import org.scalajs.dom.ext._ | ||
|
||
class CodeSnippets: | ||
|
||
private def getButtonsSection(snippet: html.Element): Option[html.Div] = snippet.querySelector("div.buttons") match { | ||
case div: html.Div => Some(div) | ||
case _ => None | ||
} | ||
|
||
def enrichSnippets() = document.querySelectorAll("div.snippet").foreach { | ||
case snippet: html.Element => | ||
snippetAnchor(snippet) | ||
handleHideableCode(snippet) | ||
handleImportedCode(snippet) | ||
copyRunButtons(snippet) | ||
} | ||
|
||
private def handleHideableCode(snippet: html.Element): Unit = { | ||
def toggleHide(e: html.Element | html.Document) = e.querySelectorAll(".hideable").foreach { | ||
case e: html.Element => e.classList.toggle("hidden") | ||
case _ => | ||
} | ||
def createShowHideButton(toggleRoot: html.Element) = { | ||
val div = document.createElement("div") | ||
div.classList.add("snippet-showhide") | ||
val p = document.createElement("p") | ||
p.textContent = "Show collapsed lines" | ||
val showHideButton = document.createElement("label") | ||
showHideButton.classList.add("snippet-showhide-button") | ||
val checkbox = document.createElement("input").asInstanceOf[html.Input] | ||
checkbox.`type` = "checkbox" | ||
val slider = document.createElement("span") | ||
slider.classList.add("slider") | ||
showHideButton.appendChild(checkbox) | ||
showHideButton.appendChild(slider) | ||
checkbox.addEventListener("change", _ => toggleHide(toggleRoot)) | ||
div.appendChild(showHideButton) | ||
div.appendChild(p) | ||
div | ||
} | ||
|
||
toggleHide(snippet) | ||
val buttonsSection = getButtonsSection(snippet) | ||
val hideables = snippet.querySelectorAll(".hideable") | ||
if hideables != null && hideables.nonEmpty then { | ||
val showHideButton = createShowHideButton(snippet) | ||
buttonsSection.foreach(_.appendChild(showHideButton)) | ||
} | ||
} | ||
|
||
private def snippetAnchor(snippet: html.Element): Unit = snippet.querySelector(".snippet-meta .snippet-label") match { | ||
case e: html.Element => | ||
val name = e.textContent.trim | ||
val anchor = document.createElement("a").asInstanceOf[html.Anchor] | ||
anchor.id = s"snippet-$name" | ||
snippet.insertBefore(anchor, snippet.firstChild) | ||
case _ => | ||
} | ||
|
||
private def handleImportedCode(snippet: html.Element): Unit = { | ||
val included = snippet.querySelectorAll("code span.include") | ||
val pre = snippet.querySelector("pre") | ||
if included != null && included.nonEmpty && pre != null then { | ||
val includesDiv = document.createElement("div") | ||
includesDiv.classList.add("included-section") | ||
includesDiv.classList.add("hideable") | ||
included | ||
.collect { case e: html.Element => e } | ||
.toList | ||
.filter(_.hasAttribute("name")) | ||
.map(_.getAttribute("name")) | ||
.distinct | ||
.map { name => | ||
val a = document.createElement("a").asInstanceOf[html.Anchor] | ||
a.classList.add("unselectable") | ||
a.href = s"#snippet-$name" | ||
a.innerHTML = s"included <b>$name</b>" | ||
a | ||
} | ||
.foreach(a => includesDiv.appendChild(a)) | ||
|
||
snippet.insertBefore(includesDiv, pre) | ||
} | ||
} | ||
|
||
private def copyRunButtons(snippet: html.Element) = { | ||
def copyButton = { | ||
val div = document.createElement("div") | ||
val button = document.createElement("button") | ||
val icon = document.createElement("i") | ||
icon.classList.add("far") | ||
icon.classList.add("fa-clone") | ||
button.appendChild(icon) | ||
button.classList.add("copy-button") | ||
button.addEventListener("click", _ => { | ||
val code = snippet.querySelectorAll("code>span:not(.hidden)") | ||
.map(_.textContent) | ||
.mkString | ||
window.navigator.clipboard.writeText(code) | ||
}) | ||
div.appendChild(button) | ||
div | ||
} | ||
def runButton = { | ||
val div = document.createElement("div") | ||
val button = document.createElement("button").asInstanceOf[html.Button] | ||
val icon = document.createElement("i") | ||
icon.classList.add("fas") | ||
icon.classList.add("fa-play") | ||
button.appendChild(icon) | ||
button.classList.add("run-button") | ||
button.addEventListener("click", _ => {}) // TODO: Run button #13065 | ||
button.disabled = true | ||
div.appendChild(button) | ||
div | ||
} | ||
val buttonsSection = getButtonsSection(snippet) | ||
buttonsSection.foreach(s => | ||
s.appendChild(copyButton) | ||
s.appendChild(runButton) | ||
) | ||
} | ||
|
||
enrichSnippets() | ||
|
28 changes: 0 additions & 28 deletions
28
scaladoc-js/src/searchbar/code-snippets/CodeSnippets.scala
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.