UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 6.49 kB
import*as Common from"../../core/common/common.js";import*as Host from"../../core/host/host.js";import*as i18n from"../../core/i18n/i18n.js";import*as Root from"../../core/root/root.js";import*as WorkspaceDiff from"../../models/workspace_diff/workspace_diff.js";import{PanelUtils}from"../../panels/utils/utils.js";import*as Diff from"../../third_party/diff/diff.js";import*as DiffView from"../../ui/components/diff_view/diff_view.js";import*as UI from"../../ui/legacy/legacy.js";import{ChangesSidebar}from"./ChangesSidebar.js";import changesViewStyles from"./changesView.css.js";const UIStrings={revertAllChangesToCurrentFile:"Revert all changes to current file",copyAllChangesFromCurrentFile:"Copy all changes from current file",noChanges:"No changes",binaryData:"Binary data",sInsertions:"{n, plural, =1 {# insertion (+)} other {# insertions (+)}}",sDeletions:"{n, plural, =1 {# deletion (-)} other {# deletions (-)}}",copy:"Copy"},str_=i18n.i18n.registerUIStrings("panels/changes/ChangesView.ts",UIStrings),i18nString=i18n.i18n.getLocalizedString.bind(void 0,str_);function diffStats(e){const t=e.reduce(((e,t)=>e+(t[0]===Diff.Diff.Operation.Insert?t[1].length:0)),0),i=e.reduce(((e,t)=>e+(t[0]===Diff.Diff.Operation.Delete?t[1].length:0)),0),o=i18nString(UIStrings.sDeletions,{n:i});return`${i18nString(UIStrings.sInsertions,{n:t})}, ${o}`}let changesViewInstance,diffUILocationRevealerInstance;export class ChangesView extends UI.Widget.VBox{emptyWidget;workspaceDiff;changesSidebar;selectedUISourceCode;#e;diffContainer;toolbar;diffStats;diffView;copyButton;copyButtonSeparator;constructor(){super(!0);const e=new UI.SplitWidget.SplitWidget(!0,!1),t=new UI.Widget.Widget;e.setMainWidget(t),e.show(this.contentElement),this.emptyWidget=new UI.EmptyWidget.EmptyWidget(""),this.emptyWidget.show(t.element),this.workspaceDiff=WorkspaceDiff.WorkspaceDiff.workspaceDiff(),this.changesSidebar=new ChangesSidebar(this.workspaceDiff),this.changesSidebar.addEventListener("SelectedUISourceCodeChanged",this.selectedUISourceCodeChanged,this),e.setSidebarWidget(this.changesSidebar),this.selectedUISourceCode=null,this.diffContainer=t.element.createChild("div","diff-container"),UI.ARIAUtils.markAsTabpanel(this.diffContainer),this.diffContainer.addEventListener("click",(e=>this.click(e))),this.diffView=this.diffContainer.appendChild(new DiffView.DiffView.DiffView),this.toolbar=new UI.Toolbar.Toolbar("changes-toolbar",t.element);const i=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.revertAllChangesToCurrentFile),"undo");i.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.revert.bind(this)),this.toolbar.appendToolbarItem(i),this.diffStats=new UI.Toolbar.ToolbarText(""),this.toolbar.appendToolbarItem(this.diffStats),this.copyButton=new UI.Toolbar.ToolbarButton(i18nString(UIStrings.copyAllChangesFromCurrentFile),"copy",UIStrings.copy),this.copyButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click,this.copyChanges.bind(this)),this.copyButtonSeparator=new UI.Toolbar.ToolbarSeparator,this.toolbar.setEnabled(!1),this.hideDiff(i18nString(UIStrings.noChanges)),this.selectedUISourceCodeChanged()}static instance(e={forceNew:null}){const{forceNew:t}=e;return changesViewInstance&&!t||(changesViewInstance=new ChangesView),changesViewInstance}selectedUISourceCodeChanged(){this.revealUISourceCode(this.changesSidebar.selectedUISourceCode()),this.selectedUISourceCode?.contentType()===Common.ResourceType.resourceTypes.Stylesheet?(this.toolbar.appendToolbarItem(this.copyButtonSeparator),this.toolbar.appendToolbarItem(this.copyButton)):(this.toolbar.removeToolbarItem(this.copyButtonSeparator),this.toolbar.removeToolbarItem(this.copyButton))}revert(){const e=this.selectedUISourceCode;e&&this.workspaceDiff.revertToOriginal(e)}async copyChanges(){const e=this.selectedUISourceCode;if(!e)return;const t=await this.workspaceDiff.requestDiff(e,{shouldFormatDiff:!0});if(!t||t?.diff.length<2)return;const i=await PanelUtils.formatCSSChangesFromDiff(t.diff);Host.InspectorFrontendHost.InspectorFrontendHostInstance.copyText(i)}click(e){if(this.selectedUISourceCode)for(const t of e.composedPath()){if(!(t instanceof HTMLElement))continue;const i=t.ownerDocument.getSelection();if(i?.toString())break;if(t.classList.contains("diff-line-content")&&t.hasAttribute("data-line-number")){let i=Number(t.dataset.lineNumber)-1;Root.Runtime.experiments.isEnabled(Root.Runtime.ExperimentName.PRECISE_CHANGES)&&this.#e&&(i=this.#e.formattedToOriginal(i,0)[0]),Common.Revealer.reveal(this.selectedUISourceCode.uiLocation(i,0),!1),e.consume(!0);break}if(t.classList.contains("diff-listing"))break}}revealUISourceCode(e){this.selectedUISourceCode!==e&&(this.selectedUISourceCode&&this.workspaceDiff.unsubscribeFromDiffChange(this.selectedUISourceCode,this.refreshDiff,this),e&&this.isShowing()&&this.workspaceDiff.subscribeToDiffChange(e,this.refreshDiff,this),this.selectedUISourceCode=e,this.refreshDiff())}wasShown(){this.refreshDiff(),this.registerCSSFiles([changesViewStyles])}async refreshDiff(){if(!this.isShowing())return;if(!this.selectedUISourceCode)return void this.renderDiffRows();const e=this.selectedUISourceCode;if(!e.contentType().isTextType())return void this.hideDiff(i18nString(UIStrings.binaryData));const t=await this.workspaceDiff.requestDiff(e,{shouldFormatDiff:Root.Runtime.experiments.isEnabled(Root.Runtime.ExperimentName.PRECISE_CHANGES)});this.selectedUISourceCode===e&&(this.#e=t?.formattedCurrentMapping,this.renderDiffRows(t?.diff))}hideDiff(e){this.diffStats.setText(""),this.toolbar.setEnabled(!1),this.diffContainer.style.display="none",this.emptyWidget.text=e,this.emptyWidget.showWidget()}renderDiffRows(e){if(!e||1===e.length&&e[0][0]===Diff.Diff.Operation.Equal)this.hideDiff(i18nString(UIStrings.noChanges));else{this.diffStats.setText(diffStats(e)),this.toolbar.setEnabled(!0),this.emptyWidget.hideWidget();const t=this.selectedUISourceCode.mimeType();this.diffContainer.style.display="block",this.diffView.data={diff:e,mimeType:t}}}}export class DiffUILocationRevealer{static instance(e={forceNew:!1}){const{forceNew:t}=e;return diffUILocationRevealerInstance&&!t||(diffUILocationRevealerInstance=new DiffUILocationRevealer),diffUILocationRevealerInstance}async reveal(e,t){if(!(e instanceof WorkspaceDiff.WorkspaceDiff.DiffUILocation))throw new Error("Internal error: not a diff ui location");await UI.ViewManager.ViewManager.instance().showView("changes.changes"),ChangesView.instance().changesSidebar.selectUISourceCode(e.uiSourceCode,t)}}