Skip to content

Commit ad61d6e

Browse files
committed
[WIP] Add lineComments CodeMirror extension - inline widget
1 parent 15c0591 commit ad61d6e

2 files changed

Lines changed: 69 additions & 15 deletions

File tree

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

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ class CommentsWidget extends WidgetType {
3737

3838
toDOM(view: EditorView): HTMLElement {
3939
const comments = (view.state.facet(lineCommentsFacet)[0] || [])[this.line.number - 1];
40-
4140
const elem = document.createElement('line-comments');
42-
elem.style.display = 'block';
43-
elem.style.backgroundColor = '#009bff80';
44-
elem.style.paddingLeft = '1rem';
4541

4642
if (comments?.length) {
4743
elem.innerText = `💬 COMMENTS (${comments?.length || 0}) FOR LINE #${this.line.number}`;
@@ -60,8 +56,8 @@ function lineCommentsDecorations(state: EditorState) {
6056
Decoration.widget({
6157
widget: new CommentsWidget(line),
6258
side: 10,
63-
inlineOrder: true,
64-
block: true,
59+
// inlineOrder: true,
60+
// block: true,
6561
}).range(line.to),
6662
);
6763
}
@@ -96,7 +92,12 @@ class CommentsCountGutterMarker extends GutterMarkerRS {
9692
const lineNumber = view.state.doc.lineAt(this.line.from).number;
9793
const comments = (view.state.facet(lineCommentsFacet)[0] || [])[lineNumber - 1];
9894
const elem = document.createElement('comments-count');
99-
elem.innerText = `${comments?.length}`;
95+
const commentsCount = comments?.length || 0;
96+
elem.innerText = `${commentsCount > 99 ? '99+' : commentsCount}`;
97+
98+
if (commentsCount > 99) {
99+
elem.className = 'cm-over-99';
100+
}
100101

101102
return elem;
102103
}
@@ -121,11 +122,25 @@ class CommentButtonGutterMarker extends GutterMarkerRS {
121122
export function lineComments() {
122123
return [
123124
lineCommentsFacet.compute(['doc'], function (state) {
124-
return Array.from({ length: state.doc.lines }).map((_v, lineNumber) =>
125-
Array.from<string>({ length: Math.random() < 0.4 ? getRandomInt(0, 6) : 0 }).map(
125+
return Array.from({ length: state.doc.lines }).map(function (_v, lineNumber) {
126+
const rnd = Math.random();
127+
128+
let commentsCount;
129+
130+
if (rnd < 0.05) {
131+
commentsCount = getRandomInt(100, 255);
132+
} else if (rnd < 0.1) {
133+
commentsCount = getRandomInt(10, 99);
134+
} else if (rnd < 0.8) {
135+
commentsCount = 0;
136+
} else {
137+
commentsCount = getRandomInt(1, 9);
138+
}
139+
140+
return Array.from<string>({ length: commentsCount }).map(
126141
(_v, index) => new LineComment({ lineNumber, author: 'Darth Programmius', text: `Comment #${index}` }),
127-
),
128-
);
142+
);
143+
});
129144
}),
130145
lineCommentsStateField,
131146
gutterRS({
@@ -140,8 +155,25 @@ export function lineComments() {
140155
}),
141156
highlightActiveLineGutterRS(),
142157
EditorView.baseTheme({
158+
'.cm-line': {
159+
'& line-comments': {
160+
display: 'block',
161+
backgroundColor: '#009bff40',
162+
paddingLeft: '1rem',
163+
marginRight: '-1rem',
164+
165+
'& + br': {
166+
display: 'none',
167+
},
168+
},
169+
170+
'& .cm-insertedLine + br': {
171+
display: 'none',
172+
},
173+
},
174+
143175
'.cm-gutters.cm-gutters-rs': {
144-
backgroundColor: '#ff000070', // 'transparent',
176+
backgroundColor: '#ffffff20', // '#ff000070', // 'transparent',
145177

146178
'& .cm-custom-gutter-rs': {
147179
minWidth: '24px',
@@ -150,14 +182,32 @@ export function lineComments() {
150182
'& .cm-gutterElement': {
151183
cursor: 'pointer',
152184

185+
'& comments-count': {
186+
display: 'block',
187+
backgroundColor: '#ffcd72c0',
188+
borderRadius: '50%',
189+
color: '#24292e',
190+
transform: 'scale(0.75)',
191+
fontWeight: '500',
192+
fontSize: '12px',
193+
194+
'&.cm-over-99': {
195+
fontSize: '9.5px',
196+
},
197+
},
198+
153199
'& comment-button': {
154-
opacity: '0.2',
200+
opacity: '0.15',
155201
},
156202

157203
'&:hover': {
158204
'& comment-button': {
159205
opacity: '1',
160206
},
207+
208+
'& comments-count': {
209+
backgroundColor: '#ffa500',
210+
},
161211
},
162212
},
163213
},

app/utils/code-mirror-themes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const BASE_STYLE = {
1818
'.cm-lineNumbers': {
1919
'& .cm-gutterElement': {
2020
display: 'flex',
21-
alignItems: 'center',
21+
alignItems: 'flex-start',
2222
justifyContent: 'flex-end',
2323
padding: '0 0.5rem 0 1rem',
2424
fontSize: '0.875em',
@@ -30,11 +30,15 @@ const BASE_STYLE = {
3030
'.cm-foldGutter': {
3131
'& .cm-gutterElement': {
3232
display: 'flex',
33-
alignItems: 'center',
33+
alignItems: 'flex-start',
3434
justifyContent: 'center',
3535
fontSize: '0.875em',
3636
minWidth: '1.2rem',
3737
color: '#94a3b8',
38+
39+
'& svg': {
40+
marginTop: '0.3rem',
41+
},
3842
},
3943
},
4044

0 commit comments

Comments
 (0)