Browse Source

delete comment logic changed

CrazyDoctor 1 year ago
parent
commit
18604f956d
3 changed files with 56 additions and 23 deletions
  1. 32 8
      src/comments/CommentsManager.ts
  2. 8 12
      src/routes/PostUpdateComment.ts
  3. 16 3
      static/page/class/script.js

+ 32 - 8
src/comments/CommentsManager.ts

@@ -66,8 +66,13 @@ class CommentsManager {
 
 	private static enqueueComment(comment: IComment, action: CommentAction): void {
 		const commentPath = `${CommentsManager.getClassDirPath(comment.root, comment.className)}/${comment.propertyName}`;
-		if(fs.existsSync(commentPath))
+		
+		action = CommentAction.Create;
+		const exists = fs.existsSync(commentPath);
+		if(exists)
 			action = CommentAction.Update;
+		if(exists && comment.text.length === 0)
+			action = CommentAction.Remove;
 
 		const queueComment: IQueueComment = Object.assign(comment, { action }) as IQueueComment;
 
@@ -97,16 +102,18 @@ class CommentsManager {
 		const commentPath = `${classDirPath}/${comment.propertyName}`;
 
 		let status = CommentUpdateStatus.Created;
-
-		if (fs.existsSync(commentPath))
+		const exists = fs.existsSync(commentPath);
+		if(exists)
 			status = CommentUpdateStatus.Updated;
+		if(exists && comment.text.length === 0)
+			status = CommentUpdateStatus.Deleted;
 
 		await fs.promises.writeFile(commentPath, commentContent, { encoding: 'utf8' });
 
 		return { commentPath, status };
 	}
 
-	public static async create(comment: IComment): Promise<CommentUpdateStatus> {
+	public static async update(comment: IComment): Promise<CommentUpdateStatus> {
 		if(CommentsManager.isSaving()) {
 			CommentsManager.enqueueComment(comment, CommentAction.Create);
 			return CommentUpdateStatus.Enqueued;
@@ -115,7 +122,21 @@ class CommentsManager {
 		CommentsManager.beginSave();
 		try {
 			const { commentPath, status } = await CommentsManager.createOrUpdateCommentFile(comment);
-			const queueComment: IQueueComment = Object.assign(comment, { action: (status === CommentUpdateStatus.Created ? CommentAction.Create : CommentAction.Update) }) as IQueueComment;
+			let action = CommentAction.Create;
+
+			switch(status) {
+			case CommentUpdateStatus.Created:
+				action = CommentAction.Create;
+				break;
+			case CommentUpdateStatus.Updated:
+				action = CommentAction.Update;
+				break;
+			case CommentUpdateStatus.Deleted:
+				action = CommentAction.Remove;
+				break;
+			}
+
+			const queueComment: IQueueComment = Object.assign(comment, { action: action }) as IQueueComment;
 			await CommentsManager.commit([queueComment], [commentPath]);
 			CommentsManager.endSave();
 			return status;
@@ -190,7 +211,8 @@ class CommentsManager {
 			const message = `"${comment.author} ${comment.action === CommentAction.Create ? 'created' : (comment.action === CommentAction.Update ? 'updated' : 'deleted')} a comment for ${comment.root}/${comment.className}:${comment.propertyName}"`;
 			commitMessages.push(message);
 
-			await execAsync(`git ${comment.action === CommentAction.Create || comment.action === CommentAction.Update ? 'add' : 'rm'} ${commentPath}`, { encoding: 'utf8', cwd: CommentsManager.CommentsFSRoot });
+			//await execAsync(`git ${comment.action === CommentAction.Create || comment.action === CommentAction.Update ? 'add' : 'rm'} ${commentPath}`, { encoding: 'utf8', cwd: CommentsManager.CommentsFSRoot });
+			await execAsync(`git add ${commentPath}`, { encoding: 'utf8', cwd: CommentsManager.CommentsFSRoot });
 		}
 
 		await execAsync(`git commit -m ${commitMessages.join(' -m ')}`, { encoding: 'utf8', cwd: CommentsManager.CommentsFSRoot });
@@ -213,7 +235,7 @@ class CommentsManager {
 				const comment = queueItem.comment;
 				const commentPath = queueItem.commentPath;
 
-				switch(comment.action) {
+				/*switch(comment.action) {
 				case CommentAction.Create:
 				case CommentAction.Update:
 					await CommentsManager.createOrUpdateCommentFile(comment);
@@ -221,7 +243,9 @@ class CommentsManager {
 				case CommentAction.Remove:
 					await CommentsManager.deleteCommentFile(comment.root, comment.className, comment.propertyName);
 					break;
-				}
+				}*/
+
+				await CommentsManager.createOrUpdateCommentFile(comment);
 
 				comments.push(comment);
 				commentsPaths.push(commentPath);

+ 8 - 12
src/routes/PostUpdateComment.ts

@@ -24,18 +24,14 @@ class PostUpdateComment extends Route {
 			return;
 		}
 
-		if(comment.length === 0) {
-			CommentsManager.delete(root, className, propertyName, session.login!);
-		} else {
-			CommentsManager.create({
-				root: root,
-				className: className,
-				propertyName: propertyName,
-				text: comment,
-				timestamp: new Date().getTime(),
-				author: session.login!
-			});			
-		}
+		CommentsManager.update({
+			root: root,
+			className: className,
+			propertyName: propertyName,
+			text: comment,
+			timestamp: new Date().getTime(),
+			author: session.login!
+		});
 
 		res.status(StatusCodes.ACCEPTED).send('ENQUEUED');
 	};

+ 16 - 3
static/page/class/script.js

@@ -131,7 +131,7 @@ class ClassPage {
 			[ClassPage.TabNames.MixedIn]:    DOM.get('.content-tab#mixedin')
 		};
 
-		this.documented = Object.keys(Comments).length;
+		this.documented = Object.keys(Comments).filter((key) => { return Comments[key].text.length > 0; }).length;
 		this.documentable = 0;
 		this.inheritedCommentsQuery = {};
 		this.inheritedCommentsFields = {};
@@ -972,18 +972,31 @@ class ClassPage {
 		case 'create':
 			this.documented++;
 			this.renderDocumentedPercentage();
-			propertyItem.append(this.createCommentDateElement(changedComment.timestamp));
+
+			Comments[changedComment.propertyName] = changedComment;
+
+			propertyItem.getFirstChild('.property-item-comment').append(this.createCommentDateElement(changedComment.timestamp));
 			break;
 		case 'update':
+			if(Comments[changedComment.propertyName].text.length === 0) {
+				this.documented++;
+				this.renderDocumentedPercentage();
+			}
+			
+			Comments[changedComment.propertyName].text = changedComment.text;
+
 			const dateElement = propertyItem.getFirstChild('.property-item-comment-date > .property-item-comment-date-date');
 			if(dateElement) {
 				dateElement.setInnerHTML(CDUtils.dateFormatUTC(changedComment.timestamp, 3, 'D.M.Y, H:I:S'));
 			} else {
-				propertyItem.append(this.createCommentDateElement(changedComment.timestamp));
+				propertyItem.getFirstChild('.property-item-comment').append(this.createCommentDateElement(changedComment.timestamp));
 			}
 			break;
 		case 'remove':
 			this.documented--;
+			if(Comments[changedComment.propertyName])
+				Comments[changedComment.propertyName].text = '';
+
 			this.renderDocumentedPercentage();
 			propertyItem.getFirstChild('.property-item-comment-date').remove();
 			break;