-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
419 lines (352 loc) · 10.8 KB
/
extension.js
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
const vscode = require('vscode')
const window = vscode.window
const workspace = vscode.workspace
const Promise = require('bluebird')
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const copyPaste = require('copy-paste')
const open = require('open')
const Cache = require('vscode-cache')
const statusBarMessageTimeout = 5000 // 5 seconds
// Quote configuration values
const quotes = {
'single': "'",
'double': '"'
}
const quoteDefault = "'"
// Cache
let cache
const recentImagesKey = 'recentImages'
// Recent images
const maxRecentImagesDefault = 10
// Import services
let services = []
let servicesDir = path.join(__dirname, 'services')
try {
let files = fs.readdirSync(servicesDir)
for (let file of files) {
services.push(require(path.join(servicesDir, file)))
}
} catch (error) {
console.log(error)
}
let activate = (context) => {
// Initialize cache
if (typeof (cache) === 'undefined') {
cache = new Cache(context)
}
let placeholderImagesDisposable = vscode.commands.registerCommand('placeholderImages.insert', () => {
// Choose from available services
window.showQuickPick(services, {
'placeHolder': 'Choose a Placeholder Image service'
}).then((service) => {
// No service was chosen
if (typeof (service) === 'undefined') {
console.error('No placeholder service was chosen')
return false
}
// Clone the service object
service = _.cloneDeep(service)
// Loop through service attributes
let chain = Object.keys(service.attributes).reduce((previous, key) => {
let attribute = service.attributes[key]
return previous.then((previousValue) => {
return new Promise((resolve, reject) => {
// Take appropriate action for attribute type
switch (attribute.action) {
// Input attribute
case 'input':
return inputAttributeAction(attribute, resolve, reject)
// Select attribute
case 'select':
return selectAttributeAction(attribute, resolve, reject)
// Select Yes/No attribute
case 'boolean':
return booleanAttributeAction(attribute, resolve, reject)
default:
break
}
})
})
}, Promise.resolve())
// End of the reducer chain
chain.then(() => {
// Show the action quick pick using the generated url
showActionPicker(service.format())
}).catch((err) => {
console.error(err)
})
}, (err) => {
console.error(err)
})
})
context.subscriptions.push(placeholderImagesDisposable)
let recentImagesDisposable = vscode.commands.registerCommand('placeholderImages.recent', () => {
// Get the recent images from the cache
let recent = cache.get(recentImagesKey, [])
// No Recent images found
if (recent.length < 1) {
// Offer insert command instead
return vscode.window.showInformationMessage('No Recent Images. Do you want choose a new image instead?', 'Yes')
.then((value) => {
if (value === 'Yes') {
vscode.commands.executeCommand('placeholderImages.insert')
}
}, (err) => {
console.error(err)
})
}
// Build list of quick pick items
let items = []
for (let url of recent) {
items.push(url)
}
// Clear recent images command
items.push('Clear recent images list')
// Show quick pick menu of recent images
window.showQuickPick(items, {
placeHolder: 'Choose a recent placeholder image'
})
.then((value) => {
// No url was chosen
if (typeof (value) === 'undefined') {
console.error('No placeholder image was chosen')
return false
}
// Clear recent images
if (value === _.last(items)) {
cache.forget(recentImagesKey)
return true
}
// Show quick picker menu of actions for the chosen url
showActionPicker(value)
}, (err) => {
console.error(err)
})
})
context.subscriptions.push(recentImagesDisposable)
}
exports.activate = activate
// Input value using input box
const inputAttributeAction = (attribute, resolve, reject) => {
// Add optional to placeHolder
const placeHolder = !attribute.required ? attribute.placeHolder + ' (optional)' : attribute.placeHolder
// Accept Promise or not
let accept = false
promiseWhile(() => {
return !accept
}, () => {
return new Promise((resolve, reject) => {
window.showInputBox({
placeHolder: placeHolder
}).then((value) => {
// No input was specified
if (typeof (value) === 'undefined') {
return reject('No input value specified')
}
value = value.trim()
// Validation via regex
const regexType = new RegExp(attribute.regex)
// Regex test or is the value optional
if (regexType.test(value)) {
attribute.value = value
accept = true
// If attribute is not required, accept undefined value
} else if (!attribute.required && value.length === 0) {
accept = true
}
return resolve()
}, (err) => {
return reject(err)
})
})
}).then(() => {
return resolve()
}).catch((err) => {
console.error(err)
})
}
// Select value from QuickPick
const selectAttributeAction = (attribute, resolve, reject) => {
// Add 'None' item if attribute is not required
if (!attribute.required) {
attribute.items.push({
label: 'None'
})
attribute.placeHolder += ' (optional)'
}
// Pick attribute value
window.showQuickPick(attribute.items, {
placeHolder: attribute.placeHolder
}).then((item) => {
if (typeof (item) === 'undefined') {
return reject('No value was selected')
}
// If value is not specified, use the trimmed, lowercase label
let value = item.value ? item.value : item.label.trim().toLowerCase()
// Ignore value
if (value !== 'none') {
attribute.value = value
}
return resolve()
}, (err) => {
return reject(err)
})
}
// Select from a Yes/No QuickPick
const booleanAttributeAction = (attribute, resolve, reject) => {
// Pick attribute value
window.showQuickPick(['Yes', 'No'], {
placeHolder: attribute.placeHolder
}).then((value) => {
if (typeof (value) === 'undefined') {
return reject('No value was selected')
}
attribute.value = value === 'Yes'
return resolve()
}, (err) => {
return reject(err)
})
}
const promiseWhile = (predicate, action) => {
function loop () {
if (!predicate()) return
return Promise.resolve(action()).then(loop)
}
return Promise.resolve().then(loop)
}
const showActionPicker = (url) => {
return new Promise((resolve, reject) => {
// Reject if any chosen file properties are missing
if (typeof (url) === 'undefined') {
reject('No url specified')
}
// Configuration
const config = vscode.workspace.getConfiguration('placeholderImages')
// Determine the quote style from configuration
const quote = quotes[config.get('quoteStyle')] || quoteDefault
// Image tag
const tag = '<img src=' + quote + url + quote + ' alt=' + quote + quote + '/>'
// Arrays of actions
let actions = []
// Insert <img> tag into document action
if (window.activeTextEditor) {
actions.push({
label: '<img>: Insert into document',
callback: () => {
insertText(tag)
}
})
}
// Copy <img> tag to clipboard action
actions.push({
label: '<img>: Copy to clipboard',
callback: () => {
copy(tag, '<img> tag copied to the clipboard')
}
})
// Insert URL into document action
if (window.activeTextEditor) {
actions.push({
label: 'URL: Insert into document',
callback: () => {
insertText(url)
}
})
}
// Copy URL to clipboard action
actions.push({
label: 'URL: Copy to clipboard',
callback: () => {
copy(url, 'URL copied to the clipboard')
}
})
// Open URL in browser action
actions.push({
label: 'URL: Open in browser',
callback: () => {
open(url)
}
})
return new Promise((resolve, reject) => {
window.showQuickPick(actions, {
placeHolder: url
}).then((action) => {
// No action was chosen
if (typeof (action) === 'undefined') {
return reject('No action was chosen')
}
// Execute action callback
action.callback()
// Fetch cached recent images
let recent = cache.get(recentImagesKey, [])
// Remove library if it already exists in array
for (let index in recent) {
// Match found, remove it from the array
if (url === recent[index]) {
recent.splice(index, 1)
break
}
}
// Push to front of recent images array
recent.unshift(url)
// Limit to maxium number of recent image urls according to configuration
let max = workspace.getConfiguration('placeholderImages').get('maxRecentImages')
max = (Number.isInteger(max) === true && max >= 1) ? max : maxRecentImagesDefault
recent = recent.slice(0, max)
// Save to cache
cache.put(recentImagesKey, recent)
resolve()
}, (err) => {
reject(err)
})
})
})
}
// Insert text into active document at cursor positions
let insertText = (text) => {
let textEditor = window.activeTextEditor
// Ignore if no active TextEditor
if (typeof (textEditor) === 'undefined') {
return false
}
// Get the active text document's uri
let uri = textEditor.document.uri
// Create a new TextEdit for each selection
let edits = []
for (let selection of textEditor.selections) {
edits.push(vscode.TextEdit.insert(selection.active, text))
}
// New WorkspaceEdit
let edit = new vscode.WorkspaceEdit()
edit.set(uri, edits)
// Applying the WorkspaceEdit
workspace.applyEdit(edit)
.then(() => {
// Clear the selection
textEditor.selection = new vscode.Selection(textEditor.selection.end, textEditor.selection.end)
}, (err) => {
console.error(err)
})
return true
}
// Copy text to clipboard and set statusBarMessage
let copy = (text, message) => {
copyPaste.copy(text, () => {
if (message) {
statusMessage(message)
}
})
}
// Set consistent status bar message using timeout with either promise or time in milliseconds
let statusMessage = (text, promise) => {
if (promise) {
window.setStatusBarMessage('cdnjs: ' + text, promise)
} else {
window.setStatusBarMessage('cdnjs: ' + text, statusBarMessageTimeout)
}
}
// this method is called when your extension is deactivated
let deactivate = () => { }
exports.deactivate = deactivate