Fix local folder title option refresh race
This commit is contained in:
@@ -160,6 +160,21 @@ import {
|
||||
titleEndpoint: '/api/documents/title',
|
||||
editorHostKind: 'leptos_tiptap_island',
|
||||
});
|
||||
const aggregateDocumentId = (aggregate) => {
|
||||
return String(aggregate?.identity?.documentId || aggregate?.identity?.document_id || aggregate?.pageId || aggregate?.page_id || '').trim();
|
||||
};
|
||||
const aggregatePageOptions = (aggregate) => {
|
||||
return aggregate?.layout?.pageOptions || aggregate?.layout?.page_options || aggregate?.layoutOptions || aggregate?.layout_options || null;
|
||||
};
|
||||
const withPageOptions = (aggregate, pageOptions) => {
|
||||
if (!aggregate || !pageOptions) return aggregate;
|
||||
const next = { ...aggregate };
|
||||
next.layout = next.layout && typeof next.layout === 'object' ? { ...next.layout } : {};
|
||||
next.layout.pageOptions = { ...pageOptions };
|
||||
next.layout_options = { ...pageOptions };
|
||||
next.layoutOptions = { ...pageOptions };
|
||||
return next;
|
||||
};
|
||||
const syncPageAggregateScript = (session, aggregate) => {
|
||||
if (!session || !aggregate) return;
|
||||
const scriptId = session.pageAggregateScriptId || '__MNOTE_PAGE_AGGREGATE__';
|
||||
@@ -171,8 +186,24 @@ import {
|
||||
document.body.appendChild(node);
|
||||
}
|
||||
try {
|
||||
node.textContent = JSON.stringify(aggregate);
|
||||
let existingAggregate = null;
|
||||
try {
|
||||
existingAggregate = JSON.parse(node.textContent || 'null');
|
||||
} catch (_) {
|
||||
existingAggregate = null;
|
||||
}
|
||||
const localWriteAt = Number(node.getAttribute('data-mnote-page-options-local-write-at') || 0);
|
||||
const preserveRecentLocalOptions = localWriteAt > 0
|
||||
&& Date.now() - localWriteAt < 15000
|
||||
&& aggregateDocumentId(existingAggregate) === aggregateDocumentId(aggregate);
|
||||
const nextAggregate = preserveRecentLocalOptions
|
||||
? withPageOptions(aggregate, aggregatePageOptions(existingAggregate))
|
||||
: aggregate;
|
||||
node.textContent = JSON.stringify(nextAggregate);
|
||||
node.setAttribute('data-mnote-page-aggregate-synced-at', String(Date.now()));
|
||||
window.dispatchEvent(new CustomEvent('mnote:page-aggregate-synced', {
|
||||
detail: { scriptId, documentId: aggregateDocumentId(nextAggregate) }
|
||||
}));
|
||||
} catch (error) {
|
||||
console.warn('mnote Page Aggregate script 同步失败', error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user