fix: change getAsset to not return a promise (#3232)

* fix: change getAsset to not return a promise

* fix: update markdown widget per getAsset changes

* test: fix editor component image test

* docs: update getAsset docs
This commit is contained in:
Erez Rokah
2020-02-13 02:12:36 +02:00
committed by GitHub
parent 2457c29baf
commit ab685e8594
30 changed files with 317 additions and 355 deletions

View File

@ -59,7 +59,7 @@ The control component receives one (1) callback as a prop: `onChange`.
* onChange (required): Should be called when the users changes the current value. It will ultimately end up updating the EntryDraft object in the Redux Store, thus updating the preview component.
* onAddAsset & onRemoveAsset (optionals): Should be invoked with an `AssetProxy` value object if the field accepts file uploads for media (images, for example). `onAddAsset` will get the current media stored in the Redux state tree while `onRemoveAsset` will remove it. AssetProxy objects are stored in the `Medias` object and referenced in the `EntryDraft` object on the state tree.
Both control and preview widgets receive a `getAsset` selector via props. Displaying the media (or its URI) for the user should always be done via `getAsset`, as it returns a Promise that resolves to an AssetProxy that can return the correct value for both medias already persisted on the server and cached media not yet uploaded.
Both control and preview widgets receive a `getAsset` selector via props. Displaying the media (or its URI) for the user should always be done via `getAsset`, as it returns an AssetProxy that can return the correct value for both medias already persisted on the server and cached media not yet uploaded.
The actual persistence of the content and medias inserted into the control component is delegated to the backend implementation. The backend will be called with the updated values and a list of assetProxy objects for each field of the entry, and should return a promise that can resolve into the persisted entry object and the list of the persisted media URIs.

View File

@ -72,53 +72,19 @@ Registers a template for a folder collection or an individual file in a file col
<script src="https://unpkg.com/netlify-cms@^2.0.0/dist/netlify-cms.js"></script>
<script>
var PostPreview = createClass({
subscribed: true,
getInitialState: function() {
return {
src: '',
};
},
_fetchAsset: function() {
const path = this.props.entry.getIn(['data', 'image']);
path &&
this.props.getAsset(path).then(value => {
if (this.subscribed) {
this.setState({ src: value.toString() });
}
});
},
componentDidMount: function() {
this._fetchAsset();
},
componentWillUnmount: function() {
this.subscribed = false;
},
componentDidUpdate: function(prevProps) {
const prevPath = prevProps.entry.getIn(['data', 'image']);
const path = this.props.entry.getIn(['data', 'image']);
if (prevPath !== path || prevProps.getAsset !== this.props.getAsset) {
this._fetchAsset();
}
},
render: function() {
var entry = this.props.entry;
return h(
'div',
{},
var image = entry.getIn(['data', 'image']);
var bg = this.props.getAsset(image);
return h('div', {},
h('h1', {}, entry.getIn(['data', 'title'])),
h('img', { src: this.state.src }),
h('div', { className: 'text' }, this.props.widgetFor('body')),
h('img', {src: bg.toString()}),
h('div', {"className": "text"}, this.props.widgetFor('body'))
);
},
}
});
CMS.registerPreviewTemplate('posts', PostPreview);
CMS.registerPreviewTemplate("posts", PostPreview);
</script>
```
### Lists and Objects
@ -212,4 +178,4 @@ Registers a template for a folder collection or an individual file in a file col
</article>
}
}
```
```