Skip to content

Commit

Permalink
fix: comment create and delete events (#7945)
Browse files Browse the repository at this point in the history
* chore: switch events to use new comment class

* fix: switch create and delete events to use JSON

* work on getting new comments to fire events

* chore: fixup tests

* chore: rename workspace comment test to comment view test

* chore: add tests for firing events

* chore: remove TODO
  • Loading branch information
BeksOmega authored Apr 1, 2024
1 parent 9effba5 commit 63eb4ec
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 225 deletions.
16 changes: 15 additions & 1 deletion core/comments/workspace_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {Workspace} from '../workspace.js';
import {Size} from '../utils/size.js';
import {Coordinate} from '../utils/coordinate.js';
import * as idGenerator from '../utils/idgenerator.js';
import * as eventUtils from '../events/utils.js';

export class WorkspaceComment {
/** The unique identifier for this comment. */
Expand Down Expand Up @@ -56,7 +57,19 @@ export class WorkspaceComment {
// TODO: File an issue to remove this once everything is migrated.
workspace.addTopComment(this as AnyDuringMigration);

// TODO(7909): Fire events.
this.fireCreateEvent();
}

private fireCreateEvent() {
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_CREATE))(this));
}
}

private fireDeleteEvent() {
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_DELETE))(this));
}
}

