mirror of
https://github.com/atom/atom.git
synced 2026-01-24 14:28:14 -05:00
Pull out image-view package into separate repo
This commit is contained in:
@@ -70,6 +70,7 @@
|
||||
"archive-view": "0.1.0",
|
||||
"autoflow": "0.1.0",
|
||||
"command-logger": "0.1.0",
|
||||
"image-view": "0.1.0",
|
||||
"spell-check": "0.1.0",
|
||||
"terminal": "0.3.0"
|
||||
},
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
'.image-view':
|
||||
'meta-+': 'image-view:zoom-in'
|
||||
'meta-=': 'image-view:zoom-in'
|
||||
'meta--': 'image-view:zoom-out'
|
||||
'meta-_': 'image-view:zoom-out'
|
||||
'meta-0': 'image-view:reset-zoom'
|
||||
@@ -1,64 +0,0 @@
|
||||
fsUtils = require 'fs-utils'
|
||||
path = require 'path'
|
||||
_ = require 'underscore'
|
||||
|
||||
# Public: Manages the states between {Editor}s, images, and the project as a whole.
|
||||
#
|
||||
# Essentially, the graphical version of a {EditSession}.
|
||||
module.exports=
|
||||
class ImageEditSession
|
||||
registerDeserializer(this)
|
||||
|
||||
@activate: ->
|
||||
# Files with these extensions will be opened as images
|
||||
imageExtensions = ['.gif', '.jpeg', '.jpg', '.png']
|
||||
Project = require 'project'
|
||||
Project.registerOpener (filePath) ->
|
||||
if _.include(imageExtensions, path.extname(filePath))
|
||||
new ImageEditSession(filePath)
|
||||
|
||||
@deserialize: ({path}={}) ->
|
||||
if fsUtils.exists(path)
|
||||
new ImageEditSession(path)
|
||||
else
|
||||
console.warn "Could not build image edit session for path '#{path}' because that file no longer exists"
|
||||
|
||||
constructor: (@path) ->
|
||||
|
||||
serialize: ->
|
||||
deserializer: 'ImageEditSession'
|
||||
path: @path
|
||||
|
||||
getViewClass: ->
|
||||
require './image-view'
|
||||
|
||||
### Public ###
|
||||
|
||||
# Retrieves the filename of the open file.
|
||||
#
|
||||
# This is `'untitled'` if the file is new and not saved to the disk.
|
||||
#
|
||||
# Returns a {String}.
|
||||
getTitle: ->
|
||||
if sessionPath = @getPath()
|
||||
path.basename(sessionPath)
|
||||
else
|
||||
'untitled'
|
||||
|
||||
# Retrieves the URI of the current image.
|
||||
#
|
||||
# Returns a {String}.
|
||||
getUri: -> @path
|
||||
|
||||
# Retrieves the path of the current image.
|
||||
#
|
||||
# Returns a {String}.
|
||||
getPath: -> @path
|
||||
|
||||
# Compares two `ImageEditSession`s to determine equality.
|
||||
#
|
||||
# Equality is based on the condition that the two URIs are the same.
|
||||
#
|
||||
# Returns a {Boolean}.
|
||||
isEqual: (other) ->
|
||||
other instanceof ImageEditSession and @getUri() is other.getUri()
|
||||
@@ -1,101 +0,0 @@
|
||||
ScrollView = require 'scroll-view'
|
||||
_ = require 'underscore'
|
||||
$ = require 'jquery'
|
||||
|
||||
# Public: Renders images in the {Editor}.
|
||||
module.exports =
|
||||
class ImageView extends ScrollView
|
||||
|
||||
### Internal ###
|
||||
|
||||
@content: ->
|
||||
@div class: 'image-view', tabindex: -1, =>
|
||||
@div class: 'image-container', =>
|
||||
@img outlet: 'image'
|
||||
|
||||
initialize: (imageEditSession) ->
|
||||
super
|
||||
|
||||
@image.load =>
|
||||
@originalHeight = @image.height()
|
||||
@originalWidth = @image.width()
|
||||
@loaded = true
|
||||
@centerImage()
|
||||
@setPath(imageEditSession?.getPath())
|
||||
|
||||
@subscribe $(window), 'resize', _.debounce((=> @centerImage()), 300)
|
||||
@command 'image-view:zoom-in', => @zoomIn()
|
||||
@command 'image-view:zoom-out', => @zoomOut()
|
||||
@command 'image-view:reset-zoom', => @resetZoom()
|
||||
|
||||
afterAttach: (onDom) ->
|
||||
return unless onDom
|
||||
|
||||
if pane = @getPane()
|
||||
@active = @is(pane.activeView)
|
||||
@subscribe pane, 'pane:active-item-changed', (event, item) =>
|
||||
wasActive = @active
|
||||
@active = @is(pane.activeView)
|
||||
@centerImage() if @active and not wasActive
|
||||
|
||||
### Public ###
|
||||
|
||||
# Places the image in the center of the {Editor}.
|
||||
centerImage: ->
|
||||
return unless @loaded and @isVisible()
|
||||
|
||||
@image.css
|
||||
'top': Math.max((@height() - @image.outerHeight()) / 2, 0)
|
||||
'left': Math.max((@width() - @image.outerWidth()) / 2, 0)
|
||||
@image.show()
|
||||
|
||||
# Indicates the path of the image.
|
||||
#
|
||||
# path - A {String} for the new image path.
|
||||
setPath: (path) ->
|
||||
if path?
|
||||
if @image.attr('src') isnt path
|
||||
@loaded = false
|
||||
@image.hide().attr('src', path)
|
||||
else
|
||||
@image.hide()
|
||||
|
||||
# Retrieve's the {Editor}'s pane.
|
||||
#
|
||||
# Returns a {Pane}.
|
||||
getPane: ->
|
||||
@parent('.item-views').parent('.pane').view()
|
||||
|
||||
# Zooms the image out.
|
||||
#
|
||||
# This is done by a factor of `0.9`.
|
||||
zoomOut: ->
|
||||
@adjustSize(0.9)
|
||||
|
||||
# Zooms the image in.
|
||||
#
|
||||
# This is done by a factor of `1.1`.
|
||||
zoomIn: ->
|
||||
@adjustSize(1.1)
|
||||
|
||||
# Zooms the image to its normal width and height.
|
||||
resetZoom: ->
|
||||
return unless @loaded and @isVisible()
|
||||
|
||||
@image.width(@originalWidth)
|
||||
@image.height(@originalHeight)
|
||||
@centerImage()
|
||||
|
||||
### Internal ###
|
||||
|
||||
adjustSize: (factor) ->
|
||||
return unless @loaded and @isVisible()
|
||||
|
||||
newWidth = @image.width() * factor
|
||||
newHeight = @image.height() * factor
|
||||
@image.width(newWidth)
|
||||
@image.height(newHeight)
|
||||
@centerImage()
|
||||
|
||||
setModel: (imageEditSession) ->
|
||||
@setPath(imageEditSession?.getPath())
|
||||
@@ -1,3 +0,0 @@
|
||||
'description': 'View images in the editor'
|
||||
'main': './lib/image-edit-session'
|
||||
'deferredDeserializers': ['ImageEditSession']
|
||||
@@ -1,78 +0,0 @@
|
||||
ImageView = require '../lib/image-view'
|
||||
ImageEditSession = require '../lib/image-edit-session'
|
||||
|
||||
describe "ImageView", ->
|
||||
[view, path] = []
|
||||
|
||||
beforeEach ->
|
||||
path = project.resolve('binary-file.png')
|
||||
view = new ImageView()
|
||||
view.attachToDom()
|
||||
view.height(100)
|
||||
|
||||
it "displays the image for a path", ->
|
||||
view.setModel(new ImageEditSession(path))
|
||||
expect(view.image.attr('src')).toBe path
|
||||
|
||||
it "centers the image in the editor", ->
|
||||
imageLoaded = false
|
||||
view.image.load =>
|
||||
imageLoaded = true
|
||||
view.setModel(new ImageEditSession(path))
|
||||
|
||||
waitsFor ->
|
||||
imageLoaded
|
||||
|
||||
runs ->
|
||||
expect(view.image.width()).toBe 10
|
||||
expect(view.image.height()).toBe 10
|
||||
expect(view.image.css('left')).toBe "#{(view.width() - view.image.outerWidth()) / 2}px"
|
||||
expect(view.image.css('top')).toBe "#{(view.height() - view.image.outerHeight()) / 2}px"
|
||||
|
||||
describe "image-view:zoom-in", ->
|
||||
it "increases the image size by 10%", ->
|
||||
imageLoaded = false
|
||||
view.image.load =>
|
||||
imageLoaded = true
|
||||
view.setModel(new ImageEditSession(path))
|
||||
|
||||
waitsFor ->
|
||||
imageLoaded
|
||||
|
||||
runs ->
|
||||
view.trigger 'image-view:zoom-in'
|
||||
expect(view.image.width()).toBe 11
|
||||
expect(view.image.height()).toBe 11
|
||||
|
||||
describe "image-view:zoom-out", ->
|
||||
it "decreases the image size by 10%", ->
|
||||
imageLoaded = false
|
||||
view.image.load =>
|
||||
imageLoaded = true
|
||||
view.setModel(new ImageEditSession(path))
|
||||
|
||||
waitsFor ->
|
||||
imageLoaded
|
||||
|
||||
runs ->
|
||||
view.trigger 'image-view:zoom-out'
|
||||
expect(view.image.width()).toBe 9
|
||||
expect(view.image.height()).toBe 9
|
||||
|
||||
describe "image-view:reset-zoom", ->
|
||||
it "restores the image to the original size", ->
|
||||
imageLoaded = false
|
||||
view.image.load =>
|
||||
imageLoaded = true
|
||||
view.setModel(new ImageEditSession(path))
|
||||
|
||||
waitsFor ->
|
||||
imageLoaded
|
||||
|
||||
runs ->
|
||||
view.trigger 'image-view:zoom-in'
|
||||
expect(view.image.width()).not.toBe 10
|
||||
expect(view.image.height()).not.toBe 10
|
||||
view.trigger 'image-view:reset-zoom'
|
||||
expect(view.image.width()).toBe 10
|
||||
expect(view.image.height()).toBe 10
|
||||
@@ -1,22 +0,0 @@
|
||||
.image-view {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: auto;
|
||||
position: relative;
|
||||
display: -webkit-flex;
|
||||
|
||||
.image-container {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
img {
|
||||
box-sizing: content-box;
|
||||
padding: 25px;
|
||||
border: 2px solid;
|
||||
background-image: url(images/transparent-background.gif);
|
||||
position: relative;
|
||||
max-width: none;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user