3 * WordPress View plugin.
8 TreeWalker = tinymce.dom.TreeWalker,
11 tinymce.create('tinymce.plugins.wpView', {
12 init : function( editor ) {
15 // Check if the `wp.mce` API exists.
16 if ( typeof wp === 'undefined' || ! wp.mce )
19 editor.onPreInit.add( function( editor ) {
20 // Add elements so we can set `contenteditable` to false.
21 editor.schema.addValidElements('div[*],span[*]');
24 // When the editor's content changes, scan the new content for
25 // matching view patterns, and transform the matches into
26 // view wrappers. Since the editor's DOM is outdated at this point,
27 // we'll wait to render the views.
28 editor.onBeforeSetContent.add( function( editor, o ) {
32 o.content = wp.mce.view.toViews( o.content );
35 // When the editor's content has been updated and the DOM has been
36 // processed, render the views in the document.
37 editor.onSetContent.add( function( editor ) {
38 wp.mce.view.render( editor.getDoc() );
41 editor.onInit.add( function( editor ) {
43 // When a view is selected, ensure content that is being pasted
44 // or inserted is added to a text node (instead of the view).
45 editor.selection.onBeforeSetContent.add( function( selection ) {
46 var view = wpView.getParentView( selection.getNode() ),
49 // If the selection is not within a view, bail.
53 // If there are no additional nodes or the next node is a
54 // view, create a text node after the current view.
55 if ( ! view.nextSibling || wpView.isView( view.nextSibling ) ) {
56 target = editor.getDoc().createTextNode('');
57 editor.dom.insertAfter( target, view );
59 // Otherwise, find the next text node.
61 walker = new TreeWalker( view.nextSibling, view.nextSibling );
62 target = walker.next();
65 // Select the `target` text node.
66 selection.select( target );
67 selection.collapse( true );
70 // When the selection's content changes, scan any new content
71 // for matching views and immediately render them.
73 // Runs on paste and on inserting nodes/html.
74 editor.selection.onSetContent.add( function( selection, o ) {
78 var node = selection.getNode();
80 if ( ! node.innerHTML )
83 node.innerHTML = wp.mce.view.toViews( node.innerHTML );
84 wp.mce.view.render( node );
88 // When the editor's contents are being accessed as a string,
89 // transform any views back to their text representations.
90 editor.onPostProcess.add( function( editor, o ) {
91 if ( ( ! o.get && ! o.save ) || ! o.content )
94 o.content = wp.mce.view.toText( o.content );
97 // Triggers when the selection is changed.
98 // Add the event handler to the top of the stack.
99 editor.onNodeChange.addToTop( function( editor, controlManager, node ) {
100 var view = wpView.getParentView( node );
102 // Update the selected view.
104 wpView.select( view );
106 // Prevent the selection from propagating to other plugins.
109 // If we've clicked off of the selected view, deselect it.
115 editor.onKeyDown.addToTop( function( editor, event ) {
116 var keyCode = event.keyCode,
119 // If a view isn't selected, let the event go on its merry way.
123 // If the caret is not within the selected view, deselect the
125 view = wpView.getParentView( editor.selection.getNode() );
126 if ( view !== selected ) {
131 // If delete or backspace is pressed, delete the view.
132 if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
133 if ( (instance = wp.mce.view.instance( selected )) ) {
139 // Let keypresses that involve the command or control keys through.
140 // Also, let any of the F# keys through.
141 if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) )
144 event.preventDefault();
148 getParentView : function( node ) {
150 if ( this.isView( node ) )
153 node = node.parentNode;
157 isView : function( node ) {
158 return (/(?:^|\s)wp-view-wrap(?:\s|$)/).test( node.className );
161 select : function( view ) {
162 if ( view === selected )
167 wp.mce.view.select( selected );
170 deselect : function() {
172 wp.mce.view.deselect( selected );
176 getInfo : function() {
178 longname : 'WordPress Views',
179 author : 'WordPress',
180 authorurl : 'http://wordpress.org',
181 infourl : 'http://wordpress.org',
188 tinymce.PluginManager.add( 'wpview', tinymce.plugins.wpView );