fix(desktop): target=_blank links in a preview tab open a new preview tab #13954
Closedjamesvillarrubia(GitHub profile, opens in a new tab)wants to merge 2 commits into
main fromrefs/pull/13954/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -662,3 +662,5 @@662662 createdAt: string;663663 }664664665+/** A `target="_blank"` link the previewed page asked to open beside itself. */666+export interface DesktopPreviewOpenLinkEvent {@@ -553,11 +556,14 @@553556 * navigating the preview tab instead destroys the opener the popup has to554557 * `postMessage` its result back to.555558 *556- * `target="_blank"` links arrive as a tab disposition and keep loading in the557- * preview tab, which is what people expect from a link inside a preview.559+ * `"new-tab"` hands a `target="_blank"` link (a tab disposition) to the web560+ * app, which opens it as another preview tab so the page that held the link561+ * stays put. Schemes a popup cannot be hardened for keep loading in place.558562 */559563 export const previewWindowOpenAction = (details: {560564 readonly url: string;561565 readonly disposition: Electron.HandlerDetails["disposition"];562-}): "popup" | "navigate" =>563- details.disposition === "new-window" && isPopupUrl(details.url) ? "popup" : "navigate";566+}): "popup" | "new-tab" | "navigate" => {567+ if (!isPopupUrl(details.url)) return "navigate";568+ if (details.disposition === "new-window") return "popup";569+ return details.disposition === "foreground-tab" || details.disposition === "background-tab"@@ -553,11 +556,14 @@553556 * navigating the preview tab instead destroys the opener the popup has to554557 * `postMessage` its result back to.555558 *556- * `target="_blank"` links arrive as a tab disposition and keep loading in the557- * preview tab, which is what people expect from a link inside a preview.559+ * `"new-tab"` hands a `target="_blank"` link (a tab disposition) to the web560+ * app, which opens it as another preview tab so the page that held the link561+ * stays put. Schemes a popup cannot be hardened for keep loading in place.558562 */559563 export const previewWindowOpenAction = (details: {560564 readonly url: string;561565 readonly disposition: Electron.HandlerDetails["disposition"];562-}): "popup" | "navigate" =>563- details.disposition === "new-window" && isPopupUrl(details.url) ? "popup" : "navigate";566+}): "popup" | "new-tab" | "navigate" => {567+ if (!isPopupUrl(details.url)) return "navigate";568+ if (details.disposition === "new-window") return "popup";569+ return details.disposition === "foreground-tab" || details.disposition === "background-tab"
- apps/desktop/src/preview/Manager.ts line 2071OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2048,6 +2063,9 @@20482063 wc.ipc.on(RECORDING_INPUT_CHANNEL, recordingInput);20492064 wc.ipc.on(MOUSE_NAVIGATE_CHANNEL, mouseNavigate);20502065 wc.setWindowOpenHandler((details) => {2051- if (previewWindowOpenAction(details) === "popup") {2066+ const action = previewWindowOpenAction(details);2067+ if (action === "popup") {20522068 return { action: "allow", overrideBrowserWindowOptions: POPUP_WINDOW_OPTIONS };20532069 }2070+ if (action === "new-tab") {2071+ runFork(emitOpenLink({ tabId, url: details.url }));apps/web/src/browser/ElectronBrowserHost.tsx line 90OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -79,3 +82,9 @@7982 });8083 }, []);818485+ // A `target="_blank"` link inside a hosted page opens as another tab of the86+ // same thread, so the page that held the link stays where it is.87+ const openPreview = useAtomCommand(previewEnvironment.open, { reportFailure: true });88+ const threadRefByRuntimeTabId = useRef(new Map<string, (typeof sessions)[number]["threadRef"]>());89+ threadRefByRuntimeTabId.current = new Map(90+ sessions.map(({ runtimeTabId, threadRef }) => [runtimeTabId, threadRef]),
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- @@ -71,9 +79,12 @@7179 // maps the result differently, so the configured defaults have to be7280 // applied explicitly or file/link opens would ignore them.7381 viewport: browserDefaultOpenViewport(defaults),74- profileId: browserDefaultOpenProfileId(defaults),82+ profileId: input.profileId ?? browserDefaultOpenProfileId(defaults),7583 },7684 });7785 return mapAtomCommandResult(result, (snapshot) => {78- applyPreviewServerSnapshot(input.threadRef, snapshot);7986 rememberPreviewUrl(input.threadRef, input.url);87+ if (input.background) {88+ updatePreviewServerSnapshot(input.threadRef, snapshot);89+ // The server's "opened" event activates the new tab; hand focus back.90+ if (previousActiveTabId) setActivePreviewTab(input.threadRef, previousActiveTabId);
juliusmarminge closed this
Sign in to join this conversation.






macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed