Free, simple, and intuitive online database diagram editor and SQL generator.
JavaScript databasedatabase-designdatabase-diagramdatabase-schemadbmldiagram-editoreditorer-diagramerderdiagramindexeddbmariadbmcporacle-databaseoracle-dbpostgresqlschema-designsqlsql-serversqlite
Show column comments on the canvas (#1006)
1ilitce43e4630179
diff --git a/src/components/EditorCanvas/Relationship.jsx b/src/components/EditorCanvas/Relationship.jsxindex 3a851873..7ee472db 100644--- a/src/components/EditorCanvas/Relationship.jsx+++ b/src/components/EditorCanvas/Relationship.jsx@@ -31,8 +31,14 @@ export default function Relationship({ data }) {x: startTable.x,y: startTable.y,comment: startTable.comment,+ fields: startTable.fields,+ },+ endTable: {+ x: endTable.x,+ y: endTable.y,+ comment: endTable.comment,+ fields: endTable.fields,},- endTable: { x: endTable.x, y: endTable.y, comment: endTable.comment },};}, [tables, data]);diff --git a/src/components/EditorCanvas/Table.jsx b/src/components/EditorCanvas/Table.jsxindex b10e9eb7..c54f0cf7 100644--- a/src/components/EditorCanvas/Table.jsx+++ b/src/components/EditorCanvas/Table.jsx@@ -2,7 +2,6 @@ import { useMemo, useState } from "react";import {Tab,ObjectType,- tableFieldHeight,tableHeaderHeight,tableColorStripHeight,} from "../../data/constants";@@ -22,7 +21,11 @@ import { useTranslation } from "react-i18next";import { resolveType } from "../../utils/customTypes";import { isRtl } from "../../i18n/utils/rtl";import i18n from "../../i18n/i18n";-import { getCommentHeight, getTableHeight } from "../../utils/utils";+import {+ getCommentHeight,+ getFieldOffsetY,+ getTableHeight,+} from "../../utils/utils";export default function Table({tableData,@@ -333,7 +336,7 @@ export default function Table({<strong>{t("default_value")}: </strong>{e.default === "" ? t("not_set") : e.default}</p>- <p>+ <p className="max-w-80"><strong>{t("comment")}: </strong>{e.comment === "" ? t("not_set") : e.comment}</p>@@ -381,13 +384,14 @@ export default function Table({function field(fieldData, index) {const fieldResolved = resolveType(database, fieldData.type);+ const showFieldComment = fieldData.comment && settings.showComments;return (<divclassName={`${index === tableData.fields.length - 1? "": "border-b border-gray-400"- } group h-[36px] px-2 py-1 flex justify-between items-center gap-1 w-full overflow-hidden`}+ } group w-full overflow-hidden`}onPointerEnter={(e) => {if (!e.isPrimary) return;@@ -412,37 +416,26 @@ export default function Table({e.target.releasePointerCapture(e.pointerId);}}>- <div- className={`${- hoveredField === index ? "text-zinc-400" : ""- } flex items-center gap-2 overflow-hidden`}- >- <button- className="shrink-0 w-[10px] h-[10px] bg-[#2f68adcc] rounded-full"- onPointerDown={(e) => {- if (!e.isPrimary) return;+ <div className="h-[36px] px-2 py-1 flex justify-between items-center gap-1">+ <div+ className={`${+ hoveredField === index ? "text-zinc-400" : ""+ } flex items-center gap-2 overflow-hidden`}+ >+ <button+ className="shrink-0 w-[10px] h-[10px] bg-[#2f68adcc] rounded-full"+ onPointerDown={(e) => {+ if (!e.isPrimary) return;- handleGripField();- setLinkingLine((prev) => ({- ...prev,- startFieldId: fieldData.id,- startTableId: tableData.id,- startX: tableData.x + 15,- startY:+ handleGripField();+ const fieldY =tableData.y +- index * tableFieldHeight +- tableHeaderHeight +- tableColorStripHeight +- getCommentHeight(- tableData.comment,+ getFieldOffsetY(+ tableData.fields,+ index,settings.tableWidth,settings.showComments,) +- 14,- endX: tableData.x + 15,- endY:- tableData.y +- index * tableFieldHeight +tableHeaderHeight +tableColorStripHeight +getCommentHeight(@@ -450,54 +443,70 @@ export default function Table({settings.tableWidth,settings.showComments,) +- 14,- }));- }}- />- <span className="overflow-hidden text-ellipsis whitespace-nowrap">- {fieldData.name}- </span>- </div>- <div className="text-zinc-400">- {hoveredField === index ? (- <Button- theme="solid"- size="small"- style={{- backgroundColor: "#d42020b3",- }}- icon={<IconMinus />}- disabled={layout.readOnly}- onClick={() => {- if (layout.readOnly) return;- deleteField(fieldData, tableData.id);+ 14;+ setLinkingLine((prev) => ({+ ...prev,+ startFieldId: fieldData.id,+ startTableId: tableData.id,+ startX: tableData.x + 15,+ startY: fieldY,+ endX: tableData.x + 15,+ endY: fieldY,+ }));}}/>- ) : settings.showDataTypes ? (- <div className="flex gap-1 items-center">- {fieldData.primary && <IconKeyStroked />}- {!fieldData.notNull && <span className="font-mono">?</span>}- <span- className={- "font-mono " +- (fieldResolved.isCustom ? "" : fieldResolved.color)- }- style={- fieldResolved.isCustom- ? { color: fieldResolved.color }- : {}- }- >- {fieldData.type +- ((fieldResolved.isSized || fieldResolved.hasPrecision) &&- fieldData.size &&- fieldData.size !== ""- ? `(${fieldData.size})`- : "")}- </span>- </div>- ) : null}+ <span className="overflow-hidden text-ellipsis whitespace-nowrap">+ {fieldData.name}+ </span>+ </div>+ <div className="text-zinc-400">+ {hoveredField === index ? (+ <Button+ theme="solid"+ size="small"+ style={{+ backgroundColor: "#d42020b3",+ }}+ icon={<IconMinus />}+ disabled={layout.readOnly}+ onClick={() => {+ if (layout.readOnly) return;+ deleteField(fieldData, tableData.id);+ }}+ />+ ) : settings.showDataTypes ? (+ <div className="flex gap-1 items-center">+ {fieldData.primary && <IconKeyStroked />}+ {!fieldData.notNull && <span className="font-mono">?</span>}+ <span+ className={+ "font-mono " ++ (fieldResolved.isCustom ? "" : fieldResolved.color)+ }+ style={+ fieldResolved.isCustom ? { color: fieldResolved.color } : {}+ }+ >+ {fieldData.type ++ ((fieldResolved.isSized || fieldResolved.hasPrecision) &&+ fieldData.size &&+ fieldData.size !== ""+ ? `(${fieldData.size})`+ : "")}+ </span>+ </div>+ ) : null}+ </div></div>+ {showFieldComment && (+ <div className="ms-3 px-3 pb-3">+ <div+ className={`text-xs line-clamp-2 ${settings.mode === "light" ? "text-zinc-600" : "text-zinc-200"}`}+ >+ {fieldData.comment}+ </div>+ </div>+ )}</div>);}diff --git a/src/context/SettingsContext.jsx b/src/context/SettingsContext.jsxindex a20880b8..48beb770 100644--- a/src/context/SettingsContext.jsx+++ b/src/context/SettingsContext.jsx@@ -13,7 +13,7 @@ const defaultSettings = {showRelationshipLabels: true,tableWidth: tableWidth,showDebugCoordinates: false,- showComments: true,+ showComments: false,};export const SettingsContext = createContext(defaultSettings);diff --git a/src/utils/calcPath.js b/src/utils/calcPath.jsindex fda524b3..f524d79e 100644--- a/src/utils/calcPath.js+++ b/src/utils/calcPath.js@@ -1,5 +1,5 @@import { tableFieldHeight, tableHeaderHeight } from "../data/constants";-import { getCommentHeight } from "./utils";+import { getCommentHeight, getFieldOffsetY } from "./utils";/*** Generates an SVG path string to visually represent a relationship between two fields.@@ -23,14 +23,24 @@ export function calcPath(r, tableWidth = 200, zoom = 1, showComments = true) {let x1 = r.startTable.x;let y1 =r.startTable.y +- r.startFieldIndex * tableFieldHeight ++ getFieldOffsetY(+ r.startTable.fields ?? [],+ r.startFieldIndex,+ tableWidth,+ showComments,+ ) +tableHeaderHeight +getCommentHeight(r.startTable.comment, tableWidth, showComments) +tableFieldHeight / 2;let x2 = r.endTable.x;let y2 =r.endTable.y +- r.endFieldIndex * tableFieldHeight ++ getFieldOffsetY(+ r.endTable.fields ?? [],+ r.endFieldIndex,+ tableWidth,+ showComments,+ ) +getCommentHeight(r.endTable.comment, tableWidth, showComments) +tableHeaderHeight +tableFieldHeight / 2;diff --git a/src/utils/utils.js b/src/utils/utils.jsindex 26a654cb..3815171f 100644--- a/src/utils/utils.js+++ b/src/utils/utils.js@@ -62,29 +62,120 @@ export function areFieldsCompatible(db, field1Type, field2Type) {return same || isCompatible;}-export function getCommentHeight(comment, containerWidth, showComments = true) {+const COMMENT_LINE_HEIGHT = 16;+const TABLE_COMMENT_MAX_LINES = 5;+const FIELD_COMMENT_MAX_LINES = 2;+const COMMENT_PADDING_X = 24;+const COMMENT_BORDERS = 4;+const COMMENT_PADDING_BOTTOM = 12;+const COMMENT_CACHE_LIMIT = 500;+const TABLE_COMMENT_INSET = COMMENT_BORDERS + COMMENT_PADDING_X;+const FIELD_COMMENT_INSET = TABLE_COMMENT_INSET + 12;++const commentHeightCache = new Map();+let commentMeasureCtx = null;++function getCommentMeasureCtx() {+ if (commentMeasureCtx) return commentMeasureCtx;+ const ctx = document.createElement("canvas").getContext("2d");+ const bodyFont = window.getComputedStyle(document.body).fontFamily || "sans-serif";+ ctx.font = `12px ${bodyFont}`;+ commentMeasureCtx = ctx;+ return ctx;+}++function countWrappedLines(comment, contentWidth, maxLines) {+ const ctx = getCommentMeasureCtx();+ const spaceWidth = ctx.measureText(" ").width;+ const paragraphs = comment.split("\n");+ let lines = 0;++ for (const paragraph of paragraphs) {+ if (lines >= maxLines) break;+ if (!paragraph) {+ lines++;+ continue;+ }++ const words = paragraph.split(/\s+/).filter(Boolean);+ let lineWidth = 0;++ for (const word of words) {+ const wordWidth = ctx.measureText(word).width;+ if (lineWidth === 0) {+ lineWidth = wordWidth;+ } else if (lineWidth + spaceWidth + wordWidth <= contentWidth) {+ lineWidth += spaceWidth + wordWidth;+ } else {+ lines++;+ if (lines >= maxLines) break;+ lineWidth = wordWidth;+ }+ }+ if (lineWidth > 0) lines++;+ }++ return Math.min(maxLines, Math.max(1, lines));+}++export function getCommentHeight(+ comment,+ containerWidth,+ showComments = true,+ inset = TABLE_COMMENT_INSET,+ maxLines = TABLE_COMMENT_MAX_LINES,+) {if (!comment || !showComments) return 0;- const paddingBottom = 12;- const borders = 4;+ const cacheKey = `${containerWidth}:${inset}:${maxLines}:${comment}`;+ const cached = commentHeightCache.get(cacheKey);+ if (cached !== undefined) return cached;- const span = document.createElement("span");- span.className = "absolute text-xs px-3 line-clamp-5";+ const contentWidth = containerWidth - inset;+ const lines =+ contentWidth <= 0 ? 1 : countWrappedLines(comment, contentWidth, maxLines);+ const height = lines * COMMENT_LINE_HEIGHT + COMMENT_PADDING_BOTTOM;- span.style.width = containerWidth - borders + "px";- span.textContent = comment;- span.id = "temp-comment-measure";+ if (commentHeightCache.size >= COMMENT_CACHE_LIMIT) {+ commentHeightCache.delete(commentHeightCache.keys().next().value);+ }+ commentHeightCache.set(cacheKey, height);+ return height;+}- document.body.appendChild(span);- const height = span.offsetHeight;- document.body.removeChild(span);+export function getFieldHeight(field, containerWidth, showComments = true) {+ return (+ tableFieldHeight ++ getCommentHeight(+ field?.comment,+ containerWidth,+ showComments,+ FIELD_COMMENT_INSET,+ FIELD_COMMENT_MAX_LINES,+ )+ );+}- return height + paddingBottom;+export function getFieldsTotalHeight(fields, containerWidth, showComments = true) {+ let total = 0;+ for (const f of fields) {+ total += getFieldHeight(f, containerWidth, showComments);+ }+ return total;+}++export function getFieldOffsetY(fields, fieldIndex, containerWidth, showComments = true) {+ let total = 0;+ const limit = Math.min(fieldIndex, fields.length);+ for (let i = 0; i < limit; i++) {+ total += getFieldHeight(fields[i], containerWidth, showComments);+ }+ return total;}export function getTableHeight(table, width, showComments = true) {return (- table.fields.length * tableFieldHeight ++ getFieldsTotalHeight(table.fields, width, showComments) +tableHeaderHeight +tableColorStripHeight +getCommentHeight(table.comment, width, showComments)