feat(project): add cell moves and width menus
This commit is contained in:
@@ -2349,6 +2349,17 @@ html.pj-goal-selecting .pj-goal-menu-btn { pointer-events: none; }
|
||||
box-shadow: inset 0 0 0 2px var(--pj-accent);
|
||||
background-image: linear-gradient(rgba(123, 104, 238, .12), rgba(123, 104, 238, .12));
|
||||
}
|
||||
.pj-goal-table td.pj-goal-texttd.is-goal-active { box-shadow: inset 0 0 0 2px var(--pj-accent); }
|
||||
.pj-goal-move-edge { display: none; position: absolute; z-index: 4; cursor: grab; }
|
||||
.is-goal-active > .pj-goal-move-edge { display: block; }
|
||||
.pj-goal-move-edge[data-edge="top"] { top: 0; left: 0; right: 0; height: 4px; }
|
||||
.pj-goal-move-edge[data-edge="bottom"] { bottom: 0; left: 0; right: 0; height: 4px; }
|
||||
.pj-goal-move-edge[data-edge="left"] { top: 4px; bottom: 4px; left: 0; width: 4px; }
|
||||
.pj-goal-move-edge[data-edge="right"] { top: 4px; bottom: 4px; right: 0; width: 4px; }
|
||||
.pj-goal-table td.is-goal-moving { opacity: .55; }
|
||||
.pj-goal-table td.is-goal-move-target { box-shadow: inset 0 0 0 3px var(--pj-accent); background-image: linear-gradient(var(--pj-accent-tint), var(--pj-accent-tint)); }
|
||||
.pj-goal-table td.is-goal-move-saving { pointer-events: none; opacity: .65; }
|
||||
html.pj-goal-moving, html.pj-goal-moving * { cursor: grabbing !important; user-select: none !important; }
|
||||
|
||||
.pj-goal-foot { display: flex; align-items: center; gap: 12px; margin: 12px 0 4px; }
|
||||
|
||||
|
||||
+163
-7
@@ -352,6 +352,13 @@
|
||||
prune: prune, onBeforeUnload: onBeforeUnload,
|
||||
savedOf: function (f) { const s = states.get(f); return s ? s.saved : undefined; },
|
||||
failedOf: function (f) { const s = states.get(f); return !!(s && s.failed); },
|
||||
acceptSaved: function (f, value) {
|
||||
const s = states.get(f);
|
||||
if (!s) return;
|
||||
clearTimeout(s.timer); s.timer = null; s.saved = value; s.failed = false;
|
||||
writeDraft(f, s);
|
||||
if (cfg.onClear) cfg.onClear(f);
|
||||
},
|
||||
};
|
||||
}
|
||||
// 저장 실패 표시 — 칸 빨간 테두리 + 안내(입력한 내용은 지우지 않는다)
|
||||
@@ -8667,13 +8674,16 @@
|
||||
}
|
||||
addColHandles();
|
||||
function naturalCellWidth(td) {
|
||||
// 원본은 건드리지 않는다. 같은 셀 안의 숨은 복사본으로 폰트·부분 서식·여백을
|
||||
// 원본은 건드리지 않는다. 같은 표 구조의 숨은 복사본으로 폰트·부분 서식·여백을
|
||||
// 그대로 재되, 화면 폭에 따른 줄바꿈만 해제한다(직접 넣은 줄바꿈은 유지).
|
||||
const probe = document.createElement("div"), copy = td.cloneNode(true);
|
||||
const measureTable = table.cloneNode(false), section = td.parentElement.parentElement.cloneNode(false), row = td.parentElement.cloneNode(false);
|
||||
probe.style.cssText = "position:fixed;left:0;top:0;visibility:hidden;pointer-events:none;width:max-content;";
|
||||
probe.setAttribute("aria-hidden", "true");
|
||||
[measureTable, section, row].forEach(function (n) { n.removeAttribute("id"); });
|
||||
measureTable.style.cssText += ";width:max-content;table-layout:auto;";
|
||||
copy.classList.add("pj-goal-autofit-measure");
|
||||
copy.querySelectorAll(".pj-goal-colresize, .pj-goal-menu-btn").forEach(function (n) { n.remove(); });
|
||||
copy.querySelectorAll(".pj-goal-colresize, .pj-goal-menu-btn, .pj-goal-move-edge").forEach(function (n) { n.remove(); });
|
||||
// input의 현재 초안은 HTML 속성과 다를 수 있고, 기본 입력칸의 내재 너비도 제외한다.
|
||||
copy.querySelectorAll("input, textarea").forEach(function (n) {
|
||||
const text = document.createElement("span");
|
||||
@@ -8691,7 +8701,9 @@
|
||||
}
|
||||
});
|
||||
copy.style.setProperty("display", "block", "important");
|
||||
probe.appendChild(copy); td.appendChild(probe);
|
||||
row.appendChild(copy); section.appendChild(row); measureTable.appendChild(section); probe.appendChild(measureTable);
|
||||
// 접힌 분기의 display:none 셀도 전체 자동 맞춤 때 측정할 수 있다.
|
||||
pane.appendChild(probe);
|
||||
try { return Math.ceil(copy.getBoundingClientRect().width / uiZoom()) + 2; }
|
||||
finally { probe.remove(); }
|
||||
}
|
||||
@@ -8707,6 +8719,36 @@
|
||||
colW[key] = width;
|
||||
applyColW(); saveColW(); fitAll(); stickKpi();
|
||||
}
|
||||
function autoFitAllCols() {
|
||||
const cells = Array.from(table.querySelectorAll("[data-goal-cols]")).map(function (td) {
|
||||
return { keys: td.dataset.goalCols.split(","), width: naturalCellWidth(td) };
|
||||
});
|
||||
Object.keys(colDefault).forEach(function (key) { colW[key] = colLimit(key)[0]; });
|
||||
cells.filter(function (cell) { return cell.keys.length === 1; }).forEach(function (cell) {
|
||||
colW[cell.keys[0]] = Math.max(colW[cell.keys[0]], cell.width);
|
||||
});
|
||||
cells.filter(function (cell) { return cell.keys.length > 1; }).forEach(function (cell) {
|
||||
const sum = cell.keys.reduce(function (w, key) { return w + colW[key]; }, 0);
|
||||
if (cell.width > sum) colW[cell.keys[cell.keys.length - 1]] += cell.width - sum;
|
||||
});
|
||||
applyColW(); saveColW(); fitAll(); stickKpi();
|
||||
}
|
||||
function widthMenuItems(td) {
|
||||
const key = (td.dataset.goalCols || "").split(",").pop();
|
||||
if (!colDefault[key]) return [];
|
||||
return [
|
||||
{ label: "현재 열 너비 자동 맞춤", icon: menuIcon("fit_screen"), onClick: function () { autoFitCol(key); } },
|
||||
{ label: "전체 열 너비 자동 맞춤", icon: menuIcon("view_week"), onClick: autoFitAllCols },
|
||||
];
|
||||
}
|
||||
table.addEventListener("contextmenu", function (e) {
|
||||
const td = e.target.closest("th, td");
|
||||
if (!td || !table.contains(td)) return;
|
||||
// 관리자 업무 셀·이름 칸은 아래의 기존 편집 메뉴에 합쳐 표시한다.
|
||||
if (isAdmin && td.closest("tbody.pj-goal-area") &&
|
||||
(td.tagName === "TH" || table.dataset.halves === "true" || td.dataset.month === "0")) return;
|
||||
e.preventDefault(); showContextMenu(e.clientX, e.clientY, widthMenuItems(td));
|
||||
});
|
||||
table.addEventListener("mousedown", function (e) {
|
||||
const h = e.target.closest(".pj-goal-colresize");
|
||||
if (!h) return;
|
||||
@@ -8987,6 +9029,12 @@
|
||||
if (ta.dataset.bound) return;
|
||||
ta.dataset.bound = "1";
|
||||
if (ta.tagName !== "TEXTAREA") window.PJGoalText.attach(ta);
|
||||
["top", "right", "bottom", "left"].forEach(function (edge) {
|
||||
const handle = document.createElement("span");
|
||||
handle.className = "pj-goal-move-edge"; handle.dataset.edge = edge;
|
||||
handle.title = "끌어서 내용과 서식 이동"; handle.setAttribute("aria-hidden", "true");
|
||||
ta.closest("td").appendChild(handle);
|
||||
});
|
||||
const r = textSaver.track(ta, ta.value);
|
||||
if (r === "restored") restored++;
|
||||
else if (r === "conflict") conflict++;
|
||||
@@ -8999,6 +9047,110 @@
|
||||
}
|
||||
bindTexts(table);
|
||||
window.PJGoalText.init(table, el("pj-goal-format"));
|
||||
let moveBusy = false, moveClickUntil = 0;
|
||||
function goalCellTd(node) {
|
||||
const td = node && node.closest && node.closest("tbody.pj-goal-area td.pj-goal-texttd");
|
||||
return td && table.contains(td) && td.querySelector(".pj-goal-text") ? td : null;
|
||||
}
|
||||
function setMoveActive(td) {
|
||||
table.querySelectorAll(".is-goal-active").forEach(function (cell) { cell.classList.remove("is-goal-active"); });
|
||||
if (td) td.classList.add("is-goal-active");
|
||||
}
|
||||
function moveBody(editor) {
|
||||
// 열의 기본 서식도 현재 표시값으로 명시해 연간·분기의 굵기가 이동 뒤 달라지지 않게 한다.
|
||||
const cs = getComputedStyle(editor), rgb = cs.color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
|
||||
const defaults = { bold: Number(cs.fontWeight) >= 600 || cs.fontWeight === "bold", italic: cs.fontStyle === "italic",
|
||||
underline: (cs.textDecorationLine || "").includes("underline") };
|
||||
const size = Math.round(parseFloat(cs.fontSize)); if (size >= 10 && size <= 32) defaults.size = size;
|
||||
if (rgb) defaults.color = "#" + rgb.slice(1).map(function (v) { return Number(v).toString(16).padStart(2, "0"); }).join("");
|
||||
return window.PJGoalText.encode(window.PJGoalText.readRuns(editor).map(function (run) { return Object.assign({}, defaults, run); }));
|
||||
}
|
||||
function moveCell(sourceTd, targetTd) {
|
||||
if (moveBusy || sourceTd === targetTd) return;
|
||||
moveBusy = true;
|
||||
return withStruct(async function () {
|
||||
const source = sourceTd.querySelector(".pj-goal-text"), target = targetTd.querySelector(".pj-goal-text");
|
||||
const fields = [source, target], attrs = fields.map(function (n) { return n.getAttribute("contenteditable"); });
|
||||
try {
|
||||
if (!document.contains(sourceTd) || !document.contains(targetTd) || !source.value) return;
|
||||
fields.forEach(function (n) { n.setAttribute("contenteditable", "false"); n.closest("td").classList.add("is-goal-move-saving"); });
|
||||
await flushTexts();
|
||||
const sourceBody = source.value, targetBody = target.value;
|
||||
if (targetBody && !await confirm("대상 셀의 내용을 덮어쓸까요? 원본의 내용과 서식으로 바뀝니다.", { okText: "덮어쓰기", cancelText: "취소" })) return;
|
||||
const res = await api("POST", "/project/api/goals/cells/move", {
|
||||
source_row: source.dataset.row, source_month: source.dataset.month,
|
||||
target_row: target.dataset.row, target_month: target.dataset.month,
|
||||
source_body: sourceBody, target_body: targetBody, body: moveBody(source),
|
||||
});
|
||||
source.value = ""; target.value = res.body;
|
||||
textSaver.acceptSaved(source, ""); textSaver.acceptSaved(target, target.value);
|
||||
fit(source); fit(target); clearMergeSel(); setMoveActive(targetTd); flashSaved(targetTd);
|
||||
showToast("내용과 서식을 이동했습니다.");
|
||||
} catch (err) {
|
||||
showToast("셀 이동 실패: " + err.message + " — 두 칸의 화면 내용은 유지됩니다. 다시 시도하거나 새로고침해 저장 상태를 확인해 주세요.", "error");
|
||||
} finally {
|
||||
fields.forEach(function (n, i) {
|
||||
if (attrs[i] == null) n.removeAttribute("contenteditable"); else n.setAttribute("contenteditable", attrs[i]);
|
||||
n.closest("td").classList.remove("is-goal-move-saving");
|
||||
});
|
||||
moveBusy = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
// 선택한 셀의 테두리는 이동, 일반 본문 드래그는 기존 글/병합 범위 선택을 유지한다.
|
||||
table.addEventListener("mousedown", function (e) {
|
||||
if (e.button !== 0 || e.shiftKey) return;
|
||||
const source = goalCellTd(e.target), handle = e.target.closest(".pj-goal-move-edge");
|
||||
if (!source) return;
|
||||
if (!handle) { if (!moveBusy) setMoveActive(source); return; }
|
||||
e.preventDefault(); e.stopImmediatePropagation();
|
||||
if (moveBusy || !source.querySelector(".pj-goal-text").value) return;
|
||||
const startX = e.clientX, startY = e.clientY;
|
||||
let moving = false, target = null, frame = null, x = startX, y = startY;
|
||||
function updateTarget() {
|
||||
table.querySelectorAll(".is-goal-move-target").forEach(function (n) { n.classList.remove("is-goal-move-target"); });
|
||||
target = goalCellTd(document.elementFromPoint(x, y));
|
||||
if (target === source) target = null;
|
||||
if (target) target.classList.add("is-goal-move-target");
|
||||
}
|
||||
function scroll() {
|
||||
const r = pane.getBoundingClientRect();
|
||||
if (x >= r.left && x <= r.right && y >= r.top && y <= r.bottom) {
|
||||
if (x < r.left + 24) pane.scrollLeft -= 16; else if (x > r.right - 24) pane.scrollLeft += 16;
|
||||
if (y < r.top + 24) pane.scrollTop -= 16; else if (y > r.bottom - 24) pane.scrollTop += 16;
|
||||
}
|
||||
updateTarget(); frame = requestAnimationFrame(scroll);
|
||||
}
|
||||
function move(ev) {
|
||||
x = ev.clientX; y = ev.clientY;
|
||||
if (!moving && Math.hypot(x - startX, y - startY) < 4) return;
|
||||
if (!moving) {
|
||||
moving = true; source.classList.add("is-goal-moving");
|
||||
document.documentElement.classList.add("pj-goal-moving");
|
||||
const selection = window.getSelection(); if (selection) selection.removeAllRanges();
|
||||
frame = requestAnimationFrame(scroll);
|
||||
}
|
||||
updateTarget();
|
||||
}
|
||||
function end(ev) {
|
||||
if (frame !== null) cancelAnimationFrame(frame);
|
||||
document.removeEventListener("mousemove", move); document.removeEventListener("mouseup", end);
|
||||
document.removeEventListener("keydown", key, true); window.removeEventListener("blur", cancel);
|
||||
source.classList.remove("is-goal-moving"); document.documentElement.classList.remove("pj-goal-moving");
|
||||
table.querySelectorAll(".is-goal-move-target").forEach(function (n) { n.classList.remove("is-goal-move-target"); });
|
||||
if (moving) moveClickUntil = Date.now() + 300;
|
||||
if (ev && moving && target) moveCell(source, target);
|
||||
}
|
||||
function cancel() { end(null); }
|
||||
function key(ev) { if (ev.key === "Escape") { ev.preventDefault(); ev.stopImmediatePropagation(); cancel(); } }
|
||||
document.addEventListener("mousemove", move); document.addEventListener("mouseup", end);
|
||||
document.addEventListener("keydown", key, true); window.addEventListener("blur", cancel);
|
||||
}, true);
|
||||
table.addEventListener("click", function (e) {
|
||||
if ((Date.now() < moveClickUntil && e.detail !== 0 && goalCellTd(e.target)) || e.target.closest(".pj-goal-move-edge")) {
|
||||
e.preventDefault(); e.stopImmediatePropagation();
|
||||
}
|
||||
}, true);
|
||||
// 칸이 세로로 길어도(옆 칸 글이 길거나 세로 병합) 칸 아무 곳이나 누르면 그 칸 입력 — 커서는 글 끝(엑셀처럼).
|
||||
// 입력칸(textarea)은 글 높이만큼이라 그 아래를 누르면 아무것도 안 되던 것. Shift+클릭(범위 고르기)·끌기는 그대로.
|
||||
table.addEventListener("click", function (e) {
|
||||
@@ -9238,7 +9390,7 @@
|
||||
return items;
|
||||
}
|
||||
function openAreaMenu(th, x, y, opts) {
|
||||
showContextMenu(x, y, areaMenuItems(th.closest("tbody"), { x: x, y: y }),
|
||||
showContextMenu(x, y, areaMenuItems(th.closest("tbody"), { x: x, y: y }).concat([null], widthMenuItems(th)),
|
||||
Object.assign({ label: textOf(th, ".pj-goal-area-text") + " 구역 메뉴" }, opts || {}));
|
||||
}
|
||||
table.addEventListener("contextmenu", function (e) {
|
||||
@@ -9326,7 +9478,7 @@
|
||||
// 2열 = 항목 칸 밖에 따로 추가(묶음의 맨 위·맨 아래 기준), 묶인 칸이면 "항목 칸 나누기"도
|
||||
const grouped = (th.rowSpan || 1) > 1;
|
||||
showContextMenu(x, y, rowMenuItems(tr, labelGroupBottom(tr), true,
|
||||
{ splitRow: grouped ? tr : null, move: grouped ? null : tr }),
|
||||
{ splitRow: grouped ? tr : null, move: grouped ? null : tr }).concat([null], widthMenuItems(th)),
|
||||
Object.assign({ label: (textOf(th, ".pj-goal-row-text") || "이름 없는") + " 줄 메뉴" }, opts || {}));
|
||||
}
|
||||
table.addEventListener("contextmenu", function (e) {
|
||||
@@ -9371,7 +9523,7 @@
|
||||
if (!td || !table.contains(td)) return;
|
||||
e.preventDefault();
|
||||
const r = yearCellRows(td);
|
||||
showContextMenu(e.clientX, e.clientY, rowMenuItems(r.top, r.bottom, r.single, { sameLabel: true }));
|
||||
showContextMenu(e.clientX, e.clientY, rowMenuItems(r.top, r.bottom, r.single, { sameLabel: true }).concat([null], widthMenuItems(td)));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9487,12 +9639,14 @@
|
||||
let drag = null;
|
||||
clearMergeSel = function () {
|
||||
sel = null;
|
||||
setMoveActive(null);
|
||||
table.querySelectorAll("td.is-merge-sel").forEach(function (td) { td.classList.remove("is-merge-sel"); });
|
||||
};
|
||||
const paint = function () {
|
||||
table.querySelectorAll("td.is-merge-sel").forEach(function (td) { td.classList.remove("is-merge-sel"); });
|
||||
if (!sel) return;
|
||||
sel.cells.forEach(function (c) { if (hits(c, sel)) c.td.classList.add("is-merge-sel"); });
|
||||
setMoveActive(sel.n === 1 ? sel.cells.find(function (c) { return hits(c, sel); }).td : null);
|
||||
};
|
||||
const select = function (tbody, a, b) {
|
||||
const cells = gridOf(tbody);
|
||||
@@ -9578,6 +9732,7 @@
|
||||
clearMergeSel();
|
||||
anchorCell = { tbody: tbody, td: td };
|
||||
drag = { tbody: tbody, td: td, moved: false };
|
||||
select(tbody, td, td);
|
||||
});
|
||||
table.addEventListener("mouseover", function (e) {
|
||||
if (!drag) return;
|
||||
@@ -9633,7 +9788,8 @@
|
||||
if (items.length) items.push(null);
|
||||
rowMenuItems(r.top, r.bottom, r.single, { sameLabel: true }).forEach(function (it) { items.push(it); });
|
||||
}
|
||||
if (!items.length) { clearMergeSel(); return; }
|
||||
if (items.length) items.push(null);
|
||||
widthMenuItems(td).forEach(function (item) { items.push(item); });
|
||||
e.preventDefault();
|
||||
showContextMenu(e.clientX, e.clientY, items);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user