-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks-paste-prop.js
More file actions
121 lines (99 loc) · 4.64 KB
/
stacks-paste-prop.js
File metadata and controls
121 lines (99 loc) · 4.64 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
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
const sketch = require('sketch');
// Helper function to display native NSAlert for errors
function showNSAlert(title, message) {
const alert = NSAlert.alloc().init();
alert.messageText = title;
alert.informativeText = message;
alert.addButtonWithTitle("OK");
alert.runModal();
}
// Safely apply primitive values to the native layer using KVC
function applySetting(nativeObj, keys, value) {
if (!nativeObj || value === undefined || value === null) return;
for (let i = 0; i < keys.length; i++) {
try {
nativeObj.setValue_forKey_(Number(value), keys[i]);
// We don't return here because we want to try ALL matching keys
// in the latest Sketch engine to be 100% sure.
} catch(e) {}
}
}
// --- MAIN EXECUTION START ---
const doc = sketch.getSelectedDocument();
if (!doc) {
showNSAlert("⚠️ Error", "No document is currently open.");
} else {
const selection = doc.selectedLayers;
if (selection.isEmpty) {
showNSAlert("⚠️ Selection Required", "Please select at least one layer.");
} else {
const pasteboard = NSPasteboard.generalPasteboard();
const clipboardString = pasteboard.stringForType(NSPasteboardTypeString);
if (!clipboardString) {
showNSAlert("❌ Empty Clipboard", "Clipboard is empty.");
} else {
let payload = null;
try { payload = JSON.parse(String(clipboardString)); } catch(e) {}
if (!payload || payload.plugin !== "StackLayoutHacks") {
showNSAlert("❌ Invalid Data", "Not a valid StackLayoutHacks payload.");
} else {
const settings = payload.settings;
let processedCount = 0;
selection.forEach(layer => {
const isContainer = layer.type === 'Group' || layer.type === 'Artboard' || layer.type === 'SymbolMaster' || layer.type === 'Frame';
if (isContainer) {
const nativeLayer = layer.sketchObject;
// 1. Initialize Stack via JS API
try {
layer.layout = { type: 'Stack' };
layer.stackLayout = {
direction: settings.axis === 1 ? 'vertical' : 'horizontal',
wraps: settings.isWrapEnabled === 1
};
} catch(e) {}
// 2. Apply Core Layout Settings (MSFlexGroupLayout)
const layout = nativeLayer.groupLayout();
if (layout) {
applySetting(layout, ["flexDirection"], settings.axis);
applySetting(layout, ["wrappingEnabled"], settings.isWrapEnabled);
applySetting(layout, ["allGuttersGap", "spacing"], settings.spacing);
applySetting(layout, ["crossAxisGutterGap", "lineSpacing"], settings.lineSpacing);
applySetting(layout, ["justifyContent", "distribution"], settings.justifyContent);
applySetting(layout, ["alignItems", "alignment"], settings.alignItems);
}
// 3. APPLY PADDING (The "Athens" Fix)
if (settings.padding) {
// UNLOCK: Force paddingSelection to 2 (Individual mode)
// This is the key to making topPadding and bottomPadding work independently!
applySetting(nativeLayer, ["paddingSelection"], 2);
// APPLY: Inject individual values
applySetting(nativeLayer, ["topPadding"], settings.padding.top);
applySetting(nativeLayer, ["rightPadding"], settings.padding.right);
applySetting(nativeLayer, ["bottomPadding"], settings.padding.bottom);
applySetting(nativeLayer, ["leftPadding"], settings.padding.left);
}
// 4. Sizing, Corners & Clipping
applySetting(nativeLayer, ["horizontalSizing", "layoutSizingHorizontal"], settings.horizontalSizing);
applySetting(nativeLayer, ["verticalSizing", "layoutSizingVertical"], settings.verticalSizing);
applySetting(nativeLayer, ["cornerStyle"], settings.cornerType);
const shouldClip = (settings.clippingBehavior !== 2);
try { layer.clipsContent = shouldClip; } catch(e) {}
try { nativeLayer.setHasClippingMask_(shouldClip); } catch(e) {}
// Refresh UI and Layout
if (nativeLayer.respondsToSelector(NSSelectorFromString("objectDidUpdate"))) {
nativeLayer.objectDidUpdate();
}
if (layout && layout.respondsToSelector(NSSelectorFromString("apply"))) {
layout.apply();
}
processedCount++;
}
});
if (processedCount > 0) {
doc.selectedLayers.clear();
sketch.UI.message(`✅ Applied Stack properties!`);
}
}
}
}
}