Skip to content

Commit f5cd0d9

Browse files
silverwindlafriksKN4CK3R
authored
Add Mermaid copy button, avoid unnecessary tooltip hide (#22225)
- Add Copy button to mermaid diagrams which copies their source. - Set tippy to not hide on click and avoid tooltip re-creation for temporary tooltips. This avoids hide and show when copying repo url. Popovers still hide the tooltip as usual. <img width="815" alt="Screenshot 2022-12-23 at 14 02 32" src="https://user-images.githubusercontent.com/115237/209341696-98e30953-f246-46d9-9157-2ececfd791c9.png"> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: KN4CK3R <[email protected]>
1 parent 3bd49f7 commit f5cd0d9

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

Diff for: web_src/js/markup/codecopy.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import {svg} from '../svg.js';
22

3-
export function renderCodeCopy() {
4-
const els = document.querySelectorAll('.markup .code-block code');
5-
if (!els.length) return;
6-
3+
export function makeCodeCopyButton() {
74
const button = document.createElement('button');
85
button.classList.add('code-copy', 'ui', 'button');
96
button.innerHTML = svg('octicon-copy');
7+
return button;
8+
}
9+
10+
export function renderCodeCopy() {
11+
const els = document.querySelectorAll('.markup .code-block code');
12+
if (!els.length) return;
1013

1114
for (const el of els) {
12-
const btn = button.cloneNode(true);
15+
const btn = makeCodeCopyButton();
1316
btn.setAttribute('data-clipboard-text', el.textContent);
1417
el.after(btn);
1518
}

Diff for: web_src/js/markup/mermaid.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import {isDarkTheme} from '../utils.js';
2+
import {makeCodeCopyButton} from './codecopy.js';
3+
24
const {mermaidMaxSourceCharacters} = window.config;
35

46
const iframeCss = `
@@ -58,7 +60,13 @@ export async function renderMermaid() {
5860
iframe.sandbox = 'allow-scripts';
5961
iframe.style.height = `${Math.ceil(parseFloat(heightStr))}px`;
6062
iframe.srcdoc = `<html><head><style>${iframeCss}</style></head><body>${svgStr}</body></html>`;
61-
el.closest('pre').replaceWith(iframe);
63+
const mermaidBlock = document.createElement('div');
64+
mermaidBlock.classList.add('mermaid-block');
65+
mermaidBlock.append(iframe);
66+
const btn = makeCodeCopyButton();
67+
btn.setAttribute('data-clipboard-text', source);
68+
mermaidBlock.append(btn);
69+
el.closest('pre').replaceWith(mermaidBlock);
6270
});
6371
} catch (err) {
6472
displayError(el, err);

Diff for: web_src/js/modules/tippy.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export function createTippy(target, opts = {}) {
66
placement: target.getAttribute('data-placement') || 'top-start',
77
animation: false,
88
allowHTML: false,
9+
hideOnClick: false,
910
interactiveBorder: 30,
1011
ignoreAttributes: true,
1112
maxWidth: 500, // increase over default 350px
@@ -46,7 +47,7 @@ export function showTemporaryTooltip(target, content) {
4647
}
4748

4849
tippy.setContent(content);
49-
tippy.show();
50+
if (!tippy.state.isShown) tippy.show();
5051
tippy.setProps({
5152
onHidden: (tippy) => {
5253
if (oldContent) {

Diff for: web_src/less/markup/codecopy.less

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.markup .code-block {
1+
.markup .code-block,
2+
.markup .mermaid-block {
23
position: relative;
34
}
45

@@ -26,7 +27,8 @@
2627
background: var(--color-secondary-dark-1) !important;
2728
}
2829

29-
.markup .code-block:hover .code-copy {
30+
.markup .code-block:hover .code-copy,
31+
.markup .mermaid-block:hover .code-copy {
3032
visibility: visible;
3133
animation: fadein .2s both;
3234
}

0 commit comments

Comments
 (0)