1 line
2.6 KiB
JavaScript
1 line
2.6 KiB
JavaScript
window.wp=window.wp||{},function(i){var s={},c={};wp.mce=wp.mce||{},wp.mce.view={defaults:{pattern:{view:Backbone.View,text:function(e){return e.options.original},toView:function(e){if(this.pattern){this.pattern.lastIndex=0;e=this.pattern.exec(e);if(e)return{index:e.index,content:e[0],options:{original:e[0],results:e}}}}},shortcode:{view:Backbone.View,text:function(e){return e.options.shortcode.string()},toView:function(e){e=wp.shortcode.next(this.shortcode,e);if(e)return{index:e.index,content:e.content,options:{shortcode:e.shortcode}}}}},add:function(e,t){var n,i,r,o=t.extend?wp.mce.view.get(t.extend):t.shortcode?wp.mce.view.defaults.shortcode:wp.mce.view.defaults.pattern;_.defaults(t,o),t.id=e,r={remove:function(){return delete c[this.el.id],this.$el.parent().remove(),n&&n.apply(this,arguments),this}},_.isFunction(t.view)?i=t.view:(i=o.view,n=t.view.remove,_.defaults(r,t.view)),n||i._mceview||(n=i.prototype.remove),t.view=i.extend(r,{_mceview:!0}),s[e]=t},get:function(e){return s[e]},remove:function(e){delete s[e]},toViews:function(e){var t,o=[{content:e}];return _.each(s,function(i,r){t=o.slice(),o=[],_.each(t,function(e){var t,n=e.content;if(e.processed)o.push(e);else{for(;n&&(t=i.toView(n));)t.index&&o.push({content:n.substring(0,t.index)}),o.push({content:wp.mce.view.toView(r,t.options),processed:!0}),n=n.slice(t.index+t.content.length);n&&o.push({content:n})}})}),_.pluck(o,"content").join("")},toView:function(e,t){var n=wp.mce.view.get(e);return n?(t=(n=new n.view(_.extend(t||{},{viewType:e}))).el.id=n.el.id||_.uniqueId("__wpmce-"),(c[t]=n).$wrapper=i(),wp.html.string({tag:"span"===n.tagName?"span":"div",attrs:{class:"wp-view-wrap wp-view-type-"+e,"data-wp-view":t,contenteditable:!1}})):""},render:function(e){i(".wp-view-wrap",e).each(function(){var e=i(this),t=wp.mce.view.instance(this);t&&(t.$wrapper=e,t.render(),t.$el.detach(),e.empty().append(t.el).append('<span data-wp-view-end class="wp-view-end"></span>'))})},toText:function(e){return e.replace(/<(?:div|span)[^>]+data-wp-view="([^"]+)"[^>]*>.*?<span[^>]+data-wp-view-end[^>]*><\/span><\/(?:div|span)>/g,function(e,t){var n,t=c[t];return t&&(n=wp.mce.view.get(t.options.viewType)),t&&n?n.text(t):""})},removeInternalAttrs:function(e){var n={};return _.each(e,function(e,t){-1===t.indexOf("data-mce")&&(n[t]=e)}),n},attrs:function(e){return wp.mce.view.removeInternalAttrs(wp.html.attrs(e))},instance:function(e){e=i(e).data("wp-view");if(e)return c[e]},select:function(e){var t=i(e);t.hasClass("selected")||(t.addClass("selected"),i(e.firstChild).trigger("select"))},deselect:function(e){var t=i(e);t.hasClass("selected")&&(t.removeClass("selected"),i(e.firstChild).trigger("deselect"))}}}(jQuery); |