1 2 // Copyright 2014 The Chromium Authors. All rights reserved. 3 // Use of this source code is governed by a BSD-style license that can be 4 // found in the LICENSE file. 5 6 /** 7 * @constructor 8 * @implements {WebInspector.SourcesView.EditorAction} 9 */ 10 WebInspector.InplaceFormatterEditorAction = function() 11 { 12 } 13 14 WebInspector.InplaceFormatterEditorAction.prototype = { 15 /** 16 * @param {!WebInspector.Event} event 17 */ 18 _editorSelected: function(event) 19 { 20 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data); 21 this._updateButton(uiSourceCode); 22 }, 23 24 /** 25 * @param {!WebInspector.Event} event 26 */ 27 _editorClosed: function(event) 28 { 29 var wasSelected = /** @type {boolean} */ (event.data.wasSelected); 30 if (wasSelected) 31 this._updateButton(null); 32 }, 33 34 /** 35 * @param {?WebInspector.UISourceCode} uiSourceCode 36 */ 37 _updateButton: function(uiSourceCode) 38 { 39 this._button.element.classList.toggle("hidden", !this._isFormattable(uiSourceCode)); 40 }, 41 42 /** 43 * @param {!WebInspector.SourcesView} sourcesView 44 * @return {!Element} 45 */ 46 button: function(sourcesView) 47 { 48 if (this._button) 49 return this._button.element; 50 51 this._sourcesView = sourcesView; 52 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorSelected, this._editorSelected.bind(this)); 53 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorClosed, this._editorClosed.bind(this)); 54 55 this._button = new WebInspector.StatusBarButton(WebInspector.UIString("Format"), "sources-toggle-pretty-print-status-bar-item"); 56 this._button.toggled = false; 57 this._button.addEventListener("click", this._formatSourceInPlace, this); 58 this._updateButton(null); 59 60 return this._button.element; 61 }, 62 63 /** 64 * @param {?WebInspector.UISourceCode} uiSourceCode 65 * @return {boolean} 66 */ 67 _isFormattable: function(uiSourceCode) 68 { 69 if (!uiSourceCode) 70 return false; 71 return uiSourceCode.contentType() === WebInspector.resourceTypes.Stylesheet 72 || uiSourceCode.project().type() === WebInspector.projectTypes.Snippets; 73 }, 74 75 _formatSourceInPlace: function() 76 { 77 var uiSourceCode = this._sourcesView.currentUISourceCode(); 78 if (!this._isFormattable(uiSourceCode)) 79 return; 80 81 if (uiSourceCode.isDirty()) 82 contentLoaded.call(this, uiSourceCode.workingCopy()); 83 else 84 uiSourceCode.requestContent(contentLoaded.bind(this)); 85 86 /** 87 * @this {WebInspector.InplaceFormatterEditorAction} 88 * @param {?string} content 89 */ 90 function contentLoaded(content) 91 { 92 var formatter = WebInspector.Formatter.createFormatter(uiSourceCode.contentType()); 93 formatter.formatContent(uiSourceCode.highlighterType(), content || "", innerCallback.bind(this)); 94 } 95 96 /** 97 * @this {WebInspector.InplaceFormatterEditorAction} 98 * @param {string} formattedContent 99 * @param {!WebInspector.FormatterSourceMapping} formatterMapping 100 */ 101 function innerCallback(formattedContent, formatterMapping) 102 { 103 if (uiSourceCode.workingCopy() === formattedContent) 104 return; 105 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode); 106 var start = [0, 0]; 107 if (sourceFrame) { 108 var selection = sourceFrame.selection(); 109 start = formatterMapping.originalToFormatted(selection.startLine, selection.startColumn); 110 } 111 uiSourceCode.setWorkingCopy(formattedContent); 112 this._sourcesView.showSourceLocation(uiSourceCode, start[0], start[1]); 113 } 114 }, 115 } 116