feat(foreign): own view sessions and clear outputs

foreign.session returns a handle from ordinary kernel/cell tables; view consumes only the 会话 binding. Sessions live in host foreign_doc. Without 会话, view cells run stateless one-shot. Add /api/session/clear and a Clear outputs button for every view page; clearing keeps persistent kernel state.
This commit is contained in:
cflmy committed 2026-10-18 12:42:15 +08:00
1 parent 01468ad976
commit fb28eee62f
21 files changed
+379 -75

No files matched your search

+36 -2
View File
@@ -3466,6 +3466,7 @@ body.notebook-page .stdin-form button {
<button type="button" id="nb-step">Step</button>
<button type="button" id="nb-state">State</button>
<button type="button" id="nb-journal">Journal</button>
<button type="button" id="nb-clear">Clear outputs</button>
<button type="button" id="nb-reload">Reload</button>
<button type="button" id="nb-debug">Debug</button>
<button type="button" id="nb-theme" title="切换浅色/深色主题">浅色</button>
@@ -3501,7 +3502,7 @@ body.notebook-page .stdin-form button {
if (!window.MARQDO_VIEW_LIVE) return;
var path = window.MARQDO_FILE_PATH || "";
if (!path) return;
var state = { open: false, kernels: [], cells: {} };
var state = { open: false, persistent: false, kernels: [], cells: {} };
function post(url, body) {
return fetch(url, {
method: "POST",
@@ -3552,6 +3553,7 @@ body.notebook-page .stdin-form button {
}
function renderSummary(s) {
if (!s) return;
state.persistent = !!s.persistent;
state.kernels = s.kernels || [];
state.cells = {};
(s.cells || []).forEach(function (c) { state.cells[c.name] = c; });
@@ -3559,6 +3561,11 @@ body.notebook-page .stdin-form button {
var el = card(c.name);
if (!el) return;
badge(el, c.status, c.status === "clean");
if (!state.persistent) {
var old = el.querySelector(".nb-kernel");
if (old) old.remove();
return;
}
var sel = el.querySelector(".nb-kernel");
if (!sel) {
sel = document.createElement("select");
@@ -3608,6 +3615,30 @@ body.notebook-page .stdin-form button {
status("run all done");
});
}
function clearDom() {
document.querySelectorAll(".code-out").forEach(function (el) {
el.hidden = true;
el.textContent = "";
el.classList.remove("err");
});
document.querySelectorAll(".nb-badge").forEach(function (el) {
el.textContent = "never";
el.className = "nb-badge";
});
var exec = document.getElementById("exec-main");
if (exec) { exec.hidden = true; exec.textContent = ""; }
var stateOut = document.getElementById("nb-state-out");
if (stateOut) { stateOut.hidden = true; stateOut.textContent = ""; }
var pullOut = document.getElementById("nb-pull-out");
if (pullOut) pullOut.textContent = "";
}
function clearOutputs() {
clearDom();
post("api/session/clear", {}).then(function (r) {
if (r.ok && r.session) renderSummary(r.session);
status(r.ok ? "outputs cleared" : (r.error || "clear failed"));
}).catch(function (e) { status(String(e)); });
}
function bind(id, fn) { var b = document.getElementById(id); if (b) b.addEventListener("click", fn); }
function init() {
var themeBtn = document.getElementById("nb-theme");
@@ -3687,6 +3718,7 @@ body.notebook-page .stdin-form button {
bind("nb-debug", function () {
window.open("debug?path=" + encodeURIComponent(path), "_blank");
});
bind("nb-clear", clearOutputs);
bind("nb-reload", function () {
post("api/session/reload", {}).then(function (r) {
if (!r.ok) { status(r.error || "reload failed"); return; }
@@ -3879,7 +3911,7 @@ window.MARQDO_AUTO_STREAM = {auto_stream};
{stdin_panel}
{stream_panel}
{plan_card}
<div class="out {status}">{out}</div>
<div class="out {status}" id="exec-main">{out}</div>
{plots}
</section>
<section class="block source-block">
@@ -4124,6 +4156,8 @@ mod tests {
assert!(ui.contains("post(\"api/session/open\""));
assert!(!ui.contains("In ["), "view cells must not mimic Jupyter In[] prompts");
assert!(!ui.contains("Out ["), "view cells must not mimic Jupyter Out[] prompts");
assert!(ui.contains("id=\"nb-clear\""), "view must expose Clear outputs");
assert!(ui.contains("api/session/clear"), "Clear outputs must call the session API");
}
}
+5
View File
@@ -189,6 +189,11 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
notebook_api::api_close(&root.root, body)
})
}
"/api/session/clear" => {
return api_debug_post(request, |body| {
notebook_api::api_clear(&root.root, body)
})
}
"/api/session/cell/run" => {
return api_debug_post(request, |body| {
notebook_api::api_cell_run(&root.root, body)
+9 -1
View File
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};
use crate::host::json::{json_to_value, value_to_json};
use crate::notebook::DocumentSession;
use crate::host::foreign_doc::DocumentSession;
use crate::value::Value;
fn sessions() -> &'static Mutex<HashMap<String, Arc<Mutex<DocumentSession>>>> {
@@ -151,6 +151,10 @@ where
}
}
pub fn api_clear(root: &Path, body: &str) -> serde_json::Value {
with_session(root, body, "cleared", |s| Ok(s.clear_outputs()))
}
pub fn api_cell_run(root: &Path, body: &str) -> serde_json::Value {
let v = body_value(body);
let cell = v.get("cell").and_then(|x| x.as_str()).unwrap_or("");
@@ -281,6 +285,10 @@ mod tests {
Some(false),
"{failed:?}"
);
let clear = api_clear(&root, r#"{"path":"shared.mq.md"}"#);
assert_eq!(clear.get("ok").and_then(|v| v.as_bool()), Some(true), "{clear:?}");
let cleared = clear.get("session").and_then(|s| s.get("cells")).and_then(|c| c.as_array()).cloned().unwrap_or_default();
assert!(cleared.iter().all(|c| c.get("runs").and_then(|v| v.as_u64()) == Some(0)));
let push = api_state_push(
&root,
r#"{"path":"shared.mq.md","kernel":"分析","name":"z","value":9}"#,