File size: 3,639 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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | // Phase 6.1 — Documents Cloud API
// Supabase CRUD only. No DOM access. Never throws to caller.
/**
* @returns {import('@supabase/supabase-js').SupabaseClient|null}
*/
function _getClient() {
return (typeof getSupabaseClient === 'function') ? getSupabaseClient() : null;
}
function _getUserId() {
return window.__bayanAuth && window.__bayanAuth.userId
? window.__bayanAuth.userId
: null;
}
/**
* Create a new document in the cloud.
* @param {string} title
* @param {string} content
* @returns {Promise<object|null>}
*/
async function createDocument(title = 'مستند جديد', content = '') {
const client = _getClient();
const userId = _getUserId();
if (!client || !userId) return null;
try {
const { data, error } = await client
.from('documents')
.insert({ user_id: userId, title, content })
.select('id, title, content, created_at, updated_at')
.single();
if (error) throw error;
return data;
} catch (err) {
console.warn('[documents-api] createDocument failed:', err.message);
return null;
}
}
/**
* Load list of documents (id, title, updated_at) for current user.
* @returns {Promise<Array>}
*/
async function loadDocuments() {
const client = _getClient();
const userId = _getUserId();
if (!client || !userId) return [];
try {
const { data, error } = await client
.from('documents')
.select('id, title, updated_at')
.eq('user_id', userId)
.order('updated_at', { ascending: false });
if (error) throw error;
return data || [];
} catch (err) {
console.warn('[documents-api] loadDocuments failed:', err.message);
return [];
}
}
/**
* Load a single document's full content.
* @param {string} id
* @returns {Promise<object|null>}
*/
async function loadDocument(id) {
const client = _getClient();
if (!client) return null;
try {
const { data, error } = await client
.from('documents')
.select('id, title, content, updated_at')
.eq('id', id)
.single();
if (error) throw error;
return data;
} catch (err) {
console.warn('[documents-api] loadDocument failed:', err.message);
return null;
}
}
/**
* Save content to existing document.
* @param {string} id
* @param {string} content
* @returns {Promise<boolean>}
*/
async function saveDocument(id, content) {
const client = _getClient();
if (!client) return false;
try {
const { error } = await client
.from('documents')
.update({ content })
.eq('id', id);
if (error) throw error;
return true;
} catch (err) {
console.warn('[documents-api] saveDocument failed:', err.message);
return false;
}
}
/**
* Rename a document.
* @param {string} id
* @param {string} title
* @returns {Promise<boolean>}
*/
async function renameDocument(id, title) {
const client = _getClient();
if (!client) return false;
try {
const { error } = await client
.from('documents')
.update({ title })
.eq('id', id);
if (error) throw error;
return true;
} catch (err) {
console.warn('[documents-api] renameDocument failed:', err.message);
return false;
}
}
/**
* Delete a document permanently.
* @param {string} id
* @returns {Promise<boolean>}
*/
async function deleteDocument(id) {
const client = _getClient();
if (!client) return false;
try {
const { error } = await client
.from('documents')
.delete()
.eq('id', id);
if (error) throw error;
return true;
} catch (err) {
console.warn('[documents-api] deleteDocument failed:', err.message);
return false;
}
}
|