Skip to content

Commit 16eb077

Browse files
authored
fix: makes PreviewCode also load ace async (#11)
1 parent a17a0d3 commit 16eb077

File tree

6 files changed

+76
-47
lines changed

6 files changed

+76
-47
lines changed

.github/workflows/main.yml

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
name: CI & Release
22
on:
3+
# Build on pushes to release branches
34
push:
5+
branches: [main]
6+
# Build on pull requests targeting release branches
7+
pull_request:
8+
branches: [main]
49
workflow_dispatch:
510
inputs:
611
release:
7-
description: 'Release new version'
12+
description: Release new version
813
required: true
914
default: false
1015
type: boolean
1116

1217
jobs:
18+
log-the-inputs:
19+
name: Log inputs
20+
runs-on: ubuntu-latest
21+
steps:
22+
- run: |
23+
echo "Inputs: $INPUTS"
24+
env:
25+
INPUTS: ${{ toJSON(inputs) }}
26+
1327
build:
14-
name: 'Lint & Build'
28+
name: Lint & Build
1529
runs-on: ubuntu-latest
1630
steps:
1731
- name: Set git to use LF
@@ -22,32 +36,33 @@ jobs:
2236
- uses: actions/setup-node@v3
2337
with:
2438
node-version: lts/*
25-
cache: 'npm'
39+
cache: npm
2640
- run: npm ci
2741
- run: npm run lint --if-present
28-
- run: npm run build
42+
- run: npm run prepublishOnly
2943

3044
test:
31-
name: 'Test'
45+
name: Test
3246
needs: build
3347
strategy:
3448
matrix:
35-
node-version: [16.x, 18.x]
36-
platform: [ubuntu-latest, macos-latest]
37-
runs-on: ${{ matrix.platform }}
49+
os: [ macos-latest, ubuntu-latest ]
50+
node: [ lts/*, current ]
51+
runs-on: ${{ matrix.os }}
3852
steps:
3953
- uses: actions/checkout@v3
4054
- uses: actions/setup-node@v3
4155
with:
42-
node-version: ${{ matrix.node-version }}
43-
cache: 'npm'
44-
- run: npm install
45-
- run: npm test
56+
node-version: ${{ matrix.node }}
57+
cache: npm
58+
- run: npm ci
59+
- run: npm test --if-present
4660

4761
release:
48-
name: 'Semantic release'
62+
name: Semantic release
4963
needs: test
5064
runs-on: ubuntu-latest
65+
# only run if opt-in during workflow_dispatch
5166
if: inputs.release == true
5267
steps:
5368
- uses: actions/checkout@v3
@@ -58,10 +73,13 @@ jobs:
5873
- uses: actions/setup-node@v3
5974
with:
6075
node-version: lts/*
61-
cache: 'npm'
62-
- run: npm install
63-
# Branches that will release new versions are defined in .releaserc.json
76+
cache: npm
77+
- run: npm ci
78+
# Branches that will release new versions are defined in .releaserc.json
6479
- run: npx semantic-release
80+
# Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state
81+
# e.g. git tags were pushed but it exited before `npm publish`
82+
if: always()
6583
env:
6684
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6785
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,15 @@ with default configuration for build & watch scripts.
179179
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
180180
on how to run this plugin with hotreload in the studio.
181181

182+
### Dev note on parcel + vite in dev mode:
183+
Vite will emit a warning when using yalc add + link in a Studio running with
184+
vite dev server.
185+
It happens because parcel appends ?currentTimestamp to the import statement for
186+
cache busting, which is [not supported by vite](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations).
187+
188+
However, the code _works_, and when building for production this is not an issue, as parcel
189+
will not suffix the timestamp in the prod bundle.
190+
182191
## Release new version
183192

184193
Run ["CI & Release" workflow](https://github.com/sanity-io/code-input/actions).

src/CodeInput.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,6 @@ export function CodeInput(props: CodeInputProps) {
276276
<EditorContainer radius={1} shadow={1} readOnly={readOnly}>
277277
{AceEditor && (
278278
<Suspense fallback={<div>Loading code editor...</div>}>
279-
(
280279
<AceEditor
281280
ref={aceEditorRef}
282281
mode={mode}
@@ -299,7 +298,6 @@ export function CodeInput(props: CodeInputProps) {
299298
onFocus={handleCodeFocus}
300299
onBlur={onBlur}
301300
/>
302-
)
303301
</Suspense>
304302
)}
305303
</EditorContainer>

src/PreviewCode.tsx

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import React, {useCallback, useEffect, useRef} from 'react'
2-
import AceEditor from 'react-ace'
1+
import React, {Suspense, useCallback, useEffect, useRef} from 'react'
32
import styled from 'styled-components'
43
import {Box} from '@sanity/ui'
54
import {ACE_EDITOR_PROPS, ACE_SET_OPTIONS} from './config'
65
import createHighlightMarkers from './createHighlightMarkers'
76
import {CodeInputType, CodeInputValue} from './types'
8-
import './ace-editor/editorSupport'
7+
import {useAceEditor} from './ace-editor/AceEditorLazy'
98

109
const PreviewContainer = styled(Box)`
1110
position: relative;
@@ -54,34 +53,39 @@ export default function PreviewCode(props: PreviewCodeProps) {
5453
const fixedLanguage = type?.options?.language
5554

5655
const mode = value?.language || fixedLanguage || 'text'
56+
const AceEditor = useAceEditor()
5757

5858
return (
5959
<PreviewContainer>
6060
<PreviewInner padding={4}>
61-
<AceEditor
62-
ref={aceEditorRef}
63-
focus={false}
64-
mode={mode}
65-
theme="monokai"
66-
width="100%"
67-
onChange={handleEditorChange}
68-
maxLines={200}
69-
readOnly
70-
wrapEnabled
71-
showPrintMargin={false}
72-
highlightActiveLine={false}
73-
cursorStart={-1}
74-
value={(value && value.code) || ''}
75-
markers={
76-
value && value.highlightedLines
77-
? createHighlightMarkers(value.highlightedLines)
78-
: undefined
79-
}
80-
tabSize={2}
81-
showGutter={false}
82-
setOptions={ACE_SET_OPTIONS}
83-
editorProps={ACE_EDITOR_PROPS}
84-
/>
61+
{AceEditor && (
62+
<Suspense fallback={<div>Loading code editor...</div>}>
63+
<AceEditor
64+
ref={aceEditorRef}
65+
focus={false}
66+
mode={mode}
67+
theme="monokai"
68+
width="100%"
69+
onChange={handleEditorChange}
70+
maxLines={200}
71+
readOnly
72+
wrapEnabled
73+
showPrintMargin={false}
74+
highlightActiveLine={false}
75+
cursorStart={-1}
76+
value={(value && value.code) || ''}
77+
markers={
78+
value && value.highlightedLines
79+
? createHighlightMarkers(value.highlightedLines)
80+
: undefined
81+
}
82+
tabSize={2}
83+
showGutter={false}
84+
setOptions={ACE_SET_OPTIONS}
85+
editorProps={ACE_EDITOR_PROPS}
86+
/>
87+
</Suspense>
88+
)}
8589
</PreviewInner>
8690
</PreviewContainer>
8791
)

src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {CodeInputLanguage} from './types'
22

3-
// NOTE: MAKE SURE THESE ALIGN WITH IMPORTS IN ./editorSupport
3+
// NOTE: MAKE SURE THESE ALIGN WITH IMPORTS IN ./ace-editor/editorSupport
44
export const SUPPORTED_LANGUAGES: CodeInputLanguage[] = [
55
{title: 'Batch file', value: 'batchfile'},
66
{title: 'C#', value: 'csharp'},

src/createHighlightMarkers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {IMarker} from 'react-ace'
1+
import type {IMarker} from 'react-ace'
22
import {css} from 'styled-components'
33

44
export const highlightMarkersCSS = css`

0 commit comments

Comments
 (0)