-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#5078: support showing panel as popover #5023
Changes from 3 commits
977b33f
0341f6e
21ebc08
5463b7a
5bd588e
873e34f
55aadeb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,13 +29,17 @@ import { | |
import { type PanelPayload } from "@/sidebar/types"; | ||
import { | ||
stopWaitingForTemporaryPanels, | ||
cancelTemporaryPanelsForExtension, | ||
cancelTemporaryPanels, | ||
waitForTemporaryPanel, | ||
} from "@/blocks/transformers/temporaryInfo/temporaryPanelProtocol"; | ||
import { CancelError, PropError } from "@/errors/businessErrors"; | ||
import { BusinessError, CancelError, PropError } from "@/errors/businessErrors"; | ||
import { getThisFrame } from "webext-messenger"; | ||
import { showModal } from "@/blocks/transformers/ephemeralForm/modalUtils"; | ||
import { IS_ROOT_AWARE_BRICK_PROPS } from "@/blocks/rootModeHelpers"; | ||
import { showPopover } from "@/blocks/transformers/temporaryInfo/popoverUtils"; | ||
|
||
type Location = "panel" | "modal"; | ||
type Location = "panel" | "modal" | "popover"; | ||
|
||
export async function createFrameSource( | ||
nonce: string, | ||
|
@@ -62,6 +66,10 @@ class DisplayTemporaryInfo extends Transformer { | |
); | ||
} | ||
|
||
override async isRootAware(): Promise<boolean> { | ||
return true; | ||
} | ||
|
||
inputSchema: Schema = { | ||
type: "object", | ||
properties: { | ||
|
@@ -76,10 +84,11 @@ class DisplayTemporaryInfo extends Transformer { | |
}, | ||
location: { | ||
type: "string", | ||
enum: ["panel", "modal"], | ||
enum: ["panel", "modal", "popover"], | ||
default: "panel", | ||
description: "The location of the information (default='panel')", | ||
}, | ||
...IS_ROOT_AWARE_BRICK_PROPS, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Taking same approach as other PR for upgrading the brick to be root aware |
||
}, | ||
required: ["body"], | ||
}; | ||
|
@@ -89,65 +98,101 @@ class DisplayTemporaryInfo extends Transformer { | |
title, | ||
body: bodyPipeline, | ||
location = "panel", | ||
isRootAware = false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Safe default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the brick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See this PR for more details on the approach: #4910 The idea is that:
|
||
}: BlockArg<{ | ||
title: string; | ||
location: Location; | ||
body: PipelineExpression; | ||
isRootAware: boolean; | ||
}>, | ||
{ | ||
logger: { | ||
context: { extensionId }, | ||
}, | ||
root, | ||
ctxt, | ||
runPipeline, | ||
runRendererPipeline, | ||
}: BlockOptions | ||
): Promise<UnknownObject | null> { | ||
expectContext("contentScript"); | ||
|
||
const target = isRootAware ? root : document; | ||
|
||
const nonce = uuidv4(); | ||
const controller = new AbortController(); | ||
|
||
const payload = (await runRendererPipeline(bodyPipeline?.__value__ ?? [], { | ||
key: "body", | ||
counter: 0, | ||
})) as PanelPayload; | ||
|
||
if (location === "panel") { | ||
await ensureSidebar(); | ||
const payload = (await runRendererPipeline( | ||
bodyPipeline?.__value__ ?? [], | ||
{ | ||
key: "body", | ||
counter: 0, | ||
}, | ||
{}, | ||
target | ||
)) as PanelPayload; | ||
|
||
switch (location) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Switch to a "switch" statement. The whitespace in the diff is bad |
||
case "panel": { | ||
await ensureSidebar(); | ||
|
||
showTemporarySidebarPanel({ | ||
extensionId, | ||
nonce, | ||
heading: title, | ||
payload, | ||
}); | ||
|
||
window.addEventListener( | ||
PANEL_HIDING_EVENT, | ||
() => { | ||
controller.abort(); | ||
}, | ||
{ | ||
signal: controller.signal, | ||
} | ||
); | ||
|
||
controller.signal.addEventListener("abort", () => { | ||
hideTemporarySidebarPanel(nonce); | ||
void stopWaitingForTemporaryPanels([nonce]); | ||
}); | ||
|
||
break; | ||
} | ||
|
||
showTemporarySidebarPanel({ | ||
extensionId, | ||
nonce, | ||
heading: title, | ||
payload, | ||
}); | ||
case "modal": { | ||
const frameSource = await createFrameSource(nonce, location); | ||
showModal(frameSource, controller); | ||
break; | ||
} | ||
|
||
window.addEventListener( | ||
PANEL_HIDING_EVENT, | ||
() => { | ||
controller.abort(); | ||
}, | ||
{ | ||
signal: controller.signal, | ||
case "popover": { | ||
const frameSource = await createFrameSource(nonce, location); | ||
if (target === document) { | ||
throw new BusinessError("Target must be an element for popover"); | ||
} | ||
); | ||
|
||
controller.signal.addEventListener("abort", () => { | ||
hideTemporarySidebarPanel(nonce); | ||
void stopWaitingForTemporaryPanels([nonce]); | ||
}); | ||
} else if (location === "modal") { | ||
const frameSource = await createFrameSource(nonce, location); | ||
showModal(frameSource, controller); | ||
} else { | ||
throw new PropError( | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- dynamic check for validated value | ||
`Invalid location: ${location}`, | ||
this.id, | ||
"location", | ||
location | ||
); | ||
await cancelTemporaryPanelsForExtension(extensionId); | ||
|
||
const onHide = async () => { | ||
await cancelTemporaryPanels([nonce]); | ||
}; | ||
|
||
showPopover(frameSource, target as HTMLElement, onHide, controller); | ||
|
||
break; | ||
} | ||
|
||
default: { | ||
throw new PropError( | ||
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- dynamic check for validated value | ||
`Invalid location: ${location}`, | ||
this.id, | ||
"location", | ||
location | ||
); | ||
} | ||
} | ||
|
||
let result = null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logging is super noisy