Skip to content

Commit 025d02f

Browse files
add transposing of comments on received operations
1 parent 28df042 commit 025d02f

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

packages/app/src/app/overmind/effects/vscode/ModelsHandler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export type onSelectionChangeData = UserSelection;
4141

4242
export type OnOperationAppliedData = {
4343
moduleShortid: string;
44+
operation: TextOperation;
4445
title: string;
4546
code: string;
4647
model: any;
@@ -294,6 +295,7 @@ export class ModelsHandler {
294295
this.isApplyingOperation = false;
295296
this.onOperationAppliedCallback({
296297
code: model.getValue(),
298+
operation,
297299
moduleShortid: module.shortid,
298300
title: module.title,
299301
model,

packages/app/src/app/overmind/namespaces/comments/actions.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CommentsFilterOption } from '@codesandbox/common/lib/types';
1+
import { CommentsFilterOption, Module } from '@codesandbox/common/lib/types';
22
import { captureException } from '@codesandbox/common/lib/utils/analytics/sentry';
33
import { getTextOperation } from '@codesandbox/common/lib/utils/diff';
44
import {
@@ -14,7 +14,7 @@ import {
1414
} from 'app/graphql/types';
1515
import { Action, AsyncAction } from 'app/overmind';
1616
import { utcToZonedTime } from 'date-fns-tz';
17-
import { Selection } from 'ot';
17+
import { Selection, TextOperation } from 'ot';
1818
import * as uuid from 'uuid';
1919

2020
import { OPTIMISTIC_COMMENT_ID } from './state';
@@ -570,3 +570,23 @@ export const onCommentRemoved: Action<CommentRemovedSubscription> = (
570570
) => {
571571
delete state.comments.comments[comment.sandbox.id][comment.id];
572572
};
573+
574+
export const transposeComments: Action<{
575+
module: Module;
576+
operation: TextOperation;
577+
}> = ({ state }, { module, operation }) => {
578+
const sandbox = state.editor.currentSandbox;
579+
if (!sandbox) {
580+
return;
581+
}
582+
const comments = state.comments.fileComments[module.path] || [];
583+
comments.forEach(fileComment => {
584+
const range = new Selection.Range(...fileComment.range);
585+
const newRange = range.transform(operation);
586+
const comment = state.comments.comments[sandbox.id][fileComment.commentId];
587+
if (comment.references && comment.references[0].type === 'code') {
588+
comment.references[0].metadata.anchor = newRange.anchor;
589+
comment.references[0].metadata.head = newRange.head;
590+
}
591+
});
592+
};

packages/app/src/app/overmind/namespaces/editor/actions.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { convertAuthorizationToPermissionType } from 'app/utils/authorization';
3333
import { clearCorrectionsFromAction } from 'app/utils/corrections';
3434
import history from 'app/utils/history';
3535
import { debounce } from 'lodash-es';
36-
import { Selection, TextOperation } from 'ot';
36+
import { TextOperation } from 'ot';
3737
import { json } from 'overmind';
3838

3939
import eventToTransform from '../../utils/event-to-transform';
@@ -335,8 +335,9 @@ export const codeSaved: AsyncAction<{
335335

336336
export const onOperationApplied: Action<{
337337
moduleShortid: string;
338+
operation: TextOperation;
338339
code: string;
339-
}> = ({ state, effects, actions }, { code, moduleShortid }) => {
340+
}> = ({ state, effects, actions }, { code, moduleShortid, operation }) => {
340341
if (!state.editor.currentSandbox) {
341342
return;
342343
}
@@ -349,6 +350,8 @@ export const onOperationApplied: Action<{
349350
return;
350351
}
351352

353+
actions.comments.transposeComments({ module, operation });
354+
352355
actions.editor.internal.updateModuleCode({
353356
module,
354357
code,
@@ -373,8 +376,6 @@ export const codeChanged: Action<{
373376
return;
374377
}
375378

376-
const sandbox = state.editor.currentSandbox;
377-
378379
const module = state.editor.currentSandbox.modules.find(
379380
m => m.shortid === moduleShortid
380381
);
@@ -410,17 +411,7 @@ export const codeChanged: Action<{
410411

411412
effects.live.sendCodeUpdate(moduleShortid, operation);
412413

413-
const comments = state.comments.fileComments[module.path] || [];
414-
comments.forEach(fileComment => {
415-
const range = new Selection.Range(...fileComment.range);
416-
const newRange = range.transform(operation);
417-
const comment =
418-
state.comments.comments[sandbox.id][fileComment.commentId];
419-
if (comment.references && comment.references[0].type === 'code') {
420-
comment.references[0].metadata.anchor = newRange.anchor;
421-
comment.references[0].metadata.head = newRange.head;
422-
}
423-
});
414+
actions.comments.transposeComments({ module, operation });
424415
}
425416

426417
actions.editor.internal.updateModuleCode({

0 commit comments

Comments
 (0)