UNPKG

@senx/discovery-code

Version:

Discovery Code Editor

100 lines (99 loc) 3.85 kB
/* * Copyright 2020-2022 SenX S.A.S. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { v4 } from "uuid"; export function commentReducer(event, state) { const dirtyLineNumbers = new Set(); const deletedCommentIds = new Set(); const dirtyCommentIds = new Set(); let comments = Object.assign({}, state.comments); switch (event.type) { case 'edit': const parent = comments[event.targetId]; if (!parent) { break; } const edit = { comment: Object.assign(Object.assign({}, parent.comment), { author: event.createdBy, dt: event.createdAt, text: event.text }), history: parent.history.concat(parent.comment), }; dirtyLineNumbers.add(edit.comment.lineNumber); console.debug('edit', event); comments[event.targetId] = edit; break; case 'delete': const selected = comments[event.targetId]; if (!selected) { break; } delete comments[event.targetId]; deletedCommentIds.add(selected.comment.id); dirtyLineNumbers.add(selected.comment.lineNumber); console.debug('delete', event); break; case 'create': if (!comments[event.id]) { comments[event.id] = new ReviewCommentState({ author: event.createdBy, dt: event.createdAt, id: event.id, lineNumber: event.lineNumber, selection: event.selection, text: event.text, parentId: event.targetId, status: ReviewCommentStatus.active, }); console.debug('insert', event); dirtyLineNumbers.add(event.lineNumber); } break; } if (dirtyLineNumbers.size) { for (const cs of Object.values(state.comments)) { if (dirtyLineNumbers.has(cs.comment.lineNumber)) { dirtyCommentIds.add(cs.comment.id); } } } return { comments, dirtyCommentIds, deletedCommentIds }; } export class ReviewCommentState { constructor(comment) { this.comment = comment; this.history = [comment]; } } export var ReviewCommentRenderState; (function (ReviewCommentRenderState) { ReviewCommentRenderState[ReviewCommentRenderState["dirty"] = 1] = "dirty"; ReviewCommentRenderState[ReviewCommentRenderState["hidden"] = 2] = "hidden"; ReviewCommentRenderState[ReviewCommentRenderState["normal"] = 3] = "normal"; })(ReviewCommentRenderState || (ReviewCommentRenderState = {})); export var ReviewCommentStatus; (function (ReviewCommentStatus) { ReviewCommentStatus[ReviewCommentStatus["active"] = 1] = "active"; ReviewCommentStatus[ReviewCommentStatus["deleted"] = 2] = "deleted"; ReviewCommentStatus[ReviewCommentStatus["edit"] = 3] = "edit"; })(ReviewCommentStatus || (ReviewCommentStatus = {})); export function reduceComments(actions, state = null) { state = state || { comments: {} }; for (const a of actions) { if (!a.id) { a.id = v4(); } state = commentReducer(a, state); } return state; }