Skip to content

Commit

Permalink
prevent empty string comments from persising saved offset data
Browse files Browse the repository at this point in the history
  • Loading branch information
riknoll committed Sep 9, 2024
1 parent 3cca149 commit b639441
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pxtblocks/fields/field_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,10 @@ export function setBlockDataForField(block: Blockly.Block, field: string, data:

export function getBlockDataForField(block: Blockly.Block, field: string) {
return getBlockData(block).fieldData[field];
}

export function deleteBlockDataForField(block: Blockly.Block, field: string) {
const blockData = getBlockData(block);
delete blockData.fieldData[field];
setBlockData(block, blockData);
}
21 changes: 20 additions & 1 deletion pxtblocks/plugins/comments/blockComment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as Blockly from "blockly";

import { TextInputBubble } from "./textinput_bubble";
import { getBlockDataForField, setBlockDataForField } from "../../fields";
import { deleteBlockDataForField, getBlockDataForField, setBlockDataForField } from "../../fields";

const eventUtils = Blockly.Events;

Expand Down Expand Up @@ -152,6 +152,19 @@ export class CommentIcon extends Blockly.icons.Icon {

/** Sets the text of this comment. Updates any bubbles if they are visible. */
setText(text: string) {
// Blockly comments are omitted from XML serialization if they're empty.
// In that case, they won't be present in the saved XML but any comment offset
// data that was previously saved will be since it's a part of the block's
// serialized data and not the comment's. In order to prevent that orphaned save
// data from persisting, we need to clear it when the user creates a new comment.

// If setText is called with the empty string while our text is already the
// empty string, that means that this comment is newly created and we can safely
// clear any pre-existing saved offset data.
if (!this.text && !text) {
this.clearSavedOffsetData();
}

const oldText = this.text;
eventUtils.fire(
new (eventUtils.get(eventUtils.BLOCK_CHANGE))(
Expand Down Expand Up @@ -321,6 +334,7 @@ export class CommentIcon extends Blockly.icons.Icon {
this.textInputBubble.setDeleteHandler(() => {
this.setBubbleVisible(false);
this.sourceBlock.setCommentText(null);
this.clearSavedOffsetData();
});
this.textInputBubble.setCollapseHandler(() => {
this.setBubbleVisible(false);
Expand Down Expand Up @@ -390,6 +404,11 @@ export class CommentIcon extends Blockly.icons.Icon {

return undefined;
}

private clearSavedOffsetData() {
deleteBlockDataForField(this.sourceBlock, COMMENT_OFFSET_X_FIELD_NAME);
deleteBlockDataForField(this.sourceBlock, COMMENT_OFFSET_Y_FIELD_NAME);
}
}

/** The save state format for a comment icon. */
Expand Down

0 comments on commit b639441

Please sign in to comment.