Skip to content

Commit

Permalink
Add language selector to saved pastes.
Browse files Browse the repository at this point in the history
  • Loading branch information
EvieePy committed May 17, 2024
1 parent 6876140 commit 1006669
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 12 deletions.
57 changes: 53 additions & 4 deletions web/static/scripts/highlights.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
let pasteStores = [];

const HIGHLIGHT_AREAS = document.querySelectorAll(".pasteArea");
const LANGUAGES = hljs.listLanguages();
let DlCount = 0;

const codes = document.querySelectorAll("pre > code");
for (let code of codes) {
for (let area of HIGHLIGHT_AREAS) {
let code = area.querySelector("pre > code");
pasteStores.push(code.textContent);

// Highlight Code Block and get Language Details...
let details = hljs.highlightAuto(code.textContent);
let highlightedLang = details.language ? details.language : "plaintext";

code.innerHTML = details.value;

// Add Line Numbers...
hljs.lineNumbersBlock(code, { singleLine: true });

let header = area.querySelector(".pasteHeader");
let langOpts = "";

for (let lang of LANGUAGES) {
if (lang == highlightedLang) {
continue
}
langOpts += `<option value="${lang}">${lang}</option>`
}

langOpts = `<option value="${highlightedLang}">${highlightedLang}</option>\n${langOpts}`
let html = `
<div class="langSelectContainer">
<label>
<input id="__lang_select_${DlCount}" list="langs" name="langSelect" placeholder="${highlightedLang}" onchange="changeLang(this, ${area.id}, ${DlCount})" /></label>
<datalist id="langs">
${langOpts}
</datalist>
</div>`

header.insertAdjacentHTML("beforeend", html);
DlCount++;
}

hljs.highlightAll();
hljs.initLineNumbersOnLoad({ singleLine: true });
function changeLang(inp, area, index) {
let chosen = inp.value;

if (!chosen) { return }
if (!LANGUAGES.includes(chosen)) { return }

if (inp.placeholder === chosen) { return }

let code = area.querySelector("pre > code");
let highlighted = hljs.highlight(pasteStores[index], { language: chosen });
code.innerHTML = highlighted.value;

// Add Line Numbers...
hljs.lineNumbersBlock(code, { singleLine: true });
inp.placeholder = chosen;
}
64 changes: 58 additions & 6 deletions web/static/scripts/highlightsHTMX.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,70 @@
let pasteStores = [];

const LANGUAGES = hljs.listLanguages();
let DlCount = 0;


document.addEventListener("htmx:afterRequest", function (evt) {
if (evt.detail.xhr.status != 200) {
return
}

if (evt.detail.target.id == "pastecontainer" || evt.detail.target.id == "content") {
const codes = document.querySelectorAll("pre > code");
for (let code of codes) {
const HIGHLIGHT_AREAS = document.querySelectorAll(".pasteArea");

for (let area of HIGHLIGHT_AREAS) {
let code = area.querySelector("pre > code");
pasteStores.push(code.textContent);

// Highlight Code Block and get Language Details...
let details = hljs.highlightAuto(code.textContent);
let highlightedLang = details.language ? details.language : "plaintext";

code.innerHTML = details.value;

// Add Line Numbers...
hljs.lineNumbersBlock(code, { singleLine: true });

let header = area.querySelector(".pasteHeader");
let langOpts = "";

for (let lang of LANGUAGES) {
if (lang == highlightedLang) {
continue
}
langOpts += `<option value="${lang}">${lang}</option>`
}

langOpts = `<option value="${highlightedLang}">${highlightedLang}</option>\n${langOpts}`
let html = `
<div class="langSelectContainer">
<label>
<input id="__lang_select_${DlCount}" list="langs" name="langSelect" placeholder="${highlightedLang}" onchange="changeLang(this, ${area.id}, ${DlCount})" /></label>
<datalist id="langs">
${langOpts}
</datalist>
</div>`

header.insertAdjacentHTML("beforeend", html);
DlCount++;
}

hljs.highlightAll();
hljs.initLineNumbersOnLoad({ singleLine: true });
}
});
});


function changeLang(inp, area, index) {
let chosen = inp.value;

if (!chosen) { return }
if (!LANGUAGES.includes(chosen)) { return }

if (inp.placeholder === chosen) { return }

let code = area.querySelector("pre > code");
let highlighted = hljs.highlight(pasteStores[index], { language: chosen });
code.innerHTML = highlighted.value;

// Add Line Numbers...
hljs.lineNumbersBlock(code, { singleLine: true });
inp.placeholder = chosen;
}
45 changes: 43 additions & 2 deletions web/static/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ textarea {
.identifierHeader {
display: flex;
flex-direction: row;
gap: 2rem;
gap: 1rem;
align-items: baseline;
}

.identifierHeaderLeft {
Expand All @@ -411,6 +412,7 @@ textarea {

.identifierHeaderLeft>a {
font-weight: 600;
text-decoration: none;
}

.identifierHeaderLeft>span {
Expand Down Expand Up @@ -498,6 +500,37 @@ textarea {
color: var(--color-security);
}

.langSelectContainer {
display: flex;
align-items: center;
}

.langSelectContainer>label {
background-color: var(--color-background--resizer);
color: var(--color-foreground);
padding: 0.25rem;
border-radius: 0.25rem;
border: 1px solid var(--color-foreground--border);
outline: none;
}

.langSelectContainer>label>input {
background-color: transparent;
color: var(--color-foreground--dim);
outline: none;
border: none;
padding: 0.25rem;
}

.langSelectContainer>label:hover {
cursor: pointer;
filter: brightness(1.1);
}

.langSelectContainer>label>input:hover {
cursor: pointer;
}

/* Theme Switch */
.themeSwitch {
--size: 1.5rem;
Expand Down Expand Up @@ -544,7 +577,7 @@ textarea {
.annotations {
font-size: 0.8em;
}

.savePaste {
padding: 0.75rem;
font-size: 0.8em;
Expand Down Expand Up @@ -588,4 +621,12 @@ textarea {
.pasteArea>textarea {
font-size: 0.8em;
}

.langSelectContainer>label {
max-width: 6rem;
}

.langSelectContainer>label>input {
max-width: 6rem;
}
}

0 comments on commit 1006669

Please sign in to comment.