Skip to content

Commit 6d2539f

Browse files
committedJan 25, 2025
[WIP] Add lineComments CodeMirror extension - lineCommentsClickHandler
1 parent 03ce3ca commit 6d2539f

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed
 

‎app/utils/code-mirror-line-comments-gutter.ts

+15-16
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,25 @@ class CommentButtonGutterMarker extends GutterMarkerRS {
4545
}
4646
}
4747

48-
function lineCommentsLineMarker(view: EditorView, line: BlockInfo) {
48+
function lineCommentsGutterLineMarker(view: EditorView, line: BlockInfo) {
4949
const lineNumber = view.state.doc.lineAt(line.from).number;
5050
const commentsCount = view.state.facet(lineDataFacet)[0]?.dataForLine(lineNumber)?.commentsCount || 0;
5151

5252
return new (commentsCount === 0 ? CommentButtonGutterMarker : CommentsCountGutterMarker)(line);
5353
}
5454

55+
function lineCommentsGutterClickHandler(view: EditorView, line: BlockInfo) {
56+
const lineNumber = view.state.doc.lineAt(line.from).number;
57+
const expandedLines = view.state.facet(expandedLineNumbersFacet)[0] || [];
58+
const newExpandedLines = expandedLines.includes(lineNumber) ? expandedLines.without(lineNumber) : [...expandedLines, lineNumber];
59+
60+
view.dispatch({
61+
effects: [expandedLineNumbersCompartment.reconfigure(expandedLineNumbersFacet.of(newExpandedLines))],
62+
});
63+
64+
return true;
65+
}
66+
5567
const lineCommentsGutterBaseTheme = EditorView.baseTheme({
5668
'.cm-lineCommentsGutter': {
5769
minWidth: '24px',
@@ -96,22 +108,9 @@ export function lineCommentsGutter() {
96108
return [
97109
gutterRS({
98110
class: 'cm-lineCommentsGutter',
99-
lineMarker: lineCommentsLineMarker,
111+
lineMarker: lineCommentsGutterLineMarker,
100112
domEventHandlers: {
101-
click: (view: EditorView, line: BlockInfo) => {
102-
const lineNumber = view.state.doc.lineAt(line.from).number;
103-
const expandedLines = view.state.facet(expandedLineNumbersFacet)[0] || [];
104-
105-
view.dispatch({
106-
effects: [
107-
expandedLineNumbersCompartment.reconfigure(
108-
expandedLineNumbersFacet.of(expandedLines.includes(lineNumber) ? expandedLines.without(lineNumber) : [...expandedLines, lineNumber]),
109-
),
110-
],
111-
});
112-
113-
return true;
114-
},
113+
click: lineCommentsGutterClickHandler,
115114
},
116115
}),
117116
lineCommentsGutterBaseTheme,

‎app/utils/code-mirror-line-comments.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,5 @@ export const toggleLineCommentsEffect = StateEffect.define<{ pos: number; isExpa
3535
});
3636

3737
export function lineComments(lineData: LineDataCollection) {
38-
return [
39-
lineDataFacet.of(lineData),
40-
expandedLineNumbersCompartment.of(expandedLineNumbersFacet.of([-999])),
41-
lineCommentsWidget(),
42-
lineCommentsGutter(),
43-
];
38+
return [lineDataFacet.of(lineData), expandedLineNumbersCompartment.of(expandedLineNumbersFacet.of([])), lineCommentsWidget(), lineCommentsGutter()];
4439
}

0 commit comments

Comments
 (0)
Failed to load comments.