Skip to content

Commit

Permalink
feat: next.js pages compatability - see readme for details
Browse files Browse the repository at this point in the history
  • Loading branch information
snorrees committed Mar 2, 2023
1 parent b54e89f commit 288398b
Show file tree
Hide file tree
Showing 9 changed files with 1,016 additions and 662 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ yalc.lock

# Compiled plugin
lib

# Output from pakg-utils we dont care about
next.js
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ const myDocument = {
]
}
```
### Next.js compatability
Next.js *without* Next 13 app directory does not support css imports from `node_modules`.

To use this plugin in this context (`pages` directory), use the `sanity-plugin-markdown/next` import instead of `sanity-plugin-markdown`:
```js
import { markdownSchema } from "sanity-plugin-markdown/next";
```

Then, make sure to add
```js
import 'easymde/dist/easymde.min.css'
```

to the top of `pages/_app.tsx`.

### Customizing the default markdown input editor

Expand Down
1,579 changes: 936 additions & 643 deletions package-lock.json

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,30 @@
".": {
"types": "./lib/index.d.ts",
"source": "./src/index.ts",
"import": "./lib/index.esm.js",
"require": "./lib/index.js",
"import": "./lib/index.esm.js",
"default": "./lib/index.esm.js"
},
"./next": {
"types": "./lib/index.d.ts",
"source": "./src/indexNext.ts",
"require": "./lib/indexNext.js",
"import": "./lib/indexNext.esm.js",
"default": "./lib/indexNext.esm.js"
},
"./package.json": "./package.json"
},
"main": "./lib/index.js",
"module": "./lib/index.esm.js",
"source": "./src/index.ts",
"types": "./lib/index.d.ts",
"typesVersions": {
"*": {
"next": [
"./lib/index.d.ts"
]
}
},
"files": [
"lib",
"sanity.json",
Expand All @@ -59,8 +73,8 @@
"devDependencies": {
"@commitlint/cli": "^17.2.0",
"@commitlint/config-conventional": "^17.2.0",
"@sanity/pkg-utils": "^2.1.0",
"@sanity/plugin-kit": "^3.1.1",
"@sanity/pkg-utils": "^2.2.6",
"@sanity/plugin-kit": "^3.1.7",
"@sanity/semantic-release-preset": "^2.0.2",
"@types/react": "^18.0.26",
"@types/styled-components": "^5.1.26",
Expand Down
4 changes: 4 additions & 0 deletions src/commonExports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {markdownSchemaType, type MarkdownDefinition} from './schema'
export {MarkdownInput, defaultMdeTools, type MarkdownInputProps} from './components/MarkdownInput'

export {markdownSchema, type MarkdownConfig} from './plugin'
40 changes: 27 additions & 13 deletions src/components/MarkdownInput.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'easymde/dist/easymde.min.css'
import {type Options as EasyMdeOptions} from 'easymde'
import React, {useCallback, useMemo} from 'react'
import SimpleMdeReact, {SimpleMDEReactProps} from 'react-simplemde-editor'
import React, {Suspense, useCallback, useMemo} from 'react'
// dont import non-types here, it will break SSR on next
import type {SimpleMDEReactProps} from 'react-simplemde-editor'
import {PatchEvent, set, StringInputProps, unset, useClient} from 'sanity'
import {MarkdownOptions} from '../schema'
import {MarkdownInputStyles} from './MarkdownInputStyles'
import {useSimpleMdeReact} from './useSimpleMdeReact'
import {Box, Text} from '@sanity/ui'

export interface MarkdownInputProps extends StringInputProps {
/**
Expand Down Expand Up @@ -77,18 +79,30 @@ export function MarkdownInput(props: MarkdownInputProps) {
[onChange]
)

const SimpleMdeReact = useSimpleMdeReact()

return (
<MarkdownInputStyles>
<SimpleMdeReact
{...reactMdeProps}
ref={ref}
value={value}
onChange={handleChange}
onBlur={onBlur}
onFocus={onFocus}
options={mdeOptions}
spellCheck={false}
/>
{SimpleMdeReact && (
<Suspense
fallback={
<Box padding={3}>
<Text>Loading editor...</Text>
</Box>
}
>
<SimpleMdeReact
{...reactMdeProps}
ref={ref}
value={value}
onChange={handleChange}
onBlur={onBlur}
onFocus={onFocus}
options={mdeOptions}
spellCheck={false}
/>
</Suspense>
)}
</MarkdownInputStyles>
)
}
12 changes: 12 additions & 0 deletions src/components/useSimpleMdeReact.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {lazy, useEffect, useState} from 'react'

export const SimpleMdeReact = lazy(() => import('react-simplemde-editor'))

export function useSimpleMdeReact() {
const [mounted, setMounted] = useState(false)
useEffect(() => {
requestAnimationFrame(() => setMounted(true))
}, [])

return mounted ? SimpleMdeReact : null
}
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export {markdownSchemaType, type MarkdownDefinition} from './schema'
export {MarkdownInput, defaultMdeTools, type MarkdownInputProps} from './components/MarkdownInput'
import 'easymde/dist/easymde.min.css'

export {markdownSchema, type MarkdownConfig} from './plugin'
export * from './commonExports'
1 change: 1 addition & 0 deletions src/indexNext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './commonExports'

0 comments on commit 288398b

Please sign in to comment.