Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: serializing disabled interactions #6847

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions core/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,15 @@ export class Block implements IASTNodeLocation, IDeletable {
!this.workspace.options.readOnly;
}

/**
* Return whether this block's own deletable property is true or false.
*
* @returns True if the block's deletable property is true, false otherwise.
*/
isOwnDeletable(): boolean {
return this.deletable_;
}

/**
* Set whether this block is deletable or not.
*
Expand All @@ -808,12 +817,23 @@ export class Block implements IASTNodeLocation, IDeletable {
* Get whether this block is movable or not.
*
* @returns True if movable.
* @internal
*/
isMovable(): boolean {
return this.movable_ && !this.isShadow_ && !this.isDeadOrDying() &&
!this.workspace.options.readOnly;
}

/**
* Return whether this block's own movable property is true or false.
*
* @returns True if the block's movable property is true, false otherwise.
* @internal
*/
isOwnMovable(): boolean {
return this.movable_;
}

/**
* Set whether this block is movable or not.
*
Expand Down Expand Up @@ -882,12 +902,22 @@ export class Block implements IASTNodeLocation, IDeletable {
* Get whether this block is editable or not.
*
* @returns True if editable.
* @internal
*/
isEditable(): boolean {
return this.editable_ && !this.isDeadOrDying() &&
!this.workspace.options.readOnly;
}

/**
* Return whether this block's own editable property is true or false.
*
* @returns True if the block's editable property is true, false otherwise.
*/
isOwnEditable(): boolean {
return this.editable_;
}

/**
* Set whether this block is editable or not.
*
Expand Down
21 changes: 21 additions & 0 deletions core/serialization/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export interface State {
x?: number;
y?: number;
collapsed?: boolean;
deletable?: boolean;
movable?: boolean;
editable?: boolean;
enabled?: boolean;
inline?: boolean;
data?: string;
Expand Down Expand Up @@ -142,6 +145,15 @@ function saveAttributes(block: Block, state: State) {
if (!block.isEnabled()) {
state['enabled'] = false;
}
if (!block.isOwnDeletable()) {
state['deletable'] = false;
}
if (!block.isOwnMovable()) {
state['movable'] = false;
}
if (!block.isOwnEditable()) {
state['editable'] = false;
}
if (block.inputsInline !== undefined &&
block.inputsInline !== block.inputsInlineDefault) {
state['inline'] = block.inputsInline;
Expand Down Expand Up @@ -437,6 +449,15 @@ function loadAttributes(block: Block, state: State) {
if (state['collapsed']) {
block.setCollapsed(true);
}
if (state['deletable'] === false) {
block.setDeletable(false);
}
if (state['movable'] === false) {
block.setMovable(false);
}
if (state['editable'] === false) {
block.setEditable(false);
}
if (state['enabled'] === false) {
block.setEnabled(false);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/mocha/serializer_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,25 @@ Serializer.Attributes.Disabled = new SerializerTestCase('Disabled',
'<xml xmlns="https://developers.google.com/blockly/xml">' +
'<block type="logic_negate" id="id******************" disabled="true" x="42" y="42"></block>' +
'</xml>');
Serializer.Attributes.NotDeletable = new SerializerTestCase('Deletable',
'<xml xmlns="https://developers.google.com/blockly/xml">' +
'<block type="logic_negate" id="id******************" deletable="false" x="42" y="42"></block>' +
'</xml>');
Serializer.Attributes.NotMovable = new SerializerTestCase('Movable',
'<xml xmlns="https://developers.google.com/blockly/xml">' +
'<block type="logic_negate" id="id******************" movable="false" x="42" y="42"></block>' +
'</xml>');
Serializer.Attributes.NotEditable = new SerializerTestCase('Editable',
'<xml xmlns="https://developers.google.com/blockly/xml">' +
'<block type="logic_negate" id="id******************" editable="false" x="42" y="42"></block>' +
'</xml>');
Serializer.Attributes.testCases = [
Serializer.Attributes.Basic,
Serializer.Attributes.Collapsed,
Serializer.Attributes.Disabled,
Serializer.Attributes.NotDeletable,
Serializer.Attributes.NotMovable,
Serializer.Attributes.NotEditable,
];

Serializer.Attributes.Inline = new SerializerTestSuite('Inline');
Expand Down