Skip to content

Commit 405f7da

Browse files
authored
fix: Fix positioning of pasted blocks and comments in RTL. (#9302)
* fix: Fix positioning of pasted blocks in RTL. * fix: Clean up after temporarily making the workspace RTL. * fix: Remove .only. * fix: Fix positioning of pasted comments in RTL. * fix: Fix positioning of text preview on collapsed comments in RTL.
1 parent b5343f3 commit 405f7da

4 files changed

Lines changed: 59 additions & 2 deletions

File tree

core/clipboard/block_paster.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export function moveBlockToNotConflict(
8383
block: BlockSvg,
8484
originalPosition: Coordinate,
8585
) {
86+
if (block.workspace.RTL) {
87+
originalPosition.x = block.workspace.getWidth() - originalPosition.x;
88+
}
8689
const workspace = block.workspace;
8790
const snapRadius = config.snapRadius;
8891
const bumpOffset = Coordinate.difference(

core/comments/comment_view.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,10 @@ export class CommentView implements IRenderedElement {
368368

369369
const textPreviewWidth =
370370
size.width - foldoutSize.getWidth() - deleteSize.getWidth();
371-
this.textPreview.setAttribute('x', `${foldoutSize.getWidth()}`);
371+
this.textPreview.setAttribute(
372+
'x',
373+
`${(this.workspace.RTL ? -1 : 1) * foldoutSize.getWidth()}`,
374+
);
372375
this.textPreview.setAttribute(
373376
'y',
374377
`${textPreviewMargin + textPreviewSize.height / 2}`,

core/xml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export function saveWorkspaceComment(
6868
if (!skipId) elem.setAttribute('id', comment.id);
6969

7070
const workspace = comment.workspace;
71-
const loc = comment.getRelativeToSurfaceXY();
71+
const loc = comment.getRelativeToSurfaceXY().clone();
7272
loc.x = workspace.RTL ? workspace.getWidth() - loc.x : loc.x;
7373
elem.setAttribute('x', `${loc.x}`);
7474
elem.setAttribute('y', `${loc.y}`);

tests/mocha/clipboard_test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,34 @@ suite('Clipboard', function () {
157157
);
158158
});
159159

160+
test('pasted blocks are bumped to not overlap in RTL', function () {
161+
this.workspace.dispose();
162+
this.workspace = Blockly.inject('blocklyDiv', {rtl: true});
163+
const block = Blockly.serialization.blocks.append(
164+
{
165+
'type': 'controls_if',
166+
'x': 38,
167+
'y': 13,
168+
},
169+
this.workspace,
170+
);
171+
const data = block.toCopyData();
172+
173+
const newBlock = Blockly.clipboard.paste(data, this.workspace);
174+
const oldBlockXY = block.getRelativeToSurfaceXY();
175+
assert.deepEqual(
176+
newBlock.getRelativeToSurfaceXY(),
177+
new Blockly.utils.Coordinate(
178+
oldBlockXY.x - Blockly.config.snapRadius,
179+
oldBlockXY.y + Blockly.config.snapRadius * 2,
180+
),
181+
);
182+
183+
// Restore an LTR workspace.
184+
this.workspace.dispose();
185+
this.workspace = Blockly.inject('blocklyDiv');
186+
});
187+
160188
test('pasted blocks are bumped to be outside the connection snap radius', function () {
161189
Blockly.serialization.workspaces.load(
162190
{
@@ -208,5 +236,28 @@ suite('Clipboard', function () {
208236
new Blockly.utils.Coordinate(40, 40),
209237
);
210238
});
239+
240+
test('pasted comments are bumped to not overlap in RTL', function () {
241+
this.workspace.dispose();
242+
this.workspace = Blockly.inject('blocklyDiv', {rtl: true});
243+
Blockly.Xml.domToWorkspace(
244+
Blockly.utils.xml.textToDom(
245+
'<xml><comment id="test" x=10 y=10/></xml>',
246+
),
247+
this.workspace,
248+
);
249+
const comment = this.workspace.getTopComments(false)[0];
250+
const data = comment.toCopyData();
251+
252+
const newComment = Blockly.clipboard.paste(data, this.workspace);
253+
const oldCommentXY = comment.getRelativeToSurfaceXY();
254+
assert.deepEqual(
255+
newComment.getRelativeToSurfaceXY(),
256+
new Blockly.utils.Coordinate(oldCommentXY.x - 30, oldCommentXY.y + 30),
257+
);
258+
// Restore an LTR workspace.
259+
this.workspace.dispose();
260+
this.workspace = Blockly.inject('blocklyDiv');
261+
});
211262
});
212263
});

0 commit comments

Comments
 (0)