UNPKG

realtimecursor

Version:

Real-time collaboration system with cursor tracking and approval workflow

352 lines 14.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RealtimeCursorSDK = void 0; const axios_1 = __importDefault(require("axios")); const socket_io_client_1 = require("socket.io-client"); class RealtimeCursorSDK { constructor(config) { this.socket = null; this.token = null; this.currentUser = null; this.config = config; this.token = config.token || null; this.api = axios_1.default.create({ baseURL: config.apiUrl, headers: { 'Content-Type': 'application/json', }, }); // Add auth token to requests if available this.api.interceptors.request.use((config) => { if (this.token && config.headers) { config.headers.Authorization = `Bearer ${this.token}`; } return config; }); } // Authentication methods login(email, password) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.post('/auth/login', { email, password }); if (response.data.success) { this.token = response.data.token; this.currentUser = response.data.user; return response.data.user; } throw new Error(response.data.message || 'Login failed'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Login failed'); } }); } register(userData) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.post('/auth/register', userData); if (response.data.success) { this.token = response.data.token; this.currentUser = response.data.user; return response.data.user; } throw new Error(response.data.message || 'Registration failed'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Registration failed'); } }); } getCurrentUser() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get('/auth/me'); if (response.data.success) { this.currentUser = response.data.user; return response.data.user; } throw new Error(response.data.message || 'Failed to get user'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get user'); } }); } logout() { this.token = null; this.currentUser = null; if (this.socket) { this.socket.disconnect(); this.socket = null; } } // Project methods getProjects() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get('/projects'); if (response.data.success) { return response.data.projects || []; } throw new Error(response.data.message || 'Failed to get projects'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get projects'); } }); } createProject(name, description) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.post('/projects', { name, description }); if (response.data.success) { return response.data.project; } throw new Error(response.data.message || 'Failed to create project'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to create project'); } }); } getProject(projectId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get(`/projects/${projectId}`); if (response.data.success) { return response.data.project; } throw new Error(response.data.message || 'Failed to get project'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get project'); } }); } updateProjectContent(projectId, content) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.put(`/projects/${projectId}/content`, { content }); if (response.data.success) { return { staged: !!response.data.staged, changeId: response.data.changeId, }; } throw new Error(response.data.message || 'Failed to update content'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to update content'); } }); } inviteUsers(projectId, userIds) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.post(`/projects/${projectId}/invite`, { userIds }); if (response.data.success) { return { invitations: response.data.invitations }; } throw new Error(response.data.message || 'Failed to invite users'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to invite users'); } }); } // Comments methods getComments(projectId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get(`/projects/${projectId}/comments`); if (response.data.success) { return response.data.comments || []; } throw new Error(response.data.message || 'Failed to get comments'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get comments'); } }); } addComment(projectId, commentData) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.post(`/projects/${projectId}/comments`, commentData); if (response.data.success) { return response.data.comment; } throw new Error(response.data.message || 'Failed to add comment'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to add comment'); } }); } // Review methods getStagedChanges(projectId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get(`/projects/${projectId}/staged-changes`); if (response.data.success) { return response.data.changes || []; } throw new Error(response.data.message || 'Failed to get staged changes'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get staged changes'); } }); } reviewChange(changeId, approve, feedback) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.put(`/staged-changes/${changeId}`, { approve, feedback }); if (response.data.success) { return { approved: response.data.approved }; } throw new Error(response.data.message || 'Failed to review change'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to review change'); } }); } // History methods getHistory(projectId) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { const response = yield this.api.get(`/projects/${projectId}/history`); if (response.data.success) { return response.data.history || []; } throw new Error(response.data.message || 'Failed to get history'); } catch (error) { throw new Error(((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.message) || error.message || 'Failed to get history'); } }); } // Real-time collaboration methods connectToProject(projectId, userInfo) { if (!this.config.socketUrl) { throw new Error('Socket URL not provided in configuration'); } // Disconnect existing socket if any if (this.socket) { this.socket.disconnect(); } // Connect to socket server this.socket = (0, socket_io_client_1.io)(this.config.socketUrl, { auth: { token: this.token } }); // Join project room this.socket.emit('join-project', { projectId, user: { id: userInfo.id, name: userInfo.name, color: userInfo.color || this.getRandomColor() } }); } onContentUpdate(callback) { if (!this.socket) throw new Error('Socket not connected'); this.socket.on('content-update', callback); } onCursorUpdate(callback) { if (!this.socket) throw new Error('Socket not connected'); this.socket.on('cursor-update', callback); } onUserJoined(callback) { if (!this.socket) throw new Error('Socket not connected'); this.socket.on('user-joined', callback); } onUserLeft(callback) { if (!this.socket) throw new Error('Socket not connected'); this.socket.on('user-left', callback); } onRoomUsers(callback) { if (!this.socket) throw new Error('Socket not connected'); this.socket.on('room-users', callback); } updateContent(content, cursorPosition) { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('content-change', { content, cursorPosition }); } updateCursor(position) { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('cursor-move', position); } updateCursorPosition(textPosition) { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('cursor-position', { textPosition }); } setTyping(isTyping) { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('user-typing', { isTyping }); } notifyContentSaved(userName) { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('content-saved', { userName }); } notifyHistoryUpdate() { if (!this.socket) throw new Error('Socket not connected'); this.socket.emit('history-update'); } disconnect() { if (this.socket) { this.socket.disconnect(); this.socket = null; } } getRandomColor() { const colors = [ '#3b82f6', '#ef4444', '#10b981', '#f59e0b', '#8b5cf6', '#06b6d4', '#f97316', '#84cc16', '#ec4899', '#6366f1', '#14b8a6', '#f43f5e' ]; return colors[Math.floor(Math.random() * colors.length)]; } } exports.RealtimeCursorSDK = RealtimeCursorSDK; exports.default = RealtimeCursorSDK; //# sourceMappingURL=legacy.js.map