feat(project): add group collapse and UI polish
This commit is contained in:
@@ -104,8 +104,38 @@
|
||||
function init(table, toolbar) {
|
||||
if (!toolbar) return;
|
||||
let saved = null;
|
||||
// body 에 두어 표 스크롤과 화면 zoom 에 잘리지 않는 선택 영역 위 팝업으로 표시한다.
|
||||
document.body.appendChild(toolbar);
|
||||
const colorButton = toolbar.querySelector("[data-goal-format='color']");
|
||||
const colorGrid = toolbar.querySelector(".pj-goal-color-grid");
|
||||
function closeColors() { colorGrid.hidden = true; colorButton.setAttribute("aria-expanded", "false"); }
|
||||
function showColor(value) {
|
||||
colorButton.querySelector(".pj-goal-color-preview").style.backgroundColor = value;
|
||||
colorGrid.querySelectorAll("[data-goal-color]").forEach(function (btn) {
|
||||
btn.setAttribute("aria-pressed", String(btn.dataset.goalColor.toLowerCase() === value.toLowerCase()));
|
||||
});
|
||||
}
|
||||
function place() {
|
||||
if (!saved || !saved.editor.isConnected) { clear(); return; }
|
||||
const sel = window.getSelection();
|
||||
if (!sel.rangeCount || sel.isCollapsed || !saved.editor.contains(sel.getRangeAt(0).commonAncestorContainer)) return;
|
||||
const range = sel.getRangeAt(0);
|
||||
if (!range.getBoundingClientRect) return; // DOM 테스트 환경에는 레이아웃이 없다.
|
||||
const rects = range.getClientRects();
|
||||
const rect = rects.length ? rects[0] : range.getBoundingClientRect();
|
||||
const paneRect = table.parentElement.getBoundingClientRect();
|
||||
if (rect.bottom < paneRect.top || rect.top > paneRect.bottom || rect.right < paneRect.left || rect.left > paneRect.right) {
|
||||
toolbar.hidden = true; closeColors(); return;
|
||||
}
|
||||
toolbar.hidden = false;
|
||||
const w = toolbar.offsetWidth, h = toolbar.offsetHeight;
|
||||
toolbar.style.left = Math.max(8, Math.min(rect.left + rect.width / 2 - w / 2, window.innerWidth - w - 8)) + "px";
|
||||
toolbar.style.top = Math.max(8, rect.top - h - 8) + "px";
|
||||
colorGrid.classList.toggle("is-below", rect.top - h - 88 < 8);
|
||||
}
|
||||
function clear() {
|
||||
saved = null;
|
||||
toolbar.hidden = true; closeColors();
|
||||
toolbar.querySelectorAll("button, input, select").forEach(function (el) { el.disabled = true; });
|
||||
}
|
||||
function capture() {
|
||||
@@ -130,8 +160,10 @@
|
||||
return chosen.every(function (r) { return r[key] === value; }) && value !== undefined ? value : fallback;
|
||||
}
|
||||
toolbar.querySelector("[data-goal-format='bold']").value = uniform("bold", false) ? "700" : "400";
|
||||
toolbar.querySelector("[data-goal-format='color']").value = uniform("color", "#1e1f21");
|
||||
showColor(uniform("color", "#000000"));
|
||||
toolbar.querySelector("[data-goal-format='size']").value = String(uniform("size", 14));
|
||||
toolbar.hidden = false;
|
||||
place();
|
||||
}
|
||||
document.addEventListener("selectionchange", capture);
|
||||
table.addEventListener("mouseup", capture);
|
||||
@@ -156,8 +188,17 @@
|
||||
});
|
||||
paint(editor, compact(out)); editor.focus(); restore(editor, saved.start, saved.end);
|
||||
editor.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
capture();
|
||||
}
|
||||
toolbar.querySelector("[data-goal-format='color']").addEventListener("input", function (e) { apply("color", e.target.value); });
|
||||
colorButton.addEventListener("click", function () {
|
||||
colorGrid.hidden = !colorGrid.hidden;
|
||||
colorButton.setAttribute("aria-expanded", String(!colorGrid.hidden));
|
||||
});
|
||||
colorGrid.addEventListener("click", function (e) {
|
||||
const btn = e.target.closest("[data-goal-color]");
|
||||
if (!btn) return;
|
||||
apply("color", btn.dataset.goalColor.toLowerCase()); closeColors();
|
||||
});
|
||||
toolbar.querySelector("[data-goal-format='bold']").addEventListener("change", function (e) { apply("bold", e.target.value === "700"); });
|
||||
toolbar.querySelector("[data-goal-format='size']").addEventListener("change", function (e) { apply("size", Number(e.target.value)); });
|
||||
toolbar.querySelector("[data-goal-format='reset']").addEventListener("click", function () { apply("reset"); });
|
||||
@@ -165,6 +206,16 @@
|
||||
if (toolbar.contains(e.target) || (saved && saved.editor.contains(e.target))) return;
|
||||
clear();
|
||||
}, true);
|
||||
toolbar.addEventListener("keydown", function (e) {
|
||||
if (e.key !== "Escape") return;
|
||||
e.preventDefault(); e.stopPropagation();
|
||||
if (!colorGrid.hidden) { closeColors(); colorButton.focus(); }
|
||||
else { if (saved) saved.editor.focus(); clear(); }
|
||||
});
|
||||
window.addEventListener("resize", place);
|
||||
window.addEventListener("scroll", function (e) { if (!toolbar.contains(e.target)) place(); }, true);
|
||||
new MutationObserver(function () { if (saved && !saved.editor.isConnected) clear(); }).observe(table, { childList: true, subtree: true });
|
||||
clear();
|
||||
}
|
||||
window.PJGoalText = { attach: attach, init: init, readRuns: readRuns, paint: paint, encode: encode };
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user