refactor: split tree shell state runtime

This commit is contained in:
lix-2026
2026-05-26 02:48:30 +08:00
parent e76fa437a3
commit b8a067ce9f
5 changed files with 86 additions and 52 deletions
@@ -10,6 +10,15 @@ import {
createTreeShellActionButton,
createTreeShellKindBadge as createKindBadge,
} from "./tree-shell-icons-runtime.js";
import {
normalizeTreeShellNumber as normalizeNumber,
normalizeTreeShellStringArray as normalizeStringArray,
normalizeTreeShellText as normalizeText,
parseTreeShellState,
parseTreeShellStateFromHtml,
resolveTreeShellMode,
resolveTreeShellTargetOrigin,
} from "./tree-shell-state-runtime.js";
function buildFileTreeRuntimeEnvironment(runtimeContext) {
const fileTreeRowById = runtimeContext.fileTreeRowById || new Map();
@@ -37,15 +46,7 @@ function startTreeShellRuntime() {
return;
}
const parseState = () => {
try {
return JSON.parse(stateElement.textContent || "{}");
} catch {
return {};
}
};
const state = parseState();
const state = parseTreeShellState(stateElement);
const rendererInput =
state.rendererInput && typeof state.rendererInput === "object"
? state.rendererInput
@@ -77,12 +78,6 @@ function startTreeShellRuntime() {
runtimeArtifact.runtimeApi && typeof runtimeArtifact.runtimeApi === "object"
? runtimeArtifact.runtimeApi
: {};
const normalizeStringArray = (value) =>
Array.isArray(value)
? value
.map((item) => (typeof item === "string" ? item.trim() : ""))
.filter(Boolean)
: [];
const hostOverride =
window.__MNOTE_TREE_SHELL_OVERRIDE__ &&
typeof window.__MNOTE_TREE_SHELL_OVERRIDE__ === "object"
@@ -128,13 +123,7 @@ function startTreeShellRuntime() {
: typeof state.activePickerItemKey === "string" && state.activePickerItemKey.trim()
? state.activePickerItemKey.trim()
: "";
const mode = (() => {
const rawMode =
typeof state.mode === "string" ? state.mode.trim() : "";
if (rawMode === "picker") return "picker";
if (rawMode === "filetree") return "filetree";
return "page";
})();
const mode = resolveTreeShellMode(state);
const allowRootPick = state.allowRootPick === true;
const excludedIds = new Set(
normalizeStringArray(rendererInput.excludedPickerIds).length > 0
@@ -171,20 +160,7 @@ function startTreeShellRuntime() {
const titleElement = document.getElementById("tree-shell-title");
const summaryElement = document.getElementById("tree-shell-summary");
const toolbarElement = document.getElementById("tree-shell-toolbar");
const targetOrigin = (() => {
try {
if (!document.referrer) return "*";
return new URL(document.referrer).origin || "*";
} catch {
return "*";
}
})();
const normalizeText = (value, fallback = "") => {
if (typeof value !== "string") return fallback;
const trimmed = value.trim();
return trimmed || fallback;
};
const targetOrigin = resolveTreeShellTargetOrigin(document);
const initialRenameRowId = (() => {
try {
return normalizeText(new URL(window.location.href).searchParams.get("renameRowId"));
@@ -202,10 +178,6 @@ function startTreeShellRuntime() {
return normalized || null;
};
const normalizeNumber = (value, fallback = Number.MAX_SAFE_INTEGER) => {
return Number.isFinite(value) ? Number(value) : fallback;
};
const normalizeRowKind = (value) => {
const normalized = normalizeText(value).toLowerCase();
if (normalized === "index") return "index";
@@ -1544,17 +1516,6 @@ function startTreeShellRuntime() {
}
};
const parseTreeShellStateFromHtml = (html) => {
const doc = new DOMParser().parseFromString(html, "text/html");
const nextStateElement = doc.getElementById("tree-shell-state");
if (!nextStateElement) return null;
try {
return JSON.parse(nextStateElement.textContent || "{}");
} catch {
return null;
}
};
const applyTreeShellStateSnapshot = (nextState, options = {}) => {
if (!nextState || typeof nextState !== "object") return false;
mediaAssets = Array.isArray(nextState.mediaAssets) ? nextState.mediaAssets : [];