const COPY_TEXT_CHANGE_OFFSET = 1000;
const COPY_BUTTON_TEXT_BEFORE = "Copy";
const COPY_BUTTON_TEXT_AFTER = "Copied";
const COPY_ERROR_MESSAGE = "ERROR";
const copyBlockCode = async (target) => {
if (!target) return;
try {
const code = decodeURI(target.dataset.code);
await navigator.clipboard.writeText(code);
target.textContent = COPY_BUTTON_TEXT_AFTER;
setTimeout(() => {
target.textContent = COPY_BUTTON_TEXT_BEFORE;
}, COPY_TEXT_CHANGE_OFFSET);
} catch (error) {
alert(COPY_ERROR_MESSAGE);
console.error(error);
}
};
const func = () => {
const codeBlocks = document.querySelectorAll("pre > code");
codeBlocks.forEach((codeBlock) => {
const codes = codeBlock.innerHTML.trim().split("\n");
const processedCodes = codes.reduce(
(prev, cur) => prev + `<div class="line">${cur}</div>`,
""
);
const copyButton = `<button type="button" class="copy-btn"
data-code="${encodeURIComponent(codeBlock.textContent)}"
onclick="copyBlockCode(this)">${COPY_BUTTON_TEXT_BEFORE}</button>`;
const codeBody = `<div class="code-body">${processedCodes}</div>`;
const codeHeader = `
<div class="code-header">
<span class="red btn"></span>
<span class="yellow btn"></span>
<span class="green btn"></span>
${copyButton}
</div>`;
codeBlock.innerHTML = codeHeader + codeBody;
});
console.log("Finished processing code blocks.");
};
window.addEventListener("load", func);
pre나 hljs로 시작하는 css코드들은 다 주석으로 수정 후 적용
pre {
position: relative;
}
pre::after {
content: attr(data-ke-language);
position: absolute;
bottom: 8px;
right: 12px;
color: #cfd2d1;
font-size: 12px;
}
.hljs {
display: flex !important;
flex-direction: column;
padding: 0 !important;
font-size: 14px;
border-radius: 8px;
box-shadow: 0 12px 24px rgb(0 0 0 / 40%);
color: #cfd2d1;
background-color: #343131;
font-family: Menlo, Courier, monospace;
}
.hljs .line {
counter-increment: line-idx;
line-height: 1.5;
}
.hljs .line:hover {
background-color: #262830;
}
.hljs .line:hover::before {
color: #cfd2d1;
}
.hljs .line::before {
content: counter(line-idx);
width: 24px;
display: inline-block;
text-align: right;
margin-right: 16px;
font-size: 0.8rem;
color: #747a7a;
}
.hljs .code-header {
display: flex;
align-items: center;
padding: 14px;
background-color: #434041;
border-radius: 8px 8px 0 0;
}
.hljs .code-header .btn {
border-radius: 50%;
width: 15px;
height: 15px;
margin: 0 5px;
}
.hljs .code-header .btn.red {
background-color: #f5655b;
}
.hljs .code-header .btn.yellow {
background-color: #f6bd3b;
}
.hljs .code-header .btn.green {
background-color: #43c645;
}
.hljs .code-body {
max-height: 600px;
margin: 32px 8px;
overflow: auto;
}
.hljs .code-body::-webkit-scrollbar {
width: 12px;
}
.hljs .code-body::-webkit-scrollbar-thumb {
background-color: rgb(1 2 3 / 80%);
border-radius: 4px;
}
.hljs .code-body::-webkit-scrollbar-corner {
display: none;
}
.hljs .copy-btn {
background-color: transparent;
border: none;
cursor: pointer;
color: #fff;
font-size: 0.75rem;
padding: 6px 0;
width: 64px;
border-radius: 4px;
margin-left: auto;
transition: 0.2s background-color;
}
.hljs .copy-btn:hover {
background-color: #555152;
}
</body>
<script defer src="./images/codeblock.js"></script>
</html>
출처
'코드 및 쿼리문 > 코드 정리' 카테고리의 다른 글
날짜 형식 (0) | 2024.07.11 |
---|---|
자동 목차에 제목4(h4) 추가 (0) | 2024.07.10 |
마우스 모양 변경 및 클릭시 파동 효과 (0) | 2024.07.10 |
관리 사이드바 (0) | 2024.07.05 |
티스토리 홈 + 로그인/로그아웃 버튼 만들기 (0) | 2024.07.05 |