wp/wp-includes/js/tinymce/plugins/wpview/editor_plugin_src.js
author ymh <ymh.work@gmail.com>
Thu, 07 Nov 2013 00:08:07 +0000
changeset 1 f6eb5a861d2f
parent 0 d970ebf37754
permissions -rw-r--r--
remove unnessary files. Make timthumb work
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
 * WordPress View plugin.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
(function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
	var VK = tinymce.VK,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
		TreeWalker = tinymce.dom.TreeWalker,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
		selected;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
	tinymce.create('tinymce.plugins.wpView', {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
		init : function( editor, url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
			var wpView = this;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
			// Check if the `wp.mce` API exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
			if ( typeof wp === 'undefined' || ! wp.mce )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
			editor.onPreInit.add( function( editor ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
				// Add elements so we can set `contenteditable` to false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
				editor.schema.addValidElements('div[*],span[*]');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
			// When the editor's content changes, scan the new content for
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
			// matching view patterns, and transform the matches into
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
			// view wrappers. Since the editor's DOM is outdated at this point,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
			// we'll wait to render the views.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
			editor.onBeforeSetContent.add( function( editor, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
				if ( ! o.content )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
				o.content = wp.mce.view.toViews( o.content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
			// When the editor's content has been updated and the DOM has been
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
			// processed, render the views in the document.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
			editor.onSetContent.add( function( editor, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
				wp.mce.view.render( editor.getDoc() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
			editor.onInit.add( function( editor ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
				// When a view is selected, ensure content that is being pasted
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
				// or inserted is added to a text node (instead of the view).
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
				editor.selection.onBeforeSetContent.add( function( selection, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
					var view = wpView.getParentView( selection.getNode() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
						walker, target;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
					// If the selection is not within a view, bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
					if ( ! view )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
						return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
					// If there are no additional nodes or the next node is a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
					// view, create a text node after the current view.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
					if ( ! view.nextSibling || wpView.isView( view.nextSibling ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
						target = editor.getDoc().createTextNode('');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
						editor.dom.insertAfter( target, view );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
					// Otherwise, find the next text node.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
					} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
						walker = new TreeWalker( view.nextSibling, view.nextSibling );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
						target = walker.next();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
					// Select the `target` text node.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
					selection.select( target );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
					selection.collapse( true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
				});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
				// When the selection's content changes, scan any new content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
				// for matching views and immediately render them.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
				//
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
				// Runs on paste and on inserting nodes/html.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
				editor.selection.onSetContent.add( function( selection, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
					if ( ! o.context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
						return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
					var node = selection.getNode();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
					if ( ! node.innerHTML )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
						return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
					node.innerHTML = wp.mce.view.toViews( node.innerHTML );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
					wp.mce.view.render( node );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
				});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
			// When the editor's contents are being accessed as a string,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
			// transform any views back to their text representations.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
			editor.onPostProcess.add( function( editor, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
				if ( ( ! o.get && ! o.save ) || ! o.content )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
				o.content = wp.mce.view.toText( o.content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
			// Triggers when the selection is changed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
			// Add the event handler to the top of the stack.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
			editor.onNodeChange.addToTop( function( editor, controlManager, node, collapsed, o ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
				var view = wpView.getParentView( node );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
				// Update the selected view.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
				if ( view ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
					wpView.select( view );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
					// Prevent the selection from propagating to other plugins.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
					return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
				// If we've clicked off of the selected view, deselect it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
					wpView.deselect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
			editor.onKeyDown.addToTop( function( editor, event ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
				var keyCode = event.keyCode,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
					view, instance;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
				// If a view isn't selected, let the event go on its merry way.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
				if ( ! selected )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
				// If the caret is not within the selected view, deselect the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
				// view and bail.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
				view = wpView.getParentView( editor.selection.getNode() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
				if ( view !== selected ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
					wpView.deselect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
				// If delete or backspace is pressed, delete the view.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
				if ( keyCode === VK.DELETE || keyCode === VK.BACKSPACE ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
					if ( (instance = wp.mce.view.instance( selected )) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
						instance.remove();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
						wpView.deselect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
				// Let keypresses that involve the command or control keys through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
				// Also, let any of the F# keys through.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
				if ( event.metaKey || event.ctrlKey || ( keyCode >= 112 && keyCode <= 123 ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
					return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
				event.preventDefault();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
			});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		getParentView : function( node ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
			while ( node ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
				if ( this.isView( node ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
					return node;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
				node = node.parentNode;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		isView : function( node ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
			return (/(?:^|\s)wp-view-wrap(?:\s|$)/).test( node.className );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		select : function( view ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
			if ( view === selected )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
				return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			this.deselect();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
			selected = view;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
			wp.mce.view.select( selected );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		deselect : function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
			if ( selected )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
				wp.mce.view.deselect( selected );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
			selected = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
		},
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		getInfo : function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
			return {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
				longname  : 'WordPress Views',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
				author    : 'WordPress',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
				authorurl : 'http://wordpress.org',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
				infourl   : 'http://wordpress.org',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
				version   : '1.0'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
			};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	});
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	// Register plugin
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	tinymce.PluginManager.add( 'wpview', tinymce.plugins.wpView );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
})();