Skip to content

Free, simple, and intuitive online database diagram editor and SQL generator.

6.7 KB
Raw Blame
import { dbToTypes } from "../data/datatypes";

import {
  tableFieldHeight,
  tableHeaderHeight,
  tableColorStripHeight,
  tableWidth as defaultTableWidth,
  minTableWidth,
  maxTableWidth,
} from "../data/constants";

export function dataURItoBlob(dataUrl) {
  const byteString = atob(dataUrl.split(",")[1]);
  const mimeString = dataUrl.split(",")[0].split(":")[1].split(";")[0];
  const arrayBuffer = new ArrayBuffer(byteString.length);
  const intArray = new Uint8Array(arrayBuffer);

  for (let i = 0; i < byteString.length; i++) {
    intArray[i] = byteString.charCodeAt(i);
  }

  return new Blob([intArray], { type: mimeString });
}

export function arrayIsEqual(arr1, arr2) {
  return JSON.stringify(arr1) === JSON.stringify(arr2);
}

export function strHasQuotes(str) {
  if (str.length < 2) return false;

  return (
    (str[0] === str[str.length - 1] && str[0] === "'") ||
    (str[0] === str[str.length - 1] && str[0] === '"') ||
    (str[0] === str[str.length - 1] && str[0] === "`")
  );
}

const keywords = [
  "NULL",
  "TRUE",
  "FALSE",
  "CURRENT_DATE",
  "CURRENT_TIME",
  "CURRENT_TIMESTAMP",
  "LOCALTIME",
  "LOCALTIMESTAMP",
  "SYSDATE",
  "SYSTIMESTAMP",
];

export function isKeyword(str) {
  if (typeof str !== "string") return false;

  return keywords.includes(str.toUpperCase());
}

export function isFunction(str) {
  return /\w+\([^)]*\)$/.test(str);
}

export function areFieldsCompatible(db, field1Type, field2Type) {
  const same = field1Type === field2Type;
  const isCompatible =
    dbToTypes[db][field1Type].compatibleWith &&
    dbToTypes[db][field1Type].compatibleWith.includes(field2Type);
  return same || isCompatible;
}

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 FIELD_BORDER_WIDTH = 1;

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 cacheKey = `${containerWidth}:${inset}:${maxLines}:${comment}`;
  const cached = commentHeightCache.get(cacheKey);
  if (cached !== undefined) return cached;

  const contentWidth = containerWidth - inset;
  const lines =
    contentWidth <= 0 ? 1 : countWrappedLines(comment, contentWidth, maxLines);
  const height = lines * COMMENT_LINE_HEIGHT + COMMENT_PADDING_BOTTOM;

  if (commentHeightCache.size >= COMMENT_CACHE_LIMIT) {
    commentHeightCache.delete(commentHeightCache.keys().next().value);
  }
  commentHeightCache.set(cacheKey, height);
  return height;
}

export function getFieldHeight(field, containerWidth, showComments = true) {
  return (
    tableFieldHeight +
    FIELD_BORDER_WIDTH +
    getCommentHeight(
      field?.comment,
      containerWidth,
      showComments,
      FIELD_COMMENT_INSET,
      FIELD_COMMENT_MAX_LINES,
    )
  );
}

export function clampTableWidth(width) {
  return Math.min(maxTableWidth, Math.max(minTableWidth, Math.round(width)));
}

export function getTableWidth(table) {
  const width = table?.width;
  return typeof width === "number" && width > 0 ? width : defaultTableWidth;
}

export function getRelationshipFields(relationship) {
  if (Array.isArray(relationship?.fields) && relationship.fields.length > 0) {
    return relationship.fields;
  }
  return [
    {
      startFieldId: relationship?.startFieldId,
      endFieldId: relationship?.endFieldId,
    },
  ];
}

export function getRelatedFieldKeysByTable(relationships = []) {
  const byTable = new Map();
  const add = (tableId, key) => {
    let keys = byTable.get(tableId);
    if (!keys) {
      keys = new Set();
      byTable.set(tableId, keys);
    }
    keys.add(key);
  };
  for (const relationship of relationships) {
    for (const pair of getRelationshipFields(relationship)) {
      add(relationship.startTableId, pair.startFieldId);
      add(relationship.endTableId, pair.endFieldId);
    }
  }
  return byTable;
}

export function getVisibleFieldEntries(table, relatedFieldKeys) {
  const fields = table.fields ?? [];

  return fields.reduce((entries, field, index) => {
    if (
      !table.collapsed ||
      relatedFieldKeys?.has(field.id) ||
      relatedFieldKeys?.has(index)
    ) {
      entries.push({ field, originalIndex: index });
    }
    return entries;
  }, []);
}

export function getVisibleFields(table, relatedFieldKeys) {
  return getVisibleFieldEntries(table, relatedFieldKeys).map(
    ({ field }) => field,
  );
}

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, showComments = true, relatedFieldKeys) {
  const width = getTableWidth(table);
  const visibleFields = getVisibleFields(table, relatedFieldKeys);

  return (
    getFieldsTotalHeight(visibleFields, width, showComments) +
    tableHeaderHeight +
    tableColorStripHeight +
    getCommentHeight(table.comment, width, showComments)
  );
}