@@ -17,6 +17,9 @@ import BasePreview from "../preview";
17
17
export default class Preview extends BasePreview {
18
18
public displayPreview : KnockoutObservable < boolean > = ko . observable ( false ) ;
19
19
public placeholderText : KnockoutObservable < string > ;
20
+ private lastBlockId : number ;
21
+ private lastTemplate : string ;
22
+ private lastRenderedHtml : string ;
20
23
private messages = {
21
24
NOT_SELECTED : $t ( "Empty Block" ) ,
22
25
LOADING : $t ( "Loading..." ) ,
@@ -57,17 +60,29 @@ export default class Preview extends BasePreview {
57
60
protected afterObservablesUpdated ( ) : void {
58
61
super . afterObservablesUpdated ( ) ;
59
62
60
- this . placeholderText ( this . messages . LOADING ) ;
61
- this . displayPreview ( false ) ;
62
-
63
63
const data = this . parent . dataStore . get ( ) ;
64
64
65
+ // Only load if something changed
66
+ if ( this . lastBlockId === data . block_id && this . lastTemplate === data . template ) {
67
+ // The mass converter will have transformed the HTML property into a directive
68
+ if ( this . lastRenderedHtml ) {
69
+ this . data . main . html ( this . lastRenderedHtml ) ;
70
+ this . displayPreview ( true ) ;
71
+ }
72
+
73
+ return ;
74
+ }
75
+
76
+ this . displayPreview ( false ) ;
77
+
65
78
if ( ! data . block_id || data . template . length === 0 ) {
66
79
this . placeholderText ( this . messages . NOT_SELECTED ) ;
67
80
68
81
return ;
69
82
}
70
83
84
+ this . placeholderText ( this . messages . LOADING ) ;
85
+
71
86
const url = Config . getConfig ( "preview_url" ) ;
72
87
const requestConfig = {
73
88
// Prevent caching
@@ -99,6 +114,10 @@ export default class Preview extends BasePreview {
99
114
} else if ( response . data . error ) {
100
115
this . placeholderText ( response . data . error ) ;
101
116
}
117
+
118
+ this . lastBlockId = data . block_id ;
119
+ this . lastTemplate = data . template ;
120
+ this . lastRenderedHtml = response . data . content ;
102
121
} )
103
122
. fail ( ( ) => {
104
123
this . placeholderText ( this . messages . UNKNOWN_ERROR ) ;
0 commit comments