| OLD | NEW |
| 1 | |
| 2 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 4 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** |
| 5 * @implements {WebInspector.SourcesView.EditorAction} |
| 6 * @unrestricted |
| 7 */ |
| 8 WebInspector.InplaceFormatterEditorAction = class { |
| 9 /** |
| 10 * @param {!WebInspector.Event} event |
| 11 */ |
| 12 _editorSelected(event) { |
| 13 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data); |
| 14 this._updateButton(uiSourceCode); |
| 15 } |
| 5 | 16 |
| 6 /** | 17 /** |
| 7 * @constructor | 18 * @param {!WebInspector.Event} event |
| 8 * @implements {WebInspector.SourcesView.EditorAction} | 19 */ |
| 9 */ | 20 _editorClosed(event) { |
| 10 WebInspector.InplaceFormatterEditorAction = function() | 21 var wasSelected = /** @type {boolean} */ (event.data.wasSelected); |
| 11 { | 22 if (wasSelected) |
| 12 }; | 23 this._updateButton(null); |
| 24 } |
| 13 | 25 |
| 14 WebInspector.InplaceFormatterEditorAction.prototype = { | 26 /** |
| 15 /** | 27 * @param {?WebInspector.UISourceCode} uiSourceCode |
| 16 * @param {!WebInspector.Event} event | 28 */ |
| 17 */ | 29 _updateButton(uiSourceCode) { |
| 18 _editorSelected: function(event) | 30 this._button.element.classList.toggle('hidden', !this._isFormattable(uiSourc
eCode)); |
| 19 { | 31 } |
| 20 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data
); | 32 |
| 21 this._updateButton(uiSourceCode); | 33 /** |
| 22 }, | 34 * @override |
| 35 * @param {!WebInspector.SourcesView} sourcesView |
| 36 * @return {!WebInspector.ToolbarButton} |
| 37 */ |
| 38 button(sourcesView) { |
| 39 if (this._button) |
| 40 return this._button; |
| 41 |
| 42 this._sourcesView = sourcesView; |
| 43 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorSel
ected, this._editorSelected.bind(this)); |
| 44 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.EditorClo
sed, this._editorClosed.bind(this)); |
| 45 |
| 46 this._button = new WebInspector.ToolbarButton(WebInspector.UIString('Format'
), 'format-toolbar-item'); |
| 47 this._button.addEventListener('click', this._formatSourceInPlace, this); |
| 48 this._updateButton(sourcesView.currentUISourceCode()); |
| 49 |
| 50 return this._button; |
| 51 } |
| 52 |
| 53 /** |
| 54 * @param {?WebInspector.UISourceCode} uiSourceCode |
| 55 * @return {boolean} |
| 56 */ |
| 57 _isFormattable(uiSourceCode) { |
| 58 if (!uiSourceCode) |
| 59 return false; |
| 60 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSystem) |
| 61 return true; |
| 62 if (WebInspector.persistence.binding(uiSourceCode)) |
| 63 return true; |
| 64 return uiSourceCode.contentType().isStyleSheet() || |
| 65 uiSourceCode.project().type() === WebInspector.projectTypes.Snippets; |
| 66 } |
| 67 |
| 68 _formatSourceInPlace() { |
| 69 var uiSourceCode = this._sourcesView.currentUISourceCode(); |
| 70 if (!this._isFormattable(uiSourceCode)) |
| 71 return; |
| 72 |
| 73 if (uiSourceCode.isDirty()) |
| 74 contentLoaded.call(this, uiSourceCode.workingCopy()); |
| 75 else |
| 76 uiSourceCode.requestContent().then(contentLoaded.bind(this)); |
| 23 | 77 |
| 24 /** | 78 /** |
| 25 * @param {!WebInspector.Event} event | 79 * @this {WebInspector.InplaceFormatterEditorAction} |
| 80 * @param {?string} content |
| 26 */ | 81 */ |
| 27 _editorClosed: function(event) | 82 function contentLoaded(content) { |
| 28 { | 83 var highlighterType = WebInspector.NetworkProject.uiSourceCodeMimeType(uiS
ourceCode); |
| 29 var wasSelected = /** @type {boolean} */ (event.data.wasSelected); | 84 WebInspector.Formatter.format( |
| 30 if (wasSelected) | 85 uiSourceCode.contentType(), highlighterType, content || '', innerCallb
ack.bind(this)); |
| 31 this._updateButton(null); | 86 } |
| 32 }, | |
| 33 | 87 |
| 34 /** | 88 /** |
| 35 * @param {?WebInspector.UISourceCode} uiSourceCode | 89 * @this {WebInspector.InplaceFormatterEditorAction} |
| 90 * @param {string} formattedContent |
| 91 * @param {!WebInspector.FormatterSourceMapping} formatterMapping |
| 36 */ | 92 */ |
| 37 _updateButton: function(uiSourceCode) | 93 function innerCallback(formattedContent, formatterMapping) { |
| 38 { | 94 if (uiSourceCode.workingCopy() === formattedContent) |
| 39 this._button.element.classList.toggle("hidden", !this._isFormattable(uiS
ourceCode)); | 95 return; |
| 40 }, | 96 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode); |
| 41 | 97 var start = [0, 0]; |
| 42 /** | 98 if (sourceFrame) { |
| 43 * @override | 99 var selection = sourceFrame.selection(); |
| 44 * @param {!WebInspector.SourcesView} sourcesView | 100 start = formatterMapping.originalToFormatted(selection.startLine, select
ion.startColumn); |
| 45 * @return {!WebInspector.ToolbarButton} | 101 } |
| 46 */ | 102 uiSourceCode.setWorkingCopy(formattedContent); |
| 47 button: function(sourcesView) | 103 this._sourcesView.showSourceLocation(uiSourceCode, start[0], start[1]); |
| 48 { | 104 } |
| 49 if (this._button) | 105 } |
| 50 return this._button; | |
| 51 | |
| 52 this._sourcesView = sourcesView; | |
| 53 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.Edito
rSelected, this._editorSelected.bind(this)); | |
| 54 this._sourcesView.addEventListener(WebInspector.SourcesView.Events.Edito
rClosed, this._editorClosed.bind(this)); | |
| 55 | |
| 56 this._button = new WebInspector.ToolbarButton(WebInspector.UIString("For
mat"), "format-toolbar-item"); | |
| 57 this._button.addEventListener("click", this._formatSourceInPlace, this); | |
| 58 this._updateButton(sourcesView.currentUISourceCode()); | |
| 59 | |
| 60 return this._button; | |
| 61 }, | |
| 62 | |
| 63 /** | |
| 64 * @param {?WebInspector.UISourceCode} uiSourceCode | |
| 65 * @return {boolean} | |
| 66 */ | |
| 67 _isFormattable: function(uiSourceCode) | |
| 68 { | |
| 69 if (!uiSourceCode) | |
| 70 return false; | |
| 71 if (uiSourceCode.project().type() === WebInspector.projectTypes.FileSyst
em) | |
| 72 return true; | |
| 73 if (WebInspector.persistence.binding(uiSourceCode)) | |
| 74 return true; | |
| 75 return uiSourceCode.contentType().isStyleSheet() | |
| 76 || uiSourceCode.project().type() === WebInspector.projectTypes.Snipp
ets; | |
| 77 }, | |
| 78 | |
| 79 _formatSourceInPlace: function() | |
| 80 { | |
| 81 var uiSourceCode = this._sourcesView.currentUISourceCode(); | |
| 82 if (!this._isFormattable(uiSourceCode)) | |
| 83 return; | |
| 84 | |
| 85 if (uiSourceCode.isDirty()) | |
| 86 contentLoaded.call(this, uiSourceCode.workingCopy()); | |
| 87 else | |
| 88 uiSourceCode.requestContent().then(contentLoaded.bind(this)); | |
| 89 | |
| 90 /** | |
| 91 * @this {WebInspector.InplaceFormatterEditorAction} | |
| 92 * @param {?string} content | |
| 93 */ | |
| 94 function contentLoaded(content) | |
| 95 { | |
| 96 var highlighterType = WebInspector.NetworkProject.uiSourceCodeMimeTy
pe(uiSourceCode); | |
| 97 WebInspector.Formatter.format(uiSourceCode.contentType(), highlighte
rType, content || "", innerCallback.bind(this)); | |
| 98 } | |
| 99 | |
| 100 /** | |
| 101 * @this {WebInspector.InplaceFormatterEditorAction} | |
| 102 * @param {string} formattedContent | |
| 103 * @param {!WebInspector.FormatterSourceMapping} formatterMapping | |
| 104 */ | |
| 105 function innerCallback(formattedContent, formatterMapping) | |
| 106 { | |
| 107 if (uiSourceCode.workingCopy() === formattedContent) | |
| 108 return; | |
| 109 var sourceFrame = this._sourcesView.viewForFile(uiSourceCode); | |
| 110 var start = [0, 0]; | |
| 111 if (sourceFrame) { | |
| 112 var selection = sourceFrame.selection(); | |
| 113 start = formatterMapping.originalToFormatted(selection.startLine
, selection.startColumn); | |
| 114 } | |
| 115 uiSourceCode.setWorkingCopy(formattedContent); | |
| 116 this._sourcesView.showSourceLocation(uiSourceCode, start[0], start[1
]); | |
| 117 } | |
| 118 }, | |
| 119 }; | 106 }; |
| OLD | NEW |