Skip to content

Commit

Permalink
feat(react-tinacms-inline): previewUrl is now optionally async
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-anderson authored and ncphillips committed Aug 5, 2020
1 parent 8e57a0e commit 3aaead3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/demo-gatsby/gatsby-config.js
Expand Up @@ -36,6 +36,7 @@ module.exports = {
resolve: "gatsby-plugin-tinacms",
options: {
manualInit: true,
enabled: true,
sidebar: {
hidden: process.env.NODE_ENV === "production",
position: "displace",
Expand Down
7 changes: 7 additions & 0 deletions packages/demo-gatsby/src/templates/blog-post.js
Expand Up @@ -201,6 +201,13 @@ function BlogPostTemplate(props) {

return media.map(m => `/images/${m.filename}`)
},
previewUrl(src) {
return new Promise(resolve => {
setTimeout(() => {
resolve(src)
}, 3000)
})
},
}}
>
<div
Expand Down
9 changes: 7 additions & 2 deletions packages/react-tinacms-editor/src/plugins/Image/nodeView.ts
Expand Up @@ -30,7 +30,7 @@ export class ImageView implements NodeView {
constructor(
node: Node,
view: EditorView,
previewSrc: (url: string) => string = Identity
private previewSrc: (src: string) => string = Identity
) {
this.node = node
this.view = view
Expand All @@ -39,14 +39,19 @@ export class ImageView implements NodeView {
this.dom.classList.add('tinacms-image-wrapper')
this.img = document.createElement('img')
const { src, align, alt, title, width, height } = node.attrs
this.img.src = previewSrc(src)
this.updateImgSrc(src)
if (height) this.img.style.height = height
if (width) this.img.style.width = width
if (align) this.img.classList.add(`align-${align}`)
if (alt) this.img.alt = alt
if (title) this.img.title = title
this.dom.appendChild(this.img)
}
async updateImgSrc(src: string) {
if (this.img) {
this.img.src = await this.previewSrc(src)
}
}

update(node: Node) {
if (this.img) {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-tinacms-editor/src/types.ts
Expand Up @@ -26,7 +26,7 @@ import { Format } from './translator'

export interface ImageProps {
upload?: (files: File[]) => Promise<string[]>
previewUrl?: (url: string) => string
previewUrl?: (url: string) => string | Promise<string>
}

export interface KeymapPlugin {
Expand Down

0 comments on commit 3aaead3

Please sign in to comment.