Skip to content

Commit

Permalink
feat(editor): Add line numbers (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltaranto committed May 26, 2021
1 parent 4a06132 commit 2d8b965
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
15 changes: 10 additions & 5 deletions cypress/support/utils.js
Expand Up @@ -86,11 +86,16 @@ export const assertFirstFrameContains = (text) => {
};

export const assertCodePaneContains = (text) => {
getCodeEditor().then(($el) => {
const code = $el.get(0).innerText;
// removes code mirrors invisible last line character placeholder
// which is inserted to preserve prettiers new line at end of string.
expect(code.replace(/[\u200b]$/, '')).to.eq(text);
getCodeEditor().within(() => {
const lines = [];
cy.get('.CodeMirror-line')
.each(($el) => lines.push($el.text()))
.then(() => {
const code = lines.join('\n');
// removes code mirrors invisible last line character placeholder
// which is inserted to preserve prettiers new line at end of string.
expect(code.replace(/[\u200b]$/, '')).to.eq(text);
});
});
};

Expand Down
42 changes: 31 additions & 11 deletions src/Playroom/CodeEditor/CodeEditor.less
Expand Up @@ -14,13 +14,29 @@
}

.marker {
width: @code-marker-size;
height: @code-marker-size;
border-radius: (@code-marker-size / 2);
background-color: @code-marker-color;
@keyframes fadeIn {
90% {
opacity: 0;
}
100% {
opacity: 1;
}
}

border-radius: @radius-large;
background-color: @critical-light;
color: @critical-dark;
position: relative;
top: (@code-marker-size / 2);
left: 10px;
left: 4px;
margin-right: 4px;
text-align: right;
opacity: 0;
padding-right: 10px;
animation-name: fadeIn;
animation-duration: 1s;
animation-timing-function: ease;
animation-iteration-count: 1;
animation-fill-mode: forwards;
}

:global {
Expand All @@ -41,10 +57,6 @@
width: @code-gutter-size;
}

.CodeMirror-gutter-elt {
position: relative;
}

.CodeMirror pre,
.CodeMirror-linenumber {
font-size: 16px;
Expand Down Expand Up @@ -102,7 +114,15 @@
background: @code-selection;
}
.CodeMirror-linenumber {
color: @white;
box-sizing: border-box;
width: @code-gutter-size !important;
padding-left: 8px;
padding-right: @code-line-gutter;
color: @gray-4;
transition: @transition-fast;
&:not(:hover) {
opacity: 0.4;
}
}
.cm-tag {
color: @code-format-tag;
Expand Down
10 changes: 9 additions & 1 deletion src/Playroom/CodeEditor/CodeEditor.tsx
Expand Up @@ -76,7 +76,14 @@ const validateCode = (editorInstance: Editor, code: string) => {
if (lineNumber) {
const marker = document.createElement('div');
marker.classList.add(styles.marker);
marker.setAttribute('title', err.message);
marker.setAttribute(
'title',
// Remove our wrapping Fragment from error message
(err.message || '')
.replace(/\<React\.Fragment\>/, '')
.replace(/\<\/React\.Fragment\>$/, '')
);
marker.innerText = String(lineNumber);
editorInstance.setGutterMarker(lineNumber - 1, styles.gutter, marker);
}
}
Expand Down Expand Up @@ -251,6 +258,7 @@ export const CodeEditor = ({ code, onChange, previewCode, hints }: Props) => {
gutters: [styles.gutter],
hintOptions: { schemaInfo: hints },
viewportMargin: 50,
lineNumbers: true,
extraKeys: {
Tab: (cm) => {
if (cm.somethingSelected()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Playroom/SplashScreen/SplashScreen.less
Expand Up @@ -15,7 +15,7 @@
align-items: center;
justify-content: center;
text-align: center;
background: @gray-5;
background: @gray-6;
color: @white;
transition: @transition-speed-slow;
}
Expand Down
14 changes: 8 additions & 6 deletions src/Playroom/variables.less
Expand Up @@ -12,7 +12,7 @@
@text-standard: normal 14px @typography-family;
@text-small: normal 12px @typography-family;
@text-xsmall: normal 10px @typography-family;
@text-neutral: @gray-4;
@text-neutral: @gray-5;

// Weight
@bold: 700;
Expand All @@ -27,19 +27,22 @@
@green-2: #1da584;
@red-1: #fed6e1;
@red-2: #e52b50;
@red-3: #c3183a;
@white: #fff;
@gray-1: #f4f4f4;
@gray-2: #e8e8e8;
@gray-3: #a7a7a7;
@gray-4: #515151;
@gray-5: #1e1e1e;
@gray-4: #767676;
@gray-5: #515151;
@gray-6: #1e1e1e;
@black: #000;

// Tones
@positive-light: @green-1;
@positive: @green-2;
@critical-light: @red-1;
@critical: @red-2;
@critical-dark: @red-3;
@neutral: @gray-2;

// Highlights
Expand Down Expand Up @@ -76,9 +79,8 @@
// Code Editor
@code-background: @white;
@code-font-family: Source Code Pro, Firacode, Hasklig, Menlo, monospace;
@code-gutter-size: @gutter-width;
@code-marker-size: @grid * 3;
@code-marker-color: @highlight;
@code-gutter-size: @gutter-width * 1.75;
@code-line-gutter: 20px;
@code-format-tag: @blue-4;
@code-format-attribute: @blue-2;
@code-format-string: @blue-3;
Expand Down

0 comments on commit 2d8b965

Please sign in to comment.