Skip to content

Commit

Permalink
last fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoMadera committed Sep 5, 2023
1 parent f75b501 commit 23297af
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 29 deletions.
5 changes: 3 additions & 2 deletions components/FullScreenLyrics/FullScreenLyrics.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function FullScreenLyrics(): ReactElement {
currentLine: line,
lyricsProgressMs,
nextLine: lyrics.lines[i + 1],
index: i,
});

return <LyricLine line={line} type={type} key={i} />;
Expand Down Expand Up @@ -76,9 +77,9 @@ export default function FullScreenLyrics(): ReactElement {
height: calc(100% + var(--border-width) * 2);
background: linear-gradient(
60deg,
${lyricsBackgroundColor || "transparent"} 0%,
${lyricsBackgroundColor ?? "transparent"} 0%,
#ffffff80 50%,
${lyricsBackgroundColor || "transparent"} 100%
${lyricsBackgroundColor ?? "transparent"} 100%
);
background-size: 300% 300%;
background-position: 0 50%;
Expand Down
3 changes: 2 additions & 1 deletion components/FullScreenLyrics/LyricLine.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { ReactElement, useEffect, useRef } from "react";

import { useAuth, useLyricsContext, useSpotify } from "hooks";
import { LineType } from "types/lyrics";
import { IFormatLyricsResponse } from "utils";

interface ILyricLineProps {
line: IFormatLyricsResponse["lines"][0];
type: "current" | "previous" | "next";
type: LineType;
}

export function LyricLine({ line, type }: ILyricLineProps): ReactElement {
Expand Down
2 changes: 1 addition & 1 deletion components/LyricsPIPButton/LyricsPIPButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function LyricsPIPButton({
font-family: Lato, sans-serif;
font-size: 2.5rem;
text-transform: uppercase;
background: ${background || "transparent"};
background: ${background ?? "transparent"};
border: none;
}
.lyrics-pip-button:hover {
Expand Down
2 changes: 1 addition & 1 deletion components/icons/LyricsPictureInPicture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default function LyricsPictureInPicture(
): ReactElement {
return (
<svg role="img" height="20" width="20" viewBox="-2 0 20 16" {...props}>
<g fill="currentColor" fill-rule="evenodd">
<g fill="currentColor" fillRule="evenodd">
<g opacity="0.6">
<path d="M1 3v9h14V3H1zm0-1h14a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H1a1 1 0 0 1-1-1V3a1 1 0 0 1 1-1z"></path>
<path d="M10 8h4v3h-4z"></path>
Expand Down
12 changes: 12 additions & 0 deletions types/lyrics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export enum LineType {
FIRST = "first",
CURRENT = "current",
PREVIOUS = "previous",
NEXT = "next",
}

export interface IAllLines {
color: string;
text: string;
type: LineType;
}
13 changes: 7 additions & 6 deletions utils/applyLyricLinePositionAndColor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { findIndexOrLast } from "./findIndexOrLast";
import { LineType } from "types/lyrics";
import {
findIndexOrLast,
LINE_HEIGHT,
LYRICS_PADDING_LEFT,
LYRICS_PIP_HEADER_HEIGH,
Expand All @@ -10,15 +11,15 @@ export function applyLyricLinePositionAndColor(
allLines: {
color: string;
text: string;
type: "current" | "previous" | "next";
type: LineType;
}[],
containerHeight: number
): void {
const containerMiddle = containerHeight / 2;
const currentLineIndex = findIndexOrLast(
allLines,
(line) => line.type === "current"
);
const currentLineIndex = findIndexOrLast(allLines, (line) => {
const validTypes = ["current", "first"];
return validTypes.includes(line.type);
});

allLines.forEach((line, index) => {
const isOneOfFirstLines =
Expand Down
37 changes: 19 additions & 18 deletions utils/getLinesFittingCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IFormatLyricsResponse } from "./formatLyrics";
import { IAllLines, LineType } from "types/lyrics";

export function getLinesFittingCanvas(
ctx: CanvasRenderingContext2D,
Expand Down Expand Up @@ -29,30 +30,34 @@ interface IGetLineInfo {
lyricLineColor: string;
lyricTextColor: string;
lyricsProgressMs: number;
index: number;
}
interface IGetLineType {
currentLine: IFormatLyricsResponse["lines"][0];
nextLine: IFormatLyricsResponse["lines"][0];
lyricsProgressMs: number;
index: number;
}

export function getLineType({
currentLine,
lyricsProgressMs,
nextLine,
}: IGetLineType): "current" | "previous" | "next" {
index,
}: IGetLineType): LineType {
const isPreviousLine =
currentLine.startTimeMs &&
Number(currentLine.startTimeMs) <= lyricsProgressMs;
const isNextLine =
nextLine?.startTimeMs && Number(nextLine?.startTimeMs) >= lyricsProgressMs;

const isCurrentLine = isPreviousLine && isNextLine;
const isFirstLine = index === 0;

const lineTypes = {
current: isCurrentLine,
previous: isPreviousLine,
next: isNextLine,
const lineTypes: Record<LineType, boolean> = {
current: !!isCurrentLine,
previous: !!isPreviousLine,
first: isFirstLine,
next: !!isNextLine,
};

const type = Object.keys(lineTypes).find(
Expand All @@ -68,21 +73,24 @@ export function getLineInfo({
lyricTextColor,
lyricsProgressMs,
nextLine,
index,
}: IGetLineInfo): {
color: string;
text: string;
type: "current" | "previous" | "next";
type: LineType;
} {
const type = getLineType({
currentLine,
lyricsProgressMs,
nextLine,
index,
});

const lineColors = {
const lineColors: Record<LineType, string> = {
current: "#fff",
previous: lyricLineColor + "80",
next: lyricTextColor,
first: lyricTextColor,
};

return {
Expand All @@ -108,16 +116,8 @@ export function getAllLinesFittingWidth({
lyricTextColor,
lyricsProgressMs,
canvasWidth,
}: IgetAllLinesFittingWidth): {
color: string;
text: string;
type: "previous" | "current" | "next";
}[] {
const allLines: {
color: string;
text: string;
type: "previous" | "current" | "next";
}[] = [];
}: IgetAllLinesFittingWidth): IAllLines[] {
const allLines: IAllLines[] = [];

lines.forEach((line, i) => {
const lineInfo = getLineInfo({
Expand All @@ -126,6 +126,7 @@ export function getAllLinesFittingWidth({
lyricsProgressMs,
lyricTextColor,
nextLine: lines[i + 1],
index: i,
});

const linesText = getLinesFittingCanvas(
Expand Down
1 change: 1 addition & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ export * from "./templateReplace";
export * from "./topBar";
export * from "./wait";
export * from "./within";
export * from "./findIndexOrLast";

0 comments on commit 23297af

Please sign in to comment.