会议模式:加文件导入/导出(剪贴板对长文本不可靠,会被截断)

用户:"会议粘贴json被截断,加上支持文件导入"

【为什么会截断】
剪贴板对**长文本**不可靠 —— 各大 ROM 都有自己的一套截断策略
(有的按字节、有的按字符、有的只对"看起来像密码/令牌"的东西截),
而会议 JSON 动不动几 KB。用户遇到的截断不是输入框的问题,是剪贴板本身的。
所以光把输入框加高没用,得给一条**不经过剪贴板**的路。

【加了什么】
  - **导出到文件…**:系统保存对话框(CreateDocument,application/json),
    文件名默认「会议名.json」。导出完提示"已导出到文件(N 字符)",
    报字符数是为了让用户能和导入时对一下、确认没缺。
  - **从文件导入…**:系统打开对话框(OpenDocument),
    读进来直接解析;解析失败会明说"这个文件不是合法的会议 JSON",
    而不是默默什么都没发生。
  - 菜单里文件那两项都标了「推荐」,并且说明了原因
    ("剪贴板对长文本不可靠,几 KB 会截断")—— 用户看得见为什么。
  - 粘贴输入框也加高了(110→140,最大 190→300),
    并且在下面显示**已粘 N 个字符**,用户能自己核对有没有被截断。

【结构上的整理】
把"导成 JSON"和"导入一份内容"抽成 meetingJson() / applyImported() 两个函数,
剪贴板导出、文件导出、文件导入、粘贴导入**四条路共用**:
只要有一处各写一遍,就会出现"文件导出的能导入、剪贴板导出的不行"这种事。
applyImported 只换图不换名字和 id(用户是在编辑这一场会议,换的是内容不是身份)。

【踩的坑(第三次了,这次记牢)】
又用 PowerShell 按行号剪贴代码块把函数挪位置,结果把 launcher 插进了
别的 composable(报 Unresolved reference 'context'/'dirty')。
**这个 3400 行的 Kotlin 文件不能再按行号剪** —— 还原后用 edit 工具
按锚点逐处改,一次就过。以后只用 edit。

【验证】
- 引擎回归 40/40,Node 与 Python 16/16 逐用例一致
- 会议 HTTP 13/13 ×2
- 截图确认:⋮ 菜单里"导出到文件…(推荐)""复制会议 JSON""从文件导入…(推荐)"
  "粘贴会议 JSON…" 四项齐全
This commit is contained in:
l
2026-10-11 03:40:34 +08:00
parent 95f90797a4
commit 066a35e1f7
3 changed files with 134 additions and 27 deletions

View File

