mirror of
https://github.com/google/blockly.git
synced 2026-01-06 08:30:13 +01:00
feat!: Add support for preserving block comment locations. (#8231)
* feat: Add support for preserving block comment locations. * chore: format the tests.
This commit is contained in:
27
core/xml.ts
27
core/xml.ts
@@ -217,12 +217,24 @@ export function blockToDom(
|
||||
const comment = block.getIcon(IconType.COMMENT)!;
|
||||
const size = comment.getBubbleSize();
|
||||
const pinned = comment.bubbleIsVisible();
|
||||
const location = comment.getBubbleLocation();
|
||||
|
||||
const commentElement = utilsXml.createElement('comment');
|
||||
commentElement.appendChild(utilsXml.createTextNode(commentText));
|
||||
commentElement.setAttribute('pinned', `${pinned}`);
|
||||
commentElement.setAttribute('h', String(size.height));
|
||||
commentElement.setAttribute('w', String(size.width));
|
||||
commentElement.setAttribute('h', `${size.height}`);
|
||||
commentElement.setAttribute('w', `${size.width}`);
|
||||
if (location) {
|
||||
commentElement.setAttribute(
|
||||
'x',
|
||||
`${
|
||||
block.workspace.RTL
|
||||
? block.workspace.getWidth() - (location.x + size.width)
|
||||
: location.x
|
||||
}`,
|
||||
);
|
||||
commentElement.setAttribute('y', `${location.y}`);
|
||||
}
|
||||
|
||||
element.appendChild(commentElement);
|
||||
}
|
||||
@@ -795,6 +807,8 @@ function applyCommentTagNodes(xmlChildren: Element[], block: Block) {
|
||||
const pinned = xmlChild.getAttribute('pinned') === 'true';
|
||||
const width = parseInt(xmlChild.getAttribute('w') ?? '50', 10);
|
||||
const height = parseInt(xmlChild.getAttribute('h') ?? '50', 10);
|
||||
let x = parseInt(xmlChild.getAttribute('x') ?? '', 10);
|
||||
const y = parseInt(xmlChild.getAttribute('y') ?? '', 10);
|
||||
|
||||
block.setCommentText(text);
|
||||
const comment = block.getIcon(IconType.COMMENT)!;
|
||||
@@ -803,8 +817,15 @@ function applyCommentTagNodes(xmlChildren: Element[], block: Block) {
|
||||
}
|
||||
// Set the pinned state of the bubble.
|
||||
comment.setBubbleVisible(pinned);
|
||||
|
||||
// Actually show the bubble after the block has been rendered.
|
||||
setTimeout(() => comment.setBubbleVisible(pinned), 1);
|
||||
setTimeout(() => {
|
||||
if (!isNaN(x) && !isNaN(y)) {
|
||||
x = block.workspace.RTL ? block.workspace.getWidth() - (x + width) : x;
|
||||
comment.setBubbleLocation(new Coordinate(x, y));
|
||||
}
|
||||
comment.setBubbleVisible(pinned);
|
||||
}, 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user