This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathimage-editor.coffee
More file actions
139 lines (114 loc) · 4.11 KB
/
image-editor.coffee
File metadata and controls
139 lines (114 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
_ = require 'underscore-plus'
path = require 'path'
fs = require 'fs-plus'
{Emitter, File, CompositeDisposable} = require 'atom'
# Editor model for an image file
module.exports =
class ImageEditor
atom.deserializers.add(this)
@deserialize: ({filePath}) ->
if fs.isFileSync(filePath)
new ImageEditor(filePath)
else
console.warn "Could not deserialize image editor for path '#{filePath}' because that file no longer exists"
constructor: (filePath) ->
@file = new File(filePath)
@uri = "file://" + encodeURI(filePath.replace(/\\/g, '/')).replace(/#/g, '%23').replace(/\?/g, '%3F')
@subscriptions = new CompositeDisposable()
@emitter = new Emitter
serialize: ->
{filePath: @getPath(), deserializer: @constructor.name}
getViewClass: ->
require './image-editor-view'
terminatePendingState: ->
@emitter.emit 'did-terminate-pending-state' if this.isEqual(atom.workspace.getActivePane().getPendingItem())
onDidTerminatePendingState: (callback) ->
@emitter.on 'did-terminate-pending-state', callback
# Register a callback for when the image file changes
onDidChange: (callback) ->
changeSubscription = @file.onDidChange(callback)
@subscriptions.add(changeSubscription)
changeSubscription
# Register a callback for when the image's title changes
onDidChangeTitle: (callback) ->
renameSubscription = @file.onDidRename(callback)
@subscriptions.add(renameSubscription)
renameSubscription
destroy: ->
@subscriptions.dispose()
# Essential: Retrieves all {ImageEditor}s in the workspace.
#
# Returns an {Array} of {ImageEditor}s.
getImageEditors: ->
atom.workspace.getPaneItems().filter (item) -> item instanceof ImageEditor
# Essential: Get the {ImageEditor}s title for display in other parts
# of the UI such as tabs.
#
# This is `'untitled'` if the image not saved to the disk.
#
# Returns a {String}.
getTitle: ->
@getFileName() ? 'untitled'
# Essential: Get unique title for display in other parts of the UI, such as
# the window title.
#
# If the image is not saved to disk its title is "untitled"
# If the image is saved, its unique title is formatted as one
# of the following,
# * "<filename>" when it is the only existing {ImageEditor} with this file name.
# * "<filename> — <unique-dir-prefix>" when other {ImageEditor}s have this file name.
#
# Returns a {String}
getLongTitle: ->
if @getPath()
fileName = @getFileName()
allPathSegments = []
for imageEditor in @getImageEditors() when imageEditor isnt this
if imageEditor.getFileName() is fileName
allPathSegments.push(imageEditor.getDirectoryPath().split(path.sep))
if allPathSegments.length is 0
return fileName
ourPathSegments = @getDirectoryPath().split(path.sep)
allPathSegments.push ourPathSegments
loop
firstSegment = ourPathSegments[0]
commonBase = _.all(allPathSegments, (pathSegments) -> pathSegments.length > 1 and pathSegments[0] is firstSegment)
if commonBase
pathSegments.shift() for pathSegments in allPathSegments
else
break
"#{fileName} \u2014 #{path.join(pathSegments...)}"
else
'untitled'
getFileName: ->
if filePath = @getPath()
path.basename(filePath)
else
null
getDirectoryPath: ->
if fullPath = @getPath()
path.dirname(fullPath)
else
null
# Retrieves the URI of the image.
#
# Returns a {String}.
getURI: -> @uri
# Retrieves the absolute path to the image.
#
# Returns a {String} path.
getPath: -> @file.getPath()
# Compares two {ImageEditor}s to determine equality.
#
# Equality is based on the condition that the two URIs are the same.
#
# Returns a {Boolean}.
isEqual: (other) ->
other instanceof ImageEditor and @getURI() is other.getURI()
# Essential: Invoke the given callback when the editor is destroyed.
#
# * `callback` {Function} to be called when the editor is destroyed.
#
# Returns a {Disposable} on which `.dispose()` can be called to unsubscribe.
onDidDestroy: (callback) ->
@emitter.on 'did-destroy', callback