feat(project): add Excel font controls and reflow

This commit is contained in:
2026-10-03 05:47:57 +09:00
parent 494d84f7c2
commit dc2008c5b3
13 changed files with 146 additions and 42 deletions
+27 -6
View File
@@ -2,12 +2,19 @@
(function () {
"use strict";
const PREFIX = "pj-rich:v1:";
const STYLE_KEYS = ["bold", "italic", "underline", "color", "size"];
const TOGGLES = ["bold", "italic", "underline"];
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;
if (node.tagName === "I" || node.tagName === "EM") s.italic = true;
if (css.fontStyle) s.italic = css.fontStyle === "italic" || css.fontStyle === "oblique";
if (node.tagName === "U") s.underline = true;
const decoration = css.textDecorationLine || css.textDecoration;
if (decoration) s.underline = decoration.includes("underline");
const size = parseInt(css.fontSize, 10);
if (size >= 10 && size <= 32) s.size = size;
if (css.color) {
@@ -38,7 +45,7 @@
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;
if (prev && STYLE_KEYS.every(function (key) { return prev[key] === run[key]; })) prev.text += run.text;
else out.push(Object.assign({}, run));
});
return out;
@@ -60,6 +67,8 @@
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));
@@ -159,9 +168,16 @@
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";
TOGGLES.forEach(function (key) {
const all = chosen.every(function (r) { return r[key] === true; });
const some = chosen.some(function (r) { return r[key] === true; });
toolbar.querySelector("[data-goal-format='" + key + "']").setAttribute("aria-pressed", all ? "true" : some ? "mixed" : "false");
});
showColor(uniform("color", "#000000"));
toolbar.querySelector("[data-goal-format='size']").value = String(uniform("size", 14));
const sizes = chosen.map(function (r) { return r.size || 14; });
toolbar.querySelector("[data-goal-format='size']").textContent = sizes.every(function (n) { return n === sizes[0]; }) ? sizes[0] + "px" : "혼합";
toolbar.querySelector("[data-goal-format='size-up']").disabled = sizes.every(function (n) { return n >= 32; });
toolbar.querySelector("[data-goal-format='size-down']").disabled = sizes.every(function (n) { return n <= 10; });
toolbar.hidden = false;
place();
}
@@ -179,7 +195,8 @@
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; }
if (key === "reset") STYLE_KEYS.forEach(function (style) { delete middle[style]; });
else if (key === "size-delta") middle.size = Math.max(10, Math.min(32, (middle.size || 14) + value));
else middle[key] = value;
out.push(middle);
if (b < end) out.push(Object.assign({}, run, { text: run.text.slice(b - pos) }));
@@ -199,8 +216,12 @@
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)); });
TOGGLES.forEach(function (key) {
const btn = toolbar.querySelector("[data-goal-format='" + key + "']");
btn.addEventListener("click", function () { apply(key, btn.getAttribute("aria-pressed") !== "true"); });
});
toolbar.querySelector("[data-goal-format='size-up']").addEventListener("click", function () { apply("size-delta", 2); });
toolbar.querySelector("[data-goal-format='size-down']").addEventListener("click", function () { apply("size-delta", -2); });
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;