-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (76 loc) · 1.9 KB
/
index.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
var position = require('position')
, query = require('query')
, convert = require('color-convert')
, Slider = require('colorslider')
, utils = require('./utils')
function getColorAt(editor, pos) {
var line = editor.getSession().doc.getLine(pos.row)
, range
range = utils.findHexAt(line, pos.column)
if (range) return range
range = utils.findExpandedAt(line, pos.column)
if (range) return range
}
module.exports = function (editor, el) {
var slider = new Slider()
, range = null
, mode = 'hex'
, changing = false
function change(color) {
if (!range) return
var r = editor.getSelectionRange()
, s = editor.getSelection()
, alpha = color.a !== 1 ? 'a' : ''
, txt
if (mode === 'hex') {
txt = color['rgba']()[alpha ? 'toString' : 'toHex']()
} else {
txt = color[mode + alpha]().toString()
}
changing = true
r.setStart(range.row, range.start)
r.setEnd(range.row, range.end)
try {
s.setRange(r)
editor.insert(txt)
} catch (e) {
}
changing = false
range.end = range.start + txt.length
}
slider.on('change', change)
editor.getSession().selection.on('changeSelection', function (e) {
if (changing) return
var r = editor.getSelectionRange()
, pos = r.start
, color
if (!r.isEmpty()) {
range = null
try {
slider.hide()
} catch (e) {}
return
}
try {
color = getColorAt(editor, pos)
} catch (e) {
range = null
try {
slider.hide()
} catch (e) {}
return
}
if (!color) {
range = null
try {
slider.hide()
} catch (e) {}
return
}
mode = color.type.slice(0, 3) // strip of the "a" from rgba and hsla
var cursor = query('.ace_cursor', el)
range = {row: pos.row, start: color.start, end: color.end}
slider.set(color.text, true)
slider.show(cursor)
})
}