fix(project): grow cells and repair Enter
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
function walk(node, inherited) {
|
||||
if (node.nodeType === 3) { add(node.nodeValue, inherited); return; }
|
||||
if (node.nodeType !== 1) return;
|
||||
if (node.hasAttribute("data-pj-caret-line")) return;
|
||||
if (node.tagName === "BR") { add("\n", inherited); return; }
|
||||
if (/^(SCRIPT|STYLE|IMG|IFRAME)$/.test(node.tagName)) return;
|
||||
const style = styles(node, inherited);
|
||||
@@ -64,27 +65,58 @@
|
||||
editor.replaceChildren();
|
||||
runs.forEach(function (r) {
|
||||
if (typeof r.text !== "string") return;
|
||||
const span = document.createElement("span");
|
||||
span.textContent = r.text;
|
||||
if (typeof r.bold === "boolean") span.style.fontWeight = r.bold ? "700" : "400";
|
||||
if (typeof r.italic === "boolean") span.style.fontStyle = r.italic ? "italic" : "normal";
|
||||
if (typeof r.underline === "boolean") span.style.textDecoration = r.underline ? "underline" : "none";
|
||||
if (/^#[\da-f]{6}$/i.test(r.color || "")) span.style.color = r.color;
|
||||
if (Number.isInteger(r.size) && r.size >= 10 && r.size <= 32) span.style.fontSize = r.size + "px";
|
||||
editor.appendChild(span.style.cssText ? span : document.createTextNode(r.text));
|
||||
editor.appendChild(nodeForRun(r));
|
||||
});
|
||||
caretLine(editor);
|
||||
}
|
||||
function nodeForRun(r) {
|
||||
const span = document.createElement("span");
|
||||
r.text.split("\n").forEach(function (line, i) {
|
||||
if (i) span.appendChild(document.createElement("br"));
|
||||
if (line) span.appendChild(document.createTextNode(line));
|
||||
});
|
||||
if (typeof r.bold === "boolean") span.style.fontWeight = r.bold ? "700" : "400";
|
||||
if (typeof r.italic === "boolean") span.style.fontStyle = r.italic ? "italic" : "normal";
|
||||
if (typeof r.underline === "boolean") span.style.textDecoration = r.underline ? "underline" : "none";
|
||||
if (/^#[\da-f]{6}$/i.test(r.color || "")) span.style.color = r.color;
|
||||
if (Number.isInteger(r.size) && r.size >= 10 && r.size <= 32) span.style.fontSize = r.size + "px";
|
||||
if (!span.style.cssText && r.text.includes("\n")) span.style.lineHeight = "inherit";
|
||||
return span.style.cssText || r.text.includes("\n") ? span : document.createTextNode(r.text);
|
||||
}
|
||||
function caretLine(editor) {
|
||||
// 마지막 개행 뒤의 빈 줄도 화면에 만들되 저장할 글에는 넣지 않는다.
|
||||
const runs = readRuns(editor), last = runs[runs.length - 1];
|
||||
let marker = editor.querySelector("[data-pj-caret-line]");
|
||||
if (last && last.text.endsWith("\n")) {
|
||||
if (!marker) {
|
||||
marker = document.createElement("br"); marker.setAttribute("data-pj-caret-line", "");
|
||||
const holder = document.createElement("span"); holder.appendChild(marker); editor.appendChild(holder);
|
||||
}
|
||||
marker.parentElement.style.fontSize = last.size ? last.size + "px" : "";
|
||||
marker.parentElement.style.lineHeight = last.size ? "normal" : getComputedStyle(editor).lineHeight;
|
||||
} else if (marker) {
|
||||
const holder = marker.parentElement; marker.remove();
|
||||
if (holder !== editor && !holder.childNodes.length) holder.remove();
|
||||
}
|
||||
}
|
||||
function restore(editor, start, end) {
|
||||
const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT);
|
||||
const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT | NodeFilter.SHOW_ELEMENT);
|
||||
const points = [], range = document.createRange();
|
||||
let node, offset = 0;
|
||||
while ((node = walker.nextNode())) {
|
||||
points.push({ node: node, start: offset, end: offset + node.length }); offset += node.length;
|
||||
const length = node.nodeType === 3 ? node.length : node.tagName === "BR" && !node.hasAttribute("data-pj-caret-line") ? 1 : 0;
|
||||
if (length) { points.push({ node: node, start: offset, end: offset + length }); offset += length; }
|
||||
}
|
||||
function point(n) { return points.find(function (p) { return n <= p.end; }) || points[points.length - 1]; }
|
||||
const a = point(start), b = point(end);
|
||||
if (!a || !b) { range.selectNodeContents(editor); range.collapse(false); }
|
||||
else { range.setStart(a.node, Math.max(0, start - a.start)); range.setEnd(b.node, Math.max(0, end - b.start)); }
|
||||
else {
|
||||
function pointAt(p, n) {
|
||||
return p.node.nodeType === 3 ? [p.node, Math.max(0, n - p.start)] :
|
||||
[p.node.parentNode, Array.from(p.node.parentNode.childNodes).indexOf(p.node) + (n > p.start ? 1 : 0)];
|
||||
}
|
||||
range.setStart(...pointAt(a, start)); range.setEnd(...pointAt(b, end));
|
||||
}
|
||||
const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range);
|
||||
return range;
|
||||
}
|
||||
@@ -98,16 +130,34 @@
|
||||
if (e.isComposing || e.keyCode === 229) return;
|
||||
if (e.key === "Enter") { e.preventDefault(); insert(editor, "\n"); }
|
||||
});
|
||||
editor.addEventListener("beforeinput", function (e) {
|
||||
if (!e.isComposing && (e.inputType === "insertParagraph" || e.inputType === "insertLineBreak")) {
|
||||
e.preventDefault(); insert(editor, "\n");
|
||||
}
|
||||
});
|
||||
editor.addEventListener("input", function (e) { if (!e.isComposing) caretLine(editor); });
|
||||
editor.addEventListener("compositionend", function () { caretLine(editor); });
|
||||
editor.addEventListener("paste", function (e) {
|
||||
e.preventDefault(); insert(editor, e.clipboardData.getData("text/plain"));
|
||||
});
|
||||
paint(editor, readRuns(editor));
|
||||
}
|
||||
function insert(editor, text) {
|
||||
if (!text) return;
|
||||
const sel = window.getSelection();
|
||||
if (!sel.rangeCount || !editor.contains(sel.getRangeAt(0).commonAncestorContainer)) return;
|
||||
const range = sel.getRangeAt(0); range.deleteContents();
|
||||
const node = document.createTextNode(text); range.insertNode(node);
|
||||
range.setStartAfter(node); range.collapse(true); sel.removeAllRanges(); sel.addRange(range);
|
||||
const range = sel.getRangeAt(0), pre = document.createRange();
|
||||
pre.selectNodeContents(editor); pre.setEnd(range.startContainer, range.startOffset);
|
||||
const offset = readRuns(pre.cloneContents()).reduce((n, r) => n + r.text.length, 0); let pos = 0, style = {};
|
||||
for (const run of readRuns(editor)) {
|
||||
pos += run.text.length;
|
||||
if (pos >= offset) { STYLE_KEYS.forEach(k => { if (run[k] !== undefined) style[k] = run[k]; }); break; }
|
||||
}
|
||||
range.deleteContents();
|
||||
const node = nodeForRun(Object.assign({ text: text }, style)); range.insertNode(node);
|
||||
const caret = node.nodeType === 3 ? node : node.lastChild;
|
||||
if (caret.nodeType === 3) range.setStart(caret, caret.length); else range.setStartAfter(caret);
|
||||
range.collapse(true); sel.removeAllRanges(); sel.addRange(range);
|
||||
editor.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
}
|
||||
function init(table, toolbar) {
|
||||
@@ -157,7 +207,8 @@
|
||||
const editor = root.closest(".pj-goal-text[contenteditable='true']");
|
||||
if (!editor || !table.contains(editor) || !editor.contains(range.endContainer)) { clear(); return; }
|
||||
const pre = document.createRange(); pre.selectNodeContents(editor); pre.setEnd(range.startContainer, range.startOffset);
|
||||
saved = { editor: editor, start: pre.toString().length, end: pre.toString().length + range.toString().length };
|
||||
const start = readRuns(pre.cloneContents()).reduce((n, r) => n + r.text.length, 0);
|
||||
saved = { editor: editor, start: start, end: start + readRuns(range.cloneContents()).reduce((n, r) => n + r.text.length, 0) };
|
||||
toolbar.querySelectorAll("button, input, select").forEach(function (el) { el.disabled = false; });
|
||||
let pos = 0;
|
||||
const chosen = readRuns(editor).filter(function (run) {
|
||||
|
||||
@@ -106,6 +106,29 @@
|
||||
{ label: "전체 열 너비 자동 맞춤", icon: opts.icon("width"), onClick: fitAll },
|
||||
] : [];
|
||||
}
|
||||
const growingCells = new Set();
|
||||
let growFrame = 0;
|
||||
function queueCellGrowth(e) {
|
||||
const field = e.target.closest("input, textarea, [contenteditable='true']");
|
||||
const td = field && field.closest("td, th");
|
||||
if (!admin || !td || !table.contains(td)) return;
|
||||
growingCells.add(td);
|
||||
if (growFrame) return;
|
||||
growFrame = requestAnimationFrame(function () {
|
||||
growFrame = 0; let changed = false;
|
||||
growingCells.forEach(function (cell) {
|
||||
if (!table.contains(cell)) return;
|
||||
const keys = (cell.dataset.meetCols || "").split(",").filter(Boolean), key = keys.at(-1);
|
||||
if (!key) return;
|
||||
const width = measure(cell) - keys.slice(0, -1).reduce((sum, k) => sum + widths[k], 0);
|
||||
if (width > widths[key]) { widths[key] = width; changed = true; }
|
||||
});
|
||||
growingCells.clear();
|
||||
if (changed) { applyWidths(); saveWidths(); }
|
||||
});
|
||||
}
|
||||
table.addEventListener("input", queueCellGrowth);
|
||||
table.addEventListener("compositionend", queueCellGrowth);
|
||||
table.addEventListener("dblclick", function (e) {
|
||||
const h = e.target.closest(".pj-meet-colresize");
|
||||
if (h) { e.preventDefault(); e.stopPropagation(); fit(h.dataset.col); }
|
||||
|
||||
@@ -8741,6 +8741,30 @@
|
||||
{ label: "전체 열 너비 자동 맞춤", icon: menuIcon("view_week"), onClick: autoFitAllCols },
|
||||
];
|
||||
}
|
||||
// 입력한 문단이 칸을 넘으면 해당 열만 늘린다. 명시적 개행·선택·초안은 유지한다.
|
||||
const growingCells = new Set();
|
||||
let growFrame = 0;
|
||||
function queueCellGrowth(e) {
|
||||
const field = e.target.closest("input, textarea, [contenteditable='true']");
|
||||
const td = field && field.closest("td, th");
|
||||
if (!isAdmin || !td || !table.contains(td)) return;
|
||||
growingCells.add(td);
|
||||
if (growFrame) return;
|
||||
growFrame = requestAnimationFrame(function () {
|
||||
growFrame = 0; let changed = false;
|
||||
growingCells.forEach(function (cell) {
|
||||
if (!table.contains(cell)) return;
|
||||
const keys = (cell.dataset.goalCols || "").split(",").filter(Boolean), key = keys[keys.length - 1];
|
||||
if (!colDefault[key]) return;
|
||||
const width = naturalCellWidth(cell) - keys.slice(0, -1).reduce((sum, k) => sum + colW[k], 0);
|
||||
if (width > colW[key]) { colW[key] = width; changed = true; }
|
||||
});
|
||||
growingCells.clear();
|
||||
if (changed) { applyColW(); saveColW(); fitAll(); stickKpi(); }
|
||||
});
|
||||
}
|
||||
table.addEventListener("input", queueCellGrowth);
|
||||
table.addEventListener("compositionend", queueCellGrowth);
|
||||
table.addEventListener("contextmenu", function (e) {
|
||||
const td = e.target.closest("th, td");
|
||||
if (!td || !table.contains(td)) return;
|
||||
|
||||
Reference in New Issue
Block a user