From d571fd6c4bcc3e0af2a69134eb245f8e6300b00d Mon Sep 17 00:00:00 2001 From: hanyd Date: Mon, 9 Sep 2024 22:58:38 +0800 Subject: [PATCH] Migrate button display methods --- .../PlayerButtonGroupComponent.tsx | 27 +++++++++++++----- .../playerButtons/StartEndSegmentButton.tsx | 28 +++++++++++++++++++ src/content.ts | 25 ----------------- 3 files changed, 48 insertions(+), 32 deletions(-) create mode 100644 src/components/playerButtons/StartEndSegmentButton.tsx diff --git a/src/components/playerButtons/PlayerButtonGroupComponent.tsx b/src/components/playerButtons/PlayerButtonGroupComponent.tsx index 0fd1214b..65998731 100644 --- a/src/components/playerButtons/PlayerButtonGroupComponent.tsx +++ b/src/components/playerButtons/PlayerButtonGroupComponent.tsx @@ -1,8 +1,9 @@ import { ConfigProvider, Popconfirm, theme } from "antd"; import * as React from "react"; +import Config from "../../config"; import InfoButtonComponent from "./InfoButton"; import PlayerButtonComponent from "./PlayerButton"; -import Config from "../../config"; +import StartEndSegmentButton from "./StartEndSegmentButton"; interface PlayerButtonGroupProps { startSegmentCallback: () => void; @@ -32,6 +33,19 @@ function PlayerButtonGroupComponent({ return !!(segment && segment?.segment?.length != 2); } + function showSubmitButton() { + return sponsorTimesSubmitting.length > 0 && !Config.config.hideUploadButtonPlayerControls; + } + + function showdeleteButton() { + if (Config.config.hideUploadButtonPlayerControls) { + return false; + } + return ( + sponsorTimesSubmitting.length > 1 || (sponsorTimesSubmitting.length > 0 && !isSegmentCreationInProgress()) + ); + } + function getPopconfirmDescription() { const message = chrome.i18n.getMessage("confirmMSG").split("\n"); return ( @@ -53,6 +67,7 @@ function PlayerButtonGroupComponent({ title="OpenSubmissionMenu" imageName="PlayerUploadIconSponsorBlocker.svg" isDraggable={false} + show={showSubmitButton()} onClick={submitCallback} > @@ -68,6 +83,7 @@ function PlayerButtonGroupComponent({ title="clearTimes" imageName="PlayerDeleteIconSponsorBlocker.svg" isDraggable={false} + show={showdeleteButton()} > @@ -80,13 +96,10 @@ function PlayerButtonGroupComponent({ onClick={cancelSegmentCallback} > - + > ); diff --git a/src/components/playerButtons/StartEndSegmentButton.tsx b/src/components/playerButtons/StartEndSegmentButton.tsx new file mode 100644 index 00000000..05ef8ee6 --- /dev/null +++ b/src/components/playerButtons/StartEndSegmentButton.tsx @@ -0,0 +1,28 @@ +import * as React from "react"; +import { forwardRef } from "react"; +import PlayerButtonComponent from "./PlayerButton"; + +interface StartEndSegmentButtonProps { + isCreatingSegment: boolean; + onClick: () => void; +} + +const StartEndSegmentButton = forwardRef(function ( + { isCreatingSegment, onClick }, + ref +) { + return ( + + ); +}); +StartEndSegmentButton.displayName = "StartSegmentButton"; + +export default StartEndSegmentButton; diff --git a/src/content.ts b/src/content.ts index 70517e3d..3beb9713 100644 --- a/src/content.ts +++ b/src/content.ts @@ -1924,32 +1924,7 @@ async function updateVisibilityOfPlayerControlsButton(): Promise { function updateEditButtonsOnPlayer(): void { // Don't try to update the buttons if we aren't on a Bilibili video page if (!getVideoID()) return; - window.dispatchEvent(new CustomEvent("sponsorTimesSubmittingChange", { detail: sponsorTimesSubmitting })); - - const creatingSegment = isSegmentCreationInProgress(); - // Show only if there are any segments to submit - const submitButtonVisible = sponsorTimesSubmitting.length > 0; - // Show only if there are any segments to delete - const deleteButtonVisible = - sponsorTimesSubmitting.length > 1 || (sponsorTimesSubmitting.length > 0 && !creatingSegment); - - // Update the elements - // playerButtons.startSegment.button.style.display = "unset"; - // playerButtons.cancelSegment.button.style.display = creatingSegment ? "unset" : "none"; - - if (creatingSegment) { - playerButtons.startSegment.image.src = chrome.runtime.getURL("icons/PlayerStopIconSponsorBlocker.svg"); - playerButtons.startSegment.button.setAttribute("title", chrome.i18n.getMessage("sponsorEnd")); - } else { - playerButtons.startSegment.image.src = chrome.runtime.getURL("icons/PlayerStartIconSponsorBlocker.svg"); - playerButtons.startSegment.button.setAttribute("title", chrome.i18n.getMessage("sponsorStart")); - } - - playerButtons.submit.button.style.display = - submitButtonVisible && !Config.config.hideUploadButtonPlayerControls ? "unset" : "none"; - playerButtons.delete.button.style.display = - deleteButtonVisible && !Config.config.hideDeleteButtonPlayerControls ? "unset" : "none"; } /**