-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArchive Artboard.sketchplugin
77 lines (56 loc) · 1.9 KB
/
Archive Artboard.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
// Populate User Flows (command control a)
#import 'lib/sandbox.js'
#import 'lib/utils.js'
var archive = getArchivePage(),
currentPage = doc.currentPage(),
currentArtboard = doc.currentPage().currentArtboard(),
sharedTextStyles = doc.documentData().layerTextStyles(),
sharedObjectStyles = doc.documentData().layerStyles();
// Main function
function init() {
// Escape early if no artboard selected
if ( !currentArtboard ) {
doc.showMessage( "You'll have to select an artboard first" );
return;
}
// Deselect layers
currentPage.deselectAllLayers();
// Copy the artboard
var artboardToArchive = currentArtboard.copy()
// Add the copy to the archive
archive.addLayers( [ artboardToArchive ] );
// Loop through layers on the copy and remove shared stayles
var artboardLayers = artboardToArchive.layers().array();
processAllLayers( artboardLayers, removeSharedStyles );
// Delete the original from the current page
currentPage.removeLayer( currentArtboard );
// Refresh inspector to reflect changes.
doc.reloadInspector();
// All done...
doc.showMessage( "Archived: " + currentArtboard.name() );
}
// Remove shared styles from layer
function removeSharedStyles( layer ) {
// Get relevant shared styles container
var sharedStyles = ( layer.class() == MSTextLayer ) ? sharedTextStyles : sharedObjectStyles;
// Check if this layer has a shared style
var style = layer.style();
var sharedStyle = sharedStyles.sharedStyleWithID( style.sharedObjectID() )
// 'Unlink' the shared style if there is one
if( sharedStyle ) {
style.setSharedObjectID( null );
}
}
// Get the 'Archive' page
function getArchivePage() {
// Get the archive page
var archive = getPageByName( '-Archive' );
// Create it if there isn't one
if ( !archive ) {
archive = addPage( '-Archive', false );
}
// And return it
return archive;
}
// Kick it all off...
init();