feat(project): add font options and selected goal text formatting

Restore the board header add button to the task side panel while keeping footer quick entry. Add safe, persisted goal text formatting and self-hosted Project font preferences with shared help updates and regression coverage.
This commit is contained in:
2026-10-02 17:28:54 +09:00
parent 035cb83d6f
commit 20232baf06
39 changed files with 756 additions and 54 deletions
+170
View File
@@ -0,0 +1,170 @@
/* 목표 셀의 선택한 글자 서식. 서버와 같은 버전 있는 텍스트 조각을 사용한다. */
(function () {
"use strict";
const PREFIX = "pj-rich:v1:";
function styles(node, inherited) {
const s = Object.assign({}, inherited);
if (node.nodeType !== 1) return s;
const css = node.style;
if (node.tagName === "B" || node.tagName === "STRONG") s.bold = true;
if (css.fontWeight) s.bold = css.fontWeight === "bold" || Number(css.fontWeight) >= 600;
const size = parseInt(css.fontSize, 10);
if (size >= 10 && size <= 32) s.size = size;
if (css.color) {
const rgb = css.color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
const hex = rgb ? "#" + rgb.slice(1).map(function (v) { return Number(v).toString(16).padStart(2, "0"); }).join("") : css.color;
if (/^#[\da-f]{6}$/i.test(hex)) s.color = hex.toLowerCase();
}
return s;
}
function readRuns(editor) {
const runs = [];
function add(text, style) { if (text) runs.push(Object.assign({ text: text }, style)); }
function walk(node, inherited) {
if (node.nodeType === 3) { add(node.nodeValue, inherited); return; }
if (node.nodeType !== 1) return;
if (node.tagName === "BR") { add("\n", inherited); return; }
if (/^(SCRIPT|STYLE|IMG|IFRAME)$/.test(node.tagName)) return;
const style = styles(node, inherited);
const block = /^(DIV|P)$/.test(node.tagName);
if (block && runs.length && !runs[runs.length - 1].text.endsWith("\n")) add("\n", inherited);
Array.from(node.childNodes).forEach(function (child) { walk(child, style); });
}
Array.from(editor.childNodes).forEach(function (child) { walk(child, {}); });
return compact(runs);
}
function compact(runs) {
const out = [];
runs.forEach(function (run) {
if (!run.text) return;
const prev = out[out.length - 1];
if (prev && prev.bold === run.bold && prev.color === run.color && prev.size === run.size) prev.text += run.text;
else out.push(Object.assign({}, run));
});
return out;
}
function encode(runs) {
runs = compact(runs);
return runs.some(function (r) { return Object.keys(r).length > 1; }) ? PREFIX + JSON.stringify(runs) : runs.map(function (r) { return r.text; }).join("");
}
function decode(value) {
if (value.startsWith(PREFIX)) {
try { const runs = JSON.parse(value.slice(PREFIX.length)); if (Array.isArray(runs)) return runs; } catch (_) {}
}
return [{ text: value }];
}
function paint(editor, runs) {
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 (/^#[\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));
});
}
function restore(editor, start, end) {
const walker = document.createTreeWalker(editor, NodeFilter.SHOW_TEXT);
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;
}
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)); }
const selection = window.getSelection(); selection.removeAllRanges(); selection.addRange(range);
return range;
}
function attach(editor) {
// 기존 자동 저장·병합·초안 복구와 같은 value 인터페이스를 제공한다.
Object.defineProperty(editor, "value", { configurable: true,
get: function () { return encode(readRuns(editor)); },
set: function (v) { paint(editor, decode(String(v || ""))); },
});
editor.addEventListener("keydown", function (e) {
if (e.isComposing || e.keyCode === 229) return;
if (e.key === "Enter") { e.preventDefault(); insert(editor, "\n"); }
});
editor.addEventListener("paste", function (e) {
e.preventDefault(); insert(editor, e.clipboardData.getData("text/plain"));
});
}
function insert(editor, text) {
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);
editor.dispatchEvent(new Event("input", { bubbles: true }));
}
function init(table, toolbar) {
if (!toolbar) return;
let saved = null;
function clear() {
saved = null;
toolbar.querySelectorAll("button, input, select").forEach(function (el) { el.disabled = true; });
}
function capture() {
// 서식 도구에 포커스를 옮길 때는 직전 글자 선택을 유지한다.
if (toolbar.contains(document.activeElement)) return;
const sel = window.getSelection();
if (!sel.rangeCount || sel.isCollapsed) { clear(); return; }
const range = sel.getRangeAt(0);
const root = range.startContainer.nodeType === 1 ? range.startContainer : range.startContainer.parentElement;
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 };
toolbar.querySelectorAll("button, input, select").forEach(function (el) { el.disabled = false; });
let pos = 0;
const chosen = readRuns(editor).filter(function (run) {
const end = pos + run.text.length, intersects = end > saved.start && pos < saved.end;
pos = end; return intersects;
});
function uniform(key, fallback) {
const value = chosen[0] && chosen[0][key];
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");
toolbar.querySelector("[data-goal-format='size']").value = String(uniform("size", 14));
}
document.addEventListener("selectionchange", capture);
table.addEventListener("mouseup", capture);
table.addEventListener("keyup", capture);
toolbar.addEventListener("mousedown", function (e) { if (e.target.closest("button")) e.preventDefault(); });
function apply(key, value) {
if (!saved || !saved.editor.isConnected) return;
const editor = saved.editor, out = []; let pos = 0;
readRuns(editor).forEach(function (run) {
const end = pos + run.text.length;
const a = Math.max(pos, saved.start), b = Math.min(end, saved.end);
if (b <= a) out.push(run);
else {
if (a > pos) out.push(Object.assign({}, run, { text: run.text.slice(0, a - pos) }));
const middle = Object.assign({}, run, { text: run.text.slice(a - pos, b - pos) });
if (key === "reset") { delete middle.color; delete middle.bold; delete middle.size; }
else middle[key] = value;
out.push(middle);
if (b < end) out.push(Object.assign({}, run, { text: run.text.slice(b - pos) }));
}
pos = end;
});
paint(editor, compact(out)); editor.focus(); restore(editor, saved.start, saved.end);
editor.dispatchEvent(new Event("input", { bubbles: true }));
}
toolbar.querySelector("[data-goal-format='color']").addEventListener("input", function (e) { apply("color", e.target.value); });
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"); });
document.addEventListener("mousedown", function (e) {
if (toolbar.contains(e.target) || (saved && saved.editor.contains(e.target))) return;
clear();
}, true);
}
window.PJGoalText = { attach: attach, init: init, readRuns: readRuns, paint: paint, encode: encode };
})();