mirror of
https://github.com/google/blockly.git
synced 2026-01-08 17:40:09 +01:00
fix: comment create and delete events (#7945)
* 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
This commit is contained in:
@@ -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. */
|
||||
@@ -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. */
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
@@ -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(
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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';
|
||||
@@ -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.
|
||||
@@ -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.
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -96,6 +108,7 @@ export class CommentCreate extends CommentBase {
|
||||
|
||||
export interface CommentCreateJson extends CommentBaseJson {
|
||||
xml: string;
|
||||
json: object;
|
||||
}
|
||||
|
||||
registry.register(
|
||||
|
||||
@@ -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';
|
||||
@@ -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.
|
||||
@@ -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});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user