Skip to content

Commit

Permalink
[#70043] Copy comments on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Trzcin authored and mgielda committed Dec 10, 2024
1 parent 82e91ac commit d09cb47
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/comments/ycomments.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,4 +564,49 @@ export class YComments {
newLine: transaction.state.doc.lineAt(newPos).number,
};
}

encodeState() {
const positions = this.positions().syncedPositions.toJSON();
const suggestions = this.suggestions.toJSON();
const resolved = this.resolver().resolvedComments.toJSON();
const lineAuthors = {};
const text = {};
const allComments = [...this.comments.peek(), ...this.resolver().resolved()];
for (const { commentId } of allComments) {
const lineData = this.lineAuthors(commentId);
lineAuthors[commentId] = lineData.lineAuthors.toJSON();
const commentText = this.getTextForComment(commentId);
text[commentId] = commentText.toString();
}

return { positions, suggestions, resolved, lineAuthors, text };
}

applyState({ positions, suggestions, resolved, lineAuthors, text }) {
this.ydoc.transact(() => {
for (const id in positions) {
this.positions().syncedPositions.set(id, positions[id]);
}
for (const id in suggestions) {
this.suggestions.set(id, suggestions[id]);
}
for (const id in resolved) {
this.resolver().resolvedComments.set(id, resolved[id]);
}
for (const id in lineAuthors) {
const lineData = this.lineAuthors(id);
lineAuthors[id].forEach((line) => {
const map = new Y.Map();
if (line.author) {
map.set("author", line.author);
}
lineData.lineAuthors.push([map]);
});
}
for (const id in text) {
const commentText = this.getTextForComment(id);
commentText.insert(0, text[id]);
}
});
}
}
2 changes: 1 addition & 1 deletion src/components/Resolved.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const ResolvedComments = ({ ycomments }) => {
useEffect(() => {
setResolvedComments(ycomments.resolver().resolved().sort(dateComparator));
ycomments.resolver().onUpdate((comments) => setResolvedComments(comments.sort(dateComparator)));
}, []);
}, [ycomments]);

return (
<ResolvedWrapper>
Expand Down
12 changes: 11 additions & 1 deletion src/myst-git/MystEditorGit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ const MystEditorGit = ({
const changeHistory = useSignal(initialHistory);
const toast = useSignal(null);
const commitSummary = useSignal(null);
const commentStateToApply = useRef(null);

useEffect(() => {
window.myst_editor[props.id].state = mystState.current;
Expand All @@ -166,6 +167,7 @@ const MystEditorGit = ({
try {
commitSummary.value = null;
const { hash, webUrl } = await commitChanges(message);
commentStateToApply.current = window.myst_editor[props.id].ycomments.encodeState();
toast.value = { text: "Changes have been commited. ", link: { text: "See in Gitlab", href: webUrl } };
switchCommit({ hash, message: summary }, true);
} catch (error) {
Expand Down Expand Up @@ -310,6 +312,14 @@ const MystEditorGit = ({
};
doc.on("afterTransaction", handleChange);

// This is done to keep this signal effect from running in a cycle when some singals are updated in applyState.
queueMicrotask(() => {
if (commentStateToApply.current != null) {
window.myst_editor[props.id].ycomments.applyState(commentStateToApply.current);
commentStateToApply.current = null;
}
});

return () => doc.off("afterTransaction", handleChange);
});

Expand Down Expand Up @@ -385,7 +395,7 @@ const MystEditorGit = ({
</button>
</Toast>
)}
{commitSummary.value && <CommitModal initialSummary={commitSummary} onSubmit={onCommit} onClose={onCommitCancel} />}
{commitSummary.value && <CommitModal initialSummary={commitSummary.value} onSubmit={onCommit} onClose={onCommitCancel} />}
<MystState.Provider value={mystState.current}>{room.value && <MystEditorPreact />}</MystState.Provider>
</MystContainer>
</StyleSheetManager>
Expand Down

0 comments on commit d09cb47

Please sign in to comment.