-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (57 loc) · 2.34 KB
/
index.js
File metadata and controls
70 lines (57 loc) · 2.34 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
/** @babel */
import path from 'path';
import sass from 'node-sass';
/* cmd+shift+s */
function run() {
const editor = atom.workspace.getActiveTextEditor();
let buffer = editor.getBuffer();
let currentSelectorLocator = '/*scss-preview-current*/';
if (!editor) {
return;
}
let isScssFile = (/\.scss$/i).test(buffer.file.path);
if (!isScssFile) {
return;
}
let markerPos = editor.getCursorBufferPosition();
let characterIndex = buffer.characterIndexForPosition(markerPos);
//Remove properties, only keep selectors
let input = buffer.cachedText;
let nextCurlyPos = input.indexOf('{', characterIndex);
let nextSemiColonPos = input.indexOf(';', characterIndex);
//Cursor is at a property, not supported atm
if (nextCurlyPos > nextSemiColonPos) return;
//Insert our custom comment that will keep this selector in the output
let modifiedInput = input.splice(nextCurlyPos + 1, 0, currentSelectorLocator);
//Remove potential compile breakers
modifiedInput = modifiedInput.replace(/@include .*;/g, '');
modifiedInput = modifiedInput.replace(/@import .*;/g, '');
//Remove variable declarations
modifiedInput = modifiedInput.replace(/\$.+=.+;/g, '');
//Remove variables
modifiedInput = modifiedInput.replace(/\$[^ |;|)]+/g, '');
//Remove properties
modifiedInput = modifiedInput.replace(/[^{|\s]+:[^{|}]+;/ig, '');
sass.render({
data: modifiedInput,
outputStyle: 'expanded'
}, (err, result) => {
if (err) {
atom.notifications.addError('Could not get full css selector.');
} else if (result) {
let resultString = result.css.toString('utf-8');
//Remove media queries for now
resultString = resultString.replace(/@media.*{/g, '');
let selector = resultString.match(/[^}]*\/\*scss-preview-current\*/);
//Remove everything but the actual string containing the selector
selector = selector[0].replace(/{[\s\S]*\/\*scss-preview-current\*/gmi, '');
atom.notifications.addInfo(selector);
}
});
};
export const activate = () => {
atom.commands.add('atom-workspace', 'scss-preview-selector:run', run);
};
String.prototype.splice = function(idx, rem, str) {
return this.slice(0, idx) + str + this.slice(idx + Math.abs(rem));
};