Skip to content

Commit bbbe025

Browse files
committed
chore: resolve all reviews
1 parent d4fce2f commit bbbe025

File tree

6 files changed

+47
-54
lines changed

6 files changed

+47
-54
lines changed

json-tool/public/assets/js/core/dom.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
/* ========== COPY FUNCTIONS ========== */
22
function copyToClipboard(text, successMessage) {
3-
navigator.clipboard
4-
.writeText(text)
3+
(navigator.clipboard?.writeText
4+
? navigator.clipboard.writeText(text)
5+
: new Promise((res, rej) => {
6+
try {
7+
const ta = Object.assign(document.createElement("textarea"), {
8+
value: text,
9+
style: "position:fixed;top:-1000px",
10+
});
11+
document.body.appendChild(ta);
12+
ta.select();
13+
document.execCommand("copy") ? res() : rej();
14+
ta.remove();
15+
} catch (e) {
16+
rej(e);
17+
}
18+
})
19+
)
520
.then(() => {
621
Swal.fire({
722
icon: "success",
@@ -200,4 +215,3 @@ function enableTabReordering(containerId) {
200215
});
201216
});
202217
}
203-

json-tool/public/assets/js/features/codegen/codegen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function generateCode(tabId) {
125125
if (lang === "typescript") code = generateTypeScript(obj, "Root");
126126
else if (lang === "python") code = generatePython(obj, "Root");
127127
else if (lang === "go") code = generateGo(obj, "Root");
128-
outputPre.innerHTML = code;
128+
outputPre.textContent = code;
129129
saveGlobalState();
130130
}
131131

json-tool/public/assets/js/features/compare/compare.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,15 @@ function compareJSONs(tabId) {
130130
}
131131
const leftFormatted = JSON.stringify(leftObj, null, 2);
132132
const rightFormatted = JSON.stringify(rightObj, null, 2);
133+
const diffPreview = diffJSONsPreview(leftFormatted, rightFormatted);
134+
const sanitizedDiffPreview = DOMPurify.sanitize(diffPreview);
135+
133136
resultDiv.innerHTML = `
134137
<div style="margin-bottom: 10px;">
135138
<button class="copy-button" onclick="copyCompareLeft('${tabId}')">Copy Left</button>
136139
<button class="copy-button" onclick="copyCompareRight('${tabId}')">Copy Right</button>
137140
</div>
138-
${diffJSONsPreview(leftFormatted, rightFormatted)}
141+
${sanitizedDiffPreview}
139142
`;
140143
leftTA.value = leftFormatted;
141144
rightTA.value = rightFormatted;
@@ -181,4 +184,4 @@ function copyCompareLeft(tabId) {
181184
function copyCompareRight(tabId) {
182185
const rightTA = document.querySelector(`#${tabId} .json-input-right`);
183186
copyToClipboard(rightTA.value, "Right JSON copied");
184-
}
187+
}

json-tool/public/assets/js/features/editor/editor.js

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function addEditorTab(tabData = null) {
5555
const saved = localStorage.getItem(tabId);
5656
if (saved) editor.setMarkdown(saved);
5757

58-
enableEditorTabReordering();
58+
enableTabReordering("editor-tabs-container");
5959
applyEditorTabDarkMode();
6060
}
6161

@@ -73,18 +73,20 @@ function switchEditorTab(tabId) {
7373
updateEditorGlobalState();
7474
}
7575

76-
function saveEditorContent(tabId) {
76+
function saveEditorContent(tabId, silent = false) {
7777
const content = editorInstances[tabId].getMarkdown();
7878
localStorage.setItem(tabId, content);
7979
updateEditorGlobalState();
80-
Swal.fire({
81-
toast: true,
82-
position: "top-end",
83-
icon: "success",
84-
title: "Autosaved",
85-
showConfirmButton: false,
86-
timer: 1500,
87-
});
80+
if (!silent) {
81+
Swal.fire({
82+
toast: true,
83+
position: "top-end",
84+
icon: "success",
85+
title: "Saved!", // Changed from Autosaved to Saved!
86+
showConfirmButton: false,
87+
timer: 1500,
88+
});
89+
}
8890
}
8991

9092
async function deleteEditorTab(tabId, event) {
@@ -105,6 +107,7 @@ async function deleteEditorTab(tabId, event) {
105107
if (!result.isConfirmed) return;
106108

107109
localStorage.removeItem(tabId);
110+
editorInstances[tabId]?.destroy();
108111
delete editorInstances[tabId];
109112
document
110113
.querySelector(`#editor-tabs-container .tab-button[data-tab="${tabId}"]`)
@@ -158,38 +161,6 @@ function loadEditorGlobalState() {
158161
}
159162
}
160163

161-
function enableEditorTabReordering() {
162-
const container = document.getElementById("editor-tabs-container");
163-
const buttons = container.querySelectorAll(".tab-button[data-tab]");
164-
buttons.forEach((btn) => {
165-
btn.draggable = true;
166-
btn.addEventListener("dragstart", (e) => {
167-
e.dataTransfer.setData("text/plain", btn.getAttribute("data-tab"));
168-
btn.classList.add("dragging");
169-
});
170-
btn.addEventListener("dragend", () => {
171-
btn.classList.remove("dragging");
172-
});
173-
btn.addEventListener("dragover", (e) => {
174-
e.preventDefault();
175-
btn.classList.add("drag-over");
176-
});
177-
btn.addEventListener("dragleave", () => {
178-
btn.classList.remove("drag-over");
179-
});
180-
btn.addEventListener("drop", (e) => {
181-
e.preventDefault();
182-
const draggedId = e.dataTransfer.getData("text/plain");
183-
const draggedBtn = container.querySelector(`[data-tab="${draggedId}"]`);
184-
btn.classList.remove("drag-over");
185-
if (draggedBtn && draggedBtn !== btn) {
186-
container.insertBefore(draggedBtn, btn);
187-
updateEditorGlobalState();
188-
}
189-
});
190-
});
191-
}
192-
193164
function applyEditorTabDarkMode() {
194165
const container = document.getElementById("editor-tabs-container");
195166
container.querySelectorAll(".tab-button").forEach((btn) => {

json-tool/public/assets/js/features/formatter/formatter.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,16 @@ function createTreeView(data, parentElement) {
340340
parent.appendChild(node);
341341
} else {
342342
const valueSpan = document.createElement("span");
343-
valueSpan.innerHTML = `
344-
<span class="tree-key">${key}: </span>
345-
<span class="${getValueTypeClass(value)}">${formatValue(
346-
value
347-
)}</span>
348-
`;
343+
344+
const keyEl = document.createElement("span");
345+
keyEl.className = "tree-key";
346+
keyEl.textContent = `${key}: `;
347+
348+
const valEl = document.createElement("span");
349+
valEl.className = getValueTypeClass(value);
350+
valEl.textContent = formatValue(value);
351+
352+
valueSpan.append(keyEl, valEl);
349353

350354
// Value context menu
351355
valueSpan.addEventListener("contextmenu", (e) => {

json-tool/public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
rel="stylesheet"
3232
href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css"
3333
/>
34+
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/2.3.10/purify.min.js"></script>
3435
</head>
3536

3637
<body>

0 commit comments

Comments
 (0)