Skip to content

Commit

Permalink
Merge pull request #1576 from marktext/unsplash-ux
Browse files Browse the repository at this point in the history
Improve image picker UX
  • Loading branch information
Jocs committed Nov 2, 2019
2 parents 1fbc0bf + be64ab6 commit 0ced076
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/muya/lib/ui/imageSelector/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
text-align: center;
display: block;
color: var(--editorColor30);
user-select: none;
}

.ag-image-selector span.description a {
Expand Down Expand Up @@ -154,13 +155,15 @@
text-align: center;
font-size: 14px;
color: var(--editorColor);
user-select: none;
}

.ag-image-selector .more {
font-size: 12px;
color: var(--editorColor);
text-align: center;
margin-bottom: 20px;
user-select: none;
}

.ag-image-selector .photo {
Expand Down
61 changes: 39 additions & 22 deletions src/muya/lib/ui/imageSelector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ImageSelector extends BaseFloat {

constructor (muya, options) {
const name = 'ag-image-selector'
const { accessKey } = options
const { unsplashAccessKey } = options
options = Object.assign(options, {
placement: 'bottom-center',
modifiers: {
Expand All @@ -26,9 +26,13 @@ class ImageSelector extends BaseFloat {
this.renderArray = []
this.oldVnode = null
this.imageInfo = null
this.unsplash = new Unsplash({
accessKey
})
if (!unsplashAccessKey) {
this.unsplash = null
} else {
this.unsplash = new Unsplash({
accessKey: unsplashAccessKey
})
}
this.photoList = []
this.loading = false
this.tab = 'link' // select or link
Expand Down Expand Up @@ -56,22 +60,27 @@ class ImageSelector extends BaseFloat {
}

Object.assign(this.state, imageInfo.token.attrs)
// load latest unsplash photos.
this.loading = true
this.unsplash.photos.listPhotos(1, 40, 'latest')
.then(toJson)
.then(json => {
this.loading = false
if (Array.isArray(json)) {
this.photoList = json
if (this.tab === 'unsplash') {
this.render()

if (this.unsplash) {
// Load latest unsplash photos.
this.loading = true
this.unsplash.photos.listPhotos(1, 40, 'latest')
.then(toJson)
.then(json => {
this.loading = false
if (Array.isArray(json)) {
this.photoList = json
if (this.tab === 'unsplash') {
this.render()
}
}
}
})
})
}

this.imageInfo = imageInfo
this.show(reference, cb)
this.render()

// Auto focus and select all content of the `src.input` element.
const input = this.imageSelectorContainer.querySelector('input.src')
if (input) {
Expand All @@ -85,6 +94,10 @@ class ImageSelector extends BaseFloat {
}

searchPhotos = (keyword) => {
if (!this.unsplash) {
return
}

this.loading = true
this.photoList = []
this.unsplash.search.photos(keyword, 1, 40)
Expand Down Expand Up @@ -253,11 +266,15 @@ class ImageSelector extends BaseFloat {
}, {
label: 'Embed link',
value: 'link'
}, {
label: 'Unsplash',
value: 'unsplash'
}]

if (this.unsplash) {
tabs.push({
label: 'Unsplash',
value: 'unsplash'
})
}

const children = tabs.map(tab => {
const itemSelector = this.tab === tab.value ? 'li.active' : 'li'
return h(itemSelector, h('span', {
Expand Down Expand Up @@ -285,7 +302,7 @@ class ImageSelector extends BaseFloat {
}
}
}, 'Choose an Image'),
h('span.description', 'Choose image from you computer.')
h('span.description', 'Choose image from your computer.')
]
} else if (tab === 'link') {
const altInput = h('input.alt', {
Expand Down Expand Up @@ -355,14 +372,14 @@ class ImageSelector extends BaseFloat {
}
}, 'Embed Image')
const bottomDes = h('span.description', [
h('span', 'Paste web image or local image path, '),
h('span', 'Paste web image or local image path. Use '),
h('a', {
on: {
click: event => {
this.toggleMode()
}
}
}, `${isFullMode ? 'simple mode' : 'full mode'}`)
}, `${isFullMode ? 'simple mode' : 'full mode'}.`)
])
bodyContent = [inputWrapper, embedButton, bottomDes]
} else {
Expand Down
1 change: 0 additions & 1 deletion src/muya/themes/default.css
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,6 @@ kbd {
border-left-color: transparent;
border-right-color: transparent;
}

} /* end not print */

@media print {
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/components/editorWithTabs/editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ export default {
Muya.use(EmojiPicker)
Muya.use(ImagePathPicker)
Muya.use(ImageSelector, {
accessKey: process.env.UNSPLASH_ACCESS_KEY,
unsplashAccessKey: process.env.UNSPLASH_ACCESS_KEY,
photoCreatorClick: this.photoCreatorClick
})
Muya.use(Transformer)
Expand Down

0 comments on commit 0ced076

Please sign in to comment.