class k { /** * @param {HTMLCanvasElement} canvasElement - 플로우차트를 그릴 Canvas 엘리먼트 */ constructor(s) { if (!s) throw new Error("CanvasRenderer에 canvas 엘리먼트가 제공되지 않았습니다."); this.canvas = s, this.ctx = this.canvas.getContext("2d"), this.config = { margin: 30, blockWidth: 100, blockHeight: 50, paddingLeft: 30, paddingTop: 10, minWidth: 600, minHeight: 800 }, this.maxWidth = 0, this.maxHeight = 0, this.nodes = [], this.allNodes = [], this.currentHoverNode = null, this.mouseX = 0, this.mouseY = 0, this.zoom = 1.22, this.offsetX = 0, this.offsetY = 0, this.fontScale = 0.81, this.branchTextMode = 0, this.arrowHeadSize = 9, this.showCoordsOnHover = !1, this.fullScreen = !0, this.colorMode = !1, this.darkMode = !1, this.isDragging = !1, this.dragStartX = 0, this.dragStartY = 0, this.dragStartOffsetX = 0, this.dragStartOffsetY = 0, this.canvas.style.cursor = "grab", this.canvas.__flowchartMouseMove && this.canvas.removeEventListener("mousemove", this.canvas.__flowchartMouseMove), this.canvas.__flowchartMouseLeave && this.canvas.removeEventListener("mouseleave", this.canvas.__flowchartMouseLeave), this.canvas.__flowchartMouseDown && this.canvas.removeEventListener("mousedown", this.canvas.__flowchartMouseDown), this.canvas.__flowchartMouseUp && (this.canvas.removeEventListener("mouseup", this.canvas.__flowchartMouseUp), window.removeEventListener("mouseup", this.canvas.__flowchartMouseUp)); const t = (l) => this._handleMouseMove(l), a = () => this._handleMouseLeave(), o = (l) => this._handleMouseDown(l), h = (l) => this._handleMouseUp(l); this.canvas.__flowchartMouseMove = t, this.canvas.__flowchartMouseLeave = a, this.canvas.__flowchartMouseDown = o, this.canvas.__flowchartMouseUp = h, this.canvas.addEventListener("mousemove", t), this.canvas.addEventListener("mouseleave", a), this.canvas.addEventListener("mousedown", o), this.canvas.addEventListener("mouseup", h), window.addEventListener("mouseup", h); } /** * 노드 배열을 바탕으로 Canvas에 플로우차트를 렌더링합니다. * @param {Array} nodes - 좌표 계산이 완료된 노드 배열 * @param {object} [options] - 커스텀 드로잉 설정 옵션 */ get showBranchText() { return this.branchTextMode !== 2; } set showBranchText(s) { this.branchTextMode = s ? 0 : 2; } draw(s, t = {}) { [ "zoom", "fontScale", "arrowHeadSize", "branchTextMode", "showCoordsOnHover", "fullScreen", "colorMode", "darkMode", "offsetX", "offsetY" ].forEach((f) => { t[f] !== void 0 && (this[f] = t[f]); }), this.config = { ...this.config, ...t }, this.nodes = s; const { margin: o, blockWidth: h, blockHeight: l, paddingLeft: e, paddingTop: i, minWidth: n, minHeight: r } = this.config, c = Math.max(...s.map((f) => f.y || 0)), d = Math.max(...s.map((f) => f.x || 0)); this.maxHeight = (c + 1) * (l + o) + i, this.maxWidth = (d + 1) * (h + o) + e, this.canvas.height = Math.max(this.maxHeight, r), this.canvas.width = Math.max(this.maxWidth, n); const g = { id: -1, type: "start", content: "start", x: 0, y: 0, next: 0 }; this.allNodes = [...s, g], this.currentHoverNode = null, this._render(); } /** * 전체 캔버스를 렌더링합니다. (호버 상태 포함) * @private */ _render() { this.fullScreen && this._calculateFitToCanvas(), this.ctx.fillStyle = this.darkMode ? "#1e1e1e" : "#ffffff", this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height), this.ctx.save(), this.ctx.translate(this.offsetX, this.offsetY), this.ctx.scale(this.zoom, this.zoom), this.allNodes.forEach((s) => { s === this.currentHoverNode && this._drawNodeHover(s), this._drawNode(s); }), this.allNodes.forEach((s) => this._drawArrow(s, this.allNodes)), this.ctx.restore(), this.currentHoverNode && this._drawTooltip(); } /** * v0.8 마우스 버튼 누름: 드래그 패닝 시작 * @private */ _handleMouseDown(s) { s.button === 0 && (this.isDragging = !0, this.dragStartX = s.clientX, this.dragStartY = s.clientY, this.dragStartOffsetX = this.offsetX, this.dragStartOffsetY = this.offsetY, this.canvas.style.cursor = "grabbing", s.preventDefault()); } /** * v0.8 마우스 버튼 해제: 드래그 종료 * @private */ _handleMouseUp(s) { this.isDragging && (this.isDragging = !1, this.canvas.style.cursor = "grab"); } /** * 마우스 이동 시 드래그 패닝 또는 호버 노드 히트 검사 * @private */ _handleMouseMove(s) { if (this.isDragging) { const g = s.clientX - this.dragStartX, f = s.clientY - this.dragStartY, x = this.canvas.getBoundingClientRect(), u = this.canvas.width / x.width, m = this.canvas.height / x.height; this.offsetX = this.dragStartOffsetX + g * u, this.offsetY = this.dragStartOffsetY + f * m, this.fullScreen && (this.fullScreen = !1), this._render(); return; } if (this.allNodes.length === 0) return; const t = this.canvas.getBoundingClientRect(), a = (s.clientX - t.left) * (this.canvas.width / t.width), o = (s.clientY - t.top) * (this.canvas.height / t.height); this.mouseX = a, this.mouseY = o; const h = (a - this.offsetX) / this.zoom, l = (o - this.offsetY) / this.zoom, { blockWidth: e, blockHeight: i, margin: n, paddingLeft: r, paddingTop: c } = this.config; let d = null; for (const g of this.allNodes) { const f = g.x * (e + n) + r, x = g.y * (i + n) + c; if (h >= f && h <= f + e && l >= x && l <= x + i) { d = g; break; } } d !== this.currentHoverNode ? (this.currentHoverNode = d, this._render()) : this.currentHoverNode && this._render(); } /** * 마우스가 캔버스를 떠났을 때 호버 초기화 및 드래그 해제 * @private */ _handleMouseLeave() { this.isDragging = !1, this.canvas.style.cursor = "grab", this.currentHoverNode !== null && (this.currentHoverNode = null, this._render()); } /** * 노드 호버 시 연한 붉은색 배경 채우기 * @private */ _drawNodeHover(s) { const { blockWidth: t, blockHeight: a, margin: o, paddingLeft: h, paddingTop: l } = this.config, e = s.x * (t + o) + h, i = s.y * (a + o) + l; this.ctx.beginPath(), s.type === "if" || s.type === "loop" ? (this.ctx.moveTo(e + t / 2, i), this.ctx.lineTo(e + t, i + a / 2), this.ctx.lineTo(e + t / 2, i + a), this.ctx.lineTo(e, i + a / 2)) : s.type === "start" || s.type === "end" ? (this.ctx.moveTo(e + a / 2, i), this.ctx.lineTo(e + t - a / 2, i), this.ctx.arc(e + t - a / 2, i + a / 2, a / 2, Math.PI / 2 * 3, Math.PI / 2, !1), this.ctx.lineTo(e + a / 2, i + a), this.ctx.arc(e + a / 2, i + a / 2, a / 2, Math.PI / 2, Math.PI / 2 * 3, !1)) : this.ctx.rect(e, i, t, a), this.ctx.closePath(), this.ctx.fillStyle = "rgba(255, 0, 0, 0.15)", this.ctx.fill(); } /** * 마우스 커서 위치에 (x, y) 논리 좌표 툴팁 출력 * @private */ _drawTooltip() { if (!this.showCoordsOnHover) return; const s = `(${this.currentHoverNode.x}, ${this.currentHoverNode.y})`; this.ctx.font = "bold 13px monospace"; const t = this.ctx.measureText(s).width, a = 6, o = t + a * 2, h = 22; let l = this.mouseX + 12, e = this.mouseY + 12; l + o > this.canvas.width && (l = this.mouseX - o - 5), e + h > this.canvas.height && (e = this.mouseY - h - 5), this.ctx.fillStyle = this.darkMode ? "rgba(30, 30, 30, 0.95)" : "rgba(255, 255, 255, 0.95)", this.ctx.strokeStyle = this.darkMode ? "#ff4d4d" : "#dc3545", this.ctx.lineWidth = 1, this.ctx.fillRect(l, e, o, h), this.ctx.strokeRect(l, e, o, h), this.ctx.fillStyle = this.darkMode ? "#ff4d4d" : "#dc3545", this.ctx.textAlign = "left", this.ctx.textBaseline = "top", this.ctx.fillText(s, l + a, e + a - 2); } /** * 개별 노드를 그립니다. (내부 메서드) * @private */ _drawNode(s) { const { blockWidth: t, blockHeight: a, margin: o, paddingLeft: h, paddingTop: l } = this.config, e = s.x * (t + o) + h, i = s.y * (a + o) + l, n = this._getNodeColors(s.type); this.ctx.beginPath(); let r = n.border; s === this.currentHoverNode && (r = this.darkMode ? "#ff4d4d" : "#dc3545"), this.ctx.strokeStyle = r, this.ctx.lineWidth = s === this.currentHoverNode ? 1.5 : 1, s.type === "if" || s.type === "loop" ? (this.ctx.moveTo(e + t / 2, i), this.ctx.lineTo(e + t, i + a / 2), this.ctx.lineTo(e + t / 2, i + a), this.ctx.lineTo(e, i + a / 2), this.ctx.closePath()) : s.type === "start" || s.type === "end" ? (this.ctx.moveTo(e + a / 2, i), this.ctx.lineTo(e + t - a / 2, i), this.ctx.arc(e + t - a / 2, i + a / 2, a / 2, Math.PI / 2 * 3, Math.PI / 2, !1), this.ctx.lineTo(e + a / 2, i + a), this.ctx.arc(e + a / 2, i + a / 2, a / 2, Math.PI / 2, Math.PI / 2 * 3, !1), this.ctx.closePath()) : this.ctx.rect(e, i, t, a), this.ctx.fillStyle = n.fill, this.ctx.fill(), this.ctx.stroke(), this._drawNodeText(s, e, i, t, a); } /** * 노드 텍스트를 크기 조절, 2줄 래핑, 말줄임을 감안하여 출력합니다. * @private */ _drawNodeText(s, t, a, o, h) { if (!s.content) return; const l = this._getNodeColors(s.type); this.ctx.fillStyle = s === this.currentHoverNode ? this.darkMode ? "#ff4d4d" : "#dc3545" : l.text, this.ctx.textAlign = "center", this.ctx.textBaseline = "middle"; const e = o - 10, i = s.type === "if" ? 0.7 * this.fontScale : 0.8 * this.fontScale, n = Math.round(20 * i), r = Math.round(10 * i); let c = []; if (s.content.includes("/") ? c = s.content.split("/") : c = [s.content], c.length === 1) { const x = c[0]; let u = -1; for (let m = n; m >= r; m--) if (this.ctx.font = `${m}px Arial`, this.ctx.measureText(x).width <= e) { u = m; break; } if (u !== -1) { this.ctx.font = `${u}px Arial`, this.ctx.fillText(x, t + o / 2, a + h / 2); return; } c = this._splitTextIntoTwo(x); } let d = -1; for (let x = n; x >= r; x--) { this.ctx.font = `${x}px Arial`; const u = this.ctx.measureText(c[0]).width, m = this.ctx.measureText(c[1]).width; if (u <= e && m <= e) { d = x; break; } } if (d !== -1) { this.ctx.font = `${d}px Arial`, this.ctx.fillText(c[0], t + o / 2, a + h / 3), this.ctx.fillText(c[1], t + o / 2, a + 2 * h / 3); return; } this.ctx.font = `${r}px Arial`; const g = this._truncateText(c[0], e), f = this._truncateText(c[1], e); this.ctx.fillText(g, t + o / 2, a + h / 3), this.ctx.fillText(f, t + o / 2, a + 2 * h / 3); } /** * 문자열을 단어(공백) 혹은 글자 수 기준으로 절반 부근에서 2줄로 분리합니다. * @private */ _splitTextIntoTwo(s) { const t = Math.floor(s.length / 2); let a = -1, o = 1 / 0; for (let h = 0; h < s.length; h++) if (s[h] === " ") { const l = Math.abs(h - t); l < o && (o = l, a = h); } return a !== -1 ? [s.substring(0, a), s.substring(a + 1)] : [s.substring(0, t), s.substring(t)]; } /** * 가용폭을 넘지 않도록 글자를 잘라내고 '...'을 붙입니다. * @private */ _truncateText(s, t) { if (this.ctx.measureText(s).width <= t) return s; let a = s; for (; a.length > 0 && this.ctx.measureText(a + "...").width > t; ) a = a.substring(0, a.length - 1); return a + "..."; } /** * 노드 간 화살표를 그립니다. (내부 메서드) * @private */ _drawArrow(s, t) { if (this.ctx.beginPath(), this.ctx.strokeStyle = this.darkMode ? "#e0e0e0" : "#000000", this.ctx.lineWidth = 1, s.type === "if" || s.type === "loop") { const a = t.find((h) => h.id === s.yes); a && this._drawSingleArrow(s.x, s.y, a.x, a.y, "Yes", t); const o = t.find((h) => h.id === s.no); if (o) this._drawSingleArrow(s.x, s.y, o.x, o.y, "No", t); else { const h = t.find((l) => l.id === s.next); h && this._drawSingleArrow(s.x, s.y, h.x, h.y, "", t); } } else if (s.type !== "end") { const a = t.find((o) => o.id === s.next); a && this._drawSingleArrow(s.x, s.y, a.x, a.y, "", t); } this.ctx.stroke(); } /** * 화살표 하나를 캔버스에 그립니다. (내부 메서드) * @private */ _drawSingleArrow(s, t, a, o, h, l) { const { blockWidth: e, blockHeight: i, margin: n, paddingLeft: r, paddingTop: c } = this.config, d = 15; this.ctx.font = `${d}px Arial`; const g = this.darkMode ? "#e0e0e0" : "#000000"; this.ctx.fillStyle = g, this.ctx.strokeStyle = g; let f = h; h === "Yes" ? this.branchTextMode === 1 ? f = "Y" : this.branchTextMode === 2 && (f = "") : h === "No" && (this.branchTextMode === 1 ? f = "N" : this.branchTextMode === 2 && (f = "")); const x = this.arrowHeadSize; if (s === a) { const u = s * (e + n) + e / 2 + r, m = t * (i + n) + i + c, w = a * (e + n) + e / 2 + r, p = o * (i + n) - 2 + c; this.ctx.fillText(f, u + n / 2, m + d / 2), this.ctx.moveTo(u, m), this.ctx.lineTo(w, p), this.ctx.lineTo(w - x / 2, p - x), this.ctx.moveTo(w, p), this.ctx.lineTo(w + x / 2, p - x); } else if (t === o) if (s < a) { const u = s * (e + n) + e + r, m = t * (i + n) + i / 2 + c, w = a * (e + n) - 2 + r, p = o * (i + n) + i / 2 + c; this.ctx.fillText(f, u + d / 2, m - d / 2), this.ctx.moveTo(u, m), this.ctx.lineTo(w, p), this.ctx.lineTo(w - x, p - x / 2), this.ctx.moveTo(w, p), this.ctx.lineTo(w - x, p + x / 2); } else { const u = s * (e + n) + e / 2 + r, m = t * (i + n) + i + c, w = a * (e + n) - 2 + r, p = o * (i + n) + i / 2 + c; this.ctx.fillText(f, u - d, m - d / 2), this.ctx.moveTo(u, m), this.ctx.lineTo(u, m + n / 4), this.ctx.arc(u - n / 4, m + n / 4, n / 4, 0, Math.PI / 2, !1), this.ctx.lineTo(w - n / 4, m + n / 2), this.ctx.arc(w - n / 4, m + n / 4, n / 4, Math.PI / 2, Math.PI, !1), this.ctx.lineTo(w - n / 2, p + n * 3 / 4), this.ctx.arc(w - n / 4, p + n / 4, n / 4, Math.PI, Math.PI * 3 / 2, !1), this.ctx.lineTo(w, p), this.ctx.lineTo(w - x, p - x / 2), this.ctx.moveTo(w, p), this.ctx.lineTo(w - x, p + x / 2); } else if (o < t) { let u = s - 1, m = !1; for (; u >= 0; u--) if (l.find((v) => v.x === u && v.y === t) !== void 0) { m = !0; break; } const w = this.branchTextMode === 0 ? "loop" : ""; if (m) { const p = s * (e + n) + e / 2 + r, v = t * (i + n) + i + c, y = a * (e + n) - 2 + r, b = o * (i + n) + i / 2 + c; this.ctx.fillText(w, p - d * 3 / 2, v + d / 2), this.ctx.moveTo(p, v), this.ctx.lineTo(p, v + n / 4), this.ctx.arc(p - n / 4, v + n / 4, n / 4, 0, Math.PI / 2, !1), this.ctx.lineTo(y - n / 4, v + n / 2), this.ctx.arc(y - n / 4, v + n / 4, n / 4, Math.PI / 2, Math.PI, !1), this.ctx.lineTo(y - n / 2, b + n * 3 / 4), this.ctx.arc(y - n / 4, b + n / 4, n / 4, Math.PI, Math.PI * 3 / 2, !1), this.ctx.lineTo(y, b), this.ctx.lineTo(y - x, b - x / 2), this.ctx.moveTo(y, b), this.ctx.lineTo(y - x, b + x / 2); } else { const p = s * (e + n) + r, v = t * (i + n) + i / 2 + c, y = a * (e + n) - 2 + r, b = o * (i + n) + i / 2 + c; this.ctx.fillText(w, p - d, v - d / 2), this.ctx.moveTo(p, v), this.ctx.lineTo(y - n / 4, v), this.ctx.arc(y - n / 4, v - n / 4, n / 4, Math.PI / 2, Math.PI, !1), this.ctx.lineTo(y - n / 2, b + n * 3 / 4), this.ctx.arc(y - n / 4, b + n / 4, n / 4, Math.PI, Math.PI * 3 / 2, !1), this.ctx.lineTo(y, b), this.ctx.lineTo(y - x, b - x / 2), this.ctx.moveTo(y, b), this.ctx.lineTo(y - x, b + x / 2); } } else { let u = t + 1, m = !1; for (; u < o; u++) if (l.find((p) => p.x === s && p.y === u) !== void 0) { m = !0; break; } if (m) { const w = s * (e + n) + e + r, p = t * (i + n) + i / 2 + c, v = a * (e + n) + e / 2 + r, y = o * (i + n) - 2 + c; this.ctx.fillText(f, w + d / 2, p - d / 2), this.ctx.moveTo(w, p), this.ctx.lineTo(w + n / 4, p), this.ctx.arc(w + n / 4, p + n / 4, n / 4, -Math.PI / 2, 0, !1), this.ctx.lineTo(w + n / 2, y - n * 3 / 4), this.ctx.arc(w + n / 4, y - n * 3 / 4, n / 4, 0, Math.PI / 2, !1), this.ctx.lineTo(v + n / 4, y - n / 2), this.ctx.arc(v + n / 4, y - n / 4, n / 4, Math.PI * 3 / 2, Math.PI, !0); } else { const w = s * (e + n) + e / 2 + r, p = t * (i + n) + i + c, v = a * (e + n) + e / 2 + r, y = o * (i + n) - 2 + c; this.ctx.fillText(f, w + d / 2, p - d / 2), this.ctx.moveTo(w, p), this.ctx.lineTo(w, y - n * 3 / 4), this.ctx.arc(w - n / 4, y - n * 3 / 4, n / 4, 0, Math.PI / 2, !1), this.ctx.lineTo(v + n / 4, y - n / 2), this.ctx.arc(v + n / 4, y - n / 4, n / 4, Math.PI * 3 / 2, Math.PI, !0); } } } /** * 플로우차트 점유 크기에 맞춰 캔버스 물리 해상도 리사이징 */ resizeCanvas() { const { margin: s, blockWidth: t, blockHeight: a, paddingLeft: o, paddingTop: h, minWidth: l, minHeight: e } = this.config, i = Math.max(...this.allNodes.map((r) => r.y || 0)), n = Math.max(...this.allNodes.map((r) => r.x || 0)); this.maxHeight = (i + 1) * (a + s) + h + s, this.maxWidth = (n + 1) * (t + s) + o + s, this.canvas.height = Math.max(this.maxHeight, e), this.canvas.width = Math.max(this.maxWidth, l); } /** * 플로우차트의 총 픽셀 점유 경계 너비/높이를 계산합니다. * @private */ _getChartBounds() { if (this.allNodes.length === 0) return { width: 0, height: 0 }; const { blockWidth: s, blockHeight: t, margin: a, paddingLeft: o, paddingTop: h } = this.config; let l = 1 / 0, e = -1 / 0, i = 1 / 0, n = -1 / 0; this.allNodes.forEach((d) => { d.x < l && (l = d.x), d.x > e && (e = d.x), d.y < i && (i = d.y), d.y > n && (n = d.y); }); const r = (e - l + 1) * (s + a) - a + o * 2, c = (n - i + 1) * (t + a) - a + h * 2; return { width: r, height: c }; } /** * 꽉 찬 화면을 위한 오토 줌 및 오프셋 좌표 자동 계산 (10% 안전 가시 마진 확보) * @private */ _calculateFitToCanvas() { const s = this._getChartBounds(); if (s.width === 0 || s.height === 0) return; const t = this.canvas.width / s.width, a = this.canvas.height / s.height; this.zoom = Math.min(t, a) * 0.9, this.offsetX = (this.canvas.width - s.width * this.zoom) / 2, this.offsetY = (this.canvas.height - s.height * this.zoom) / 2; } /** * 현재 캔버스 크기에 꽉 차도록 줌인/줌아웃을 수행합니다. (Auto Fit) */ fitToCanvas() { this._calculateFitToCanvas(), this._render(); } /** * 다크모드 및 컬러모드에 대응되는 노드 유형별 채우기/선/텍스트 색상 목록 반환 * @private */ _getNodeColors(s) { if (this.darkMode) if (this.colorMode) switch (s) { case "start": case "end": return { fill: "#1b4332", border: "#2d6a4f", text: "#d8f3dc" }; case "if": return { fill: "#5c3d03", border: "#8c6003", text: "#ffd166" }; case "loop": return { fill: "#4a154b", border: "#6b114d", text: "#f3d2c1" }; default: return { fill: "#1d3557", border: "#457b9d", text: "#f1faee" }; } else return { fill: "#2d2d2d", border: "#e0e0e0", text: "#e0e0e0" }; else if (this.colorMode) switch (s) { case "start": case "end": return { fill: "#d4edda", border: "#c3e6cb", text: "#155724" }; case "if": return { fill: "#fff3cd", border: "#ffeeba", text: "#856404" }; case "loop": return { fill: "#f8d7da", border: "#f5c6cb", text: "#721c24" }; default: return { fill: "#cce5ff", border: "#b8daff", text: "#004085" }; } else return { fill: "#ffffff", border: "#000000", text: "#000000" }; } /** * 렌더링된 플로우차트를 이미지 파일(PNG)로 다운로드합니다. * @param {string} [filename='flowchart.png'] - 저장할 파일 이름 */ download(s = "flowchart.png") { const t = document.createElement("canvas"); t.width = this.maxWidth, t.height = this.maxHeight; const a = t.getContext("2d"), o = this.currentHoverNode; this.currentHoverNode = null, this._render(); const h = this.ctx.getImageData(0, 0, this.maxWidth, this.maxHeight); a.putImageData(h, 0, 0); const l = document.createElement("a"); l.href = t.toDataURL("image/png"), l.download = s, l.click(), this.currentHoverNode = o, this._render(); } } class N { /** * 노드들의 상대적 그리드 좌표(x, y)와 점유 너비/높이(width, height)를 계산하여 노드 객체에 주입합니다. * @param {Array} nodes - 파싱된 노드 배열 * @returns {Array} - 좌표 계산이 완료된 노드 배열 */ calculatePositions(s) { if (!s || s.length === 0) return []; const t = (a, o, h, l, e) => { const i = s.find((c) => c.id === a); if (!i) return [1, 1]; if (i.x === o && i.y === h) return [i.width || 1, i.height || 1]; i.type === "loop" && (e = i.id); const n = i.x !== void 0 ? o - i.x : 0, r = i.y !== void 0 ? h - i.y : 0; if (i.x = o, i.y = h, i.yes !== void 0) { let c = 1, d = 1, g = 1, f = 1; if ([c, d] = t(i.yes, o + 1, h, i.next, e), i.no !== void 0 ? ([g, f] = t(i.no, o, h + 1, i.next, e), d > 1 && g > 1 ? ([c, d] = t(i.yes, o + g, h, i.next, e), i.width = c + g) : ([c, d] = t(i.yes, o + 1, h, i.next, e), i.width = Math.max(1 + c, g)), i.height = Math.max(d, f + 1)) : (i.width = c + 1, i.height = d), i.next !== l) { const [x, u] = t(i.next, o, h + i.height, l, e); i.width = Math.max(i.width, x + 1, c + x), i.height += u; } } else if (i.type === "end" || i.next === l) i.width = 1, i.height = 1; else { const c = s.find((g) => g.id === i.next), d = s.find((g) => g.id === e); if (c !== void 0 && (c.x === void 0 || n === o - c.x && r === h + 1 - c.y) && (d === void 0 || i.next !== e && i.next !== d.next)) { const [g, f] = t(i.next, o, h + 1, l, e); i.width = Math.max(1, g), i.height = f + 1; } else i.width = 1, i.height = 1; } return [i.width, i.height]; }; return t(0, 0, 1, 0, void 0), s; } } class I { /** * 소스코드를 줄 단위로 잘라 표준 포맷으로 포매팅합니다. * @param {string} code - 정렬되지 않은 원본 소스코드 * @returns {string} - 표준 포맷으로 정렬된 소스코드 */ format(s) { throw new Error("format() 메서드가 구현되지 않았습니다."); } /** * 소스코드를 분석하여 플로우차트용 표준 노드 구조로 변환합니다. * @param {string} code - 원본 소스코드 * @returns {Array} - 플로우차트 그리기에 사용할 표준 노드 배열 */ parse(s) { throw new Error("parse() 메서드가 구현되지 않았습니다."); } } class $ extends I { constructor() { super(), this.maxId = 1, this.loopId = [], this.breakId = []; } /** * C코드를 정렬하고 괄호를 보정하여 표준 형식으로 변환합니다. * @param {string} code * @returns {string} */ format(s) { if (!s) return ""; let t = s.split(` `).map((o) => o.trim()).filter((o) => o !== "").join(` `); for (; t.includes(" "); ) t = t.replace(" ", " "); t = t.replace(/(if|while)\s*\(((?:[^()]*|\([^()]*\))+)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 ($2) { $3 }"), t = t.replace(/for\s*\(([^;]*);([^;]+);([^)]*)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "for ($1; $2; $3) { $4 }"), t = t.replace(/(else)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 { $2 }"); for (let o = 0; o < 5; o++) t = t.replace(/(if\s*\((?:[^()]*|\([^()]*\))+\)|while\s*\((?:[^()]*|\([^()]*\))+\)|for\s*\([^)]+\)|else)\s*([^{;\s\n][^{]*\{[^}]*\})/g, "$1 { $2 }"); for (t = t.replace(/;/g, `; `), t = t.replace(/\{/g, `{ `), t = t.replace(/}/g, `} `), t = t.replace(/\}\s*\n\s*else/g, "} else"), t = t.replace(/\s*\n\s*{/g, " {"), t = t.replace(/\n\s+{/g, "{"); t.includes(` `); ) t = t.replace(` `, ` `); t = t.replace(/for\s*\(([^;]*);\s*\n\s*([^;]+);\s*\n\s*([^)]*)\)/g, "for ($1; $2; $3)"); let a = 0; return t.split(` `).map((o) => { o = o.replace(/^\s/g, ""), o.includes("}") && a--; const h = " ".repeat(Math.max(0, a)) + o; return o.includes("{") && a++, h; }).join(` `).trim(); } /** * C코드를 노드로 재구성합니다. * @param {string} code * @returns {Array} */ parse(s) { if (!s) return []; this.maxId = 1, this.loopId = [], this.breakId = []; const a = this.format(s).split(` `), o = this._getNodeC(a, 0, a.length), h = { id: a.length, type: "end", content: "end" }; return o.push(h), o; } /** * C코드 라인들을 순회하며 재귀적으로 노드를 생성합니다. (내부 함수) * @private */ _getNodeC(s, t, a) { this.maxId = Math.max(s.length + 2, this.maxId); const o = []; for (let h = 0; h < s.length; h++) { const l = s[h].trim(), e = {}; if (l.startsWith("//")) { e.id === void 0 && (e.id = h + t, e.header = l.slice(2)); continue; } if (e.id === void 0 && (e.id = h + t), l.startsWith("if")) { e.type = "if"; const i = l.match(/\(((?:[^()]*|\([^()]*\))+)\)/); e.content = i ? i[1] : ""; let n, r = 1, c = []; for (n = h + 1; n < s.length; n++) { if (s[n].includes("}") && r--, r === 0) { e.yes = h + 1 + t, e.next = n + 1 + t, e.next === s.length + t && (e.next = a), c = s.slice(h + 1, n), h = n; break; } s[n].includes("{") && r++; } let d = []; if (n < s.length && s[n].includes("else")) { e.no = n + t + 1; let f = 1; for (n++; n < s.length; n++) { if (s[n].includes("}") && f--, f === 0) { e.next = n + t + 1, e.next === s.length + t && (e.next = a), d = s.slice(e.no - t, n), this._getNodeC(d, e.no, e.next).forEach((u) => o.push(u)), h = n; break; } s[n].includes("{") && f++; } } this._getNodeC(c, e.yes, e.next).forEach((f) => o.push(f)); } else if (l.startsWith("for")) { const i = {}, n = {}, r = l.match(/for\s*\(([^;]*);([^;]+);((?:[^()]*|\([^()]*\))+)\)/); r ? (e.content = r[1].trim(), i.content = r[2].trim(), n.content = r[3].trim()) : (e.content = "", i.content = "", n.content = ""), e.content !== "" ? (e.type = "plain", i.id = this.maxId + 1, this.maxId = i.id, i.type = "loop", this.loopId.push(i.id), e.next = i.id, o.push(i)) : (e.type = "loop", e.content = i.content, this.loopId.push(e.id)), n.content !== "" && (n.id = this.maxId + 1, this.maxId = n.id, n.type = "plain", n.next = i.id !== void 0 ? i.id : e.id, o.push(n)); let c = 1; for (let d = h + 1; d < s.length; d++) { if (s[d].includes("}") && c--, c === 0) { i.yes = h + 1 + t, i.next = d + 1 + t, i.next === s.length + t && (i.next = a), this.breakId.push(i.next); const g = s.slice(h + 1, d); n.id === void 0 && (n.id = e.type === "loop" ? e.id : i.id), this._getNodeC(g, i.yes, n.id).forEach((x) => o.push(x)), e.type === "loop" && (e.next = i.next, e.yes = i.yes), h = d; break; } s[d].includes("{") && c++; } } else l.startsWith("break;") ? (e.content = "break", e.type = "plain", e.next = this.breakId.pop()) : l.startsWith("continue;") ? (e.content = "continue", e.type = "plain", e.next = this.loopId.pop()) : (e.type = "plain", e.content = l.replace(";", ""), e.id - t === s.length - 1 ? e.next = a : e.next = h + 1 + t); o.push(e); } return o; } } class P extends I { /** * Java코드를 줄 단위로 잘라 표준 포맷으로 포매팅합니다. * @param {string} code * @returns {string} */ format(s) { if (!s) return ""; let t = s.split(` `).map((o) => o.trim()).filter((o) => o !== "").join(` `); for (; t.includes(" "); ) t = t.replace(" ", " "); t = t.replace(/(if|while)\s*\(((?:[^()]*|\([^()]*\))+)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 ($2) { $3 }"), t = t.replace(/for\s*\(([^;]*);([^;]+);([^)]*)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "for ($1; $2; $3) { $4 }"), t = t.replace(/(else)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 { $2 }"); for (let o = 0; o < 5; o++) t = t.replace(/(if\s*\((?:[^()]*|\([^()]*\))+\)|while\s*\((?:[^()]*|\([^()]*\))+\)|for\s*\([^)]+\)|else)\s*([^{;\s\n][^{]*\{[^}]*\})/g, "$1 { $2 }"); for (t = t.replace(/;/g, `; `), t = t.replace(/\{/g, `{ `), t = t.replace(/}/g, `} `), t = t.replace(/\}\s*\n\s*else/g, "} else"), t = t.replace(/\s*\n\s*{/g, " {"), t = t.replace(/\n\s+{/g, "{"); t.includes(` `); ) t = t.replace(` `, ` `); t = t.replace(/for\s*\(([^;]*);\s*\n\s*([^;]+);\s*\n\s*([^)]*)\)/g, "for ($1; $2; $3)"); let a = 0; return t.split(` `).map((o) => { o = o.replace(/^\s/g, ""), o.includes("}") && a--; const h = " ".repeat(Math.max(0, a)) + o; return o.includes("{") && a++, h; }).join(` `).trim(); } /** * Java코드를 노드로 재구성합니다. * @param {string} code * @returns {Array} */ parse(s) { if (!s) return []; this.maxId = 1, this.loopId = [], this.breakId = []; const a = this.format(s).split(` `), o = this._getNodeJava(a, 0, a.length), h = { id: a.length, type: "end", content: "end" }; return o.push(h), o; } /** * Java코드 라인들을 순회하며 재귀적으로 노드를 생성합니다. (내부 함수) * @private */ _getNodeJava(s, t, a) { this.maxId = Math.max(s.length + 2, this.maxId); const o = []; for (let h = 0; h < s.length; h++) { const l = s[h].trim(), e = {}; if (l.startsWith("//")) { e.id === void 0 && (e.id = h + t, e.header = l.slice(2)); continue; } if (e.id === void 0 && (e.id = h + t), l.startsWith("if")) { e.type = "if"; const i = l.match(/\(((?:[^()]*|\([^()]*\))+)\)/); e.content = i ? i[1] : ""; let n, r = 1, c = []; for (n = h + 1; n < s.length; n++) { if (s[n].includes("}") && r--, r === 0) { e.yes = h + 1 + t, e.next = n + 1 + t, e.next === s.length + t && (e.next = a), c = s.slice(h + 1, n), h = n; break; } s[n].includes("{") && r++; } let d = []; if (n < s.length && s[n].includes("else")) { e.no = n + t + 1; let f = 1; for (n++; n < s.length; n++) { if (s[n].includes("}") && f--, f === 0) { e.next = n + t + 1, e.next === s.length + t && (e.next = a), d = s.slice(e.no - t, n), this._getNodeJava(d, e.no, e.next).forEach((u) => o.push(u)), h = n; break; } s[n].includes("{") && f++; } } this._getNodeJava(c, e.yes, e.next).forEach((f) => o.push(f)); } else if (l.startsWith("for")) { const i = {}, n = {}, r = l.match(/for\s*\(([^;]*);([^;]+);((?:[^()]*|\([^()]*\))+)\)/); r ? (e.content = r[1].trim(), i.content = r[2].trim(), n.content = r[3].trim()) : (e.content = "", i.content = "", n.content = ""), e.content !== "" ? (e.type = "plain", i.id = this.maxId + 1, this.maxId = i.id, i.type = "loop", this.loopId.push(i.id), e.next = i.id, o.push(i)) : (e.type = "loop", e.content = i.content, this.loopId.push(e.id)), n.content !== "" && (n.id = this.maxId + 1, this.maxId = n.id, n.type = "plain", n.next = i.id !== void 0 ? i.id : e.id, o.push(n)); let c = 1; for (let d = h + 1; d < s.length; d++) { if (s[d].includes("}") && c--, c === 0) { i.yes = h + 1 + t, i.next = d + 1 + t, i.next === s.length + t && (i.next = a), this.breakId.push(i.next); const g = s.slice(h + 1, d); n.id === void 0 && (n.id = e.type === "loop" ? e.id : i.id), this._getNodeJava(g, i.yes, n.id).forEach((x) => o.push(x)), e.type === "loop" && (e.next = i.next, e.yes = i.yes), h = d; break; } s[d].includes("{") && c++; } } else l.startsWith("break;") ? (e.content = "break", e.type = "plain", e.next = this.breakId.pop()) : l.startsWith("continue;") ? (e.content = "continue", e.type = "plain", e.next = this.loopId.pop()) : (e.type = "plain", e.content = l.replace(";", ""), e.id - t === s.length - 1 ? e.next = a : e.next = h + 1 + t); o.push(e); } return o; } } class T extends I { /** * JS코드를 줄 단위로 잘라 표준 포맷으로 포매팅합니다. * @param {string} code * @returns {string} */ format(s) { if (!s) return ""; let t = s.split(` `).map((o) => o.trim()).filter((o) => o !== "").join(` `); for (; t.includes(" "); ) t = t.replace(" ", " "); t = t.replace(/(if|while)\s*\(((?:[^()]*|\([^()]*\))+)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 ($2) { $3 }"), t = t.replace(/for\s*\(([^;]*);([^;]+);([^)]*)\)[\s\n]*([^{;\s\n][^;\n]*;)/g, "for ($1; $2; $3) { $4 }"), t = t.replace(/(else)[\s\n]*([^{;\s\n][^;\n]*;)/g, "$1 { $2 }"); for (let o = 0; o < 5; o++) t = t.replace(/(if\s*\((?:[^()]*|\([^()]*\))+\)|while\s*\((?:[^()]*|\([^()]*\))+\)|for\s*\([^)]+\)|else)\s*([^{;\s\n][^{]*\{[^}]*\})/g, "$1 { $2 }"); for (t = t.replace(/;/g, `; `), t = t.replace(/\{/g, `{ `), t = t.replace(/}/g, `} `), t = t.replace(/\}\s*\n\s*else/g, "} else"), t = t.replace(/\s*\n\s*{/g, " {"), t = t.replace(/\n\s+{/g, "{"); t.includes(` `); ) t = t.replace(` `, ` `); t = t.replace(/for\s*\(([^;]*);\s*\n\s*([^;]+);\s*\n\s*([^)]*)\)/g, "for ($1; $2; $3)"); let a = 0; return t.split(` `).map((o) => { o = o.replace(/^\s/g, ""), o.includes("}") && a--; const h = " ".repeat(Math.max(0, a)) + o; return o.includes("{") && a++, h; }).join(` `).trim(); } /** * JS코드를 노드로 재구성합니다. * @param {string} code * @returns {Array} */ parse(s) { if (!s) return []; this.maxId = 1, this.loopId = [], this.breakId = []; const a = this.format(s).split(` `), o = this._getNodeJS(a, 0, a.length), h = { id: a.length, type: "end", content: "end" }; return o.push(h), o; } /** * JS코드 라인들을 순회하며 재귀적으로 노드를 생성합니다. (내부 함수) * @private */ _getNodeJS(s, t, a) { this.maxId = Math.max(s.length + 2, this.maxId); const o = []; for (let h = 0; h < s.length; h++) { const l = s[h].trim(), e = {}; if (l.startsWith("//")) { e.id === void 0 && (e.id = h + t, e.header = l.slice(2)); continue; } if (e.id === void 0 && (e.id = h + t), l.startsWith("if")) { e.type = "if"; const i = l.match(/\(((?:[^()]*|\([^()]*\))+)\)/); e.content = i ? i[1] : ""; let n, r = 1, c = []; for (n = h + 1; n < s.length; n++) { if (s[n].includes("}") && r--, r === 0) { e.yes = h + 1 + t, e.next = n + 1 + t, e.next === s.length + t && (e.next = a), c = s.slice(h + 1, n), h = n; break; } s[n].includes("{") && r++; } let d = []; if (n < s.length && s[n].includes("else")) { e.no = n + t + 1; let f = 1; for (n++; n < s.length; n++) { if (s[n].includes("}") && f--, f === 0) { e.next = n + t + 1, e.next === s.length + t && (e.next = a), d = s.slice(e.no - t, n), this._getNodeJS(d, e.no, e.next).forEach((u) => o.push(u)), h = n; break; } s[n].includes("{") && f++; } } this._getNodeJS(c, e.yes, e.next).forEach((f) => o.push(f)); } else if (l.startsWith("for")) { const i = {}, n = {}, r = l.match(/for\s*\(([^;]*);([^;]+);((?:[^()]*|\([^()]*\))+)\)/); r ? (e.content = r[1].trim(), i.content = r[2].trim(), n.content = r[3].trim()) : (e.content = "", i.content = "", n.content = ""), e.content !== "" ? (e.type = "plain", i.id = this.maxId + 1, this.maxId = i.id, i.type = "loop", this.loopId.push(i.id), e.next = i.id, o.push(i)) : (e.type = "loop", e.content = i.content, this.loopId.push(e.id)), n.content !== "" && (n.id = this.maxId + 1, this.maxId = n.id, n.type = "plain", n.next = i.id !== void 0 ? i.id : e.id, o.push(n)); let c = 1; for (let d = h + 1; d < s.length; d++) { if (s[d].includes("}") && c--, c === 0) { i.yes = h + 1 + t, i.next = d + 1 + t, i.next === s.length + t && (i.next = a), this.breakId.push(i.next); const g = s.slice(h + 1, d); n.id === void 0 && (n.id = e.type === "loop" ? e.id : i.id), this._getNodeJS(g, i.yes, n.id).forEach((x) => o.push(x)), e.type === "loop" && (e.next = i.next, e.yes = i.yes), h = d; break; } s[d].includes("{") && c++; } } else l.startsWith("break;") ? (e.content = "break", e.type = "plain", e.next = this.breakId.pop()) : l.startsWith("continue;") ? (e.content = "continue", e.type = "plain", e.next = this.loopId.pop()) : (e.type = "plain", e.content = l.replace(";", ""), e.id - t === s.length - 1 ? e.next = a : e.next = h + 1 + t); o.push(e); } return o; } } class _ extends I { /** * Python코드를 정형 포맷으로 포매팅합니다. * @param {string} code * @returns {string} */ format(s) { if (!s) return ""; const t = s.split(` `).filter((o) => o.trim() !== ""); if (t.length === 0) return ""; let a = 4; for (const o of t) { const h = o.match(/^(\s+)/); if (h) { a = h[1].length; break; } } return t.map((o) => { const h = o.match(/^(\s*)/), l = h ? h[1].length : 0, e = Math.round(l / a); return " ".repeat(e) + o.trim(); }).join(` `); } /** * Python코드를 노드로 재구성합니다. * @param {string} code * @returns {Array} */ parse(s) { if (!s) return []; this.maxId = 1, this.loopId = [], this.breakId = []; const a = this.format(s).split(` `), o = this._getNodePython(a, 0, a.length), h = { id: a.length * 3 + 2, // ID 유실 방지를 위해 충분히 여유있는 ID 부여 type: "end", content: "end" }; o.push(h); const l = h.id; return o.forEach((e) => { e.next === a.length && (e.next = l); }), o; } /** * 줄의 들여쓰기(공백 수)를 계산합니다. * @private */ _getIndent(s) { const t = s.match(/^(\s*)/); return t ? t[1].length : 0; } /** * Python코드 라인들을 순회하며 재귀적으로 노드를 생성합니다. (내부 함수) * @private */ _getNodePython(s, t, a) { this.maxId = Math.max(s.length * 3 + 2, this.maxId); const o = []; for (let h = 0; h < s.length; h++) { const l = s[h], e = l.trim(); if (!e) continue; const i = {}; if (e.startsWith("#")) { i.id === void 0 && (i.id = h + t, i.header = e.slice(1).trim()); continue; } i.id === void 0 && (i.id = h + t); const n = this._getIndent(l); if (e.startsWith("if ") || e.startsWith("elif ")) { i.type = "if"; const r = e.match(/^(?:if|elif)\s+(.+):$/); i.content = r ? r[1].trim() : e; let c = h + 1; const d = []; for (; c < s.length; ) { const x = s[c]; if (x.trim() === "") { c++; continue; } if (this._getIndent(x) <= n) break; d.push(x), c++; } i.yes = h + 1 + t, i.next = c + t, i.next === s.length + t && (i.next = a); let g = []; if (c < s.length) { const x = s[c], u = x.trim(); if (this._getIndent(x) === n && (u.startsWith("else:") || u.startsWith("elif "))) { i.no = c + t; let m = c + 1; for (; m < s.length; ) { const p = s[m]; if (p.trim() === "") { m++; continue; } if (this._getIndent(p) <= n) break; g.push(p), m++; } u.startsWith("else:") ? i.no = c + 1 + t : u.startsWith("elif ") && (g.unshift(x), i.no = c + t), i.next = m + t, i.next === s.length + t && (i.next = a), this._getNodePython(g, i.no, i.next).forEach((p) => o.push(p)), h = m - 1; } else h = c - 1; } else h = c - 1; this._getNodePython(d, i.yes, i.next).forEach((x) => o.push(x)); } else if (e.startsWith("for ")) { const r = {}, c = {}, d = e.match(/for\s+(\w+)\s+in\s+range\(([^)]+)\):/); if (d) { const u = d[1], m = d[2].split(",").map((y) => y.trim()); let w = "0", p = "0", v = "1"; m.length === 1 ? p = m[0] : m.length === 2 ? (w = m[0], p = m[1]) : m.length === 3 && (w = m[0], p = m[1], v = m[2]), i.content = `${u} = ${w}`, r.content = `${u} < ${p}`, c.content = v === "1" ? `${u}++` : `${u} += ${v}`; } else { const u = e.match(/for\s+(\w+)\s+in\s+([^:]+):/); i.content = "", r.content = u ? `${u[1]} in ${u[2]}` : e.replace(":", ""), c.content = ""; } i.content !== "" ? (i.type = "plain", r.id = this.maxId + 1, this.maxId = r.id, r.type = "loop", this.loopId.push(r.id), i.next = r.id, o.push(r)) : (i.type = "loop", i.content = r.content, this.loopId.push(i.id)), c.content !== "" && (c.id = this.maxId + 1, this.maxId = c.id, c.type = "plain", c.next = r.id !== void 0 ? r.id : i.id, o.push(c)); let g = h + 1; const f = []; for (; g < s.length; ) { const u = s[g]; if (u.trim() === "") { g++; continue; } if (this._getIndent(u) <= n) break; f.push(u), g++; } r.yes = h + 1 + t, r.next = g + t, r.next === s.length + t && (r.next = a), this.breakId.push(r.next), c.id === void 0 && (c.id = i.type === "loop" ? i.id : r.id), this._getNodePython(f, r.yes, c.id).forEach((u) => o.push(u)), i.type === "loop" && (i.next = r.next, i.yes = r.yes), h = g - 1; } else if (e.startsWith("while ")) { i.type = "loop"; const r = e.match(/^while\s+(.+):$/); i.content = r ? r[1].trim() : e, this.loopId.push(i.id); let c = h + 1; const d = []; for (; c < s.length; ) { const f = s[c]; if (f.trim() === "") { c++; continue; } if (this._getIndent(f) <= n) break; d.push(f), c++; } i.yes = h + 1 + t, i.next = c + t, i.next === s.length + t && (i.next = a), this.breakId.push(i.next), this._getNodePython(d, i.yes, i.id).forEach((f) => o.push(f)), h = c - 1; } else e === "break" ? (i.content = "break", i.type = "plain", i.next = this.breakId.pop()) : e === "continue" ? (i.content = "continue", i.type = "plain", i.next = this.loopId.pop()) : (i.type = "plain", i.content = e, i.id - t === s.length - 1 ? i.next = a : i.next = h + 1 + t); o.push(i); } return o; } } const S = { c: new $(), java: new P(), javascript: new T(), js: new T(), python: new _(), py: new _() }; function W(M, s, t, a = {}) { const o = s.toLowerCase(), h = S[o]; if (!h) throw new Error(`지원하지 않는 언어입니다: ${s}. 현재 지원 언어: C, Java, JavaScript, Python`); const l = h.parse(M), e = new N(), i = e.calculatePositions(l), n = new k(t); return n.draw(i, a), { renderer: n, parser: h, layoutEngine: e, nodes: i }; } export { $ as CParser, k as CanvasRenderer, P as JavaParser, T as JavaScriptParser, N as LayoutEngine, _ as PythonParser, W as default, W as drawFlowchart };