Radish alpha
r
Radicle desktop app
Radicle
Git (anonymous pull)
Log in to clone via SSH
Simplify to only allow single line comments
✓ CI success Rūdolfs Ošiņš committed 1 year ago
commit 436de162e94ba216f8fcd5826b5eee2a917adfbb
parent aa955a8a1d5de19276b788a4af48d0cdfbecb9f5
1 passed (1 total) View logs
1 file changed +27 -198
modified src/components/Diff.svelte
@@ -6,11 +6,9 @@
    file: string;
    start: SelectionAnchor;
    end: SelectionAnchor;
-
    startHunkIdx: number;
-
    endHunkIdx: number;
-
    startLineIdx: number;
-
    endLineIdx: number;
-
    codeLocation: CodeLocation | undefined;
+
    lineIdx: number;
+
    hunkIdx: number;
+
    codeLocation: CodeLocation;
  }

  import type { CodeLocation } from "@bindings/cob/thread/CodeLocation";
@@ -41,10 +39,6 @@
  const { file, rid, createComment, head, codeCommentThreads }: Props =
    $props();

-
  // TODO: implement generating CodeLocation based on range selection.
-
  const enableLineRangeSelection = false;
-

-
  let dragging = $state(false);
  let selection: Selection | undefined = $state(undefined);

  function lineNumber(line: Modification, side: Side): number | undefined {
@@ -65,7 +59,6 @@
    }
  }

