Skip to content

Commit

Permalink
fix: initial v3 work
Browse files Browse the repository at this point in the history
  • Loading branch information
robinpyon committed Jun 15, 2022
1 parent 1e12e3c commit 4517bb5
Show file tree
Hide file tree
Showing 80 changed files with 29,120 additions and 7,145 deletions.
3 changes: 0 additions & 3 deletions .d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
declare module 'part:@sanity/*'
declare module 'part:sanity-plugin-media/*'
declare module 'react-file-icon'
declare module 'theme-ui'
6 changes: 4 additions & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.eslintrc.js
commitlint.config.js
coverage
dist
lib
lint-staged.config.js
node_modules
node_modules

.parcel-cache
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: CI
on:
- push
- pull_request
jobs:
test:
runs-on: ${{ matrix.platform }}
name: Node.js ${{ matrix.node-version }} / ${{ matrix.platform }}
strategy:
fail-fast: false
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
node-version:
- 16
steps:
- name: Set git to use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm run lint
- run: npm run build
#- run: npm test
14 changes: 12 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Compiled files
dist
lib

# Dependency directories
/node_modules
Expand All @@ -15,4 +15,14 @@ yarn-error.log*
.DS_Store

# VS Code
.vscode
.vscode

# Parcel
.parcel-cache

# Yarn
yarn.lock

# IntelliJ
.idea/
*.iml
96 changes: 70 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Sanity Media

> ⚠️ This version of `sanity-plugin-media` is for [Sanity Studio V3](https://www.sanity.io/blog/sanity-studio-v3-developer-preview), which is currently in developer preview.
>
> The Studio V2 compatible version can be found on the [V2 branch](https://github.com/robinpyon/sanity-plugin-media).
## What is it?

![npm-v](https://img.shields.io/npm/v/sanity-plugin-media?style=flat-square)
![npm-dw](https://img.shields.io/npm/dw/sanity-plugin-media?style=flat-square)

Expand Down Expand Up @@ -40,51 +46,67 @@ _Individual asset view_
- Built with the same [UI components Sanity uses](https://www.sanity.io/ui) under the hood
- Fully responsive and mobile friendly

## Install
## Install (V3 Studio only)

In your Sanity project folder:
In your Sanity project folder, install the plugin and its peer dependency:

```sh
sanity install media
npm install --save sanity-plugin-media@v3-studio @mdx-js/react
```

This will add the Media plugin as a standalone tool, accessible via your studio menu.
or

If you're happy with Sanity's default image and file pickers, then this is all you need to do!

### Enabling it as a global [custom asset source](https://www.sanity.io/docs/custom-asset-sources)
```sh
yarn add sanity-plugin-media@v3-studio @mdx-js/react
```

You'll need to do this if you want to use the plugin when selecting images or files.
`@mdx-js/react` will be removed as a dependency in a future version.

This plugin exposes `part:sanity-plugin-media/asset-source` which you import when defining custom asset sources.
## Usage

In `sanity.json`, add the following snippet to the `parts` array.
Add it as a plugin in your `sanity.config.ts` (or .js) file:

_File asset support requires Sanity 2.16.x or greater._
```js
import {media} from 'sanity-plugin-media'

```json
{
"implements": "part:@sanity/form-builder/input/image/asset-sources",
"path": "./parts/assetSources.js"
},
{
"implements": "part:@sanity/form-builder/input/file/asset-sources",
"path": "./parts/assetSources.js"
},
export default createConfig({
// ...
plugins: [media()]
})
```

In `./parts/assetSources.js`:
This will enable the Media plugin as both a standalone tool (accessible in your studio menu) and as an additional asset source for your image and file fields.

```js
import MediaAssetSource from 'part:sanity-plugin-media/asset-source'
### Customizing the asset source

export default [MediaAssetSource]
```
You can configure your studio to use this asset source either exclusively, or conditionally enable it based on the type of asset (image or file).

That's it! The plugin will now pop up every time you try select an image or file.
```js
import {media, mediaAssetSource} from 'sanity-plugin-media'

export default createConfig({
// ...
plugins: [media()],
form: {
// Don't use this plugin when selecting files only (but allow all other enabled asset sources)
file: {
assetSources: previousAssetSources => {
return previousAssetSources.filter(assetSource => assetSource !== mediaAssetSource)
}
}
}
})
```

## Known issues

<details>
<summary>(V3) Where has the references tab gone?</summary>

- This will be enabled in a future version!

</details>

<details>
<summary>There isn't a way to edit asset fields directly from the desk (without opening the plugin)</summary>

Expand Down Expand Up @@ -233,3 +255,25 @@ Contributions, issues and feature requests are welcome!
## License

MIT. See [license](LICENSE)

## Develop & test

Make sure to run `npm run build` once, then run

```bash
npm run link-watch
```

In another shell, `cd` to your test studio and run:

```bash
npx yalc add sanity-plugin-media && yarn install
```

Now, changes in this repo will be automatically built and pushed to the studio,
triggering hotreload. Yalc avoids issues with react-hooks that are typical when using yarn/npm link.

### About build & watch

This plugin uses [@sanity/plugin-sdk](https://github.com/sanity-io/plugin-sdk)
with default configuration for build & watch scripts.

0 comments on commit 4517bb5

Please sign in to comment.