Skip to content

Commit

Permalink
fix: makes PreviewCode also load ace async (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Aug 17, 2022
1 parent a17a0d3 commit 16eb077
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 47 deletions.
50 changes: 34 additions & 16 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
name: CI & Release
on:
# Build on pushes to release branches
push:
branches: [main]
# Build on pull requests targeting release branches
pull_request:
branches: [main]
workflow_dispatch:
inputs:
release:
description: 'Release new version'
description: Release new version
required: true
default: false
type: boolean

jobs:
log-the-inputs:
name: Log inputs
runs-on: ubuntu-latest
steps:
- run: |
echo "Inputs: $INPUTS"
env:
INPUTS: ${{ toJSON(inputs) }}
build:
name: 'Lint & Build'
name: Lint & Build
runs-on: ubuntu-latest
steps:
- name: Set git to use LF
Expand All @@ -22,32 +36,33 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: 'npm'
cache: npm
- run: npm ci
- run: npm run lint --if-present
- run: npm run build
- run: npm run prepublishOnly

test:
name: 'Test'
name: Test
needs: build
strategy:
matrix:
node-version: [16.x, 18.x]
platform: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.platform }}
os: [ macos-latest, ubuntu-latest ]
node: [ lts/*, current ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm install
- run: npm test
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test --if-present

release:
name: 'Semantic release'
name: Semantic release
needs: test
runs-on: ubuntu-latest
# only run if opt-in during workflow_dispatch
if: inputs.release == true
steps:
- uses: actions/checkout@v3
Expand All @@ -58,10 +73,13 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: lts/*
cache: 'npm'
- run: npm install
# Branches that will release new versions are defined in .releaserc.json
cache: npm
- run: npm ci
# Branches that will release new versions are defined in .releaserc.json
- run: npx semantic-release
# Don't allow interrupting the release step if the job is cancelled, as it can lead to an inconsistent state
# e.g. git tags were pushed but it exited before `npm publish`
if: always()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ with default configuration for build & watch scripts.
See [Testing a plugin in Sanity Studio](https://github.com/sanity-io/plugin-kit#testing-a-plugin-in-sanity-studio)
on how to run this plugin with hotreload in the studio.

### Dev note on parcel + vite in dev mode:
Vite will emit a warning when using yalc add + link in a Studio running with
vite dev server.
It happens because parcel appends ?currentTimestamp to the import statement for
cache busting, which is [not supported by vite](https://github.com/rollup/plugins/tree/master/packages/dynamic-import-vars#limitations).

However, the code _works_, and when building for production this is not an issue, as parcel
will not suffix the timestamp in the prod bundle.

## Release new version

Run ["CI & Release" workflow](https://github.com/sanity-io/code-input/actions).
Expand Down
2 changes: 0 additions & 2 deletions src/CodeInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ export function CodeInput(props: CodeInputProps) {
<EditorContainer radius={1} shadow={1} readOnly={readOnly}>
{AceEditor && (
<Suspense fallback={<div>Loading code editor...</div>}>
(
<AceEditor
ref={aceEditorRef}
mode={mode}
Expand All @@ -299,7 +298,6 @@ export function CodeInput(props: CodeInputProps) {
onFocus={handleCodeFocus}
onBlur={onBlur}
/>
)
</Suspense>
)}
</EditorContainer>
Expand Down
58 changes: 31 additions & 27 deletions src/PreviewCode.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import React, {useCallback, useEffect, useRef} from 'react'
import AceEditor from 'react-ace'
import React, {Suspense, useCallback, useEffect, useRef} from 'react'
import styled from 'styled-components'
import {Box} from '@sanity/ui'
import {ACE_EDITOR_PROPS, ACE_SET_OPTIONS} from './config'
import createHighlightMarkers from './createHighlightMarkers'
import {CodeInputType, CodeInputValue} from './types'
import './ace-editor/editorSupport'
import {useAceEditor} from './ace-editor/AceEditorLazy'

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

const mode = value?.language || fixedLanguage || 'text'
const AceEditor = useAceEditor()

return (
<PreviewContainer>
<PreviewInner padding={4}>
<AceEditor
ref={aceEditorRef}
focus={false}
mode={mode}
theme="monokai"
width="100%"
onChange={handleEditorChange}
maxLines={200}
readOnly
wrapEnabled
showPrintMargin={false}
highlightActiveLine={false}
cursorStart={-1}
value={(value && value.code) || ''}
markers={
value && value.highlightedLines
? createHighlightMarkers(value.highlightedLines)
: undefined
}
tabSize={2}
showGutter={false}
setOptions={ACE_SET_OPTIONS}
editorProps={ACE_EDITOR_PROPS}
/>
{AceEditor && (
<Suspense fallback={<div>Loading code editor...</div>}>
<AceEditor
ref={aceEditorRef}
focus={false}
mode={mode}
theme="monokai"
width="100%"
onChange={handleEditorChange}
maxLines={200}
readOnly
wrapEnabled
showPrintMargin={false}
highlightActiveLine={false}
cursorStart={-1}
value={(value && value.code) || ''}
markers={
value && value.highlightedLines
? createHighlightMarkers(value.highlightedLines)
: undefined
}
tabSize={2}
showGutter={false}
setOptions={ACE_SET_OPTIONS}
editorProps={ACE_EDITOR_PROPS}
/>
</Suspense>
)}
</PreviewInner>
</PreviewContainer>
)
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {CodeInputLanguage} from './types'

// NOTE: MAKE SURE THESE ALIGN WITH IMPORTS IN ./editorSupport
// NOTE: MAKE SURE THESE ALIGN WITH IMPORTS IN ./ace-editor/editorSupport
export const SUPPORTED_LANGUAGES: CodeInputLanguage[] = [
{title: 'Batch file', value: 'batchfile'},
{title: 'C#', value: 'csharp'},
Expand Down
2 changes: 1 addition & 1 deletion src/createHighlightMarkers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {IMarker} from 'react-ace'
import type {IMarker} from 'react-ace'
import {css} from 'styled-components'

export const highlightMarkersCSS = css`
Expand Down

0 comments on commit 16eb077

Please sign in to comment.