Skip to content

Commit 46ecd8b

Browse files
committed
I realized I could cut down on the processing in the loop 🎠
1 parent 0e07b65 commit 46ecd8b

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

js/codeblock.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ class WorkerPool {
6363
}
6464
}
6565

66+
const createClipButton = () => {
67+
const clip = document.createElement('button');
68+
69+
clip.className = 'fa-copy clip-button';
70+
clip.setAttribute('aria-label', 'Copy to clipboard');
71+
clip.innerHTML = '<div class="tooltiptext"></div>';
72+
73+
const buttons = document.createElement('div');
74+
75+
buttons.className = 'buttons';
76+
buttons.insertBefore(clip, buttons.firstChild);
77+
78+
return buttons;
79+
};
80+
6681
const codeCopy = trigger => {
6782
const elem = trigger.target;
6883

@@ -99,17 +114,7 @@ export const codeBlock = async () => {
99114
return;
100115
}
101116

102-
// capture hover event in iOS
103-
if (globalThis.ontouchstart !== undefined) {
104-
document.addEventListener('touchstart', () => {}, { once: false, passive: true });
105-
}
106-
107-
const clip = document.createElement('button');
108-
109-
clip.className = 'fa-copy clip-button';
110-
clip.setAttribute('aria-label', 'Copy to clipboard');
111-
clip.innerHTML = '<i class="tooltiptext"></i>';
112-
117+
const clipButton = createClipButton();
113118
const workerPool = new WorkerPool(codeQuery.length);
114119

115120
for (const code of codeQuery) {
@@ -132,12 +137,16 @@ export const codeBlock = async () => {
132137
console.error(e);
133138
});
134139

135-
const buttons = document.createElement('div');
136-
buttons.className = 'buttons';
137-
buttons.insertBefore(document.importNode(clip, true), buttons.firstChild);
138-
buttons.addEventListener('mousedown', codeCopy);
139-
140140
const parent = code.parentNode;
141-
parent.insertBefore(buttons, parent.firstChild);
141+
142+
const cb = document.importNode(clipButton, true);
143+
cb.addEventListener('mousedown', codeCopy);
144+
145+
parent.insertBefore(cb, parent.firstChild);
146+
}
147+
148+
// capture hover event in iOS
149+
if (globalThis.ontouchstart !== undefined) {
150+
document.addEventListener('touchstart', () => {}, { once: false, passive: true });
142151
}
143152
};

0 commit comments

Comments
 (0)