@@ -14,7 +14,7 @@ import "../binding/sortable";
14
14
import "../binding/sortable-children" ;
15
15
import ContentTypeCollection from "../content-type-collection" ;
16
16
import ContentTypeCollectionInterface from "../content-type-collection.types" ;
17
- import ContentTypeConfigInterface from "../content-type-config.types" ;
17
+ import ContentTypeConfigInterface , { ConfigFieldInterface } from "../content-type-config.types" ;
18
18
import createContentType from "../content-type-factory" ;
19
19
import ContentTypeMenu from "../content-type-menu" ;
20
20
import Edit from "../content-type-menu/edit" ;
@@ -46,6 +46,13 @@ export default class Preview implements PreviewInterface {
46
46
public isPlaceholderVisible : KnockoutObservable < boolean > = ko . observable ( true ) ;
47
47
public isEmpty : KnockoutObservable < boolean > = ko . observable ( true ) ;
48
48
49
+ /**
50
+ * Provide preview data as an object which can be queried
51
+ *
52
+ * @deprecated please use getOptionValue directly
53
+ */
54
+ public previewData : { [ key : string ] : KnockoutObservable < any > } = { } ;
55
+
49
56
/**
50
57
* Fields that should not be considered when evaluating whether an object has been configured.
51
58
*
@@ -82,6 +89,7 @@ export default class Preview implements PreviewInterface {
82
89
"empty-placeholder-background" : this . isPlaceholderVisible ,
83
90
} ) ;
84
91
this . bindEvents ( ) ;
92
+ this . populatePreviewData ( ) ;
85
93
}
86
94
87
95
/**
@@ -144,6 +152,15 @@ export default class Preview implements PreviewInterface {
144
152
this . contentType . dataStore . set ( key , value ) ;
145
153
}
146
154
155
+ /**
156
+ * Retrieve the value for an option
157
+ *
158
+ * @param key
159
+ */
160
+ public getOptionValue ( key : string ) {
161
+ return this . contentType . dataStore . get ( key ) ;
162
+ }
163
+
147
164
/**
148
165
* Set state based on mouseover event for the preview
149
166
*
@@ -581,4 +598,37 @@ export default class Preview implements PreviewInterface {
581
598
582
599
this . isPlaceholderVisible ( paddingBottom + paddingTop + minHeight >= 130 ) ;
583
600
}
601
+
602
+ /**
603
+ * Populate the preview data with calls to the supported getOptionValue method
604
+ *
605
+ * @deprecated this function is only included to preserve backwards compatibility, use getOptionValue directly
606
+ */
607
+ private populatePreviewData ( ) : void {
608
+ if ( this . config . fields ) {
609
+ _ . each ( this . config . fields , ( fields ) => {
610
+ _ . keys ( fields ) . forEach ( ( key : string ) => {
611
+ this . previewData [ key ] = ko . observable ( "" ) ;
612
+ } ) ;
613
+ } ) ;
614
+ }
615
+
616
+ // Subscribe to this content types data in the store
617
+ this . contentType . dataStore . subscribe (
618
+ ( data : DataObject ) => {
619
+ _ . forEach ( data , ( value , key ) => {
620
+ const optionValue = this . getOptionValue ( key ) ;
621
+ if ( ko . isObservable ( this . previewData [ key ] ) ) {
622
+ this . previewData [ key ] ( optionValue ) ;
623
+ } else {
624
+ if ( _ . isArray ( optionValue ) ) {
625
+ this . previewData [ key ] = ko . observableArray ( optionValue ) ;
626
+ } else {
627
+ this . previewData [ key ] = ko . observable ( optionValue ) ;
628
+ }
629
+ }
630
+ } ) ;
631
+ } ,
632
+ ) ;
633
+ }
584
634
}
0 commit comments