@@ -55,6 +55,9 @@ import com.doubao.lynxclone.data.toJson
import com.doubao.lynxclone.data.toMeeting
import com.doubao.lynxclone.data.MeetingResult
import androidx.compose.foundation.BorderStroke
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import com.doubao.lynxclone.data.toMeeting
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
@@ -354,6 +357,70 @@ private fun MeetingEditorScreen(vm: AppViewModel, initial: Meeting, onBack: () -
}
}
/** 把当前这张图导成 JSON 文本(导出到剪贴板和导出到文件共用) */
fun meetingJson(): String = JSONObject()
.put("name", meeting.name)
.put("nodes", JSONArray().apply { meeting.nodes.forEach { put(it.toJson()) } })
.put("edges", JSONArray().apply { meeting.edges.forEach { put(it.toJson()) } })
.put("vars", JSONArray().apply { meeting.vars.forEach { put(it.toJson()) } })
.toString(2)
/** 导入一份会议内容(只换图,不换名字和 id) */
fun applyImported(m: Meeting) {
meeting = meeting.copy(nodes = m.nodes, edges = m.edges, vars = m.vars)
dirty = true
persist()
}
// ★ 文件导入 / 导出走**系统文件选择器**。
//
// 为什么必须加这条:剪贴板对**长文本**不可靠 —— 各大 ROM 都有自己的一套
// 截断策略(有的按字节、有的按字符、有的只对"看起来像密码"的东西截),
// 而会议 JSON 动不动几 KB。用户反馈的"粘贴 JSON 被截断"就是这个。
// 存成文件没有这个问题,也是能长期保存、能发给别人的那条路。
var pendingExport by remember { mutableStateOf("") }
val exportLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.CreateDocument("application/json"),
) { uri ->
if (uri != null) {
val ok = runCatching {
context.contentResolver.openOutputStream(uri)?.use { os ->
os.write(pendingExport.toByteArray(Charsets.UTF_8))
}
true
}.getOrElse { false }
android.widget.Toast.makeText(
context,
if (ok) "已导出到文件(${pendingExport.length} 字符)" else "导出失败:路径不可写",
android.widget.Toast.LENGTH_SHORT,
).show()
}
}
val importLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.OpenDocument(),
) { uri ->
if (uri != null) {
val text = runCatching {
context.contentResolver.openInputStream(uri)?.use { ins ->
ins.readBytes().toString(Charsets.UTF_8)
}.orEmpty()
}.getOrElse { "" }
val m = runCatching { JSONObject(text.trim()).toMeeting() }.getOrElse { null }
if (m != null && m.nodes.isNotEmpty()) {
applyImported(m)
android.widget.Toast.makeText(
context,
"已从文件导入 ${m.nodes.size} 个节点",
android.widget.Toast.LENGTH_SHORT,
).show()
} else {
android.widget.Toast.makeText(
context, "这个文件不是合法的会议 JSON", android.widget.Toast.LENGTH_SHORT,
).show()
}
}
}
fun updateNode(node: MeetingNode) {
meeting = meeting.copy(nodes = meeting.nodes.map { if (it.id == node.id) node else it })
dirty = true
@@ -1133,6 +1200,8 @@ if (edge.kind == "exec") {
onSensitivity = { dragSensitivity = it },
meeting = meeting,
onImport = { showMenu = false; showImport = true },
onExportFile = { pendingExport = meetingJson(); exportLauncher.launch("${meeting.name.ifBlank { "doubao" }}.json") },
onImportFile = { importLauncher.launch(arrayOf("application/json", "text/plain", "*/*")) },
onExport = { json ->
clipboard.setText(androidx.compose.ui.text.AnnotatedString(json))
android.widget.Toast
@@ -1760,6 +1829,10 @@ private fun EditorMenuDialog(
meeting: Meeting,
onExport: (String) -> Unit,
onImport: () -> Unit,
/** 导出到文件(系统保存对话框) */
onExportFile: () -> Unit,
/** 从文件导入(系统打开对话框) */
onImportFile: () -> Unit,
onDismiss: () -> Unit,
) {
AlertDialog(
@@ -1807,6 +1880,15 @@ private fun EditorMenuDialog(
lineHeight = 16.sp,
)
Spacer(Modifier.height(6.dp))
Text(
"导出到文件…(推荐:剪贴板对长文本不可靠,几 KB 会截断)",
color = Doubao.Blue,
fontSize = 13.sp,
modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.clickable { onExportFile() }
.padding(horizontal = 8.dp, vertical = 6.dp),
)
Text(
"复制会议 JSON(${meeting.nodes.size} 节点 · ${meeting.edges.size} 连线 · ${meeting.vars.size} 变量)",
color = Doubao.Blue,
@@ -1842,6 +1924,18 @@ private fun EditorMenuDialog(
lineHeight = 16.sp,
)
Spacer(Modifier.height(6.dp))
// ★ 文件导入放在粘贴**前面**,并且标"推荐":
// 剪贴板对长文本不可靠(各大 ROM 有自己的截断策略),
// 会议 JSON 动不动几 KB,走文件才稳。
Text(
"从文件导入…(推荐)",
color = Doubao.Blue,
fontSize = 13.sp,
modifier = Modifier
.clip(RoundedCornerShape(8.dp))
.clickable { onImportFile() }
.padding(horizontal = 8.dp, vertical = 6.dp),
)
Text(
"粘贴会议 JSON…",
color = Doubao.Blue,
@@ -2067,7 +2161,7 @@ private fun ImportDialog(
OutlinedTextField(
value = text,
onValueChange = { text = it; error = "" },
modifier = Modifier.fillMaxWidth().heightIn(min = 110.dp, max = 190.dp),
modifier = Modifier.fillMaxWidth().heightIn(min = 140.dp, max = 300.dp),
placeholder = { Text("{\"nodes\": [...], \"edges\": [...]}", fontSize = 11.sp) },
textStyle = TextStyle(fontSize = 11.sp),
label = { Text("会议 JSON", fontSize = 11.sp) },