chore: delete old comments (#8038)

* chore!: delete old comment classes and references

* chore: PR comments
This commit is contained in:
Beka Westberg
2024-04-17 21:33:36 +00:00
committed by GitHub
parent a865744b7e
commit 7d8f88a4f1
8 changed files with 18 additions and 1575 deletions

View File

@@ -203,8 +203,6 @@ import * as VariablesDynamic from './variables_dynamic.js';
import * as WidgetDiv from './widgetdiv.js';
import {Workspace} from './workspace.js';
import {WorkspaceAudio} from './workspace_audio.js';
import {WorkspaceComment} from './workspace_comment.js';
import {WorkspaceCommentSvg} from './workspace_comment_svg.js';
import {WorkspaceDragger} from './workspace_dragger.js';
import {WorkspaceSvg} from './workspace_svg.js';
import * as Xml from './xml.js';
@@ -581,8 +579,6 @@ export {VariableModel};
export {VerticalFlyout};
export {Workspace};
export {WorkspaceAudio};
export {WorkspaceComment};
export {WorkspaceCommentSvg};
export {WorkspaceDragger};
export {WorkspaceSvg};
export {ZoomControls};

View File

@@ -6,7 +6,7 @@
// Former goog.module ID: Blockly.bumpObjects
import {RenderedWorkspaceComment} from './comments.js';
import {RenderedWorkspaceComment} from './comments/rendered_workspace_comment.js';
import type {Abstract} from './events/events_abstract.js';
import type {BlockCreate} from './events/events_block_create.js';
import type {BlockMove} from './events/events_block_move.js';
@@ -165,7 +165,7 @@ function extractObjectFromEvent(
case eventUtils.COMMENT_MOVE:
object = workspace.getCommentById(
(e as CommentCreate | CommentMove).commentId!,
) as AnyDuringMigration as RenderedWorkspaceComment;
) as RenderedWorkspaceComment;
break;
}
return object;

View File

