Skip to content

Commit

Permalink
chore: Update sidebar styling and behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
jmservera committed May 24, 2024
1 parent 6c40936 commit 5a78cbc
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 140 deletions.
31 changes: 31 additions & 0 deletions web/_includes/_code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function codeCopy() {
$("pre code").each(function () {
var button = $("<button>")
.attr("type", "button")
.attr("title", "Copy")
.addClass("copy-button")
.text("📋");
$(this).parent().prepend(button);
});

$(".copy-button").click(function () {
let btn = $(this);
var code = $(this).siblings("code").text();
var position = $(this).offset();

navigator.clipboard.writeText(code).then(
function () {
btn.text("✅");
setTimeout(() => btn.text("📋"), 1800);
},
function () {
// failure
alert("Failed to copy text");
}
);
});
}

$(document).ready(function() {
codeCopy();
});
126 changes: 126 additions & 0 deletions web/_includes/_custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
function hierarchyMenu(steps, changeIndex){
var hierarchy = [$('<ul>')];
var currentLevel = 2;

var headings = $('.page-content h2, .page-content h3');
headings.each(function(i, heading) {
var level = parseInt(heading.tagName.slice(1));
var id = $(heading).attr('id');
var text = $(heading).text();
var link = $('<a>').attr('href', `#${id}`).text(text);

//TODO: manage hashchange at main level
$(window).on('hashchange', function(e){
if(location.hash==`#${id}`){
var step;
if(level==2){
step=$($(heading).parents('.section')[0]).find('.step:first')[0];
}
else{
step=$(heading).parents('.step')[0];
}
let current=steps.index(step);
changeIndex(current);
}
});

var item = $('<li>').append(link);

if (level > currentLevel) {
var newLevel = $('<ul>');
// var button = $('<button>').attr('type','button').addClass('collapser').text('+').click(function() {
// newLevel.toggle();
// button.text(newLevel.is(':visible') ? '-' : '+');
// });
hierarchy[hierarchy.length - 1].children().last().addClass('collapser').addClass('uncollapsedMarker');
hierarchy[hierarchy.length - 1].children().last().append(newLevel);
hierarchy.push(newLevel);
} else if (level < currentLevel) {
hierarchy.pop();
}

hierarchy[hierarchy.length - 1].append(item);
currentLevel = level;
});

$('#sidebar-index').append(hierarchy[0]);
$('#sidebar-toggle').click(function() {
$('#sidebar-index').toggleClass('collapsed');
$('#sidebar-index').hasClass('collapsed') ? $(this).text('<') : $(this).text('>');
});

$('#sidebar-index .collapser').on('click', function(event) {
// This code will run when the caret is clicked
if (event.target.tagName === 'A') {
return;
}
$(this).children('ul').toggle();
// Check if the parent li has the class 'collapsedMarker' or 'uncollapsedMarker'
if ($(this).hasClass('collapsedMarker')) {
// If it has the class 'collapsedMarker', remove it and add the class 'uncollapsedMarker'
$(this).removeClass('collapsedMarker').addClass('uncollapsedMarker');
} else if ($(this).hasClass('uncollapsedMarker')) {
// If it has the class 'uncollapsedMarker', remove it and add the class 'collapsedMarker'
$(this).removeClass('uncollapsedMarker').addClass('collapsedMarker');
}
});
}



function tutorial(){
let steps = $('.step');
let currentIndex = 0;

// Show the first section
steps[currentIndex].classList.add('active');
$(steps[currentIndex]).parents('.section')[0].classList.add('active');

let changeIndex= (index)=>{
$(steps[currentIndex]).parents('.section')[0].classList.remove('active');
steps[currentIndex].classList.remove('active');
currentIndex=index;
steps[currentIndex].classList.add('active');
$(steps[currentIndex]).parents('.section')[0].classList.add('active');
if(currentIndex>0){
$('button.prev').removeAttr('disabled');
}
else{
$('button.prev').attr('disabled','disabled');
}
if(currentIndex<steps.length-1){
$('button.next').removeAttr('disabled');
}
else{
$('button.next').attr('disabled','disabled');
}
};

let movePrev=() => {
if (currentIndex > 0) {
changeIndex(currentIndex-1);
}
};
let moveNext= () => {
if (currentIndex < steps.length - 1) {
changeIndex(currentIndex+1);
}
}

$('button.prev').each((i,btn)=>btn.addEventListener('click',movePrev,false));
$('button.next').each((i,btn)=>btn.addEventListener('click',moveNext,false));

changeIndex(0);

hierarchyMenu(steps,changeIndex);
}

$(document).ready(function() {
tutorial();
//TODO: Fix the issue with the hashchange event
if(location.hash){
let oldLoc=location.href;
window.location=location+"&";
window.location=oldLoc;
}
});
38 changes: 31 additions & 7 deletions web/_sass/_sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,37 @@
z-index: 1;
}

#sidebar-index .collapser{
position:absolute;
display: block;
margin-left: -1.5em;
margin-top: -4em;
padding: 0;
z-index: 1;
// #sidebar-index .collapser{
// position:absolute;
// display: block;
// margin-left: -1.5em;
// margin-top: -4em;
// padding: 0;
// z-index: 1;
// }

