design: plan browser runtime extraction
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// MNote 资源打开运行时外置模块。
|
||||
// 当前先承接 SIDEBAR_TREE_JS 中的低风险纯辅助函数。
|
||||
// 本文件以 type="module" 加载,执行时机可能晚于 inline script。
|
||||
// SIDEBAR_TREE_JS 会在运行时优先委托到 window.__mnoteResourceOpenRuntime。
|
||||
// 模块尚未加载或加载失败时,inline script 保留原实现兜底。
|
||||
|
||||
function localFilePathFromAssetId(assetId) {
|
||||
var value = String(assetId || '').trim();
|
||||
return value.indexOf('local-file:') === 0
|
||||
? value.slice('local-file:'.length)
|
||||
: value.indexOf('local:asset:') === 0
|
||||
? value.slice('local:asset:'.length)
|
||||
: '';
|
||||
}
|
||||
|
||||
function buildLocalFileOpenUrl(relativePath, download) {
|
||||
var rootUri = currentRootUri();
|
||||
if (!rootUri || !relativePath) return '';
|
||||
var url = new URL('/api/local-folder/files/open', window.location.origin);
|
||||
url.searchParams.set('rootUri', rootUri);
|
||||
url.searchParams.set('path', relativePath);
|
||||
if (download) url.searchParams.set('download', 'true');
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
function buildOnlyOfficeOpenPath(input) {
|
||||
var params = new URLSearchParams();
|
||||
params.set('fileUrl', input.fileUrl || '');
|
||||
params.set('fileName', input.fileName || '未命名资源');
|
||||
params.set('fileType', input.fileType || 'docx');
|
||||
if (input.assetId) params.set('assetId', input.assetId);
|
||||
if (input.documentId) params.set('documentId', input.documentId);
|
||||
if (input.userId) params.set('userId', input.userId);
|
||||
params.set('mode', input.mode || 'view');
|
||||
if (input.documentId && input.assetId) {
|
||||
return '/office/' + encodeURIComponent(input.documentId) + '/' + encodeURIComponent(input.assetId) + '?' + params.toString();
|
||||
}
|
||||
return '/onlyoffice?' + params.toString();
|
||||
}
|
||||
|
||||
function currentRootUri() {
|
||||
var params = new URLSearchParams(window.location.search);
|
||||
var fromUrl = (params.get('rootUri') || '').trim();
|
||||
if (fromUrl) return fromUrl;
|
||||
return document.body instanceof HTMLElement
|
||||
? (document.body.getAttribute('data-mnote-root-uri') || '').trim()
|
||||
: '';
|
||||
}
|
||||
|
||||
window.__mnoteResourceOpenRuntime = {
|
||||
localFilePathFromAssetId: localFilePathFromAssetId,
|
||||
buildLocalFileOpenUrl: buildLocalFileOpenUrl,
|
||||
buildOnlyOfficeOpenPath: buildOnlyOfficeOpenPath
|
||||
};
|
||||
@@ -94,6 +94,10 @@ pub fn build_router(state: AppState) -> Router {
|
||||
"/api/editor/image-placeholder.svg",
|
||||
get(web_shell::editor_image_placeholder_asset),
|
||||
)
|
||||
.route(
|
||||
"/api/mnote-browser-runtime/resource-open-runtime.js",
|
||||
get(web_shell::resource_open_runtime_asset),
|
||||
)
|
||||
.route("/api/search/documents", post(search::documents))
|
||||
.route(
|
||||
"/api/search/local-index/refresh",
|
||||
|
||||
@@ -4366,6 +4366,21 @@ pub async fn editor_image_placeholder_asset() -> Response {
|
||||
.unwrap_or_else(|_| Response::new(Body::empty()))
|
||||
}
|
||||
|
||||
pub async fn resource_open_runtime_asset() -> Response {
|
||||
// include_str! 路径相对于当前源文件 (src/routes/web_shell.rs -> ../../browser/)
|
||||
const JS: &str = include_str!("../../browser/resource-open-runtime.js");
|
||||
Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/javascript; charset=utf-8",
|
||||
)
|
||||
.header(header::CACHE_CONTROL, "no-store")
|
||||
.header(HEADER_MNOTE_WEB_OWNER, "mnote-web")
|
||||
.body(Body::from(JS))
|
||||
.unwrap_or_else(|_| Response::new(Body::empty()))
|
||||
}
|
||||
|
||||
pub async fn leptos_tiptap_manifest() -> Response {
|
||||
let manifest = json!({
|
||||
"entryAssetPath": "mnote-leptos-tiptap-spike-island.js",
|
||||
|
||||
@@ -2562,6 +2562,10 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function buildOnlyOfficeOpenPath(input) {
|
||||
var _rto_ = window.__mnoteResourceOpenRuntime;
|
||||
if (_rto_ && typeof _rto_.buildOnlyOfficeOpenPath === 'function') {
|
||||
return _rto_.buildOnlyOfficeOpenPath(input);
|
||||
}
|
||||
var params = new URLSearchParams();
|
||||
params.set('fileUrl', input.fileUrl || '');
|
||||
params.set('fileName', input.fileName || '未命名资源');
|
||||
@@ -2671,11 +2675,19 @@ const SIDEBAR_TREE_JS: &str = r##"
|
||||
}
|
||||
|
||||
function localFilePathFromAssetId(assetId) {
|
||||
var _rto_ = window.__mnoteResourceOpenRuntime;
|
||||
if (_rto_ && typeof _rto_.localFilePathFromAssetId === 'function') {
|
||||
return _rto_.localFilePathFromAssetId(assetId);
|
||||
}
|
||||
var value = String(assetId || '').trim();
|
||||
return value.indexOf('local-file:') === 0 ? value.slice('local-file:'.length) : value.indexOf('local:asset:') === 0 ? value.slice('local:asset:'.length) : '';
|
||||
}
|
||||
|
||||
function buildLocalFileOpenUrl(relativePath, download) {
|
||||
var _rto_ = window.__mnoteResourceOpenRuntime;
|
||||
if (_rto_ && typeof _rto_.buildLocalFileOpenUrl === 'function') {
|
||||
return _rto_.buildLocalFileOpenUrl(relativePath, download);
|
||||
}
|
||||
var rootUri = currentRootUri();
|
||||
if (!rootUri || !relativePath) return '';
|
||||
var url = new URL('/api/local-folder/files/open', window.location.origin);
|
||||
@@ -10639,6 +10651,7 @@ pub fn PageLayout(
|
||||
<div hidden data-testid="mnote-admin-access-policy-template-admin" inner_html={admin_access_policy_template}></div>
|
||||
<div hidden data-testid="mnote-admin-access-policy-template-user" inner_html={user_access_policy_template}></div>
|
||||
<script inner_html={crate::ssr::pages::admin::ADMIN_POLICY_SCRIPT.to_string()}></script>
|
||||
<script type="module" src="/api/mnote-browser-runtime/resource-open-runtime.js"></script>
|
||||
<script inner_html={SIDEBAR_TREE_JS.to_string()}></script>
|
||||
<script id="mnote-tree-live-controller" inner_html={TREE_LIVE_CONTROLLER_JS.to_string()}></script>
|
||||
</aside>
|
||||
|
||||
Reference in New Issue
Block a user