Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

revert: 回退 #3029 原生 WebView 分享导出

Lizibcacfaea0479
diff --git a/apps/mobile/app/sessions/[sessionId].tsx b/apps/mobile/app/sessions/[sessionId].tsx
index ec467b772f..80fb6b60bf 100644
--- a/apps/mobile/app/sessions/[sessionId].tsx
+++ b/apps/mobile/app/sessions/[sessionId].tsx
@@ -34,7 +34,6 @@ import {
} from 'expo-audio';
import {
addScreenshotListener,
- renderConversationShareHtmlToPng,
} from 'xdt-screenshot-monitor';
import { useFocusEffect, useLocalSearchParams, useNavigation, useRouter } from 'expo-router';
import { forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, useSyncExternalStore, type ReactNode, type RefObject, type SetStateAction } from 'react';
@@ -101,7 +100,6 @@ import {
type ShareableMessageViewport,
} from '@/session/MessageRenderer';
import {
- bundledAssetToDataUri,
cleanupConversationSharePngTemps,
deleteConversationSharePngTemp,
writeConversationSharePngTemp,
@@ -111,7 +109,6 @@ import {
type ConversationShareSvgHandle,
} from '@/session/ConversationShareSvg';
import {
- buildConversationShareHtml,
type ConversationShareMessage,
type ConversationShareWebViewColors,
} from '@/session/conversationShareWebViewHtml';
@@ -613,15 +610,6 @@ const REOPEN_MESSAGE_WINDOW_LIMITS = [20, 10, 5, 1] as const;
// 覆盖 settling 窗口上限(10s)之后仍无任何在途证据的场景。
const TAIL_RETRY_HIDE_TIMEOUT_MS = 15_000;
const SCREENSHOT_SHARE_ACTIVATION_DEBOUNCE_MS = 1_200;
-const nativeConversationShareAvailable = Platform.OS === 'ios';
-
-// 原生 WKWebView 只能稳定读取 data URI;SVG 兜底直接使用同一组 bundle asset。
-// eslint-disable-next-line @typescript-eslint/no-require-imports
-const shareCharacterAsset = require('../../assets/share/cindy-share-character.jpg');
-// eslint-disable-next-line @typescript-eslint/no-require-imports
-const shareLogoLightAsset = require('../../assets/login/login-wordmark.png');
-// eslint-disable-next-line @typescript-eslint/no-require-imports
-const shareLogoDarkAsset = require('../../assets/login/login-wordmark-dark.png');
/**
* 排队消息「复用 composer 编辑」的会话内状态:clientId 定位队列条目,
@@ -1016,9 +1004,6 @@ export default function SessionScreen() {
const shareOperationSeqRef = useRef(0);
const [conversationShareBusy, setConversationShareBusy] = useState(false);
const [shareSelectionTriggeredByScreenshot, setShareSelectionTriggeredByScreenshot] = useState(false);
- const [shareCharacterSrc, setShareCharacterSrc] = useState<string | null>(null);
- const [shareLogoSrc, setShareLogoSrc] = useState<string | null>(null);
- const shareLogoModeRef = useRef<string | null>(null);
// chat-text-quote:待随下一条消息发送的选中文字引用(全局 store,消息流选区
// 按钮 / 文件预览页写入;发送时拼进正文,命中本地命令时保留)。
const quotes = useSessionQuotes(sessionId);
@@ -1098,28 +1083,6 @@ export default function SessionScreen() {
};
}, [sessionId]),
);
- useEffect(() => {
- if (!nativeConversationShareAvailable || !shareSelectionActive) return undefined;
- let cancelled = false;
- const logoNeedsLoad = shareLogoModeRef.current !== mode || !shareLogoSrc;
- void Promise.all([
- shareCharacterSrc
- ? Promise.resolve(shareCharacterSrc)
- : bundledAssetToDataUri(shareCharacterAsset, 'image/jpeg'),
- logoNeedsLoad
- ? bundledAssetToDataUri(
- mode === 'dark' ? shareLogoDarkAsset : shareLogoLightAsset,
- 'image/png',
- )
- : Promise.resolve(shareLogoSrc),
- ]).then(([character, logo]) => {
- if (cancelled) return;
- shareLogoModeRef.current = mode;
- setShareCharacterSrc(character);
- setShareLogoSrc(logo);
- });
- return () => { cancelled = true; };
- }, [mode, shareCharacterSrc, shareLogoSrc, shareSelectionActive]);
const [composerFocused, setComposerFocused] = useState(false);
const [composerInputContentHeight, setComposerInputContentHeight] = useState(COMPOSER_INPUT_SINGLE_LINE_CONTENT_HEIGHT);
const [voiceDraftCaretFrame, setVoiceDraftCaretFrame] = useState({ left: 0, top: 0 });
@@ -6036,30 +5999,6 @@ export default function SessionScreen() {
textTertiary: colors.textTertiary,
dark: mode === 'dark',
}), [colors, mode]);
- const conversationShareHtml = useMemo(() => {
- if (
- !nativeConversationShareAvailable
- || !shareSelectionActive
- || selectedShareMessages.length === 0
- ) return '';
- return buildConversationShareHtml({
- allShareableIds,
- characterSrc: shareCharacterSrc ?? undefined,
- colors: conversationShareColors,
- contentWidth: windowDimensions.width,
- logoSrc: shareLogoModeRef.current === mode ? shareLogoSrc ?? undefined : undefined,
- selectedMessages: selectedShareMessages,
- });
- }, [
- allShareableIds,
- conversationShareColors,
- mode,
- selectedShareMessages,
- shareCharacterSrc,
- shareLogoSrc,
- shareSelectionActive,
- windowDimensions.width,
- ]);
const enterShareSelection = useCallback((clientId: string) => {
Keyboard.dismiss();
setShareSelectionTriggeredByScreenshot(false);
@@ -6072,30 +6011,10 @@ export default function SessionScreen() {
shareSelectionStore.exit();
}, []);
const exportConversationSharePng = useCallback(async () => {
- const nativeShareAssetsReady = Boolean(
- nativeConversationShareAvailable
- && shareCharacterSrc
- && shareLogoSrc
- && shareLogoModeRef.current === mode,
- );
- if (conversationShareHtml && nativeShareAssetsReady) {
- try {
- const nativeBase64 = await renderConversationShareHtmlToPng({
- html: conversationShareHtml,
- width: windowDimensions.width,
- });
- if (nativeBase64) {
- console.info('[conversation-share] native webview export succeeded');
- return nativeBase64;
- }
- } catch (error) {
- console.warn('[conversation-share] native webview export failed; falling back to svg', error);
- }
- }
const svg = conversationShareSvgRef.current;
if (!svg) throw new Error('conversation share svg renderer is unavailable');
return svg.exportPng();
- }, [conversationShareHtml, mode, shareCharacterSrc, shareLogoSrc, windowDimensions.width]);
+ }, []);
const shareSelectedConversation = useCallback(async () => {
if (
conversationShareBusy
@@ -6125,8 +6044,10 @@ export default function SessionScreen() {
const sharing = await import('expo-sharing');
if (!isShareOperationActive()) return;
await sharing.shareAsync(localUri, { mimeType: 'image/png' });
- if (!isShareOperationActive()) return;
+ // shareAsync 返回时系统扩展仍可能继续读取该 URL;只要系统分享调用成功,
+ // 即使当前选择操作已失活,也要把文件留给下一次有界清理。
shareCompleted = true;
+ if (!isShareOperationActive()) return;
setShareSelectionTriggeredByScreenshot(false);
shareSelectionStore.exit();
} catch (error) {
@@ -6134,8 +6055,8 @@ export default function SessionScreen() {
console.warn('[conversation-share] failed to generate or open share image', error);
Alert.alert(t('session.screen.shareFailedTitle'), t('session.screen.shareImageFailed'));
} finally {
- // shareAsync 返回后,iOS 分享扩展仍可能继续读取该 URL。成功写入的文件留在
- // cache 目录交给下一次有界清理;失败、取消或中途失活则立即删除当前产物。
+ // 失败、取消或分享调用前失活时立即清理;成功产物留给系统扩展,
+ // 并由下一次分享开始前的有界清理回收。
if (!shareCompleted && localUri) await deleteConversationSharePngTemp(localUri);
if (shareOperationSeqRef.current === operationSeq) setConversationShareBusy(false);
}
diff --git a/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts b/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
index 51ca68b264..ba0c58c674 100644
--- a/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
+++ b/apps/mobile/src/__tests__/conversationShareWebViewHtml.test.ts
@@ -149,7 +149,7 @@ describe('buildConversationShareHtml 富内容导出', () => {
expect(html).toContain(`alt="${i18n.t('message.renderer.imageFallbackTitle')}"`);
});
- it('限制原生与降级 renderer 的完整源尺寸,并安全保留已分享 PNG', () => {
+ it('限制原生 renderer 源尺寸,使用 SVG 主路径并安全保留已分享 PNG', () => {
const nativeSource = readFileSync(
resolve(
process.cwd(),
@@ -167,17 +167,6 @@ describe('buildConversationShareHtml 富内容导出', () => {
).replace(/\r\n/g, '\n');
expect(nativeSource).toContain('conversationShareMaxSourcePixels');
- expect(nativeSource).toContain('UIWindow(windowScene: windowScene)');
- expect(nativeSource).toContain('hostingWindow.rootViewController = viewController');
- expect(nativeSource).toContain('hostingWindow?.isHidden = true');
- expect(nativeSource).toContain('no active window scene');
- expect(nativeSource).toContain('UIWindow.Level.normal.rawValue + 1');
- expect(nativeSource).toContain('hostingWindow.alpha = 0.01');
- expect(nativeSource).toContain('waitForWebContentPaint(webView)');
- expect(nativeSource).toContain('requestAnimationFrame(resolve)');
- expect(nativeSource).toContain('merged.hasVisibleVariation');
- expect(nativeSource).toContain('Conversation share PNG is blank.');
- expect(nativeSource).toContain('format.scale = 1');
expect(nativeSource).toContain(
'captureWidth * captureHeight <= conversationShareMaxSourcePixels',
);
@@ -190,29 +179,16 @@ describe('buildConversationShareHtml 富内容导出', () => {
expect(webViewSource).toContain(
'files.slice(SHARE_PNG_RETAIN_COUNT, SHARE_PNG_RETAIN_COUNT + SHARE_PNG_CLEANUP_BATCH)',
);
- expect(sessionSource).toContain('deleteConversationSharePngTemp');
expect(sessionSource).toContain('cleanupConversationSharePngTemps');
- expect(sessionSource).toContain('cache 目录交给下一次有界清理');
- expect(sessionSource).toContain('if (!shareCompleted && localUri)');
- expect(sessionSource).toContain('<ConversationShareSvg');
- expect(sessionSource).toContain(
- "nativeConversationShareAvailable = Platform.OS === 'ios'",
- );
expect(sessionSource).toContain(
- 'if (!nativeConversationShareAvailable || !shareSelectionActive) return undefined;',
+ 'if (!shareCompleted && localUri) await deleteConversationSharePngTemp(localUri);',
);
expect(sessionSource).toContain(
- '!nativeConversationShareAvailable\n || !shareSelectionActive',
+ "await sharing.shareAsync(localUri, { mimeType: 'image/png' });\n // shareAsync 返回时系统扩展仍可能继续读取该 URL;只要系统分享调用成功,\n // 即使当前选择操作已失活,也要把文件留给下一次有界清理。\n shareCompleted = true;\n if (!isShareOperationActive()) return;",
);
- expect(sessionSource).toContain(
- 'nativeConversationShareAvailable\n && shareCharacterSrc',
- );
- expect(sessionSource).toContain('renderConversationShareHtmlToPng({');
- expect(sessionSource).toContain('nativeShareAssetsReady');
- expect(sessionSource).toContain('native webview export succeeded');
- expect(sessionSource).toContain('falling back to svg');
- expect(sessionSource).not.toContain('OTA webview export failed; falling back to svg');
+ expect(sessionSource).toContain('<ConversationShareSvg');
expect(sessionSource).toContain('return svg.exportPng();');
+ expect(sessionSource).not.toContain('renderConversationShareHtmlToPng({');
});
it('使用 Mobile 获批的克制页脚尺寸', () => {