forked from jodyheavener/CSS-Buddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApply to Selected.sketchplugin
101 lines (71 loc) · 3.1 KB
/
Apply to Selected.sketchplugin
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
// (cmd shift a)
#import 'assets/css-parse.js'
#import 'assets/css-apply.js'
#import 'assets/helpers.js'
var ApplyToSelected;
ApplyToSelected = (function() {
"use strict";
function ApplyToSelected() {
if (selection.count() == 0) return this.noSelection();
var selectionLoop, layer, layerType, cssInput, cssData, result;
selectionLoop = selection.objectEnumerator();
while (layer = selectionLoop.nextObject()) {
if (layer.isKindOfClass(MSShapeGroup)) layerType = "shape";
if (layer.isKindOfClass(MSTextLayer)) layerType = "text";
if (layer.isKindOfClass(MSArtboardGroup)) layerType = "artboard";
if (!layerType) return this.notSupported();
cssInput = this.getCSSInput(layer.name());
if (cssInput) cssData = new ParseCSS(cssInput[0]);
if (cssData) result = new ApplyCSS(layer, layerType, cssData, Number(cssInput[1]));
// If there are any failed rules, warn
if (result && result.length > 0) this.unsuccessfulStyles(result.unhandled);
};
return;
};
ApplyToSelected.prototype.noSelection = function() {
var warnModal;
warnModal = alertWindow();
warnModal.setMessageText("Uh oh!");
warnModal.setInformativeText("You must first select a layer.");
return warnModal.runModal();
};
ApplyToSelected.prototype.notSupported = function() {
var warnModal;
warnModal = alertWindow();
warnModal.setMessageText("Uh oh!");
warnModal.setInformativeText("Sorry, that type of layer isn't supported.");
return warnModal.runModal();
};
ApplyToSelected.prototype.getCSSInput = function(layerName) {
var returnedInput, inputModal, inputField, overwriteToggle, modalResponse;
inputField = NSTextField.alloc().initWithFrame(NSMakeRect(0, 0, 300, 200));
inputField.setFont(NSFont.fontWithName_size("Monaco", 12));
inputField.enablesReturnKeyAutomatically = false;
overwriteToggle = NSButton.alloc().initWithFrame(NSMakeRect(0, 0, 300, 15));
overwriteToggle.setButtonType(NSSwitchButton);
overwriteToggle.setTitle("Overwrite Fills, Borders, and Shadows?");
inputModal = alertWindow(true);
inputModal.setMessageText("Apply CSS to Selected Layer: " + layerName);
inputModal.setInformativeText("Type or Paste in your CSS to be applied to this layer.\nControl + Return for new line.");
inputModal.addAccessoryView(inputField);
inputModal.addAccessoryView(overwriteToggle);
modalResponse = inputModal.runModal();
if (modalResponse != 1000) return;
var cssInput = valueAtIndex(inputModal, 0);
if (cssInput.length() > 0) return [cssInput, valueAtIndex(inputModal, 1)];
};
ApplyToSelected.prototype.unsuccessfulStyles = function(rules) {
var errorModal, i = 0;
errorModal = alertWindow();
errorModal.setMessageText("Oops!");
errorModal.setInformativeText("The following styles could not be applied:");
while (i < rules.length) {
var rule = arrayObjectInfo(rules[i])
errorModal.addTextLabelWithValue(rule[0] + ": " + rule[1] + ";");
i = i + 1;
};
errorModal.runModal();
};
return ApplyToSelected;
})();
new ApplyToSelected();