23 function resetViews( content ) { |
23 function resetViews( content ) { |
24 function callback( match, $1 ) { |
24 function callback( match, $1 ) { |
25 return '<p>' + window.decodeURIComponent( $1 ) + '</p>'; |
25 return '<p>' + window.decodeURIComponent( $1 ) + '</p>'; |
26 } |
26 } |
27 |
27 |
28 if ( ! content ) { |
28 if ( ! content || content.indexOf( ' data-wpview-' ) === -1 ) { |
29 return content; |
29 return content; |
30 } |
30 } |
31 |
31 |
32 return content |
32 return content |
33 .replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g, callback ) |
33 .replace( /<div[^>]+data-wpview-text="([^"]+)"[^>]*>(?:\.|[\s\S]+?wpview-end[^>]+>\s*<\/span>\s*)?<\/div>/g, callback ) |
50 // Pass on body class name changes from the editor to the wpView iframes. |
50 // Pass on body class name changes from the editor to the wpView iframes. |
51 editor.on( 'wp-body-class-change', function() { |
51 editor.on( 'wp-body-class-change', function() { |
52 var className = editor.getBody().className; |
52 var className = editor.getBody().className; |
53 |
53 |
54 editor.$( 'iframe[class="wpview-sandbox"]' ).each( function( i, iframe ) { |
54 editor.$( 'iframe[class="wpview-sandbox"]' ).each( function( i, iframe ) { |
55 // Make sure it is a local iframe |
55 // Make sure it is a local iframe. |
56 // jshint scripturl: true |
56 // jshint scripturl: true |
57 if ( ! iframe.src || iframe.src === 'javascript:""' ) { |
57 if ( ! iframe.src || iframe.src === 'javascript:""' ) { |
58 try { |
58 try { |
59 iframe.contentWindow.document.body.className = className; |
59 iframe.contentWindow.document.body.className = className; |
60 } catch( er ) {} |
60 } catch( er ) {} |
77 |
77 |
78 if ( ! event.load ) { |
78 if ( ! event.load ) { |
79 node = editor.selection.getNode(); |
79 node = editor.selection.getNode(); |
80 |
80 |
81 if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) { |
81 if ( node && node !== editor.getBody() && /^\s*https?:\/\/\S+\s*$/i.test( event.content ) ) { |
82 // When a url is pasted or inserted, only try to embed it when it is in an empty paragrapgh. |
82 // When a url is pasted or inserted, only try to embed it when it is in an empty paragraph. |
83 node = editor.dom.getParent( node, 'p' ); |
83 node = editor.dom.getParent( node, 'p' ); |
84 |
84 |
85 if ( node && /^[\s\uFEFF\u00A0]*$/.test( editor.$( node ).text() || '' ) ) { |
85 if ( node && /^[\s\uFEFF\u00A0]*$/.test( editor.$( node ).text() || '' ) ) { |
86 // Make sure there are no empty inline elements in the <p> |
86 // Make sure there are no empty inline elements in the <p>. |
87 node.innerHTML = ''; |
87 node.innerHTML = ''; |
88 } else { |
88 } else { |
89 return; |
89 return; |
90 } |
90 } |
91 } |
91 } |
109 // Replace views with their text. |
109 // Replace views with their text. |
110 editor.on( 'postprocess', function( event ) { |
110 editor.on( 'postprocess', function( event ) { |
111 event.content = resetViews( event.content ); |
111 event.content = resetViews( event.content ); |
112 } ); |
112 } ); |
113 |
113 |
114 // Replace views with their text inside undo levels. |
114 // Prevent adding of undo levels when replacing wpview markers |
115 // This also prevents that new levels are added when there are changes inside the views. |
115 // or when there are changes only in the (non-editable) previews. |
116 editor.on( 'beforeaddundo', function( event ) { |
116 editor.on( 'beforeaddundo', function( event ) { |
117 event.level.content = resetViews( event.level.content ); |
117 var lastContent; |
|
118 var newContent = event.level.content || ( event.level.fragments && event.level.fragments.join( '' ) ); |
|
119 |
|
120 if ( ! event.lastLevel ) { |
|
121 lastContent = editor.startContent; |
|
122 } else { |
|
123 lastContent = event.lastLevel.content || ( event.lastLevel.fragments && event.lastLevel.fragments.join( '' ) ); |
|
124 } |
|
125 |
|
126 if ( |
|
127 ! newContent || |
|
128 ! lastContent || |
|
129 newContent.indexOf( ' data-wpview-' ) === -1 || |
|
130 lastContent.indexOf( ' data-wpview-' ) === -1 |
|
131 ) { |
|
132 return; |
|
133 } |
|
134 |
|
135 if ( resetViews( lastContent ) === resetViews( newContent ) ) { |
|
136 event.preventDefault(); |
|
137 } |
118 } ); |
138 } ); |
119 |
139 |
120 // Make sure views are copied as their text. |
140 // Make sure views are copied as their text. |
121 editor.on( 'drop objectselected', function( event ) { |
141 editor.on( 'drop objectselected', function( event ) { |
122 if ( isView( event.targetClone ) ) { |
142 if ( isView( event.targetClone ) ) { |
156 } |
176 } |
157 } |
177 } |
158 } ); |
178 } ); |
159 |
179 |
160 editor.addButton( 'wp_view_edit', { |
180 editor.addButton( 'wp_view_edit', { |
161 tooltip: 'Edit|button', // '|button' is not displayed, only used for context |
181 tooltip: 'Edit|button', // '|button' is not displayed, only used for context. |
162 icon: 'dashicon dashicons-edit', |
182 icon: 'dashicon dashicons-edit', |
163 onclick: function() { |
183 onclick: function() { |
164 var node = editor.selection.getNode(); |
184 var node = editor.selection.getNode(); |
165 |
185 |
166 if ( isView( node ) ) { |
186 if ( isView( node ) ) { |