Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Consume File-Icons' element-icon service #281

Merged
merged 11 commits into from
Oct 30, 2017
3 changes: 2 additions & 1 deletion lib/default-file-icons.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs-plus')
const path = require('path')

module.exports =
class DefaultFileIcons {
iconClassForPath (filePath) {
const extension = path.extname(filePath)
Expand All @@ -23,3 +22,5 @@ class DefaultFileIcons {
}
}
}

module.exports = new DefaultFileIcons()
21 changes: 0 additions & 21 deletions lib/file-icons.js

This file was deleted.

48 changes: 22 additions & 26 deletions lib/fuzzy-finder-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import path from 'path'
import SelectListView from 'atom-select-list'

import {repositoryForPath} from './helpers'
import FileIcons from './file-icons'
import IconServices from './icon-services'

export default class FuzzyFinderView {
constructor () {
Expand Down Expand Up @@ -39,6 +39,10 @@ export default class FuzzyFinderView {
this.confirm()
},
didChangeQuery: () => {
if (this.iconDisposables) {
this.iconDisposables.dispose()
this.iconDisposables = null
}
const isLineJump = this.isQueryALineJump()
if (!this.previousQueryWasLineJump && isLineJump) {
this.previousQueryWasLineJump = true
Expand All @@ -63,9 +67,10 @@ export default class FuzzyFinderView {
const li = document.createElement('li')
li.classList.add('two-lines')

const repository = repositoryForPath(filePath)
this.filePath = filePath
const repository = repositoryForPath(this.filePath)
if (repository) {
const status = repository.getCachedPathStatus(filePath)
const status = repository.getCachedPathStatus(this.filePath)
if (repository.isStatusNew(status)) {
const div = document.createElement('div')
div.classList.add('status', 'status-added', 'icon', 'icon-diff-added')
Expand All @@ -77,30 +82,21 @@ export default class FuzzyFinderView {
}
}

const iconClasses = FileIcons.getService().iconClassForPath(filePath, 'fuzzy-finder')
let classList
if (Array.isArray(iconClasses)) {
classList = iconClasses
} else if (iconClasses) {
classList = iconClasses.toString().split(/\s+/g)
} else {
classList = []
}

const fileBasename = path.basename(filePath)
const fileBasename = path.basename(this.filePath)
const baseOffset = projectRelativePath.length - fileBasename.length
const primaryLine = document.createElement('div')
primaryLine.classList.add('primary-line', 'file', 'icon', ...classList)
primaryLine.dataset.name = fileBasename
primaryLine.dataset.path = projectRelativePath
primaryLine.appendChild(highlight(fileBasename, matches, baseOffset))
li.appendChild(primaryLine)

const secondaryLine = document.createElement('div')
secondaryLine.classList.add('secondary-line', 'path', 'no-icon')
secondaryLine.appendChild(highlight(projectRelativePath, matches, 0))
li.appendChild(secondaryLine)

this.primaryLine = document.createElement('div')
this.primaryLine.dataset.name = fileBasename
this.primaryLine.dataset.path = projectRelativePath
this.primaryLine.classList.add('primary-line', 'file', 'icon')
this.primaryLine.appendChild(highlight(fileBasename, matches, baseOffset))
li.appendChild(this.primaryLine)

this.secondaryLine = document.createElement('div')
this.secondaryLine.classList.add('secondary-line', 'path', 'no-icon')
this.secondaryLine.appendChild(highlight(projectRelativePath, matches, 0))
li.appendChild(this.secondaryLine)

IconServices.updateIcon(this)
return li
}
})
Expand Down
59 changes: 59 additions & 0 deletions lib/icon-services.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const DefaultFileIcons = require('./default-file-icons')
const {Emitter, CompositeDisposable} = require('atom')

class IconServices {
constructor () {
this.emitter = new Emitter()
this.elementIcons = null
this.elementIconDisposables = new CompositeDisposable()
this.fileIcons = DefaultFileIcons
}

onDidChange (callback) {
return this.emitter.on('did-change', callback)
}

resetElementIcons () {
this.setElementIcons(null)
}

resetFileIcons () {
this.setFileIcons(DefaultFileIcons)
}

setElementIcons (service) {
if (service !== this.elementIcons) {
if (this.elementIconDisposables != null) {
this.elementIconDisposables.dispose()
}
if (service) { this.elementIconDisposables = new CompositeDisposable() }
this.elementIcons = service
return this.emitter.emit('did-change')
}
}

setFileIcons (service) {
if (service !== this.fileIcons) {
this.fileIcons = service
return this.emitter.emit('did-change')
}
}

updateIcon (view) {
if (this.elementIcons) {
const disposable = this.elementIcons(view.primaryLine, view.filePath)
this.elementIconDisposables.add(disposable)
} else {
let classList = []
const iconClasses = this.fileIcons.iconClassForPath(view.filePath, 'fuzzy-finder')
if (Array.isArray(iconClasses)) {
classList = iconClasses
} else if (iconClasses) {
classList = iconClasses.toString().split(/\s+/g)
}
view.primaryLine.classList.add(...classList)
}
}
}

module.exports = new IconServices()
11 changes: 8 additions & 3 deletions lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {Disposable} = require('atom')
const FileIcons = require('./file-icons')
const IconServices = require('./icon-services')

module.exports = {
activate (state) {
Expand Down Expand Up @@ -48,9 +48,14 @@ module.exports = {
this.active = false
},

consumeElementIcons (service) {
IconServices.setElementIcons(service)
return new Disposable(() => IconServices.resetElementIcons())
},

consumeFileIcons (service) {
FileIcons.setService(service)
return new Disposable(() => FileIcons.resetService())
IconServices.setFileIcons(service)
return new Disposable(() => IconServices.resetFileIcons())
},

serialize () {
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
"versions": {
"1.0.0": "consumeFileIcons"
}
},
"file-icons.element-icons": {
"versions": {
"1.0.0": "consumeElementIcons"
}
}
},
"configSchema": {
Expand Down
6 changes: 2 additions & 4 deletions spec/fuzzy-finder-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,6 @@ describe('FuzzyFinder', () => {
})

describe('file icons', () => {
const fileIcons = new DefaultFileIcons()

it('defaults to text', () => {
waitsForPromise(() => atom.workspace.open('sample.js'))

Expand All @@ -1202,7 +1200,7 @@ describe('FuzzyFinder', () => {

runs(() => {
const firstResult = bufferView.element.querySelector('li .primary-line')
expect(fileIcons.iconClassForPath(firstResult.dataset.path)).toBe('icon-file-text')
expect(DefaultFileIcons.iconClassForPath(firstResult.dataset.path)).toBe('icon-file-text')
})
})

Expand All @@ -1220,7 +1218,7 @@ describe('FuzzyFinder', () => {

runs(() => {
const firstResult = bufferView.element.querySelector('li .primary-line')
expect(fileIcons.iconClassForPath(firstResult.dataset.path)).toBe('icon-file-media')
expect(DefaultFileIcons.iconClassForPath(firstResult.dataset.path)).toBe('icon-file-media')
})
})
})
Expand Down