Skip to content

Commit

Permalink
Remove mv2 code final (#8523)
Browse files Browse the repository at this point in the history
* src/background/browserAction

* src/background/sidePanel

* simplify comments, remove unused code paths

* fix tests

* code comments

* cleanup comments

* remove mv2 code from sidebar

* lint and ts fixes

* removes more mv3 checks

* further cleanup

* remove mv2 sessionStorage shim

* fix strictNullChecks issue

* reverts bad deletion

* cleanup

* cleanup
  • Loading branch information
grahamlangford authored May 29, 2024
1 parent 63db07b commit d73837e
Show file tree
Hide file tree
Showing 17 changed files with 85 additions and 407 deletions.
2 changes: 0 additions & 2 deletions src/background/setToolbarBadge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jest.mock("@/mv3/api", () => ({
setBadgeBackgroundColor: jest.fn(),
setBadgeText: jest.fn(),
},
// TODO: Remove when MV2 code is dropped from src/contentScript
isMV3: jest.fn(() => true),
}));

describe("setToolbarBadge", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import { showModal } from "@/contentScript/modalDom";

jest.mock("@/utils/iframeUtils");
jest.mock("@/contentScript/modalDom");
jest.mock("@/contentScript/sidebarDomControllerLite");

const showModalMock = jest.mocked(showModal);

Expand Down
21 changes: 0 additions & 21 deletions src/contentScript/browserActionInstantHandler.ts

This file was deleted.

20 changes: 0 additions & 20 deletions src/contentScript/contentScriptCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ import initFloatingActions from "@/components/floatingActions/initFloatingAction
import { initSidebarActivation } from "@/contentScript/sidebarActivation";
import { initPerformanceMonitoring } from "@/contentScript/performanceMonitoring";
import { initRuntime } from "@/runtime/reducePipeline";
import { initSidebarFocusEvents } from "./sidebarController";
import {
isSidebarFrameVisible,
removeSidebarFrame,
} from "@/contentScript/sidebarDomControllerLite";
import { isMV3 } from "@/mv3/api";
import { onContextInvalidated } from "webext-events";
import { setPlatform } from "@/platform/platformContext";
import { markDocumentAsFocusableByUser } from "@/utils/focusTracker";
import contentScriptPlatform from "@/contentScript/contentScriptPlatform";
Expand Down Expand Up @@ -95,24 +88,11 @@ export async function init(): Promise<void> {

void initNavigation();

initSidebarFocusEvents();
void initSidebarActivation();

// Let the partner page know
initPartnerIntegrations();
void initFloatingActions();

void initPerformanceMonitoring();

onContextInvalidated.addListener(() => {
// The sidebar breaks when the context is invalidated, so it's best to close it
// In MV3, this happens automatically
if (!isMV3() && isSidebarFrameVisible()) {
removeSidebarFrame();
// TODO: Also notify closure in MV3.
// There it's more complicated to show this message ONLY if the sidebar was open
// because the sidebar is closed before this listener is called.
void notifyContextInvalidated();
}
});
}
127 changes: 24 additions & 103 deletions src/contentScript/sidebarController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import * as contentScriptApi from "@/contentScript/messenger/strict/api";
import { isEmpty, throttle } from "lodash";
import { signalFromEvent } from "abort-utils";
import { SimpleEventTarget } from "@/utils/SimpleEventTarget";
import * as sidebarMv2 from "@/contentScript/sidebarDomControllerLite";
import { getSidebarElement } from "@/contentScript/sidebarDomControllerLite";
import { type Except } from "type-fest";
import { type RunArgs, RunReason } from "@/types/runtimeTypes";
import { type UUID } from "@/types/stringTypes";
Expand All @@ -42,12 +40,9 @@ import { getTemporaryPanelSidebarEntries } from "@/platform/panels/panelControll
import { getFormPanelSidebarEntries } from "@/platform/forms/formController";
import { memoizeUntilSettled } from "@/utils/promiseUtils";
import { getTimedSequence } from "@/types/helpers";
import { isMV3 } from "@/mv3/api";
import { focusCaptureDialog } from "@/contentScript/focusCaptureDialog";
import { isLoadedInIframe } from "@/utils/iframeUtils";
import { showMySidePanel } from "@/background/messenger/strict/api";
import focusController from "@/utils/focusController";
import selectionController from "@/utils/selectionController";
import { getTopLevelFrame, messenger } from "webext-messenger";
import {
getSidebarTargetForCurrentTab,
Expand All @@ -56,8 +51,6 @@ import {
import pRetry from "p-retry";
import { hideNotification, showNotification } from "@/utils/notify";

const HIDE_SIDEBAR_EVENT_NAME = "pixiebrix:hideSidebar";

/**
* Event listeners triggered when the sidebar shows and is ready to receive messages.
*/
Expand All @@ -72,7 +65,7 @@ let modActivationPanelEntry: ModActivationPanelEntry | null = null;
* Only one check at a time
* Cannot throttle because subsequent checks need to be able to be made immediately
*/
const isSidePanelOpenMv3 = memoizeUntilSettled(async () => {
export const isSidePanelOpen = memoizeUntilSettled(async () => {
try {
await messenger(
"SIDEBAR_PING",
Expand All @@ -85,10 +78,6 @@ const isSidePanelOpenMv3 = memoizeUntilSettled(async () => {
}
});

export const isSidePanelOpen = isMV3()
? isSidePanelOpenMv3
: sidebarMv2.isSidebarFrameVisible;

// - Only start one ping at a time
// - Limit to one request every second (if the user closes the sidebar that quickly, we likely see those errors anyway)
// - Throw custom error if the sidebar doesn't respond in time
Expand Down Expand Up @@ -155,30 +144,25 @@ export async function showSidebarInTopFrame() {

// We do not await the promise here since we want to show the sidebar as soon as possible to avoid the possibility
// of the user needing to provide another user gesture to open the sidebar.
const sidebarInitiallyOpenPromise = isSidePanelOpenMv3();
const sidebarInitiallyOpenPromise = isSidePanelOpen();

if (isLoadedInIframe()) {
console.warn("showSidebarInTopFrame should not be called in an iframe");
}

// Defensively handle accidental calls from iframes
if (isMV3() || isLoadedInIframe()) {
try {
await showMySidePanel();
} catch (error) {
if (!isUserGestureRequiredError(error)) {
throw error;
}

await focusCaptureDialog({
message: 'Click "Open Sidebar" to open the mod sidebar',
buttonText: "Open Sidebar",
signal: signalFromEvent(sidebarShowEvents, sidebarShowEvents.coreEvent),
});
await showMySidePanel();
try {
await showMySidePanel();
} catch (error) {
if (!isUserGestureRequiredError(error)) {
throw error;
}
} else if (!sidebarMv2.isSidebarFrameVisible()) {
sidebarMv2.insertSidebarFrame();

await focusCaptureDialog({
message: 'Click "Open Sidebar" to open the mod sidebar',
buttonText: "Open Sidebar",
signal: signalFromEvent(sidebarShowEvents, sidebarShowEvents.coreEvent),
});
await showMySidePanel();
}

await pingSidebar();
Expand Down Expand Up @@ -587,29 +571,17 @@ export function getReservedPanelEntries(): {
function sidePanelOnCloseSignal(): AbortSignal {
const controller = new AbortController();
expectContext("contentScript");
if (isMV3()) {
window.addEventListener(
"resize",
async () => {
// TODO: Replace with official event when available
// Official event requested in https://github.com/w3c/webextensions/issues/517
if (!(await isSidePanelOpenMv3())) {
controller.abort();
}
},
{ signal: controller.signal },
);
} else {
window.addEventListener(
HIDE_SIDEBAR_EVENT_NAME,
() => {
window.addEventListener(
"resize",
async () => {
// TODO: Replace with official event when available
// Official event requested in https://github.com/w3c/webextensions/issues/517
if (!(await isSidePanelOpen())) {
controller.abort();
},
{
signal: controller.signal,
},
);
}
}
},
{ signal: controller.signal },
);

return controller.signal;
}
Expand All @@ -618,54 +590,3 @@ export function sidePanelOnClose(callback: () => void): void {
const signal = sidePanelOnCloseSignal();
signal.addEventListener("abort", callback, { once: true });
}

export function initSidebarFocusEvents(): void {
if (!isMV3()) {
// Add listeners to track keep track of focus with the MV2 sidebar. When the user interacts
// with the MV2 sidebar, the sidebar gets set as the document.activeElement. Required for brick
// functionality such as InsertAtCursorEffect
sidebarShowEvents.add(() => {
const sidebar = getSidebarElement();

if (!sidebar) {
// Should always exist because sidebarShowEvents is called on Sidebar App initialization
return;
}

// Save focus on initial load, because the user may have `mouseenter`ed the sidebar before the React App
// fired the sidebarShowEvent event. For example, if the user clicked the browserAction toolbar button and
// immediately `mouseenter`ed the sidebar (because the top of the sidebar is very close to the top browserAction)
if (document.activeElement !== sidebar) {
focusController.save();
}

const closeSignal = sidePanelOnCloseSignal();

// Can't detect clicks in the sidebar itself. So need to just watch for enter/leave the sidebar element
sidebar.addEventListener(
"mouseenter",
() => {
// If the user clicks into the sidebar and then leaves the sidebar, don't set the focus to the sidebar
// when they re-enter the sidebar
if (document.activeElement !== sidebar) {
// FIXME: If the user closes the sidebar when these two items are stored,
// both controllers will be stuck that way until some other .restore()/.clear() call resets it. It will need a "sidebar hide" listener to ensure it doesn't happen
// https://github.com/pixiebrix/pixiebrix-extension/pull/7842#discussion_r1516015396
focusController.save();
selectionController.save();
}
},
{ passive: true, capture: true, signal: closeSignal },
);

sidebar.addEventListener(
"mouseleave",
() => {
focusController.clear();
selectionController.clear();
},
{ passive: true, capture: true, signal: closeSignal },
);
});
}
}
Loading

0 comments on commit d73837e

Please sign in to comment.