UNPKG

@yuebai008/cli

Version:

Command line interface for rapid qg-minigame development

1 lines 5.08 kB
import*as Common from"../../core/common/common.js";import*as SDK from"../../core/sdk/sdk.js";import*as Workspace from"../workspace/workspace.js";import{ContentProviderBasedProject}from"./ContentProviderBasedProject.js";import{NetworkProject}from"./NetworkProject.js";import{metadataForURL}from"./ResourceUtils.js";const uiSourceCodeToStyleMap=new WeakMap;export class StylesSourceMapping{#e;#t;#s;#o;constructor(e,t){this.#e=e;const s=this.#e.target();this.#t=new ContentProviderBasedProject(t,"css:"+s.id(),Workspace.Workspace.projectTypes.Network,"",!1),NetworkProject.setTargetForProject(this.#t,s),this.#s=new Map,this.#o=[this.#e.addEventListener(SDK.CSSModel.Events.StyleSheetAdded,this.styleSheetAdded,this),this.#e.addEventListener(SDK.CSSModel.Events.StyleSheetRemoved,this.styleSheetRemoved,this),this.#e.addEventListener(SDK.CSSModel.Events.StyleSheetChanged,this.styleSheetChanged,this)]}rawLocationToUILocation(e){const t=e.header();if(!t||!this.acceptsHeader(t))return null;const s=this.#s.get(t.resourceURL());if(!s)return null;let o=e.lineNumber,r=e.columnNumber;if(t.isInline&&t.hasSourceURL){o-=t.lineNumberInSource(0);const e=t.columnNumberInSource(o,0);void 0===e?r=e:r-=e}return s.getUiSourceCode().uiLocation(o,r)}uiLocationToRawLocations(e){const t=uiSourceCodeToStyleMap.get(e.uiSourceCode);if(!t)return[];const s=[];for(const o of t.getHeaders()){let t=e.lineNumber,r=e.columnNumber;o.isInline&&o.hasSourceURL&&(r=o.columnNumberInSource(t,e.columnNumber||0),t=o.lineNumberInSource(t)),s.push(new SDK.CSSModel.CSSLocation(o,t,r))}return s}acceptsHeader(e){return!e.isConstructedByNew()&&(!(e.isInline&&!e.hasSourceURL&&"inspector"!==e.origin)&&!!e.resourceURL())}styleSheetAdded(e){const t=e.data;if(!this.acceptsHeader(t))return;const s=t.resourceURL();let o=this.#s.get(s);o?o.addHeader(t):(o=new StyleFile(this.#e,this.#t,t),this.#s.set(s,o))}styleSheetRemoved(e){const t=e.data;if(!this.acceptsHeader(t))return;const s=t.resourceURL(),o=this.#s.get(s);o&&(1===o.getHeaders().size?(o.dispose(),this.#s.delete(s)):o.removeHeader(t))}styleSheetChanged(e){const t=this.#e.styleSheetHeaderForId(e.data.styleSheetId);if(!t||!this.acceptsHeader(t))return;const s=this.#s.get(t.resourceURL());s&&s.styleSheetChanged(t)}dispose(){for(const e of this.#s.values())e.dispose();this.#s.clear(),Common.EventTarget.removeEventListeners(this.#o),this.#t.removeProject()}}export class StyleFile{#e;#t;headers;uiSourceCode;#o;#r;#i;#n;#d;constructor(e,t,s){this.#e=e,this.#t=t,this.headers=new Set([s]);const o=e.target(),r=s.resourceURL(),i=metadataForURL(o,s.frameId,r);this.uiSourceCode=this.#t.createUISourceCode(r,s.contentType()),uiSourceCodeToStyleMap.set(this.uiSourceCode,this),NetworkProject.setInitialFrameAttribution(this.uiSourceCode,s.frameId),this.#t.addUISourceCodeWithProvider(this.uiSourceCode,this,i,"text/css"),this.#o=[this.uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyChanged,this.workingCopyChanged,this),this.uiSourceCode.addEventListener(Workspace.UISourceCode.Events.WorkingCopyCommitted,this.workingCopyCommitted,this)],this.#r=new Common.Throttler.Throttler(StyleFile.updateTimeout),this.#i=!1}addHeader(e){this.headers.add(e),NetworkProject.addFrameAttribution(this.uiSourceCode,e.frameId)}removeHeader(e){this.headers.delete(e),NetworkProject.removeFrameAttribution(this.uiSourceCode,e.frameId)}styleSheetChanged(e){if(console.assert(this.headers.has(e)),this.#d||!this.headers.has(e))return;const t=this.mirrorContent.bind(this,e,!0);this.#r.schedule(t,!1)}workingCopyCommitted(){if(this.#n)return;const e=this.mirrorContent.bind(this,this.uiSourceCode,!0);this.#r.schedule(e,!0)}workingCopyChanged(){if(this.#n)return;const e=this.mirrorContent.bind(this,this.uiSourceCode,!1);this.#r.schedule(e,!1)}async mirrorContent(e,t){if(this.#i)return void this.styleFileSyncedForTest();let s=null;if(e===this.uiSourceCode)s=this.uiSourceCode.workingCopy();else{s=(await e.requestContent()).content}if(null===s||this.#i)return void this.styleFileSyncedForTest();e!==this.uiSourceCode&&(this.#n=!0,this.uiSourceCode.addRevision(s),this.#n=!1),this.#d=!0;const o=[];for(const r of this.headers)r!==e&&o.push(this.#e.setStyleSheetText(r.id,s,t));await Promise.all(o),this.#d=!1,this.styleFileSyncedForTest()}styleFileSyncedForTest(){}dispose(){this.#i||(this.#i=!0,this.#t.removeUISourceCode(this.uiSourceCode.url()),Common.EventTarget.removeEventListeners(this.#o))}contentURL(){return console.assert(this.headers.size>0),this.headers.values().next().value.originalContentProvider().contentURL()}contentType(){return console.assert(this.headers.size>0),this.headers.values().next().value.originalContentProvider().contentType()}requestContent(){return console.assert(this.headers.size>0),this.headers.values().next().value.originalContentProvider().requestContent()}searchInContent(e,t,s){return console.assert(this.headers.size>0),this.headers.values().next().value.originalContentProvider().searchInContent(e,t,s)}static updateTimeout=200;getHeaders(){return this.headers}getUiSourceCode(){return this.uiSourceCode}}