This repository has been archived by the owner on Dec 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.coffee
113 lines (89 loc) · 4.08 KB
/
main.coffee
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
{CompositeDisposable, File, Emitter} = require 'atom'
clipboard = require 'clipboard'
path = require 'path'
imageFactory = require './image-factory'
CustomNameView = require './custom-name-view'
filenameHelper = require './filename-helper'
module.exports =
subscriptions: null
emitter: null
activate: (state) ->
@subscriptions = new CompositeDisposable
@emitter = new Emitter
successHandler = (imageMarkup) =>
atom.notifications.addSuccess 'Image inserted and stored.', detail: imageMarkup
@emitter.emit 'did-image-insert', imageMarkup: imageMarkup
errorHandler = (error) ->
atom.notifications.addError error.toString(), detail: error.stack or '', dismissable: true
console.error error
@subscriptions.add atom.commands.onWillDispatch (event) =>
if event.type is 'core:paste'
activeEditor = atom.workspace.getActiveTextEditor()
return unless activeEditor
grammar = activeEditor.getGrammar()
return unless grammar and grammar.scopeName is 'source.asciidoc'
# Native image support
if @isImage()
@pasteNativeImage event, activeEditor
.then successHandler
.catch errorHandler
# Image URL support
else if atom.config.get 'asciidoc-image-helper.urlSupport.enable'
clipboardText = @readClipboardText()
if @isImageUrl clipboardText
@pasteImageUrl event, activeEditor, clipboardText
.then successHandler
.catch errorHandler
# Native image support
pasteNativeImage: (event, activeEditor) ->
event.stopImmediatePropagation()
# since Atom v1.28.0 https://github.com/atom/atom/releases/tag/v1.28.0:
# - nativeImage.toPng() -> nativeImage.toPNG()
# It's related to the upgrade to Electon 2.0:
# - https://github.com/electron/electron/blob/master/docs/api/breaking-changes.md#breaking-api-changes-20
imgbuffer = clipboard.readImage().toPNG()
currentFile = new File activeEditor.getPath()
imageFileName = filenameHelper.generateImageName path.basename(currentFile.getBaseName()), imgbuffer
new Promise (resolve, rejet) ->
if atom.config.get 'asciidoc-image-helper.customFilenames'
dialog = new CustomNameView initialImageName: imageFileName
dialog.attach()
dialog.onDidConfirm (custom) ->
resolve imageFactory.createImage activeEditor, currentFile, imgbuffer, custom.imageName
else
resolve imageFactory.createImage activeEditor, currentFile, imgbuffer, imageFileName
# Image URL support
pasteImageUrl: (event, activeEditor, clipboardText) ->
event.stopImmediatePropagation()
imageFileName = filenameHelper.cleanImageFilename path.basename clipboardText
new Promise (resolve, rejet) ->
if atom.config.get 'asciidoc-image-helper.customFilenames'
dialog = new CustomNameView initialImageName: imageFileName
dialog.attach()
dialog.onDidConfirm (custom) ->
resolve imageFactory.copyImage activeEditor, clipboardText, custom.imageName
else
resolve imageFactory.copyImage activeEditor, clipboardText, imageFileName
isImage: ->
not clipboard.readImage().isEmpty()
isImageUrl: (clipboardText) ->
imageExtensions = atom.config.get 'asciidoc-image-helper.urlSupport.imageExtensions'
safeImageExtensions = imageExtensions.map (ext) -> ext.toLowerCase()
clipboardText?.length? and path.extname(clipboardText)?.toLowerCase() in safeImageExtensions and new File(clipboardText).existsSync()
readClipboardText: ->
clipboardText = clipboard.readText()
# windows specific
windowsFilePattern = /^file:[\/]{2,3}(.*)$/
if clipboardText.match windowsFilePattern
clipboardText = windowsFilePattern.exec(clipboardText)[1]
# windows specific
windowsPathPattern = /^\"(.*)\"$/
if clipboardText.match windowsPathPattern
clipboardText = windowsPathPattern.exec(clipboardText)[1]
clipboardText
onDidInsert: (callback) ->
@emitter.on 'did-image-insert', callback
deactivate: ->
@subscriptions?.dispose()
@emitter?.dispose()
serialize: ->