/** Sets the text of the comment. */
Expand Down Expand Up @@ -165,6 +178,7 @@ export class WorkspaceComment {
/** Disposes of this comment. */
dispose() {
this.disposing = true;
this.fireDeleteEvent();
this.workspace.removeTopComment(this as AnyDuringMigration);
this.disposed = true;
}
Expand Down
17 changes: 6 additions & 11 deletions core/events/events_comment_base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
*/
// Former goog.module ID: Blockly.Events.CommentBase

import * as utilsXml from '../utils/xml.js';
import type {WorkspaceComment} from '../workspace_comment.js';
import * as Xml from '../xml.js';

import type {WorkspaceComment} from '../comments/workspace_comment.js';
import * as comments from '../serialization/workspace_comments.js';
import {
Abstract as AbstractEvent,
AbstractEventJson,
Expand Down Expand Up @@ -102,12 +100,10 @@ export class CommentBase extends AbstractEvent {
) {
const workspace = event.getEventWorkspace_();
if (create) {
const xmlElement = utilsXml.createElement('xml');
if (!event.xml) {
throw new Error('Ecountered a comment event without proper xml');
if (!event.json) {
throw new Error('Encountered a comment event without proper json');
}
xmlElement.appendChild(event.xml);
Xml.domToWorkspace(xmlElement, workspace);
comments.append(event.json, workspace);
} else {
if (!event.commentId) {
throw new Error(
Expand All @@ -119,8 +115,7 @@ export class CommentBase extends AbstractEvent {
if (comment) {
comment.dispose();
} else {
// Only complain about root-level block.
console.warn("Can't uncreate non-existent comment: " + event.commentId);
console.warn("Can't delete non-existent comment: " + event.commentId);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/events/events_comment_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Former goog.module ID: Blockly.Events.CommentChange

import * as registry from '../registry.js';
import type {WorkspaceComment} from '../workspace_comment.js';
import type {WorkspaceComment} from '../comments/workspace_comment.js';

import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
Expand Down
23 changes: 18 additions & 5 deletions core/events/events_comment_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// Former goog.module ID: Blockly.Events.CommentCreate

import * as registry from '../registry.js';
import type {WorkspaceComment} from '../workspace_comment.js';
import type {WorkspaceComment} from '../comments/workspace_comment.js';
import * as comments from '../serialization/workspace_comments.js';
import * as utilsXml from '../utils/xml.js';
import * as Xml from '../xml.js';

import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
import type {Workspace} from '../workspace.js';
Expand All @@ -29,6 +29,9 @@ export class CommentCreate extends CommentBase {
/** The XML representation of the created workspace comment. */
xml?: Element | DocumentFragment;

/** The JSON representation of the created workspace comment. */
json?: comments.State;

/**
* @param opt_comment The created comment.
* Undefined for a blank event.
Expand All @@ -37,10 +40,11 @@ export class CommentCreate extends CommentBase {
super(opt_comment);

if (!opt_comment) {
return;
return; // Blank event to be populated by fromJson.
}
// Blank event to be populated by fromJson.
this.xml = opt_comment.toXmlWithXY();

this.xml = Xml.saveWorkspaceComment(opt_comment);
this.json = comments.save(opt_comment, {addCoordinates: true});
}

// TODO (#1266): "Full" and "minimal" serialization.
Expand All @@ -57,7 +61,14 @@ export class CommentCreate extends CommentBase {
'the constructor, or call fromJson',
);
}
if (!this.json) {
throw new Error(
'The comment JSON is undefined. Either pass a block to ' +
'the constructor, or call fromJson',
);
}
json['xml'] = Xml.domToText(this.xml);
json['json'] = this.json;
return json;
}

Expand All @@ -81,6 +92,7 @@ export class CommentCreate extends CommentBase {
event ?? new CommentCreate(),
) as CommentCreate;
newEvent.xml = utilsXml.textToDom(json['xml']);
newEvent.json = json['json'];
return newEvent;
}

Expand All @@ -96,6 +108,7 @@ export class CommentCreate extends CommentBase {

export interface CommentCreateJson extends CommentBaseJson {
xml: string;
json: object;
}

registry.register(
Expand Down
19 changes: 16 additions & 3 deletions core/events/events_comment_delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// Former goog.module ID: Blockly.Events.CommentDelete

import * as registry from '../registry.js';
import type {WorkspaceComment} from '../workspace_comment.js';

import type {WorkspaceComment} from '../comments/workspace_comment.js';
import * as comments from '../serialization/workspace_comments.js';
import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
import * as utilsXml from '../utils/xml.js';
Expand All @@ -29,6 +29,9 @@ export class CommentDelete extends CommentBase {
/** The XML representation of the deleted workspace comment. */
xml?: Element;

/** The JSON representation of the created workspace comment. */
json?: comments.State;

/**
* @param opt_comment The deleted comment.
* Undefined for a blank event.
Expand All @@ -40,7 +43,8 @@ export class CommentDelete extends CommentBase {
return; // Blank event to be populated by fromJson.
}

this.xml = opt_comment.toXmlWithXY();
this.xml = Xml.saveWorkspaceComment(opt_comment);
this.json = comments.save(opt_comment, {addCoordinates: true});
}

/**
Expand All @@ -65,7 +69,14 @@ export class CommentDelete extends CommentBase {
'the constructor, or call fromJson',
);
}
if (!this.json) {
throw new Error(
'The comment JSON is undefined. Either pass a block to ' +
'the constructor, or call fromJson',
);
}
json['xml'] = Xml.domToText(this.xml);
json['json'] = this.json;
return json;
}

Expand All @@ -89,12 +100,14 @@ export class CommentDelete extends CommentBase {
event ?? new CommentDelete(),
) as CommentDelete;
newEvent.xml = utilsXml.textToDom(json['xml']);
newEvent.json = json['json'];
return newEvent;
}
}

export interface CommentDeleteJson extends CommentBaseJson {
xml: string;
json: object;
}

registry.register(
Expand Down
2 changes: 1 addition & 1 deletion core/events/events_comment_move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import * as registry from '../registry.js';
import {Coordinate} from '../utils/coordinate.js';
import type {WorkspaceComment} from '../workspace_comment.js';
import type {WorkspaceComment} from '../comments/workspace_comment.js';

import {CommentBase, CommentBaseJson} from './events_comment_base.js';
import * as eventUtils from './utils.js';
Expand Down
4 changes: 2 additions & 2 deletions core/xml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function workspaceToDom(workspace: Workspace, skipId = false): Element {
}

/** Serializes the given workspace comment to XML. */
function saveWorkspaceComment(
export function saveWorkspaceComment(
comment: WorkspaceComment,
skipId = false,
): Element {
Expand Down Expand Up @@ -495,7 +495,7 @@ export function domToWorkspace(xml: Element, workspace: Workspace): string[] {
}

/** Deserializes the given comment state into the given workspace. */
function loadWorkspaceComment(
export function loadWorkspaceComment(
elem: Element,
workspace: Workspace,
): WorkspaceComment {
Expand Down
Loading

0 comments on commit 63eb4ec

Please sign in to comment.