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