li.uncollapsedMarker {
list-style-type: none;
position: relative;

&::before {
content: "-";
position: absolute;
left: -1.2em;
top: 0em;
}
}

li.collapsedMarker {
list-style-type: none;
position: relative;

&::before {
content: "+";
position: absolute;
left: -1.2em;
top: 0em;
}
}

#sidebar-index a {
Expand Down
135 changes: 5 additions & 130 deletions web/assets/js/custom.js
Original file line number Diff line number Diff line change
@@ -1,130 +1,5 @@
function hierarchyMenu(steps, changeIndex){
var hierarchy = [$('<ul>')];
var currentLevel = 2;

var headings = $('.page-content h2, .page-content h3');
headings.each(function(i, heading) {
var level = parseInt(heading.tagName.slice(1));
var id = $(heading).attr('id');
var text = $(heading).text();
var link = $('<a>').attr('href', `#${id}`).text(text);

//TODO: manage hashchange at main level
$(window).on('hashchange', function(e){
if(location.hash==`#${id}`){
var step;
if(level==2){
step=$($(heading).parents('.section')[0]).find('.step:first')[0];
}
else{
step=$(heading).parents('.step')[0];
}
let current=steps.index(step);
changeIndex(current);
}
});

var item = $('<li>').append(link);

if (level > currentLevel) {
var newLevel = $('<ul>');
var button = $('<button>').attr('type','button').addClass('collapser').text('+').click(function() {
newLevel.toggle();
button.text(newLevel.is(':visible') ? '-' : '+');
});
hierarchy[hierarchy.length - 1].children().last().append(button);
hierarchy[hierarchy.length - 1].children().last().append(newLevel);
hierarchy.push(newLevel);
} else if (level < currentLevel) {
hierarchy.pop();
}

hierarchy[hierarchy.length - 1].append(item);
currentLevel = level;
});

$('#sidebar-index').append(hierarchy[0]);
$('#sidebar-toggle').click(function() {
$('#sidebar-index').toggleClass('collapsed');
$('#sidebar-index').hasClass('collapsed') ? $(this).text('<') : $(this).text('>');
});
}

function codeCopy(){
$('pre code').each(function() {
var button = $('<button>').attr('type', 'button').attr('title','Copy').addClass('copy-button').text('📋');
$(this).parent().prepend(button);
});

$('.copy-button').click(function() {
let btn = $(this);
var code = $(this).siblings('code').text();
var position = $(this).offset();

navigator.clipboard.writeText(code).then(function() {
btn.text('✅');
setTimeout(()=>btn.text('📋'),1800);
}, function() {
// failure
alert('Failed to copy text');
});
});
}

function tutorial(){
let steps = $('.step');
let currentIndex = 0;

// Show the first section
steps[currentIndex].classList.add('active');
$(steps[currentIndex]).parents('.section')[0].classList.add('active');

let changeIndex= (index)=>{
$(steps[currentIndex]).parents('.section')[0].classList.remove('active');
steps[currentIndex].classList.remove('active');
currentIndex=index;
steps[currentIndex].classList.add('active');
$(steps[currentIndex]).parents('.section')[0].classList.add('active');
if(currentIndex>0){
$('button.prev').removeAttr('disabled');
}
else{
$('button.prev').attr('disabled','disabled');
}
if(currentIndex<steps.length-1){
$('button.next').removeAttr('disabled');
}
else{
$('button.next').attr('disabled','disabled');
}
};

let movePrev=() => {
if (currentIndex > 0) {
changeIndex(currentIndex-1);
}
};
let moveNext= () => {
if (currentIndex < steps.length - 1) {
changeIndex(currentIndex+1);
}
}

$('button.prev').each((i,btn)=>btn.addEventListener('click',movePrev,false));
$('button.next').each((i,btn)=>btn.addEventListener('click',moveNext,false));

changeIndex(0);

hierarchyMenu(steps,changeIndex);
}

$(document).ready(function() {
codeCopy();
tutorial();
//TODO: Fix the issue with the hashchange event
if(location.hash){
let oldLoc=location.href;
window.location=location+"&";
window.location=oldLoc;
}
});
---
---

{% include _custom.js %}
{% include _code.js %}
7 changes: 4 additions & 3 deletions web/promptlab.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,6 @@ We hope you find this lab engaging and insightful. We encourage you to continue

Happy prompting!
![You are the Prompt Hero](./img/prompt%20hero%20badge.png)
</div>
</div>
<div class="section" markdown="1">

### Additional Resources

Expand All @@ -342,6 +339,10 @@ If you want to learn more, here you have a few interesting resources to extend y
* [OpenAI Prompt Engineering](https://platform.openai.com/docs/guides/prompt-engineering/prompt-engineering): A guide to prompt engineering by the GPT-4 creators.
* [Prompt Engineering Techniques](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/advanced-prompt-engineering): Tips and tricks for prompting with Azure OpenAI Service.

</div>
</div>
<div class="section" markdown="1">

## Glossary

<div class="step" markdown="1">
Expand Down

0 comments on commit 5a78cbc

Please sign in to comment.