refactor: extract conflict panel host mount helper

This commit is contained in:
lix-2026
2026-05-25 08:38:05 +08:00
parent f33807eacc
commit 0245b09301
3 changed files with 45 additions and 4 deletions
@@ -17,12 +17,29 @@ function clearSessionConflictSurface(session, deps) {
? deps.sessionViews(session)
: [];
views.forEach(function(view) {
var host = view.runtimeDescriptor.root.closest('.document-pane') || view.runtimeDescriptor.root;
var host = conflictPanelHost(view && view.runtimeDescriptor && view.runtimeDescriptor.root);
if (!(host instanceof HTMLElement)) return;
host.querySelectorAll('[data-testid="mnote-editor-conflict-panel"]').forEach(function(node) { node.remove(); });
});
}
function conflictPanelHost(root) {
if (!(root instanceof Element)) return null;
return root.closest('.document-pane') || root;
}
function mountSessionConflictPanel(root, panel) {
var host = conflictPanelHost(root);
if (!(host instanceof HTMLElement) || !(panel instanceof HTMLElement)) return false;
var header = host.querySelector('.document-shell-header');
if (header && header.parentNode) {
header.parentNode.insertBefore(panel, header.nextSibling);
} else {
host.prepend(panel);
}
return true;
}
function createSessionConflictPanel(session, message, deps) {
deps = deps || {};
var externalConflictMessage = String(deps.externalConflictMessage || '').trim() || '文件已在外部修改';
@@ -185,6 +202,7 @@ function runSessionConflictAction(action, deps) {
window.__mnoteDocumentConflictPanelRuntime = {
clearSessionConflictSurface: clearSessionConflictSurface,
createSessionConflictPanel: createSessionConflictPanel,
mountSessionConflictPanel: mountSessionConflictPanel,
populateSessionConflictDiffPanel: populateSessionConflictDiffPanel,
runSessionConflictAction: runSessionConflictAction
};
+12 -3
View File
@@ -2139,8 +2139,6 @@ pub(crate) fn render_editor_island_adapter_script() -> &'static str {
const renderSessionConflictSurface = (session, message) => {
clearSessionConflictSurface(session);
sessionViews(session).forEach((view) => {
const host = view.runtimeDescriptor.root.closest('.document-pane') || view.runtimeDescriptor.root;
if (!(host instanceof HTMLElement)) return;
const runtime = window.__mnoteDocumentConflictPanelRuntime;
const handleConflictActionError = (error) => {
setSessionStatus(session, 'external-change-conflict', error instanceof Error ? error.message : String(error));
@@ -2211,6 +2209,13 @@ pub(crate) fn render_editor_island_adapter_script() -> &'static str {
});
}
if (runtime && typeof runtime.mountSessionConflictPanel === 'function') {
if (runtime.mountSessionConflictPanel(view.runtimeDescriptor.root, panel)) {
return;
}
}
const host = view.runtimeDescriptor.root.closest('.document-pane') || view.runtimeDescriptor.root;
if (!(host instanceof HTMLElement)) return;
const header = host.querySelector('.document-shell-header');
if (header && header.parentNode) {
header.parentNode.insertBefore(panel, header.nextSibling);
@@ -5628,6 +5633,9 @@ mod tests {
assert!(html.contains("mnote-conflict-open-diff"));
assert!(html.contains("mnote-conflict-merge-text"));
assert!(html.contains("mnote-conflict-merge-save"));
assert!(
html.contains("runtime.mountSessionConflictPanel(view.runtimeDescriptor.root, panel)")
);
assert!(html.contains("agent run"));
assert!(!html.contains(
"setInterval(() => {\n void pollLocalMarkdownExternalChange();\n }, 1200);"
@@ -5665,9 +5673,10 @@ mod tests {
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("onUseCurrent"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("onUseDisk"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("onSaveMerge"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("function runSessionConflictAction"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("function mountSessionConflictPanel"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS
.contains("runSessionConflictAction: runSessionConflictAction"));
assert!(DOCUMENT_CONFLICT_PANEL_RUNTIME_JS.contains("function runSessionConflictAction"));
}
#[tokio::test]