Skip to content

Commit 01b4a81

Browse files
Fix adding line comment
1 parent 8b5a7ca commit 01b4a81

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1+
import { COMMENTS } from '@codesandbox/common/lib/utils/feature-flags';
12
import { notificationState } from '@codesandbox/common/lib/utils/notifications';
23
import {
34
NotificationMessage,
45
NotificationStatus,
56
} from '@codesandbox/notifications/lib/state';
67

7-
import { COMMENTS } from '@codesandbox/common/lib/utils/feature-flags';
88
import { KeyCode, KeyMod } from './keyCodes';
99

1010
// Copied from 'common/actions' in vscode
@@ -168,7 +168,9 @@ export class Workbench {
168168
label: 'Comment on code',
169169
category: 'Comments',
170170
run: () => {
171-
this.controller.getSignal('comments.createComment')();
171+
this.controller.getSignal('comments.createComment')({
172+
isLineComment: false,
173+
});
172174
},
173175
});
174176
}

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ import {
1313
CommentRemovedSubscription,
1414
} from 'app/graphql/types';
1515
import { Action, AsyncAction } from 'app/overmind';
16+
import {
17+
indexToLineAndColumn,
18+
lineAndColumnToIndex,
19+
} from 'app/overmind/utils/common';
1620
import { utcToZonedTime } from 'date-fns-tz';
1721
import { Selection, TextOperation } from 'ot';
1822
import * as uuid from 'uuid';
@@ -111,7 +115,9 @@ export const onCommentClick: Action<{
111115
}
112116

113117
if (!commentIds.length) {
114-
actions.comments.createComment();
118+
actions.comments.createComment({
119+
isLineComment: true,
120+
});
115121
} else if (commentIds.length === 1) {
116122
actions.comments.selectComment({
117123
commentId: commentIds[0],
@@ -211,7 +217,9 @@ export const selectComment: AsyncAction<{
211217
}
212218
};
213219

214-
export const createComment: AsyncAction = async ({ state, effects }) => {
220+
export const createComment: AsyncAction<{
221+
isLineComment: boolean;
222+
}> = async ({ state, effects }, { isLineComment }) => {
215223
if (!state.user || !state.editor.currentSandbox) {
216224
return;
217225
}
@@ -222,10 +230,23 @@ export const createComment: AsyncAction = async ({ state, effects }) => {
222230
let codeReference: CodeReference | null = null;
223231
const selection = state.live.currentSelection;
224232
if (selection) {
233+
let anchor =
234+
selection.primary.selection[0] || selection.primary.cursorPosition;
235+
let head =
236+
selection.primary.selection[1] || selection.primary.cursorPosition;
237+
238+
if (isLineComment) {
239+
const codeLines = state.editor.currentModule.code.split('\n');
240+
const { lineNumber } = indexToLineAndColumn(codeLines, anchor);
241+
const newAnchor = lineAndColumnToIndex(codeLines, lineNumber, 1);
242+
243+
anchor = newAnchor;
244+
head = newAnchor;
245+
}
246+
225247
codeReference = {
226-
anchor:
227-
selection.primary.selection[0] || selection.primary.cursorPosition,
228-
head: selection.primary.selection[1] || selection.primary.cursorPosition,
248+
anchor,
249+
head,
229250
code: selection.primary.selection.length
230251
? state.editor.currentModule.code.substr(
231252
selection.primary.selection[0],

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,9 @@ export const forkExternalSandbox: AsyncAction<{
529529
try {
530530
const forkedSandbox = await effects.api.forkSandbox(sandboxId, body);
531531

532+
// We currently have to delete the comments due to smooth forking. This will get better
533+
// with EditorSandbox class
534+
delete state.comments.comments[sandboxId];
532535
state.editor.sandboxes[forkedSandbox.id] = forkedSandbox;
533536
effects.router.updateSandboxUrl(forkedSandbox, { openInNewWindow });
534537
} catch (error) {

packages/app/src/app/pages/Sandbox/Editor/Workspace/screens/Comments/Dialog/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ENTER, ESC } from '@codesandbox/common/lib/utils/keycodes';
22
import { hasPermission } from '@codesandbox/common/lib/utils/permission';
3-
43
import {
54
Avatar,
65
Element,

0 commit comments

Comments
 (0)