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:
Aaron Dodson
2024-06-27 11:11:45 -07:00
committed by GitHub
parent b0b7d78ad0
commit 989c91f626
6 changed files with 158 additions and 5 deletions

View File

@@ -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);
}
}