Skip to content

Commit 9058762

Browse files
committed
feat: 增加拷贝逻辑
1 parent 979935c commit 9058762

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed
Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,42 @@
1-
export function init(id) {
2-
const list = document.getElementById(id);
3-
console.log(list);
1+
import Data from '../../../_content/BootstrapBlazor/modules/data.js';
2+
import EventHandler from '../../../_content/BootstrapBlazor/modules/event-handler.js';
3+
import { copy } from '../../../_content/BootstrapBlazor/modules/utility.js';
4+
export function init(id) {
5+
const el = document.getElementById(id);
6+
const tooltips = [];
7+
[...el.querySelectorAll('svg')].forEach(s => {
8+
const use = s.querySelector('use');
9+
const url = use.getAttribute('href');
10+
const segs = url.split('#');
11+
if (segs.length == 2) {
12+
const title = segs[1];
13+
s.parentElement.setAttribute('data-bs-original-title', title);
14+
tooltips.push(new bootstrap.Tooltip(s.parentElement));
15+
}
16+
});
17+
EventHandler.on(el, 'click', 'div', e => {
18+
const div = e.delegateTarget;
19+
const href = div.querySelector('use').getAttribute('href');
20+
const text = `<svg xmlns="http://www.w3.org/2000/svg"><use href="${href}"></use></svg>`;
21+
copy(text);
22+
const tooltip = bootstrap.Tooltip.getInstance(div);
23+
tooltip.setContent({ '.tooltip-inner': 'Copy' });
24+
const handler = setTimeout(() => {
25+
clearTimeout(handler);
26+
tooltip.setContent({ '.tooltip-inner': div.getAttribute('data-bs-original-title') });
27+
}, 1000);
28+
});
29+
Data.set(id, {
30+
el,
31+
tooltips
32+
})
433
}
534

635
export function dispose(id) {
36+
const data = Data.get(id);
37+
Data.remove(id);
738

39+
const { el, tooltips } = data;
40+
EventHandler.off(el, 'click');
41+
tooltips.forEach(t => t.dispose());
842
}

0 commit comments

Comments
 (0)