UNPKG

@minecraft/creator-tools

Version:

Minecraft Creator Tools command line and libraries.

1 lines 16.3 kB
"use strict";(self.webpackChunkmctools_app=self.webpackChunkmctools_app||[]).push([[88],{68088:(t,e,i)=>{i.d(e,{default:()=>A});var s=i(74848),o=i(96540),a=i(75065),n=i(58807),r=i(14466),d=i(94134),l=i(69890),h=i(11641),p=i(20982),c=i(66188),g=i(75236),u=i(18458),m=i(59701),f=i(40267),_=i(96997),y=i(38524),v=i(73053),F=i(35419),S=i(66940),j=i(22275),b=(i(59296),i(74239));class C extends o.Component{editor;_activeModel;_monaco=null;_trackingUpdates=!1;_scriptsFolder;_modelReloadPending=!1;_isMounted=!1;_navigationDecorations;_lastNavigationRequestId;_suppressOnChange=!1;constructor(t){super(t),this._handleContentUpdated=this._handleContentUpdated.bind(this),this._handleEditorWillMount=this._handleEditorWillMount.bind(this),this._handleEditorDidMount=this._handleEditorDidMount.bind(this),this._handleFileStateChanged=this._handleFileStateChanged.bind(this),this._handleFileStateRemoved=this._handleFileStateRemoved.bind(this),this._handleSnippetGalleryCommand=this._handleSnippetGalleryCommand.bind(this),this._handleNewSearch=this._handleNewSearch.bind(this),this._updateModels=this._updateModels.bind(this),this._doUpdate=this._doUpdate.bind(this),this.persist=this.persist.bind(this),this._considerFormat=this._considerFormat.bind(this),this._handleFileStateChanged=this._handleFileStateChanged.bind(this),this._handleFileStateRemoved=this._handleFileStateRemoved.bind(this),this._handleFileStateAdded=this._handleFileStateAdded.bind(this),this._zoomIn=this._zoomIn.bind(this),this._zoomOut=this._zoomOut.bind(this);let e="";void 0!==t.file&&(e=C.getUriForFile(t.file,t.scriptLanguage)),this.state={pathToEdit:e,isLoaded:!1,contentReady:void 0!==t.initialContent},this._ensureContentLoaded=this._ensureContentLoaded.bind(this)}_handleNewSearch(t){if(!this.state)return;const e=t.target.value,i=S.A.getElementLeft(t.currentTarget);this.setState({pathToEdit:this.state.pathToEdit,isLoaded:this.state.isLoaded,snippetSearch:e,proposedLeft:i})}static getDerivedStateFromProps(t,e){if(t.file){const i=C.getUriForFile(t.file,t.scriptLanguage);if(null==e)return{pathToEdit:i,isLoaded:!1,contentReady:void 0!==t.initialContent};if(i!==e.pathToEdit)return e.pathToEdit=i,e.contentReady=!1,e}return null}_handleEditorWillMount(t){if(this._monaco!==t){this._monaco=t;const e=this._monaco.languages.typescript;e.javascriptDefaults.setDiagnosticsOptions({noSemanticValidation:!1,noSyntaxValidation:!1}),e.javascriptDefaults.setCompilerOptions({target:e.ScriptTarget.ESNext,module:e.ModuleKind.ESNext,lib:["es2022"],allowNonTsExtensions:!0}),e.typescriptDefaults.setCompilerOptions({lib:["es2022"],allowJs:!0,target:99,allowNonTsExtensions:!0,noImplicitAny:!0})}this._injectLibModels(),this._ensureModels(t)}_injectLibModels(){if(!this._monaco)return;const t=this._monaco.languages.typescript;if(t&&t.typescriptDefaults){const e=t.typescriptDefaults.getExtraLibs();let i=!1,s=!1;for(const t in e)"@minecraft/server.d.ts"===t&&(i=!0),"@minecraft/math.d.ts"===t&&(s=!0);if(!i){let e=null;e=this.props.project&&this.props.project.scriptVersion===g.tF.stable10?f.A.stable10TypeDefs:f.A.stable20TypeDefs;let i=encodeURI("file://fs/global.d.ts");const s="interface GlobalConsole { warn: (message: string) => void; log: (message: string) => void;}; declare var console: GlobalConsole; ";if(t.javascriptDefaults.addExtraLib(s,i),t.typescriptDefaults.addExtraLib(s,i),e)for(let i=0;i<e.typeDefs.length;i++){const s=e.typeDefs[i],o=s.name+".d.ts";let a=s.content.join("\n");a="declare module '"+s.name+"' {"+a+"}",t.javascriptDefaults.addExtraLib(a,o),t.typescriptDefaults.addExtraLib(a,o)}}if(!s){const t=this._monaco.languages.typescript,e=f.A.libs;if(e)for(let i=0;i<e.typeDefs.length;i++){const s=e.typeDefs[i],o=s.name;if(o.length>0){let e=s.content.join("\n");const i=o+".d.ts";e="declare module '"+o+"' {"+e+"}",t.javascriptDefaults.addExtraLib(e,i),t.typescriptDefaults.addExtraLib(e,i)}}}}}componentWillUnmount(){this._isMounted=!1,this._scriptsFolder&&(this._trackingUpdates=!1,this._scriptsFolder.storage.onFileAdded.unsubscribe(this._handleFileStateAdded),this._scriptsFolder.storage.onFileRemoved.unsubscribe(this._handleFileStateRemoved))}async _ensureModels(t){let e=null;this.props.project&&this.props.project.scriptVersion===g.tF.stable10?(await f.A.loadStable10ScriptTypes(),e=f.A.stable10TypeDefs):(await f.A.loadStable20ScriptTypes(),e=f.A.stable20TypeDefs),await this._doUpdate();const i=await f.A.getLibs();if(e&&i){if(this.props.project){const e=await this.props.project.getMainScriptsFolder();let i;if(e)i=e;else{const t=await this.props.project.getBehaviorPackScriptsFolder();if(void 0!==t)i=t;else{if(null===this.props.project.projectFolder)return;i=this.props.project.projectFolder}}try{await this._loadModelsFromFolder(t,i)}catch(t){r.Ay.debugAlert("Error loading TS/JS models from folder: "+t)}this._trackingUpdates||(this._trackingUpdates=!0,this._scriptsFolder=i,i.storage.onFileAdded.has(this._handleFileStateAdded)||i.storage.onFileAdded.subscribe(this._handleFileStateAdded),i.storage.onFileContentsUpdated.has(this._handleFileStateChanged)||i.storage.onFileContentsUpdated.subscribe(this._handleFileStateChanged),i.storage.onFileRemoved.has(this._handleFileStateRemoved)||i.storage.onFileRemoved.subscribe(this._handleFileStateRemoved))}this._injectLibModels(),void 0===this.state||this.state.isLoaded||this.setState({pathToEdit:this.state.pathToEdit,isLoaded:!0,snippetSearch:this.state.snippetSearch,proposedLeft:this.state.proposedLeft})}}_handleFileStateRemoved(t,e){this._updateFiles(e)}_handleFileStateAdded(t,e){this._updateFiles(e.storageRelativePath)}_handleFileStateChanged(t,e){e.sourceId!==this.getEditorInstanceId()&&this._updateFiles(e.file.storageRelativePath)}_updateFiles(t){if(void 0===this._monaco)return;const e=m.Ay.getTypeFromName(t);"js"!==e&&"mjs"!==e&&"ts"!==e||this._modelReloadPending||(this._modelReloadPending=!0,window.setTimeout(this._updateModels,50))}_updateModels(){void 0!==this._monaco&&this._modelReloadPending&&(this._modelReloadPending=!1,this._ensureModels(this._monaco))}async _loadModelsFromFolder(t,e){e.isLoaded||await e.load();for(const i in e.files){const s=e.files[i];if(void 0!==s){const i=m.Ay.getTypeFromName(s.name);if("js"===i||"ts"===i)await this._ensureModelForFile(t,e,s),"ts"===i&&this.props.file&&this.compileTsToJs(s,!0);else if(m.Ay.isFileStorageItem(s)){const e=await m.Ay.getFileStorageFolder(s);e&&"string"!=typeof e&&await this._loadModelsFromFolder(t,e)}}}for(const i in e.folders){const s=e.folders[i];void 0!==s&&await this._loadModelsFromFolder(t,s)}}static getUriForFile(t,e){let i="file://fs"+m.Ay.ensureStartsWithDelimiter(t.storageRelativePath);return i=i.replace(/ /gi,"__"),i=encodeURI(i),i}getEditorInstanceId(){return"JavaScriptEditor|"+(this.props.file?this.props.file.extendedPath:"noFile")}async _ensureModelForFile(t,e,i){const s=C.getUriForFile(i,this.props.scriptLanguage);let o="javascript",n="";if(i.isContentLoaded||await i.loadContent(),"ts"===i.type){o="typescript";const t=e.ensureFile(m.Ay.getBaseFromName(i.name)+".ts");t.isContentLoaded||await t.loadContent(),""===t.content||void 0===t.content||null===t.content||t.content instanceof Uint8Array?(n=i.content,t.setContentIfSemanticallyDifferent(n,a.C.regularEdit,this.getEditorInstanceId())):n=t.content}else n=i.content;if(void 0!==i.content&&"string"==typeof i.content){const e=t.Uri.parse(s);let a=t.editor.getModel(e);null==a?(this._suppressOnChange=!0,a=t.editor.createModel(n,o,e),this._suppressOnChange=!1):a.getValue().trim()!==i.content.trim()&&(this._suppressOnChange=!0,a.setValue(i.content),this._suppressOnChange=!1)}}_handleEditorDidMount(t,e){this.editor=t,this._monaco=e,void 0!==this.editor&&this._applyNavigationTarget(this.props.navigationTarget)}_handleContentUpdated(t,e){this._suppressOnChange||(this.editor&&this.props.file&&!this.props.readOnly&&t&&this.props.file.setContentIfSemanticallyDifferent(t,a.C.regularEdit,this.getEditorInstanceId()),!this.props.file&&this.props.onUpdateContent&&void 0!==t&&this.props.onUpdateContent(t))}componentDidMount(){this._isMounted=!0,this._doUpdate(),this._ensureContentLoaded()}componentDidUpdate(t,e){t.file!==this.props.file&&this._ensureContentLoaded(),!this.props.file&&this.editor&&void 0!==this.props.initialContent&&t.initialContent!==this.props.initialContent&&(this._suppressOnChange=!0,this.editor.setValue(this.props.initialContent),this._suppressOnChange=!1),t.navigationTarget!==this.props.navigationTarget&&this._applyNavigationTarget(this.props.navigationTarget)}_applyNavigationTarget(t){if(!this.editor||!t)return;if(this._lastNavigationRequestId===t.requestId)return;this._lastNavigationRequestId=t.requestId;let e=t.lineNumber,i=t.column??1;if(!e&&t.searchText){const s=this.editor.getModel();if(s){const o=s.findMatches(t.searchText,!1,!1,!1,null,!0);o.length>0&&(e=o[0].range.startLineNumber,i=o[0].range.startColumn)}}if(!e)return;this.editor.revealLineInCenter(e),this.editor.setPosition({lineNumber:e,column:i}),this.editor.focus(),this._navigationDecorations&&this._navigationDecorations.clear();const s=this._monaco?new this._monaco.Range(e,1,e,1):void 0;s&&(this._navigationDecorations=this.editor.createDecorationsCollection([{range:s,options:{isWholeLine:!0,className:"pie-highlight-line"}}]),window.setTimeout(()=>{this._navigationDecorations?.clear()},2e3))}async _ensureContentLoaded(){const t=this.props.file;if(void 0===this.props.initialContent){if(t)if(t.isContentLoaded&&void 0!==t.content)!this.state.contentReady&&this._isMounted&&this.setState({contentReady:!0});else try{await t.loadContent(),this._isMounted&&this.props.file===t&&this.setState({contentReady:!0})}catch(e){r.Ay.debug(`JavaScriptEditor: Failed to preload content for ${t.storageRelativePath}: ${e}`),this._isMounted&&this.setState({contentReady:!0})}}else!this.state.contentReady&&this._isMounted&&this.setState({contentReady:!0})}async _considerFormat(){if(this.editor&&this.editor.getModel()&&this.props.project&&this.props.project.creatorTools.formatBeforeSave){const t=this.editor.getAction("editor.action.formatDocument");if(t)try{await t.run()}catch(t){r.Ay.debug("JavaScriptEditor: format action failed, likely due to model change: "+t)}}}async persist(){let t=!1;if(this.editor&&this.props.file&&!this.props.readOnly){const e=this.props.file;if(this.editor.getModel()&&this.props.project&&this.props.project.creatorTools.formatBeforeSave){const t=this.editor.getAction("editor.action.formatDocument");if(t)try{await t.run()}catch(t){r.Ay.debug("JavaScriptEditor: format action failed during persist: "+t)}}const i=this.editor.getModel()?.uri;if(void 0===i||null===this._monaco)return!1;if(i.toString()!==this._monaco.Uri.parse(C.getUriForFile(e,this.props.scriptLanguage)).toString())return!1;const s=this.editor.getValue();s&&this.props.file.setContentIfSemanticallyDifferent(s,a.C.regularEdit,this.getEditorInstanceId())&&(t=!0,"ts"===m.Ay.getTypeFromName(e.name)&&await this.compileTsToJs(e,!0))}return t}async compileTsToJs(t,e){if("ts"===m.Ay.getTypeFromName(t.name)&&this.props.project){const i=C.getUriForFile(t,g.NJ.typeScript),s=await this.props.project.ensureDefaultScriptsFolder();let o=t.getFolderRelativePath(s);if(o){o.toLowerCase().endsWith(".ts")&&(o=o.substring(0,o.length-3)+".js");const t=await this.props.project.ensureLibScriptsFolder(),s=await t.ensureFileFromRelativePath(o);if(!this._monaco)return;const n=await this._monaco.languages.typescript.getTypeScriptWorker(),r=this._monaco.Uri.parse(i.toString()),d=await n(r),l=await d.getEmitOutput(r.toString());if(l.outputFiles.length>0){const t=l.outputFiles[0].text;s.setContentIfSemanticallyDifferent(t,a.C.versionlessEdit,this.getEditorInstanceId()),e&&s.saveContent()}}}}_zoomIn(){if(this.editor){let t=this.editor.getAction("editor.action.fontZoomIn");t&&(t.run(),this._updateZoom())}}_zoomOut(){if(this.editor){let t=this.editor.getAction("editor.action.fontZoomOut");t&&(t.run(),this._updateZoom())}}_updateZoom(){if(void 0===this.editor||null===this._monaco)return;const t=this.editor.getOption(this._monaco.editor.EditorOption.fontSize);void 0!==t&&this.props.onUpdatePreferredTextSize(Math.round(t))}async _doUpdate(){this.props.project&&this.props.project.scriptVersion===g.tF.stable10?f.A.stable10TypeDefs||await f.A.loadStable10ScriptTypes():f.A.stable20TypeDefs||await f.A.loadStable20ScriptTypes()}async _handleSnippetGalleryCommand(t,e){if(void 0===this.editor||!e.sampleSet)return;this.setState({pathToEdit:this.state.pathToEdit,isLoaded:this.state.isLoaded,snippetSearch:void 0});const i=await f.A.getSnippet(e.sampleSet,e.id);if(!i)return;let s;s=i.body.join("\n"),void 0!==s&&this.props.project&&(s=u.A.replaceCommonItems(s,this.props.project.name),this.editor.trigger("keyboard","type",{text:s}))}render(){let t=(0,s.jsx)(s.Fragment,{}),e=(0,s.jsx)(s.Fragment,{});const i="calc(100vh - "+this.props.heightOffset+"4px)",o="calc(100vh - "+(this.props.heightOffset+36)+"px)";if(!this.state.contentReady&&!this.props.initialContent&&this.props.file)return(0,s.jsxs)("div",{className:"jse-area",style:{minHeight:i,maxHeight:i},children:[(0,s.jsx)("div",{className:"jse-toolBar",children:(0,s.jsx)(d.A,{direction:"row",spacing:1,"aria-label":"Script editor toolbar"})}),(0,s.jsx)("div",{className:"jse-content",children:(0,s.jsx)("div",{className:"jse-editor",children:(0,s.jsxs)("div",{className:"jse-loading-placeholder",style:{height:o},children:[(0,s.jsx)("div",{className:"jse-loading-spinner"}),(0,s.jsx)("div",{className:"jse-loading-message",children:"Loading content..."})]})})})]});const a=(0,j.kt)();if(null!==this.props.file){let e;void 0!==this.props.setActivePersistable&&this.props.setActivePersistable(this),this.props.file&&(e=C.getUriForFile(this.props.file,this.props.scriptLanguage));let i="vs-dark";_.Ay.theme===_.g3.light&&(i="vs"),t=(0,s.jsx)("div",{className:"jse-editor",children:(0,s.jsx)(n.Ay,{height:o,theme:i,defaultLanguage:"typescript",path:e,defaultValue:this.props.file?void 0:this.props.initialContent??"",keepCurrentModel:!!this.props.file,options:{fontSize:this.props.preferredTextSize,readOnly:this.props.readOnly,formatOnPaste:!0,formatOnType:!0,quickSuggestions:!0,autoIndent:"full"},beforeMount:this._handleEditorWillMount,onMount:this._handleEditorDidMount,onChange:this._handleContentUpdated},"editor")},"editor")}const r=[];if(null!==f.A.snippetsFolder&&f.A.snippetsFolder.files)for(const t in f.A.snippetsFolder.files){const e=f.A.snippetsFolder.files[t];if(e){const t=m.Ay.getJsonObject(e);if(t)for(const e in t)t[e]&&r.push(e)}}const g=[];return this.props.readOnly?g.push((0,s.jsx)("div",{className:"jse-toolBarLabel",children:this.props.intl.formatMessage({id:"project_editor.js_ed.read_only"})},"roLabel")):g.push((0,s.jsx)("div",{children:(0,s.jsx)(l.A,{size:"small",id:"projSearch",className:"home-search",placeholder:this.props.intl.formatMessage({id:"project_editor.js_ed.snippet_placeholder"}),value:this.state.snippetSearch||"",onChange:this._handleNewSearch})},"projSearch")),this.state.snippetSearch&&this.state.snippetSearch.length>=3&&this.props.creatorTools.gallery&&(e=(0,s.jsx)("div",{className:"jse-snippets-overlay",style:{backgroundColor:a.contentBackground,color:a.contentForeground,left:this.state.proposedLeft?this.state.proposedLeft:void 0},children:(0,s.jsx)(v.Ay,{creatorTools:this.props.creatorTools,theme:this.props.theme,view:F.f.smallImage,gallery:this.props.creatorTools.gallery,search:this.state.snippetSearch,filterOn:[y.S.codeSample,y.S.editorCodeSample],onGalleryItemCommand:this._handleSnippetGalleryCommand})},"pageOverlay")),(0,s.jsxs)("div",{className:"jse-area",style:{minHeight:i,maxHeight:i},children:[(0,s.jsxs)("div",{className:"jse-commands",children:[(0,s.jsx)("div",{className:"jse-toolBarArea",children:(0,s.jsxs)(d.A,{direction:"row",spacing:1,"aria-label":this.props.intl.formatMessage({id:"project_editor.js_ed.toolbar_aria"}),children:[(0,s.jsx)(h.A,{onClick:this._zoomIn,title:"Zoom into the text editor","aria-label":"Zoom in",size:"small",children:(0,s.jsx)(p.g,{icon:c.KmH,className:"fa-lg"})}),(0,s.jsx)(h.A,{onClick:this._zoomOut,title:"Zoom out of the text editor","aria-label":"Zoom out",size:"small",children:(0,s.jsx)(p.g,{icon:c.YBE,className:"fa-lg"})})]})}),(0,s.jsx)("div",{className:"jse-extraArea",children:g})]}),e,t]})}}const A=(0,b.g)(C)}}]);