Skip to content

Commit 82b99ab

Browse files
gratcliffjulshotal
andauthored
fix(readonly): wip styled output (#21)
* fix(readonly): wip styled output * fix: line nums/newlines displaying almost properly * fix(styled): cleaving off any linebreaks near string end * fix(style): only committing a single new line when single line break * fix: edge case where end output idx was not string Co-authored-by: julshotal <jgh6364@g.rit.edu>
1 parent f750c36 commit 82b99ab

File tree

2 files changed

+47
-17
lines changed

2 files changed

+47
-17
lines changed

public/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ ReactDOM.render(
2222
{ ch: 0, line: 1 },
2323
],
2424
[
25-
{ ch: 0, line: 2 },
26-
{ ch: 0, line: 3 },
25+
{ ch: 0, line: 4 },
26+
{ ch: 0, line: 5 },
2727
],
2828
],
2929
}

src/codeMirror/index.jsx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@ WrappedLine.propTypes = {
2929
// Final Styled Output
3030
// Includes Highlighted | Overlayed | null styled lines
3131
const StructuredOutput = ({ gutteredInput, highlights = [] }) =>
32-
gutteredInput.map((ac, idx) => (
33-
<WrappedLine
34-
key={`cm-wrapped-${idx}`}
35-
child={ac}
36-
className={['cm-linerow', highlights.length ? highlights[idx] : ''].join(' ')}
37-
/>
38-
));
32+
gutteredInput.map((ac, idx) => {
33+
return (
34+
<WrappedLine
35+
key={`cm-wrapped-${idx}`}
36+
child={ac}
37+
className={['cm-linerow', highlights.length ? highlights[idx] : ''].join(' ')}
38+
/>
39+
);
40+
});
3941

4042
StructuredOutput.propTypes = {
4143
gutteredInput: PropTypes.any,
@@ -68,23 +70,51 @@ const highlightedLines = (ranges, totalLength) => {
6870
};
6971

7072
const StyledSyntaxHighlighter = ({ output, ranges }) => {
73+
const lineBreakRegex = /\r?\n/;
7174
const gutteredOutput = [];
7275
let bucket = [];
7376
let lineNumber = 1;
7477

78+
const incrementLine = newLine => {
79+
// If new line, we'll manually add it to a bucket
80+
if (newLine) bucket.push('\n');
81+
// Regardless, we'll add the bucket to our larger output and start a new numbered div
82+
gutteredOutput.push(bucket);
83+
lineNumber += 1;
84+
bucket = [defaultLineJsx(lineNumber)];
85+
};
86+
87+
const enumerateMatches = (o, final) => {
88+
// Case where a single line break
89+
if (o.length === 1) {
90+
incrementLine(true);
91+
// Case with multiple consecutive line breaks
92+
} else {
93+
const matches = o.split(lineBreakRegex);
94+
matches.forEach(m => {
95+
if (m.length) {
96+
bucket.push(m);
97+
// If we've found a match right before the end of our output, we pad it
98+
if (final) incrementLine();
99+
} else {
100+
incrementLine(true);
101+
}
102+
});
103+
}
104+
};
105+
75106
output.unshift(defaultLineJsx(1));
76107
output.forEach((o, idx) => {
77-
const lineBreakRegex = /\n/g;
78-
79108
if (idx === output.length - 1) {
80-
bucket.push(o);
81-
gutteredOutput.push(bucket);
109+
if (typeof o === 'string') enumerateMatches(o, true);
110+
else {
111+
bucket.push(o);
112+
gutteredOutput.push(bucket);
113+
}
82114
} else if (!lineBreakRegex.test(o)) {
83115
bucket.push(o);
84-
} else {
85-
gutteredOutput.push(bucket);
86-
lineNumber += 1;
87-
bucket = [defaultLineJsx(lineNumber)];
116+
} else if (typeof o === 'string') {
117+
enumerateMatches(o);
88118
}
89119
});
90120

0 commit comments

Comments
 (0)