接入原生壳剪贴板读取能力
新增 clipboard.readText HostBridge 契约和 H5 facade 移动端通过 expo-clipboard 读取纯文本剪贴板 桌面端通过 Tauri clipboard-manager 读取纯文本剪贴板 更新壳能力检查、测试、方案文档和共享决策记录
This commit is contained in:
@@ -143,6 +143,7 @@ const requiredMainSnippets = [
|
||||
'"app.setTitle"',
|
||||
'"app.setBadgeCount"',
|
||||
'"clipboard.writeText"',
|
||||
'"clipboard.readText"',
|
||||
'"file.exportText"',
|
||||
'"file.exportImage"',
|
||||
'"file.importImage"',
|
||||
@@ -159,6 +160,8 @@ const requiredMainSnippets = [
|
||||
'import_image_file_payload',
|
||||
'set_title',
|
||||
'set_badge_count',
|
||||
'read_text()',
|
||||
'normalize_clipboard_text',
|
||||
'window.theme()',
|
||||
'WindowEvent::Focused',
|
||||
'WindowEvent::DragDrop',
|
||||
|
||||
@@ -30,6 +30,7 @@ const BADGE_COUNT_MAX: i64 = 99999;
|
||||
const DESKTOP_NETWORK_CHECK_TIMEOUT_MS: u64 = 1200;
|
||||
const LOCAL_NOTIFICATION_TITLE_MAX_LENGTH: usize = 80;
|
||||
const LOCAL_NOTIFICATION_BODY_MAX_LENGTH: usize = 240;
|
||||
const CLIPBOARD_TEXT_MAX_LENGTH: usize = 100000;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -101,6 +102,7 @@ fn capabilities() -> Vec<&'static str> {
|
||||
"network.status",
|
||||
"network.statusChanged",
|
||||
"clipboard.writeText",
|
||||
"clipboard.readText",
|
||||
"file.exportText",
|
||||
"file.exportImage",
|
||||
"file.importImage",
|
||||
@@ -248,6 +250,10 @@ fn badge_count_payload(request: &HostBridgeRequest) -> Result<Option<i64>, HostB
|
||||
Ok(if count == 0 { None } else { Some(count) })
|
||||
}
|
||||
|
||||
fn normalize_clipboard_text(text: String) -> String {
|
||||
text.chars().take(CLIPBOARD_TEXT_MAX_LENGTH).collect()
|
||||
}
|
||||
|
||||
fn normalize_plain_text(
|
||||
value: Option<&str>,
|
||||
max_length: usize,
|
||||
@@ -859,6 +865,15 @@ async fn host_bridge_request(
|
||||
Err(error) => failed(request.id, "host_error", error.to_string()),
|
||||
}
|
||||
}
|
||||
"clipboard.readText" => match app.clipboard().read_text() {
|
||||
Ok(text) => ok(
|
||||
request.id,
|
||||
json!({
|
||||
"text": normalize_clipboard_text(text),
|
||||
}),
|
||||
),
|
||||
Err(error) => failed(request.id, "host_error", error.to_string()),
|
||||
},
|
||||
"file.exportText" => {
|
||||
let (file_name, content) = match export_text_payload(&request) {
|
||||
Ok(payload) => payload,
|
||||
@@ -1130,6 +1145,10 @@ mod tests {
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!("app.setBadgeCount")));
|
||||
assert!(result["capabilities"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.contains(&json!("clipboard.readText")));
|
||||
assert!(result["capabilities"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
@@ -1186,6 +1205,15 @@ mod tests {
|
||||
assert_eq!(error.message, "text is required");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clipboard_text_is_truncated_to_contract_limit() {
|
||||
assert_eq!(normalize_clipboard_text("作品号 PZ-1".to_string()), "作品号 PZ-1");
|
||||
assert_eq!(
|
||||
normalize_clipboard_text("a".repeat(CLIPBOARD_TEXT_MAX_LENGTH + 10)).len(),
|
||||
CLIPBOARD_TEXT_MAX_LENGTH
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_notification_payload_is_normalized() {
|
||||
let mut request = request("notification.showLocal");
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"build": {
|
||||
"beforeDevCommand": "npm --prefix ../.. run dev:web",
|
||||
"beforeBuildCommand": "npm --prefix ../.. run build:raw && npm run typecheck",
|
||||
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,file.exportText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"devUrl": "http://127.0.0.1:3000/?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"frontendDist": "../../../dist"
|
||||
},
|
||||
"app": {
|
||||
@@ -14,7 +14,7 @@
|
||||
{
|
||||
"create": false,
|
||||
"label": "main",
|
||||
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,file.exportText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"url": "index.html?clientRuntime=native_app&clientType=native_app&hostShell=tauri_desktop&hostPlatform=unknown&hostVersion=0.1.0&bridgeVersion=1&hostCapabilities=host.getRuntime,appearance.getColorScheme,app.lifecycle,share.open,share.setTarget,navigation.openNativePage,app.openExternalUrl,app.setTitle,app.setBadgeCount,network.status,network.statusChanged,clipboard.writeText,clipboard.readText,file.exportText,file.exportImage,file.importImage,file.imageDropped,notification.showLocal",
|
||||
"title": "Genarrative",
|
||||
"width": 1280,
|
||||
"height": 820,
|
||||
|
||||
@@ -179,6 +179,7 @@ for (const snippet of [
|
||||
'file.exportText',
|
||||
'file.exportImage',
|
||||
'file.importImage',
|
||||
'clipboard.readText',
|
||||
'notification.showLocal',
|
||||
'network.status',
|
||||
'getMobileNetworkStatus',
|
||||
@@ -187,9 +188,11 @@ for (const snippet of [
|
||||
'Notifications.getPermissionsAsync',
|
||||
'Notifications.requestPermissionsAsync',
|
||||
'Sharing.shareAsync',
|
||||
'Clipboard.getStringAsync',
|
||||
'ImagePicker.launchImageLibraryAsync',
|
||||
'ImagePicker.requestMediaLibraryPermissionsAsync',
|
||||
'normalizeHostBridgeExportFileName',
|
||||
'normalizeHostBridgeClipboardText',
|
||||
'base64Data',
|
||||
]) {
|
||||
if (!bridgeSource.includes(snippet)) {
|
||||
@@ -218,6 +221,7 @@ for (const capability of [
|
||||
'navigation.canGoBack',
|
||||
'app.openExternalUrl',
|
||||
'clipboard.writeText',
|
||||
'clipboard.readText',
|
||||
'file.exportText',
|
||||
'file.exportImage',
|
||||
'file.importImage',
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import * as Clipboard from 'expo-clipboard';
|
||||
import * as Haptics from 'expo-haptics';
|
||||
import * as ImagePicker from 'expo-image-picker';
|
||||
import * as Linking from 'expo-linking';
|
||||
@@ -43,6 +44,7 @@ const DENIED_NOTIFICATION_PERMISSION = {
|
||||
} as NotificationPermissionStatus;
|
||||
|
||||
vi.mock('expo-clipboard', () => ({
|
||||
getStringAsync: vi.fn(),
|
||||
setStringAsync: vi.fn(),
|
||||
}));
|
||||
|
||||
@@ -198,6 +200,9 @@ afterEach(() => {
|
||||
vi.mocked(Appearance.getColorScheme).mockReset();
|
||||
vi.mocked(Appearance.getColorScheme).mockReturnValue('light');
|
||||
vi.mocked(Haptics.impactAsync).mockReset();
|
||||
vi.mocked(Clipboard.getStringAsync).mockReset();
|
||||
vi.mocked(Clipboard.getStringAsync).mockResolvedValue('作品号 PZ-1');
|
||||
vi.mocked(Clipboard.setStringAsync).mockReset();
|
||||
vi.mocked(ImagePicker.launchImageLibraryAsync).mockReset();
|
||||
vi.mocked(ImagePicker.launchImageLibraryAsync).mockResolvedValue({
|
||||
canceled: true,
|
||||
@@ -271,6 +276,7 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
'network.statusChanged',
|
||||
'navigation.canGoBack',
|
||||
'app.setBadgeCount',
|
||||
'clipboard.readText',
|
||||
'notification.showLocal',
|
||||
]),
|
||||
);
|
||||
@@ -400,6 +406,28 @@ describe('handleMobileHostBridgeMessage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
test('clipboard.readText 读取 Expo 系统剪贴板文本', async () => {
|
||||
vi.mocked(Clipboard.getStringAsync).mockResolvedValue('作品号 PZ-1');
|
||||
|
||||
const response = await send(request('clipboard.readText'));
|
||||
|
||||
expect(expectOk(response).result).toEqual({
|
||||
text: '作品号 PZ-1',
|
||||
});
|
||||
expect(Clipboard.getStringAsync).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('clipboard.readText 读取失败时返回 host_error', async () => {
|
||||
vi.mocked(Clipboard.getStringAsync).mockRejectedValue(
|
||||
new Error('clipboard unavailable'),
|
||||
);
|
||||
|
||||
const response = await send(request('clipboard.readText'));
|
||||
|
||||
const failedResponse = expectFailed(response);
|
||||
expect(failedResponse.error.code).toBe('host_error');
|
||||
});
|
||||
|
||||
test('haptics.impact 调起 Expo 触觉反馈', async () => {
|
||||
const response = await send(
|
||||
request('haptics.impact', {
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
} from 'react-native';
|
||||
|
||||
import {
|
||||
type ClipboardReadTextResult,
|
||||
type ClipboardWriteTextPayload,
|
||||
type FileExportImagePayload,
|
||||
type FileExportImageResult,
|
||||
@@ -30,6 +31,7 @@ import {
|
||||
type HostBridgeResponse,
|
||||
type NavigateNativePagePayload,
|
||||
normalizeHostBridgeBadgeCount,
|
||||
normalizeHostBridgeClipboardText,
|
||||
normalizeHostBridgeColorScheme,
|
||||
normalizeHostBridgeExportFileName,
|
||||
normalizeHostBridgeExternalUrl,
|
||||
@@ -74,6 +76,7 @@ export const MOBILE_HOST_CAPABILITIES: HostBridgeCapability[] = [
|
||||
'network.status',
|
||||
'network.statusChanged',
|
||||
'clipboard.writeText',
|
||||
'clipboard.readText',
|
||||
'file.exportText',
|
||||
'file.exportImage',
|
||||
'file.importImage',
|
||||
@@ -229,6 +232,20 @@ async function writeClipboard(payload: unknown) {
|
||||
return true;
|
||||
}
|
||||
|
||||
async function readClipboard(): Promise<ClipboardReadTextResult> {
|
||||
const result = normalizeHostBridgeClipboardText(
|
||||
await Clipboard.getStringAsync(),
|
||||
);
|
||||
if (!result) {
|
||||
throw {
|
||||
code: 'host_error',
|
||||
message: 'clipboard text unavailable',
|
||||
} satisfies HostBridgeError;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
async function exportTextFile(payload: unknown): Promise<FileExportTextResult> {
|
||||
const exportPayload = payload as FileExportTextPayload | undefined;
|
||||
const content = exportPayload?.content;
|
||||
@@ -606,6 +623,8 @@ async function handleRequest(request: HostBridgeRequest) {
|
||||
return ok(request, await getMobileNetworkStatus());
|
||||
case 'clipboard.writeText':
|
||||
return ok(request, await writeClipboard(request.payload));
|
||||
case 'clipboard.readText':
|
||||
return ok(request, await readClipboard());
|
||||
case 'file.exportText':
|
||||
return ok(request, await exportTextFile(request.payload));
|
||||
case 'file.exportImage':
|
||||
|
||||
Reference in New Issue
Block a user