Files
Aislo/tools/est-test/native-cell-clip.js
T
2026-11-12 13:14:05 +09:00

50 lines
2.6 KiB
JavaScript

// 원본 셀복사·셀붙여넣기(TfrmEstMain ActionCellCopy·ActionCellPaste) — 마지막으로 누른 칸의 열을, 고른 행들(없으면 그 행)만큼 복사하고
// 다른 칸을 누른 뒤 그 행부터 아래로 차례로 붙여넣는다(열이 달라도 됨). 코드·결과비목 칸은 연결 처리가 따로 있어 붙여넣기 거부.
const EstCellClip = (() => {
let last = null;
function copy(state) {
if (!last) throw Error("복사할 칸을 먼저 누르세요.");
const rows = state.project.tables[last.table] || [],
picked = [...state.selection].filter((i) => rows[i]).sort((a, b) => a - b),
list = picked.includes(last.row) ? picked : [last.row];
state.cellClip = { field: last.field, values: list.map((i) => rows[i][last.field] ?? null) };
const text = state.cellClip.values
.map((v) => (last.field === "LineData" ? EstText.display(v ?? "") : (v ?? "")))
.join("\n");
navigator.clipboard?.writeText(text).catch(() => {});
state.notice = `${list.length}칸을 복사했습니다.`;
}
function paste(state) {
if (!last) throw Error("붙여넣을 칸을 먼저 누르세요.");
if (!state.cellClip?.values.length) throw Error("복사한 칸이 없습니다.");
if (["Code", "SCheck"].includes(last.field))
throw Error("코드·결과비목 칸은 셀붙여넣기를 쓸 수 없습니다. 칸에서 직접 고치세요.");
const rows = state.project.tables[last.table] || [],
numeric = EstModel.numeric.has(last.field);
const values = state.cellClip.values.map((v) => {
if (!numeric) return v == null ? "" : String(v);
const n = v === "" || v == null ? null : Number(v);
if (n !== null && !Number.isFinite(n))
throw Error("수치 칸에는 수치만 붙여넣을 수 있습니다.");
return n;
});
if (last.row + values.length > rows.length) throw Error("붙여넣을 행이 모자랍니다.");
EstApp.change((project) => {
const table = project.tables[last.table];
values.forEach((v, i) => (table[last.row + i][last.field] = v));
});
}
if (typeof document !== "undefined" && document.addEventListener)
document.addEventListener("focusin", (event) => {
const t = event.target.closest?.("[data-table][data-field][data-row]");
if (t) last = { table: t.dataset.table, row: Number(t.dataset.row), field: t.dataset.field };
});
function handle(name, state) {
if (name === "cell-copy") copy(state);
else if (name === "cell-paste") paste(state);
else return false;
return true;
}
return { handle };
})();