feat: comment collapse event (#7949)

* feat: define comment collapse event

* feat: add firing collapse events

* chore: add round-trip test

* chore: add tests for firing collapse events

* chore: format
This commit is contained in:
Beka Westberg
2024-04-04 22:48:34 +00:00
committed by GitHub
parent c0e6e6745f
commit fd177358ea
7 changed files with 195 additions and 0 deletions

View File

@@ -82,6 +82,15 @@ export class WorkspaceComment {
}
}
/** Fires a comment collapse event. */
private fireCollapseEvent(newCollapsed: boolean) {
if (eventUtils.isEnabled()) {
eventUtils.fire(
new (eventUtils.get(eventUtils.COMMENT_COLLAPSE))(this, newCollapsed),
);
}
}
/** Sets the text of the comment. */
setText(text: string) {
const oldText = this.text;
@@ -107,6 +116,7 @@ export class WorkspaceComment {
/** Sets whether the comment is collapsed or not. */
setCollapsed(collapsed: boolean) {
this.collapsed = collapsed;
this.fireCollapseEvent(collapsed);
}
/** Returns whether the comment is collapsed or not. */

View File

@@ -24,6 +24,10 @@ import {CommentChange, CommentChangeJson} from './events_comment_change.js';
import {CommentCreate, CommentCreateJson} from './events_comment_create.js';
import {CommentDelete} from './events_comment_delete.js';
import {CommentMove, CommentMoveJson} from './events_comment_move.js';
import {
CommentCollapse,
CommentCollapseJson,
} from './events_comment_collapse.js';
import {MarkerMove, MarkerMoveJson} from './events_marker_move.js';
import {Selected, SelectedJson} from './events_selected.js';
import {ThemeChange, ThemeChangeJson} from './events_theme_change.js';
@@ -73,6 +77,8 @@ export {CommentCreateJson};
export {CommentDelete};
export {CommentMove};
export {CommentMoveJson};
export {CommentCollapse};
export {CommentCollapseJson};
export {FinishedLoading};
export {MarkerMove};
export {MarkerMoveJson};

View File

@@ -0,0 +1,103 @@
/**
* @license
* Copyright 2024 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import * as registry from '../registry.js';
import {WorkspaceComment} from '../comments/workspace_comment.js';
import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
import type {Workspace} from '../workspace.js';
export class CommentCollapse extends CommentBase {
override type = eventUtils.COMMENT_COLLAPSE;
constructor(
comment?: WorkspaceComment,
public newCollapsed?: boolean,
) {
super(comment);
if (!comment) {
return; // Blank event to be populated by fromJson.
}
}
/**
* Encode the event as JSON.
*
* @returns JSON representation.
*/
override toJson(): CommentCollapseJson {
const json = super.toJson() as CommentCollapseJson;
if (this.newCollapsed === undefined) {
throw new Error(
'The new collapse value undefined. Either call recordNew, or ' +
'call fromJson',
);
}
json['newCollapsed'] = this.newCollapsed;
return json;
}
/**
* Deserializes the JSON event.
*
* @param event The event to append new properties to. Should be a subclass
* of CommentCollapse, but we can't specify that due to the fact that
* parameters to static methods in subclasses must be supertypes of
* parameters to static methods in superclasses.
* @internal
*/
static fromJson(
json: CommentCollapseJson,
workspace: Workspace,
event?: any,
): CommentCollapse {
const newEvent = super.fromJson(
json,
workspace,
event ?? new CommentCollapse(),
) as CommentCollapse;
newEvent.newCollapsed = json.newCollapsed;
return newEvent;
}
/**
* Run a collapse event.
*
* @param forward True if run forward, false if run backward (undo).
*/
override run(forward: boolean) {
const workspace = this.getEventWorkspace_();
if (!this.commentId) {
throw new Error(
'The comment ID is undefined. Either pass a comment to ' +
'the constructor, or call fromJson',
);
}
// TODO: Remove cast when we update getCommentById.
const comment = workspace.getCommentById(
this.commentId,
) as unknown as WorkspaceComment;
if (!comment) {
console.warn(
"Can't collapse or uncollapse non-existent comment: " + this.commentId,
);
return;
}
comment.setCollapsed(forward ? !!this.newCollapsed : !this.newCollapsed);
}
}
export interface CommentCollapseJson extends CommentBaseJson {
newCollapsed: boolean;
}
registry.register(
registry.Type.EVENT,
eventUtils.COMMENT_COLLAPSE,
CommentCollapse,
);

View File

@@ -180,6 +180,9 @@ export const COMMENT_CHANGE = 'comment_change';
*/
export const COMMENT_MOVE = 'comment_move';
/** Type of event that moves a comment. */
export const COMMENT_COLLAPSE = 'comment_collapse';
/**
* Name of event that records a workspace load.
*/