Skip to content

Commit ed352f6

Browse files
authored
Merge pull request mendix#7423 from mendix/db-copy-anchor
Copy anchor to clipboard
2 parents 3ba581c + 5e9705f commit ed352f6

File tree

3 files changed

+67
-43
lines changed

3 files changed

+67
-43
lines changed

assets/js/anchor.js

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
1-
/*
2-
* Copyright 2018 Google LLC
3-
Licensed under the Apache License, Version 2.0 (the "License");
4-
you may not use this file except in compliance with the License.
5-
You may obtain a copy of the License at
6-
https://www.apache.org/licenses/LICENSE-2.0
7-
Unless required by applicable law or agreed to in writing, software
8-
distributed under the License is distributed on an "AS IS" BASIS,
9-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10-
See the License for the specific language governing permissions and
11-
limitations under the License.
12-
*/
1+
'use strict';
132

14-
(function ($) {
15-
'use strict';
3+
document.addEventListener('DOMContentLoaded', () => {
4+
// Append anchor links to headings in Markdown
5+
const article = document.getElementsByTagName('main')[0];
6+
if (!article) {
7+
return;
8+
}
169

17-
// Headers' anchor link that shows on hover
18-
$(function () {
19-
// append anchor links to headings in markdown.
20-
var article = document.getElementsByTagName('main')[0];
21-
if (!article) {
22-
return;
23-
}
24-
var headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
25-
headings.forEach(function (heading) {
26-
if (heading.id) {
27-
var a = document.createElement('a');
28-
// set visibility: hidden, not display: none to avoid layout change
29-
a.style.visibility = 'hidden';
30-
// [a11y] hide this from screen readers, etc..
31-
a.setAttribute('aria-hidden', 'true');
32-
// material insert_link icon in svg format
33-
a.innerHTML = ' <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="24" height="24" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>';
34-
a.href = '#' + heading.id;
35-
heading.insertAdjacentElement('beforeend', a);
36-
heading.addEventListener('mouseenter', function () {
37-
a.style.visibility = 'initial';
38-
});
39-
heading.addEventListener('mouseleave', function () {
40-
a.style.visibility = 'hidden';
41-
});
42-
}
43-
});
44-
});
10+
// Select all headings in the article
11+
const headings = article.querySelectorAll('h1, h2, h3, h4, h5, h6');
12+
headings.forEach((heading) => {
13+
if (heading.id) {
14+
const a = document.createElement('a');
15+
a.setAttribute('aria-hidden', 'true');
16+
a.classList.add('anchor-icon'); // Add anchor-icon class (_styles_project.scss)
17+
a.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" fill="var(--bs-body-color)" width="20" height="20" viewBox="0 0 32 32"><path d="M18.9001 7.22C20.5201 5.6 23.1601 5.6 24.7801 7.22C26.4001 8.84 26.4001 11.48 24.7801 13.1L20.4001 17.48L21.8101 18.89L26.1901 14.51C28.5901 12.11 28.5901 8.2 26.1901 5.8C23.7901 3.4 19.8801 3.4 17.4801 5.8L13.1001 10.18L14.5101 11.59L18.8901 7.21L18.9001 7.22Z"/><path d="M10.16 28C11.74 28 13.32 27.4 14.52 26.2L18.9 21.82L17.49 20.41L13.11 24.79C11.49 26.41 8.85002 26.41 7.23002 24.79C5.61002 23.17 5.61002 20.53 7.23002 18.91L11.61 14.53L10.2 13.12L5.82002 17.5C3.42002 19.9 3.42002 23.81 5.82002 26.21C7.02002 27.41 8.60002 28.01 10.18 28.01L10.16 28Z"/><path d="M9.44971 21.1336L21.124 9.45927L22.5383 10.8735L10.8639 22.5478L9.44971 21.1336Z"/></svg>`;
18+
a.href = '#' + heading.id;
4519

46-
}(jQuery));
20+
// Append anchor element to heading
21+
heading.appendChild(a);
22+
23+
// Show icon when hovering over heading
24+
heading.addEventListener('mouseenter', () => {
25+
a.style.display = 'inline-block';
26+
});
27+
28+
// Hide icon when mouse leaves heading
29+
heading.addEventListener('mouseleave', () => {
30+
a.style.display = 'none';
31+
});
32+
33+
// Initialize Bootstrap tooltip
34+
const tooltip = new bootstrap.Tooltip(a, {
35+
title: 'Copy link to clipboard',
36+
placement: 'right',
37+
trigger: 'hover focus'
38+
});
39+
40+
// Add on-click behavior
41+
a.addEventListener('click', (event) => {
42+
event.preventDefault(); // Prevent scrolling
43+
const href = a.href;
44+
navigator.clipboard.writeText(href); // Copy to clipboard
45+
window.history.pushState({}, document.title, href); // Update URL
46+
// Update tooltip text
47+
const tooltipInner = document.querySelector('div.tooltip-inner');
48+
if (tooltipInner) {
49+
tooltipInner.textContent = 'Copied!';
50+
}
51+
});
52+
}
53+
});
54+
});

assets/scss/_styles_project.scss

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,19 @@
100100
padding-bottom: 0px;
101101
}
102102

103+
// Anchor icon styling for anchor.js
104+
105+
.anchor-icon {
106+
display: none;
107+
padding: 0rem .3rem .1rem .3rem;
108+
margin: -.5rem 0rem -.5rem .3rem;
109+
border-radius: 4px;
110+
}
111+
112+
.anchor-icon:hover {
113+
background: $gray-light;
114+
}
115+
103116
// _code.scss - Code formatting
104117

105118
.td-content {

assets/scss/_variables_project.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,8 @@ $navbar-dark-hover-color: rgba($white, 0.5) !default;
165165
$navbar-dark-active-color: $white !default;
166166
$navbar-dark-disabled-color: rgba($white, 0.25) !default;
167167

168+
// Tooltip
169+
$tooltip-bg: $secondary;
170+
168171
// The yiq lightness value that determines when the lightness of color changes from "dark" to "light".
169172
$yiq-contrasted-threshold: 200 !default;

0 commit comments

Comments
 (0)