-
  console.log({ codeCommentThreads });
  function findLineComment(line: Modification) {
    return codeCommentThreads?.find(t => {
      if (line.type === "addition") {
@@ -97,10 +90,6 @@
    }
  }

-
  function deselect() {
-
    selection = undefined;
-
  }
-

  function filePath(file: FileDiff, side: Side): string {
    if (file.status === "moved" || file.status === "copied") {
      if (side === "left") {
@@ -113,11 +102,7 @@
    }
  }

-
  function endDragSelection() {
-
    dragging = false;
-
  }
-

-
  function startDragSelection(
+
  function selectLine(
    e: MouseEvent,
    file: FileDiff,
    side: Side,
@@ -128,22 +113,12 @@
    e.preventDefault();
    e.stopPropagation();

-
    // Allow line selection only when createComment is
-
    // supplied (i.e. in reviews).
-
    if (!createComment) {
-
      return;
-
    }
-

-
    dragging = true;
-

    selection = {
      file: filePath(file, side),
      start: determineSelectedAnchor(side, line),
      end: determineSelectedAnchor(side, line),
-
      startHunkIdx: hunkIdx,
-
      endHunkIdx: hunkIdx,
-
      startLineIdx: lineIdx,
-
      endLineIdx: lineIdx,
+
      hunkIdx: hunkIdx,
+
      lineIdx: lineIdx,
      codeLocation: {
        commit: head,
        path: filePath(file, side),
@@ -171,105 +146,12 @@
    };
  }

-
  function endShiftSelection(
-
    e: MouseEvent,
-
    file: FileDiff,
-
    side: Side,
-
    line: Modification,
-
    hunkIdx: number,
-
    lineIdx: number,
-
  ) {
-
    e.preventDefault();
-
    e.stopPropagation();
-

-
    if (!enableLineRangeSelection) {
-
      return;
-
    }
-

-
    if (
-
      selection &&
-
      // Prevent inter-hunk selection.
-
      hunkIdx === selection.startHunkIdx &&
-
      // Prevent reverse selection (i.e. end marker before start marker).
-
      lineIdx >= selection.startLineIdx
-
    ) {
-
      // Prevent setting the end marker before the start marker
-
      // on the same line.
-
      if (
-
        selection.startLineIdx === lineIdx &&
-
        selection.start.side === "right" &&
-
        side === "left"
-
      ) {
-
        return;
-
      }
-
      selection = {
-
        file: filePath(file, side),
-
        start: selection.start,
-
        end: determineSelectedAnchor(side, line),
-
        startHunkIdx: selection.startHunkIdx,
-
        endHunkIdx: hunkIdx,
-
        startLineIdx: selection.startLineIdx,
-
        endLineIdx: lineIdx,
-
        codeLocation: undefined,
-
      };
-
    }
-
  }
-

-
  function addToDragSelection(
-
    file: FileDiff,
-
    side: Side,
-
    line: Modification,
-
    hunkIdx: number,
-
    lineIdx: number,
-
  ) {
-
    if (!enableLineRangeSelection) {
-
      return;
-
    }
-

-
    if (
-
      dragging &&
-
      selection &&
-
      // Prevent inter-hunk selection.
-
      hunkIdx === selection.startHunkIdx &&
-
      // Prevent reverse selection (i.e. end marker before start marker).
-
      lineIdx >= selection.startLineIdx
-
    ) {
-
      // Prevent setting the end marker before the start marker
-
      // on the same line.
-
      if (
-
        selection.startLineIdx === lineIdx &&
-
        selection.start.side === "right" &&
-
        side === "left"
-
      ) {
-
        return;
-
      }
-

-
      selection = {
-
        file: filePath(file, side),
-
        start: selection.start,
-
        end: determineSelectedAnchor(side, line),
-
        startHunkIdx: selection.startHunkIdx,
-
        endHunkIdx: hunkIdx,
-
        startLineIdx: selection.startLineIdx,
-
        endLineIdx: lineIdx,
-
        codeLocation: undefined,
-
      };
-
    }
-
  }
-

  function isSelected(file: string, hunkIdx: number, lineIdx: number) {
-
    if (!selection || selection.file !== file) {
-
      return false;
-
    }
    return (
-
      hunkIdx >= selection.startHunkIdx &&
-
      hunkIdx <= selection.endHunkIdx &&
-
      (hunkIdx === selection.startHunkIdx
-
        ? lineIdx >= selection.startLineIdx
-
        : true) &&
-
      (hunkIdx === selection.endHunkIdx
-
        ? lineIdx <= selection.endLineIdx
-
        : true)
+
      selection &&
+
      selection.file === file &&
+
      selection.hunkIdx === hunkIdx &&
+
      selection.lineIdx === lineIdx
    );
  }
</script>
@@ -315,28 +197,6 @@
  .context > .sign {
    color: var(--color-foreground-disabled);
  }
-
  /*
-
  .start-marker::before {
-
    content: "";
-
    position: absolute;
-
    top: 0;
-
    left: 0;
-
    width: 4px;
-
    height: 4px;
-
    background-color: yellow;
-
    z-index: 1;
-
  }
-
  .end-marker::after {
-
    content: "";
-
    position: absolute;
-
    top: 0;
-
    right: 0;
-
    height: 4px;
-
    width: 4px;
-
    background-color: cyan;
-
    z-index: 1;
-
  }
-
  */
  /* Workaround for faking hover color change on end marker while dragging. */
  .start-marker,
  .end-marker {
@@ -392,13 +252,12 @@
    user-select: text;
    width: 100%;
    word-break: break-word;
-
    display: flex;
-
    align-items: flex-start;
  }
  .comment-icon {
    margin-left: auto;
    margin-right: 1rem;
    margin-top: 3px;
+
    align-self: flex-start;
  }
  .thread {
    background-color: var(--color-fill-float-hover);
@@ -421,8 +280,6 @@
  }
</style>

-
<svelte:window onpointerup={endDragSelection} onpointerdown={deselect} />
-

<div class="container">
  {#if file.diff.type === "plain"}
    {#each file.diff.hunks as hunk, hunkIdx}
@@ -441,11 +298,8 @@
            class:addition={line.type === "addition"}
            class:deletion={line.type === "deletion"}
            class:context={line.type === "context"}
-
            class:selected={isSelected(
-
              filePath(file, "left"),
-
              hunkIdx,
-
              lineIdx,
-
            )}>
+
            class:selected={!comment &&
+
              isSelected(filePath(file, "left"), hunkIdx, lineIdx)}>
            <div
              class="left"
              class:start-marker={selection?.start.side === "left" &&
@@ -453,15 +307,9 @@
              class:end-marker={selection?.end.side === "left" &&
                selection.end.lineNumber === lineNumber(line, "left")}
              onpointerdown={e => {
-
                if (e.shiftKey) {
-
                  endShiftSelection(e, file, "left", line, hunkIdx, lineIdx);
-
                } else {
-
                  startDragSelection(e, file, "left", line, hunkIdx, lineIdx);
+
                if (createComment && !comment) {
+
                  selectLine(e, file, "left", line, hunkIdx, lineIdx);
                }
-
              }}
-
              onpointerup={endDragSelection}
-
              onpointerenter={() => {
-
                addToDragSelection(file, "left", line, hunkIdx, lineIdx);
              }}>
              {lineNumber(line, "left")}
            </div>
@@ -473,15 +321,9 @@
              class:end-marker={selection?.end.side === "right" &&
                selection.end.lineNumber === lineNumber(line, "right")}
              onpointerdown={e => {
-
                if (e.shiftKey) {
-
                  endShiftSelection(e, file, "right", line, hunkIdx, lineIdx);
-
                } else {
-
                  startDragSelection(e, file, "right", line, hunkIdx, lineIdx);
+
                if (createComment && !comment) {
+
                  selectLine(e, file, "right", line, hunkIdx, lineIdx);
                }
-
              }}
-
              onpointerup={endDragSelection}
-
              onpointerenter={() => {
-
                addToDragSelection(file, "right", line, hunkIdx, lineIdx);
              }}>
              {lineNumber(line, "right")}
            </div>
@@ -502,24 +344,18 @@
                      `<span class="global-syntax ${paint.style}">${escape(paint.item)}</span>`,
                  )
                  .join("")}
-
                {#if comment}
-
                  <div class="global-flex comment-icon">
-
                    <Icon name="comment" />
-
                  </div>
-
                {/if}
              </div>
            {:else if line.line !== ""}
-
              <div class="code">
-
                {line.line}
-
                {#if comment}
-
                  <div class="global-flex comment-icon">
-
                    <Icon name="comment" />
-
                  </div>
-
                {/if}
-
              </div>
+
              <div class="code">{line.line}</div>
            {:else}
              <div class="code"><br /></div>
            {/if}
+

+
            <div class="global-flex comment-icon">
+
              {#if comment}
+
                <Icon name="comment" />
+
              {/if}
+
            </div>
          </div>

          {#if comment}
@@ -532,12 +368,7 @@
                  if (!createComment) {
                    return;
                  }
-
                  await createComment(
-
                    body,
-
                    embeds,
-
                    comment.root.id,
-
                    comment.root.location,
-
                  );
+
                  await createComment(body, embeds, comment.root.id);
                }}
                canEditComment={() => {
                  return undefined;
@@ -545,7 +376,7 @@
            </div>
          {/if}

-
          {#if !dragging && selection?.endHunkIdx === hunkIdx && selection.endLineIdx === lineIdx && selection.codeLocation && !comment && createComment}
+
          {#if selection && selection.hunkIdx === hunkIdx && selection.lineIdx === lineIdx && selection.codeLocation && createComment}
            <div
              class="comment-form"
              onpointerdown={e => {
@@ -557,9 +388,7 @@
                  : ""}{selection.file.split("/").slice(-1)}:{selection.start
                  .side === "left"
                  ? "L"
-
                  : "R"}{selection.start.lineNumber} ->
-
                {selection.end.side === "left" ? "L" : "R"}{selection.end
-
                  .lineNumber}
+
                  : "R"}{selection.start.lineNumber}
              </div>
              <CommentToggleInput
                disallowEmptyBody