@@ -55,8 +55,7 @@ export class WorkspaceComment {
) {
this.id = id && !workspace.getCommentById(id) ? id : idGenerator.genUid();
// TODO: File an issue to remove this once everything is migrated.
workspace.addTopComment(this as AnyDuringMigration);
workspace.addTopComment(this);
this.fireCreateEvent();
}
@@ -209,7 +208,7 @@ export class WorkspaceComment {
dispose() {
this.disposing = true;
this.fireDeleteEvent();
this.workspace.removeTopComment(this as AnyDuringMigration);
this.workspace.removeTopComment(this);
this.disposed = true;
}

View File

@@ -30,7 +30,6 @@ import * as math from './utils/math.js';
import type * as toolbox from './utils/toolbox.js';
import {VariableMap} from './variable_map.js';
import type {VariableModel} from './variable_model.js';
import type {WorkspaceComment as OldWorkspaceComment} from './workspace_comment.js';
import {WorkspaceComment} from './comments/workspace_comment.js';
import {IProcedureMap} from './interfaces/i_procedure_map.js';
import {ObservableProcedureMap} from './observable_procedure_map.js';
@@ -101,8 +100,8 @@ export class Workspace implements IASTNodeLocation {
connectionChecker: IConnectionChecker;
private readonly topBlocks: Block[] = [];
private readonly topComments: OldWorkspaceComment[] = [];
private readonly commentDB = new Map<string, OldWorkspaceComment>();
private readonly topComments: WorkspaceComment[] = [];
private readonly commentDB = new Map<string, WorkspaceComment>();
private readonly listeners: Function[] = [];
protected undoStack_: Abstract[] = [];
protected redoStack_: Abstract[] = [];
@@ -169,8 +168,8 @@ export class Workspace implements IASTNodeLocation {
* a's index.
*/
private sortObjects_(
a: Block | OldWorkspaceComment,
b: Block | OldWorkspaceComment,
a: Block | WorkspaceComment,
b: Block | WorkspaceComment,
): number {
const offset =
Math.sin(math.toRadians(Workspace.SCAN_ANGLE)) * (this.RTL ? -1 : 1);
@@ -267,7 +266,7 @@ export class Workspace implements IASTNodeLocation {
* @param comment comment to add.
* @internal
*/
addTopComment(comment: OldWorkspaceComment) {
addTopComment(comment: WorkspaceComment) {
this.topComments.push(comment);
// Note: If the comment database starts to hold block comments, this may
@@ -288,7 +287,7 @@ export class Workspace implements IASTNodeLocation {
* @param comment comment to remove.
* @internal
*/
removeTopComment(comment: OldWorkspaceComment) {
removeTopComment(comment: WorkspaceComment) {
if (!arrayUtils.removeElem(this.topComments, comment)) {
throw Error(
"Comment not present in workspace's list of top-most " + 'comments.',
@@ -307,9 +306,9 @@ export class Workspace implements IASTNodeLocation {
* @returns The top-level comment objects.
* @internal
*/
getTopComments(ordered = false): OldWorkspaceComment[] {
getTopComments(ordered = false): WorkspaceComment[] {
// Copy the topComments list.
const comments = new Array<OldWorkspaceComment>().concat(this.topComments);
const comments = new Array<WorkspaceComment>().concat(this.topComments);
if (ordered && comments.length > 1) {
comments.sort(this.sortObjects_.bind(this));
}
@@ -751,7 +750,7 @@ export class Workspace implements IASTNodeLocation {
* @returns The sought after comment, or null if not found.
* @internal
*/
getCommentById(id: string): OldWorkspaceComment | null {
getCommentById(id: string): WorkspaceComment | null {
return this.commentDB.get(id) ?? null;
}

View File

@@ -1,397 +0,0 @@
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* Object representing a code comment on the workspace.
*
* @class
*/
// Former goog.module ID: Blockly.WorkspaceComment
import type {CommentMove} from './events/events_comment_move.js';
import * as eventUtils from './events/utils.js';
import {Coordinate} from './utils/coordinate.js';
import * as idGenerator from './utils/idgenerator.js';
import * as xml from './utils/xml.js';
import type {Workspace} from './workspace.js';
/**
* Class for a workspace comment.
*/
export class WorkspaceComment {
id: string;
protected xy_: Coordinate;
protected height_: number;
protected width_: number;
protected RTL: boolean;
private deletable = true;
private movable = true;
private editable = true;
protected content_: string;
/** Whether this comment has been disposed. */
protected disposed_ = false;
/** @internal */
isComment = true;
/**
* @param workspace The block's workspace.
* @param content The content of this workspace comment.
* @param height Height of the comment.
* @param width Width of the comment.
* @param opt_id Optional ID. Use this ID if provided, otherwise create a new
* ID.
*/
constructor(
public workspace: Workspace,
content: string,
height: number,
width: number,
opt_id?: string,
) {
this.id =
opt_id && !workspace.getCommentById(opt_id)
? opt_id
: idGenerator.genUid();
workspace.addTopComment(this);
/**
* The comment's position in workspace units. (0, 0) is at the workspace's
* origin; scale does not change this value.
*/
this.xy_ = new Coordinate(0, 0);
/**
* The comment's height in workspace units. Scale does not change this
* value.
*/
this.height_ = height;
/**
* The comment's width in workspace units. Scale does not change this
* value.
*/
this.width_ = width;
this.RTL = workspace.RTL;
this.content_ = content;
WorkspaceComment.fireCreateEvent(this);
}
/**
* Dispose of this comment.
*
* @internal
*/
dispose() {
if (this.disposed_) {
return;
}
if (eventUtils.isEnabled()) {
eventUtils.fire(new (eventUtils.get(eventUtils.COMMENT_DELETE))(this));
}
// Remove from the list of top comments and the comment database.
this.workspace.removeTopComment(this);
this.disposed_ = true;
}
// Height, width, x, and y are all stored on even non-rendered comments, to
// preserve state if you pass the contents through a headless workspace.
/**
* Get comment height.
*
* @returns Comment height.
* @internal
*/
getHeight(): number {
return this.height_;
}
/**
* Set comment height.
*
* @param height Comment height.
* @internal
*/
setHeight(height: number) {
this.height_ = height;
}
/**
* Get comment width.
*
* @returns Comment width.
* @internal
*/
getWidth(): number {
return this.width_;
}
/**
* Set comment width.
*
* @param width comment width.
* @internal
*/
setWidth(width: number) {
this.width_ = width;
}
/**
* Get stored location.
*
* @returns The comment's stored location.
* This is not valid if the comment is currently being dragged.
* @internal
*/
getRelativeToSurfaceXY(): Coordinate {
return new Coordinate(this.xy_.x, this.xy_.y);
}
/**
* Move a comment by a relative offset.
*
* @param dx Horizontal offset, in workspace units.
* @param dy Vertical offset, in workspace units.
* @internal
*/
moveBy(dx: number, dy: number) {
const event = new (eventUtils.get(eventUtils.COMMENT_MOVE))(
this,
) as CommentMove;
this.xy_.translate(dx, dy);
event.recordNew();
eventUtils.fire(event);
}
/**
* Get whether this comment is deletable or not.
*
* @returns True if deletable.
* @internal
*/
isDeletable(): boolean {
return (
this.deletable && !(this.workspace && this.workspace.options.readOnly)
);
}
/**
* Set whether this comment is deletable or not.
*
* @param deletable True if deletable.
* @internal
*/
setDeletable(deletable: boolean) {
this.deletable = deletable;
}
/**
* Get whether this comment is movable or not.
*
* @returns True if movable.
* @internal
*/
isMovable(): boolean {
return this.movable && !(this.workspace && this.workspace.options.readOnly);
}
/**
* Set whether this comment is movable or not.
*
* @param movable True if movable.
* @internal
*/
setMovable(movable: boolean) {
this.movable = movable;
}
/**
* Get whether this comment is editable or not.
*
* @returns True if editable.
*/
isEditable(): boolean {
return (
this.editable && !(this.workspace && this.workspace.options.readOnly)
);
}
/**
* Set whether this comment is editable or not.
*
* @param editable True if editable.
*/
setEditable(editable: boolean) {
this.editable = editable;
}
/**
* Returns this comment's text.
*
* @returns Comment text.
* @internal
*/
getContent(): string {
return this.content_;
}
/**
* Set this comment's content.
*
* @param content Comment content.
* @internal
*/
setContent(content: string) {
if (this.content_ !== content) {
eventUtils.fire(
new (eventUtils.get(eventUtils.COMMENT_CHANGE))(
this,
this.content_,
content,
),
);
this.content_ = content;
}
}
/**
* Encode a comment subtree as XML with XY coordinates.
*
* @param opt_noId True if the encoder should skip the comment ID.
* @returns Tree of XML elements.
* @internal
*/
toXmlWithXY(opt_noId?: boolean): Element {
const element = this.toXml(opt_noId);
element.setAttribute('x', String(Math.round(this.xy_.x)));
element.setAttribute('y', String(Math.round(this.xy_.y)));
element.setAttribute('h', String(this.height_));
element.setAttribute('w', String(this.width_));
return element;
}
/**
* Encode a comment subtree as XML, but don't serialize the XY coordinates.
* This method avoids some expensive metrics-related calls that are made in
* toXmlWithXY().
*
* @param opt_noId True if the encoder should skip the comment ID.
* @returns Tree of XML elements.
* @internal
*/
toXml(opt_noId?: boolean): Element {
const commentElement = xml.createElement('comment');
if (!opt_noId) {
commentElement.id = this.id;
}
commentElement.textContent = this.getContent();
return commentElement;
}
/**
* Fire a create event for the given workspace comment, if comments are
* enabled.
*
* @param comment The comment that was just created.
* @internal
*/
static fireCreateEvent(comment: WorkspaceComment) {
if (eventUtils.isEnabled()) {
const existingGroup = eventUtils.getGroup();
if (!existingGroup) {
eventUtils.setGroup(true);
}
try {
eventUtils.fire(
new (eventUtils.get(eventUtils.COMMENT_CREATE))(comment),
);
} finally {
eventUtils.setGroup(existingGroup);
}
}
}
/**
* Decode an XML comment tag and create a comment on the workspace.
*
* @param xmlComment XML comment element.
* @param workspace The workspace.
* @returns The created workspace comment.
* @internal
*/
static fromXml(xmlComment: Element, workspace: Workspace): WorkspaceComment {
const info = WorkspaceComment.parseAttributes(xmlComment);
const comment = new WorkspaceComment(
workspace,
info.content,
info.h,
info.w,
info.id,
);
const xmlX = xmlComment.getAttribute('x');
const xmlY = xmlComment.getAttribute('y');
const commentX = xmlX ? parseInt(xmlX, 10) : NaN;
const commentY = xmlY ? parseInt(xmlY, 10) : NaN;
if (!isNaN(commentX) && !isNaN(commentY)) {
comment.moveBy(commentX, commentY);
}
WorkspaceComment.fireCreateEvent(comment);
return comment;
}
/**
* Decode an XML comment tag and return the results in an object.
*
* @param xml XML comment element.
* @returns An object containing the id, size, position, and comment string.
* @internal
*/
static parseAttributes(xml: Element): {
id: string;
w: number;
h: number;
x: number;
y: number;
content: string;
} {
const xmlH = xml.getAttribute('h');
const xmlW = xml.getAttribute('w');
const xmlX = xml.getAttribute('x');
const xmlY = xml.getAttribute('y');
const xmlId = xml.getAttribute('id');
if (!xmlId) {
throw new Error('No ID present in XML comment definition.');
}
return {
id: xmlId,
// The height of the comment in workspace units, or 100 if not specified.
h: xmlH ? parseInt(xmlH) : 100,
// The width of the comment in workspace units, or 100 if not specified.
w: xmlW ? parseInt(xmlW) : 100,
// The x position of the comment in workspace coordinates, or NaN if not
// specified in the XML.
x: xmlX ? parseInt(xmlX) : NaN,
// The y position of the comment in workspace coordinates, or NaN if not
// specified in the XML.
y: xmlY ? parseInt(xmlY) : NaN,
content: xml.textContent ?? '',
};
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -68,13 +68,12 @@ import * as VariablesDynamic from './variables_dynamic.js';
import * as WidgetDiv from './widgetdiv.js';
import {Workspace} from './workspace.js';
import {WorkspaceAudio} from './workspace_audio.js';
import {WorkspaceComment as OldWorkspaceComment} from './workspace_comment.js';
import {WorkspaceCommentSvg as OldWorkspaceCommentSvg} from './workspace_comment_svg.js';
import {WorkspaceComment} from './comments/workspace_comment.js';
import {ZoomControls} from './zoom_controls.js';
import {ContextMenuOption} from './contextmenu_registry.js';
import * as renderManagement from './render_management.js';
import {LayerManager} from './layer_manager.js';
import {RenderedWorkspaceComment} from './comments/rendered_workspace_comment.js';
/** Margin around the top/bottom/left/right after a zoomToFit call. */
const ZOOM_TO_FIT_MARGIN = 20;
@@ -2143,8 +2142,8 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg {
*
* @param comment comment to add.
*/
override addTopComment(comment: OldWorkspaceComment) {
this.addTopBoundedElement(comment as OldWorkspaceCommentSvg);
override addTopComment(comment: WorkspaceComment) {
this.addTopBoundedElement(comment as RenderedWorkspaceComment);
super.addTopComment(comment);
}
@@ -2153,8 +2152,8 @@ export class WorkspaceSvg extends Workspace implements IASTNodeLocationSvg {
*
* @param comment comment to remove.
*/
override removeTopComment(comment: OldWorkspaceComment) {
this.removeTopBoundedElement(comment as OldWorkspaceCommentSvg);
override removeTopComment(comment: WorkspaceComment) {
this.removeTopBoundedElement(comment as RenderedWorkspaceComment);
super.removeTopComment(comment);
}