<commit_before>import * as ReactDOM from "react-dom";
import { ISpace, IBoard, getConnections, getSpaceIndex, getCurrentBoard, forEachEventParameter } from "./boards";
import { BoardType, Space, SpaceSubtype } from "./types";
import { degreesToRadians } from "./utils/number";
import { spaces } from "./spaces";
import { getImage } from "./images";
import { $$hex } from "./utils/debug";
import * as React from "react";
import { RightClickMenu } from "./rightclick";
import { attachToCanvas, detachFromCanvas } from "./interaction";
import { getEvent } from "./events/events";
import { getDistinctColor } from "./utils/colors";

type Canvas = HTMLCanvasElement;
type CanvasContext = CanvasRenderingContext2D;

function getEditorContentTransform(board: IBoard, editor: HTMLElement) {
  let board_offset_x = Math.floor((editor.offsetWidth - board.bg.width) / 2);
  board_offset_x = Math.max(0, board_offset_x);
  let board_offset_y = Math.floor((editor.offsetHeight - board.bg.height) / 2);
  board_offset_y = Math.max(0, board_offset_y);

  return `translateX(${board_offset_x}px) translateY(${board_offset_y}px)`;
}

function _renderConnections(lineCanvas: Canvas, lineCtx: CanvasContext, board: IBoard, clear: boolean = true) {
  if (clear)
    lineCtx.clearRect(0, 0, lineCanvas.width, lineCanvas.height);

  // Draw connecting lines.
  const links = board.links = board.links || {};
  for (let startSpace in links) {
    const x1 = board.spaces[startSpace].x;
    const y1 = board.spaces[startSpace].y;

    const endLinks = getConnections(parseInt(startSpace), board)!;
    let x2, y2;
    let bidirectional = false;
    for (let i = 0; i < endLinks.length; i++) {
      x2 = board.spaces[endLinks[i]].x;
      y2 = board.spaces[endLinks[i]].y;
      bidirectional = _isConnectedTo(links, endLinks[i], startSpace);
      if (bidirectional && parseInt(startSpace) > endLinks[i])
        continue;
      _drawConnection(lineCtx, x1, y1, x2, y2, bidirectional);
    }
  }
}

function _renderAssociations(
  canvas: Canvas, context: CanvasContext,
  board: IBoard, selectedSpaces?: ISpace[] | null
) {
  context.clearRect(0, 0, canvas.width, canvas.height);

  if (!selectedSpaces || selectedSpaces.length !== 1)
    return;

  // Draw associated spaces in event params.
  let associationNum = 0;
  forEachEventParameter(board, (parameter, event, space) => {
    if (parameter.type === "Space") {
      if (selectedSpaces[0] !== space)
        return; // Only draw associations for the selected space.

      const associatedSpaceIndex =
        event.parameterValues && event.parameterValues[parameter.name];
      if (typeof associatedSpaceIndex === "number") {
        const associatedSpace = board.spaces[associatedSpaceIndex];
        if (!associatedSpace)
          return; // I guess maybe this could happen?

        const dotsColor = `rgba(${getDistinctColor(associationNum).join(", ")}, 0.5)`;
        drawAssociation(context, space.x, space.y, associatedSpace.x, associatedSpace.y, dotsColor);
      }
      associationNum++;
    }
  });
}

function _isConnectedTo(links: any, start: number, end: any) {<commit_msg>Fix association coloring with multiple events on same space.<commit_after>import * as ReactDOM from "react-dom";
import { ISpace, IBoard, getConnections, getSpaceIndex, getCurrentBoard, forEachEventParameter, ISpaceEvent } from "./boards";
import { BoardType, Space, SpaceSubtype } from "./types";
import { degreesToRadians } from "./utils/number";
import { spaces } from "./spaces";
import { getImage } from "./images";
import { $$hex } from "./utils/debug";
import * as React from "react";
import { RightClickMenu } from "./rightclick";
import { attachToCanvas, detachFromCanvas } from "./interaction";
import { getEvent } from "./events/events";
import { getDistinctColor } from "./utils/colors";

type Canvas = HTMLCanvasElement;
type CanvasContext = CanvasRenderingContext2D;

function getEditorContentTransform(board: IBoard, editor: HTMLElement) {
  let board_offset_x = Math.floor((editor.offsetWidth - board.bg.width) / 2);
  board_offset_x = Math.max(0, board_offset_x);
  let board_offset_y = Math.floor((editor.offsetHeight - board.bg.height) / 2);
  board_offset_y = Math.max(0, board_offset_y);

  return `translateX(${board_offset_x}px) translateY(${board_offset_y}px)`;
}

function _renderConnections(lineCanvas: Canvas, lineCtx: CanvasContext, board: IBoard, clear: boolean = true) {
  if (clear)
    lineCtx.clearRect(0, 0, lineCanvas.width, lineCanvas.height);

  // Draw connecting lines.
  const links = board.links = board.links || {};
  for (let startSpace in links) {
    const x1 = board.spaces[startSpace].x;
    const y1 = board.spaces[startSpace].y;

    const endLinks = getConnections(parseInt(startSpace), board)!;
    let x2, y2;
    let bidirectional = false;
    for (let i = 0; i < endLinks.length; i++) {
      x2 = board.spaces[endLinks[i]].x;
      y2 = board.spaces[endLinks[i]].y;
      bidirectional = _isConnectedTo(links, endLinks[i], startSpace);
      if (bidirectional && parseInt(startSpace) > endLinks[i])
        continue;
      _drawConnection(lineCtx, x1, y1, x2, y2, bidirectional);
    }
  }
}

function _renderAssociations(
  canvas: Canvas, context: CanvasContext,
  board: IBoard, selectedSpaces?: ISpace[] | null
) {
  context.clearRect(0, 0, canvas.width, canvas.height);

  if (!selectedSpaces || selectedSpaces.length !== 1)
    return;

  // Draw associated spaces in event params.
  let lastEvent: ISpaceEvent;
  let associationNum = 0;
  forEachEventParameter(board, (parameter, event, space) => {
    if (parameter.type === "Space") {
      if (selectedSpaces[0] !== space)
        return; // Only draw associations for the selected space.

      // Reset coloring for each event.
      if (lastEvent !== event) {
        associationNum = 0;
      }
      lastEvent = event;

      const associatedSpaceIndex =
        event.parameterValues && event.parameterValues[parameter.name];
      if (typeof associatedSpaceIndex === "number") {
        const associatedSpace = board.spaces[associatedSpaceIndex];
        if (!associatedSpace)
          return; // I guess maybe this could happen?

        const dotsColor = `rgba(${getDistinctColor(associationNum).join(", ")}, 0.5)`;
        drawAssociation(context, space.x, space.y, associatedSpace.x, associatedSpace.y, dotsColor);
      }
      associationNum++;
    }
  });
}

function _isConnectedTo(links: any, start: number, end: any) {
{'fraction_non_alphanumeric': 0.13123359580052493, 'fraction_numerical': 0.007874015748031496, 'mean_word_length': 3.929984779299848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5062710', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1967, 'n_words': 510}