File size: 1,013 Bytes
a281968
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Phase 6.1 — Documents State
// In-memory state only. No DOM access. No Supabase.

const _docState = {
  currentDocumentId: null,
  currentDocumentTitle: 'مستند جديد',
  hasUnsavedChanges: false
};

/** @returns {{ currentDocumentId, currentDocumentTitle, hasUnsavedChanges }} */
function getDocState() {
  return { ..._docState };
}

/**
 * Update state fields and fire bayan:docstate event.
 * @param {Partial<typeof _docState>} updates
 */
function setDocState(updates) {
  Object.assign(_docState, updates);
  window.dispatchEvent(new CustomEvent('bayan:docstate', { detail: { ..._docState } }));
}

/** Mark document as having unsaved changes. */
function markDirty() {
  if (!_docState.hasUnsavedChanges) {
    setDocState({ hasUnsavedChanges: true });
  }
}

/** Mark document as clean (saved). */
function markClean() {
  setDocState({ hasUnsavedChanges: false });
}

/** True when a cloud document is currently open. */
function hasOpenDocument() {
  return !!_docState.currentDocumentId;
}