Skip to content

챗봇 UI 수정 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/main/resources/static/css/question/chatbot.css
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
position: relative;
width: fit-content;
margin-top: -2px;
font-family: "NanumSquare Neo OTF-cBd", Helvetica;
font-size: 20px;
line-height: 40px;
color: #000000;
Expand Down
59 changes: 37 additions & 22 deletions src/main/resources/static/js/translateTextForHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ document.addEventListener("DOMContentLoaded", function () {
// 초기 로딩된 요소에 대해 포맷팅 적용
classesToFormat.forEach(classNames => {
document.querySelectorAll(`.${classNames.replace(' ', '.')}`).forEach(element => {
if (classNames === 'message user') {
formatTextWithLineBreakOnly(element);
} else {
formatElementText(element);
}
formatElement(element, classNames);
});
});

Expand All @@ -18,16 +14,12 @@ document.addEventListener("DOMContentLoaded", function () {
mutationsList.forEach(function (mutation) {
mutation.addedNodes.forEach(function (node) {
if (node.nodeType === Node.ELEMENT_NODE) {
classesToFormat.some(classNames => {
if (node.classList.contains(...classNames.split(' '))) {
if (classNames === 'message user') {
formatTextWithLineBreakOnly(node);
} else {
formatElementText(node);
}
return true;
classesToFormat.forEach(classNames => {
const classList = classNames.split(' ');
// 모든 클래스가 있는지 확인
if (classList.every(className => node.classList.contains(className))) {
formatElement(node, classNames);
}
return false;
});
}
});
Expand All @@ -39,20 +31,43 @@ document.addEventListener("DOMContentLoaded", function () {
});

// 포맷팅 함수
function formatElement(element, classNames) {

if (classNames === 'message user') {
formatTextWithLineBreakOnly(element);
} else {
formatElementText(element);
}
}

function formatElementText(element) {
let formattedText = element.textContent.replace(/\n/g, '<br/>');
// element의 텍스트를 줄바꿈으로 나누어 배열로 저장
let lines = element.textContent.split('\n');

// 각 줄을 처리하여 포맷팅
let formattedLines = lines.map(line => {
let formattedLine = line;

// #으로 시작하는 텍스트를 대제목과 소제목으로 변환
formattedText = formattedText.replace(/^### (.*?)(<br\/>|$)/gm, '<span style="font-weight: bold; font-size: 1.2em;">$1</span>');
formattedText = formattedText.replace(/^## (.*?)(<br\/>|$)/gm, '<span style="font-weight: bold; font-size: 1.4em;">$1</span>');
formattedText = formattedText.replace(/^# (.*?)(<br\/>|$)/gm, '<span style="font-weight: bold; font-size: 1.6em;">$1</span>');
// 줄의 시작 부분에서 #을 확인하여 대제목과 소제목으로 변환
if (formattedLine.startsWith('# ')) {
formattedLine = formattedLine.replace(/^# (.*)/, '<span style="font-weight: bold; font-size: 1.6em;">$1</span>');
} else if (formattedLine.startsWith('## ')) {
formattedLine = formattedLine.replace(/^## (.*)/, '<span style="font-weight: bold; font-size: 1.4em;">$1</span>');
} else if (formattedLine.startsWith('### ')) {
formattedLine = formattedLine.replace(/^### (.*)/, '<span style="font-weight: bold; font-size: 1.2em;">$1</span>');
}

// **로 둘러싸인 단어를 굵게 표시
formattedText = formattedText.replace(/\*\*(.*?)\*\*/g, '<span style="font-weight: bold; font-size: 1em;">$1</span>');
// **로 둘러싸인 단어를 굵게 표시
formattedLine = formattedLine.replace(/\*\*(.*?)\*\*/g, '<span style="font-weight: bold; font-size: 1em;">$1</span>');

element.innerHTML = formattedText;
return formattedLine; // 포맷팅된 줄 반환
});

// 포맷팅된 줄들을 다시 줄바꿈으로 연결
element.innerHTML = formattedLines.join('<br/>');
}


// message.user와 같이 줄바꿈만 적용
function formatTextWithLineBreakOnly(element) {
element.innerHTML = element.textContent.replace(/\n/g, '<br/>');
Expand Down
Loading