Skip to content

Commit

Permalink
feat(@tinacms/core): add cms.media.open top make showing the media ma…
Browse files Browse the repository at this point in the history
…nager easier
  • Loading branch information
ncphillips committed Sep 14, 2020
1 parent 77573cb commit eea3081
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/@tinacms/core/src/cms.ts
Expand Up @@ -118,7 +118,7 @@ export class CMS {

events = new EventBus()

media = new MediaManager(new DummyMediaStore())
media = new MediaManager(new DummyMediaStore(), this.events)

/**
* @hidden
Expand Down
15 changes: 14 additions & 1 deletion packages/@tinacms/core/src/media.ts
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
*/

import { EventBus } from './event'

/**
* Represents an individual file in the MediaStore
*/
Expand Down Expand Up @@ -119,7 +121,14 @@ export interface MediaList {
* ```
*/
export class MediaManager implements MediaStore {
constructor(public store: MediaStore) {}
constructor(public store: MediaStore, private events: EventBus) {}

open(options: SelectMediaOptions = {}) {
this.events.dispatch({
type: 'media:open',
...options,
})
}

get accept() {
return this.store.accept
Expand All @@ -137,3 +146,7 @@ export class MediaManager implements MediaStore {
return this.store.list(options)
}
}

export interface SelectMediaOptions {
onSelect?(media: Media): void
}
3 changes: 1 addition & 2 deletions packages/@tinacms/fields/src/plugins/ImageFieldPlugin.tsx
Expand Up @@ -76,8 +76,7 @@ export const ImageField = wrapFieldsWithMeta<InputProps, ImageProps>(props => {
previewSrc={src}
loading={srcIsLoading}
onClick={() => {
cms.events.dispatch({
type: 'media:open',
cms.media.open({
onSelect(media: any) {
if (media.filename == props.input.value) {
props.input.onChange('') // trigger rerender
Expand Down

0 comments on commit eea3081

Please sign in to comment.