@yuebai008/cli
Version:
Command line interface for rapid qg-minigame development
1 lines • 2.5 kB
JavaScript
import*as i18n from"../../core/i18n/i18n.js";import*as Formatter from"../../models/formatter/formatter.js";import*as Persistence from"../../models/persistence/persistence.js";import*as UI from"../../ui/legacy/legacy.js";import{Events,registerEditorAction}from"./SourcesView.js";const UIStrings={formatS:"Format {PH1}",format:"Format"},str_=i18n.i18n.registerUIStrings("panels/sources/InplaceFormatterEditorAction.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);let inplaceFormatterEditorActionInstance;export class InplaceFormatterEditorAction{button;sourcesView;constructor(){}static instance(t={forceNew:null}){const{forceNew:e}=t;return inplaceFormatterEditorActionInstance&&!e||(inplaceFormatterEditorActionInstance=new InplaceFormatterEditorAction),inplaceFormatterEditorActionInstance}editorSelected(t){const e=t.data;this.updateButton(e)}editorClosed(t){const{wasSelected:e}=t.data;e&&this.updateButton(null)}updateButton(t){const e=this.isFormattable(t);this.button.element.classList.toggle("hidden",!e),t&&e&&this.button.setTitle(i18nString(UIStrings.formatS,{PH1:t.name()}))}getOrCreateButton(t){return this.button||(this.sourcesView=t,this.sourcesView.addEventListener(Events.EditorSelected,this.editorSelected.bind(this)),this.sourcesView.addEventListener(Events.EditorClosed,this.editorClosed.bind(this)),this.button=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.format),"brackets"),this.button.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.formatSourceInPlace,this),this.updateButton(t.currentUISourceCode())),this.button}isFormattable(t){return!!t&&(!!t.project().canSetFileContent()||null!==Persistence.Persistence.PersistenceImpl.instance().binding(t))}formatSourceInPlace(){const t=this.sourcesView.currentUISourceCode();t&&this.isFormattable(t)&&(t.isDirty()?this.contentLoaded(t,t.workingCopy()):t.requestContent().then((e=>{this.contentLoaded(t,e.content||"")})))}async contentLoaded(t,e){const o=t.mimeType(),{formattedContent:r,formattedMapping:n}=await Formatter.ScriptFormatter.format(t.contentType(),o,e);this.formattingComplete(t,r,n)}formattingComplete(t,e,o){if(t.workingCopy()===e)return;const r=this.sourcesView.viewForFile(t);let n=[0,0];if(r){const t=r.textEditor.toLineColumn(r.textEditor.state.selection.main.head);n=o.originalToFormatted(t.lineNumber,t.columnNumber)}t.setWorkingCopy(e),this.sourcesView.showSourceLocation(t,{lineNumber:n[0],columnNumber:n[1]})}}registerEditorAction(InplaceFormatterEditorAction.instance);