11import { DefinedPropertiesKey } from '../WrappedObject'
2- import { Group } from './Group'
2+ import { Group , GroupBehavior } from './Group'
33import { Rectangle } from '../models/Rectangle'
44import { Types } from '../enums'
55import { Factory } from '../Factory'
@@ -19,11 +19,18 @@ export class Artboard extends Group {
1919 constructor ( artboard = { } ) {
2020 if ( ! artboard . sketchObject ) {
2121 // eslint-disable-next-line no-param-reassign
22- artboard . sketchObject = Factory . createNative ( Artboard )
22+ artboard . sketchObject = Factory . createNative ( Group )
2323 . alloc ( )
24- . initWithFrame ( new Rectangle ( 0 , 0 , 100 , 100 ) . asCGRect ( ) )
24+ . initWithFrame_behavior (
25+ new Rectangle ( 0 , 0 , 100 , 100 ) . asCGRect ( ) ,
26+ GroupBehavior . Frame
27+ )
2528 }
2629 super ( artboard )
30+ // Mimics behaviour implemented at the controller level where they call
31+ // `MSLayer.adjustAfterInsert()` which will apply the default styling.
32+ this . background . enabled = true
33+ // eslint-enable no-param-reassign
2734 }
2835
2936 /**
@@ -43,11 +50,9 @@ export class Artboard extends Group {
4350
4451Artboard . type = Types . Artboard
4552Artboard [ DefinedPropertiesKey ] = { ...Group [ DefinedPropertiesKey ] }
46- Factory . registerClass ( Artboard , MSArtboardGroup )
47- Factory . registerClass ( Artboard , MSImmutableArtboardGroup )
53+ Factory . registerAlias ( Artboard , Group )
4854
4955delete Artboard [ DefinedPropertiesKey ] . flow
50- delete Artboard [ DefinedPropertiesKey ] . style
5156delete Artboard [ DefinedPropertiesKey ] . locked
5257delete Artboard [ DefinedPropertiesKey ] . hidden
5358delete Artboard [ DefinedPropertiesKey ] . transform
@@ -68,13 +73,30 @@ Artboard.define('flowStartPoint', {
6873Artboard . defineObject ( 'background' , {
6974 enabled : {
7075 get ( ) {
71- return Boolean ( Number ( this . _object . hasBackgroundColor ( ) ) )
76+ return (
77+ this . _object . style &&
78+ this . _object . style ( ) . fills &&
79+ this . _object . style ( ) . fills ( ) . length > 0
80+ )
7281 } ,
7382 set ( enabled ) {
7483 if ( this . _parent . isImmutable ( ) ) {
7584 return
7685 }
77- this . _object . setHasBackgroundColor ( enabled )
86+ const style = this . _object . style ? this . _object . style ( ) : undefined
87+ if ( ! style ) {
88+ return
89+ }
90+ if ( enabled ) {
91+ const numFills = style . fills ? style . fills ( ) . length : 0
92+ if ( numFills === 0 ) {
93+ // Create a default fill if enabling and no fills exist
94+ style . addStylePartOfType ( 0 ) // 0 is for fills
95+ }
96+ } else {
97+ // Remove all fills if disabling
98+ style . removeAllStyleFills ( )
99+ }
78100 } ,
79101 } ,
80102 includedInExport : {
@@ -90,13 +112,26 @@ Artboard.defineObject('background', {
90112 } ,
91113 color : {
92114 get ( ) {
93- return colorToString ( this . _object . backgroundColor ( ) )
115+ const firstFill = this . _object . style
116+ ? this . _object . style ( ) . firstEnabledFill ( )
117+ : undefined
118+ return firstFill ? colorToString ( firstFill . color ( ) ) : '#00000000'
94119 } ,
95120 set ( color ) {
96121 if ( this . _parent . isImmutable ( ) ) {
97122 return
98123 }
99- this . _object . setBackgroundColor ( Color . from ( color ) . toMSColor ( ) )
124+ if ( ! this . _object . style ) {
125+ return
126+ }
127+ if (
128+ ! this . _object . style ( ) . fills ||
129+ this . _object . style ( ) . fills ( ) . length === 0
130+ ) {
131+ this . _object . style ( ) . addStylePartOfType ( 0 ) // Add a fill if none exists
132+ }
133+ const firstFill = this . _object . style ( ) . firstEnabledFill ( )
134+ firstFill . color = Color . from ( color ) . toMSColor ( )
100135 } ,
101136 } ,
102137} )
0 commit comments