Skip to content
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

Table of contents menu for wikis #9711

Merged
merged 1 commit into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions app/assets/stylesheets/wiki.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,57 @@ a.navbar-offset {
visibility: hidden;
}

#toc-menu:hover, .toc-active {
background-color: #cbd5e0;
}

.js-toc {
max-height: 339px;
max-width: 298px;
top: 40px;
overflow: auto;
line-height: 2;
border: 1px solid #e1e4e8;
}

.toc-list {
list-style: none;
background-color: #fff;
z-index: 2000;
margin-bottom: 0;
padding: 0px 15px 15px 15px;
}

.is-collapsible {
padding-left: 20px;
padding-bottom: 5px;
list-style: none;
}

.toc-list-item {
padding: 0;
font-weight: initial;
text-align: start;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

.toc-list-item a {
color: #343a40;
text-decoration: none;
}

.toc-list-item a:hover {
color: #0c82c4;
}

@media (max-width: 606px) {
.js-toc {
max-width: 260px;
}
}

@media (max-width: 768px) {
.add-subscriptions {
margin-top:10px;
Expand Down
1 change: 1 addition & 0 deletions app/javascript/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ console.log('Hello World from Webpacker')
var componentRequireContext = require.context("components", true);
var ReactRailsUJS = require("react_ujs");
ReactRailsUJS.useContext(componentRequireContext);
const tocbot = require('tocbot');
4 changes: 4 additions & 0 deletions app/views/wiki/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<a aria-label="Practice in a realtime doc" href="/talk/<%= @node.slug_from_path %>" rel="tooltip" title="Practice in a realtime doc." data-placement="top"><i class="fa fa-comment"></i></a> |
<a aria-label="Link to wiki" href="/n/<%= @node.id %>"><i style="color:#888;" class="fa fa-link"></i></a> <span class="d-none d-xl-inline"><a href="/n/<%= @node.id %>">#<%= @node.id %></a></span>
</span>
<details class="d-inline-flex flex-column position-relative">
<summary id="toc-menu" class="btn btn-light" role="button" aria-label="Table of Contents" aria-haspopup="menu"><i class="fa fa-list-ul" aria-hidden="true"></i></summary>
<span class="js-toc position-absolute shadow p-0 pt-4 rounded" role="menu"></span>
</details>
</div>
</div>
</div>
Expand Down
32 changes: 31 additions & 1 deletion app/views/wiki/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<div class="row">

<div class="col-lg-9">
<div class="col-lg-9" id="main-content">

<%= render partial: "wiki/header" %>

Expand Down Expand Up @@ -75,5 +75,35 @@
<% end %>

setupWiki(<%= @node.id %>, "<%= @node.title %>", <%= params[:raw] == 'true' %>, <%= current_user.nil? != true %>);

$('#main-content h1, h2, h3, h4, h5, h6').each(function (){
//convert the heading's text to lowercase and remove all spaces
let headingId = $(this).text().toLowerCase().replace(/\s/g, '');
$(this).attr('id', headingId);
});

tocbot.init({
// Where to render the table of contents.
tocSelector: '.js-toc',
// Where to grab the headings to build the table of contents.
contentSelector: '#main-content',
// Which headings to grab inside of the contentSelector element.
headingSelector: 'h1, h2, h3, h4, h5, h6',
// For headings inside relative or absolute positioned containers within content.
hasInnerContainers: true,
});

$('.toc-list li').first().css('font-weight', '600');

$(document).on('click', function (e) {
let tocBtn = $('#toc-menu')
if(!tocBtn.is(e.target) && tocBtn.has(e.target).length === 0) {
//closes the menu when there's a click outside the menu
$('details').removeAttr('open');
tocBtn.removeClass('toc-active');
} else {
tocBtn.addClass('toc-active');
}
});
});
</script>
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"react_ujs": "^2.6.1",
"short-code-forms": "jywarren/short-code-forms#~0.0.1",
"simple-data-grapher": "^2.0.0",
"tocbot": "^4.12.3",
"typeahead.js": "^0.11.1",
"typeahead.js-browserify": "Javier-Rotelli/typeahead.js-browserify#~1.0.7",
"urlhash": "^0.1.3",
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12893,6 +12893,11 @@ to-space-case@0.1.2:
dependencies:
to-no-case "0.1.1"

tocbot@^4.12.3:
version "4.12.3"
resolved "https://registry.yarnpkg.com/tocbot/-/tocbot-4.12.3.tgz#173dd144835d54f4c60cc57b2dbe569e10df0b74"
integrity sha512-1uA4rNZgY3a9OjwmIV076ksNr72PdxlLj7DD+0pKXUh1vNf6/GRL8nJT4ZtmhX9NRVPLPOehe0NEmwf2ukDTSg==

toidentifier@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
Expand Down