Skip to content

Commit

Permalink
Merge pull request #15 from BohemianCoding/feature/21123
Browse files Browse the repository at this point in the history
Store & reuse search terms
  • Loading branch information
bomberstudios authored Feb 26, 2019
2 parents f3a3c3a + 2e763b0 commit f6d054d
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/DataProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,20 @@ function extractPhotoId (searchTerm) {
}

export function onSearchPhoto (context) {
const searchTerm = UI.getStringFromUser('Search Unsplash for…', 'People').trim()
// 21123: retrieve previous search term. If multiple layers are selected, find the first search term
// in the group…
let selectedLayers = sketch.getSelectedDocument().selectedLayers.layers
let previousTerms = selectedLayers.map(layer => Settings.layerSettingForKey(layer, 'unsplash.search.term'))
let firstPreviousTerm = previousTerms.find(term => term !== undefined)
let previousTerm = firstPreviousTerm || 'People'
// TODO: use `UI.getInputFromUser`
// TODO: do not perform search if user hits Cancel
// TODO: support multiple selected layers with different search terms for each
const searchTerm = UI.getStringFromUser('Search Unsplash for…', previousTerm).trim()
if (searchTerm !== 'null') {
selectedLayers.forEach(layer => {
Settings.setLayerSettingForKey(layer, 'unsplash.search.term', searchTerm)
})
if (containsPhotoId(searchTerm)) {
setImageForContext(context, null, extractPhotoId(searchTerm))
} else {
Expand All @@ -82,7 +94,7 @@ export default function onImageDetails () {
} else {
// This layer doesn't have an Unsplash photo set, do nothing.
// Alternatively, show an explanation of what the user needs to do to make this work…
UI.message(`To get a random photo, click Data › Unsplash Random Photo in the toolbar, or right click the layer › Data Feeds › Unsplash Random Photo`)
UI.message(`To get a random photo, click Data › Unsplash Random Photo in the toolbar, or right click the layer › Data Feeds › Unsplash Random Photo`)
}
})
} else {
Expand Down

0 comments on commit f6d054d

Please sign in to comment.