mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-26 17:43:48 +01:00
Linting ScriptEditor
This commit is contained in:
parent
f72d4b89d4
commit
08a4a1649f
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
||||||
import React, { useState, useEffect, useRef, useMemo } from "react";
|
import React, { useState, useEffect, useRef, useMemo } from "react";
|
||||||
import Editor, { Monaco } from "@monaco-editor/react";
|
import Editor, { Monaco } from "@monaco-editor/react";
|
||||||
import * as monaco from "monaco-editor";
|
import * as monaco from "monaco-editor";
|
||||||
@ -254,7 +255,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
|
|
||||||
|
|
||||||
// When the editor is mounted
|
// When the editor is mounted
|
||||||
function onMount(editor: IStandaloneCodeEditor, monaco: Monaco) {
|
function onMount(editor: IStandaloneCodeEditor, monaco: Monaco): void {
|
||||||
editorRef.current = editor;
|
editorRef.current = editor;
|
||||||
monacoRef.current = monaco;
|
monacoRef.current = monaco;
|
||||||
|
|
||||||
@ -262,7 +263,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
|
|
||||||
if (props.filename) {
|
if (props.filename) {
|
||||||
// Check if file is already opened
|
// Check if file is already opened
|
||||||
let openScriptIndex = openScripts.findIndex(script => script.fileName === props.filename && script.hostname === props.hostname);
|
const openScriptIndex = openScripts.findIndex(script => script.fileName === props.filename && script.hostname === props.hostname);
|
||||||
if (openScriptIndex !== -1) {
|
if (openScriptIndex !== -1) {
|
||||||
// Script is already opened
|
// Script is already opened
|
||||||
if (openScripts[openScriptIndex].model === undefined || openScripts[openScriptIndex].model === null || openScripts[openScriptIndex].model.isDisposed()) {
|
if (openScripts[openScriptIndex].model === undefined || openScripts[openScriptIndex].model === null || openScripts[openScriptIndex].model.isDisposed()) {
|
||||||
@ -276,7 +277,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
updateRAM(openScripts[openScriptIndex].code);
|
updateRAM(openScripts[openScriptIndex].code);
|
||||||
} else {
|
} else {
|
||||||
// Open script
|
// Open script
|
||||||
var newScript = new OpenScript(props.filename, props.code, props.hostname, new monacoRef.current.Position(0, 0), monacoRef.current.editor.createModel(props.code, 'javascript'));
|
const newScript = new OpenScript(props.filename, props.code, props.hostname, new monacoRef.current.Position(0, 0), monacoRef.current.editor.createModel(props.code, 'javascript'));
|
||||||
setOpenScripts(oldArray => [...oldArray, newScript]);
|
setOpenScripts(oldArray => [...oldArray, newScript]);
|
||||||
setCurrentScript({ ...newScript });
|
setCurrentScript({ ...newScript });
|
||||||
editorRef.current.setModel(newScript.model);
|
editorRef.current.setModel(newScript.model);
|
||||||
@ -322,17 +323,17 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// When the code is updated within the editor
|
// When the code is updated within the editor
|
||||||
function updateCode(newCode?: string) {
|
function updateCode(newCode?: string): void {
|
||||||
if (newCode === undefined) return;
|
if (newCode === undefined) return;
|
||||||
updateRAM(newCode);
|
updateRAM(newCode);
|
||||||
if (editorRef.current !== null) {
|
if (editorRef.current !== null) {
|
||||||
var newPos = editorRef.current.getPosition();
|
const newPos = editorRef.current.getPosition();
|
||||||
if (newPos === null) return;
|
if (newPos === null) return;
|
||||||
setCurrentScript(oldScript => ({ ...oldScript!, code: newCode, lastPosition: newPos! }))
|
setCurrentScript(oldScript => ({ ...oldScript!, code: newCode, lastPosition: newPos! }))
|
||||||
if (currentScript !== null) {
|
if (currentScript !== null) {
|
||||||
let curIndex = openScripts.findIndex(script => script.fileName === currentScript.fileName && script.hostname === currentScript.hostname);
|
const curIndex = openScripts.findIndex(script => script.fileName === currentScript.fileName && script.hostname === currentScript.hostname);
|
||||||
let newArr = [...openScripts];
|
const newArr = [...openScripts];
|
||||||
let tempScript = currentScript;
|
const tempScript = currentScript;
|
||||||
tempScript.code = newCode;
|
tempScript.code = newCode;
|
||||||
newArr[curIndex] = tempScript;
|
newArr[curIndex] = tempScript;
|
||||||
setOpenScripts([...newArr]);
|
setOpenScripts([...newArr]);
|
||||||
@ -466,7 +467,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
props.router.toTerminal();
|
props.router.toTerminal();
|
||||||
}
|
}
|
||||||
|
|
||||||
function reorder(list: Array<OpenScript>, startIndex: number, endIndex: number) {
|
function reorder(list: Array<OpenScript>, startIndex: number, endIndex: number): OpenScript[] {
|
||||||
const result = Array.from(list);
|
const result = Array.from(list);
|
||||||
const [removed] = result.splice(startIndex, 1);
|
const [removed] = result.splice(startIndex, 1);
|
||||||
result.splice(endIndex, 0, removed);
|
result.splice(endIndex, 0, removed);
|
||||||
@ -474,7 +475,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function onDragEnd(result: any) {
|
function onDragEnd(result: any): void{
|
||||||
// Dropped outside of the list
|
// Dropped outside of the list
|
||||||
if (!result.destination) {
|
if (!result.destination) {
|
||||||
result
|
result
|
||||||
@ -486,10 +487,10 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
setOpenScripts(items);
|
setOpenScripts(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
function onTabClick(index: number) {
|
function onTabClick(index: number): void {
|
||||||
if (currentScript !== null) {
|
if (currentScript !== null) {
|
||||||
// Save currentScript to openScripts
|
// Save currentScript to openScripts
|
||||||
let curIndex = openScripts.findIndex(script => script.fileName === currentScript.fileName && script.hostname === currentScript.hostname);
|
const curIndex = openScripts.findIndex(script => script.fileName === currentScript.fileName && script.hostname === currentScript.hostname);
|
||||||
openScripts[curIndex] = currentScript;
|
openScripts[curIndex] = currentScript;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -507,17 +508,17 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onTabClose(index: number) {
|
async function onTabClose(index: number): Promise<void> {
|
||||||
// See if the script on the server is up to date
|
// See if the script on the server is up to date
|
||||||
let closingScript = openScripts[index];
|
const closingScript = openScripts[index];
|
||||||
let savedOpenScripts: Array<OpenScript> = JSON.parse(window.localStorage.getItem('scriptEditorOpenScripts')!);
|
const savedOpenScripts: Array<OpenScript> = JSON.parse(window.localStorage.getItem('scriptEditorOpenScripts')!);
|
||||||
let savedScriptIndex = savedOpenScripts.findIndex(script => script.fileName === closingScript.fileName && script.hostname === closingScript.hostname);
|
const savedScriptIndex = savedOpenScripts.findIndex(script => script.fileName === closingScript.fileName && script.hostname === closingScript.hostname);
|
||||||
let savedScriptCode = '';
|
let savedScriptCode = '';
|
||||||
if (savedScriptIndex !== -1) {
|
if (savedScriptIndex !== -1) {
|
||||||
savedScriptCode = savedOpenScripts[savedScriptIndex].code;
|
savedScriptCode = savedOpenScripts[savedScriptIndex].code;
|
||||||
}
|
}
|
||||||
|
|
||||||
let serverScriptIndex = GetServer(closingScript.hostname)?.scripts.findIndex(script => script.filename === closingScript.fileName);
|
const serverScriptIndex = GetServer(closingScript.hostname)?.scripts.findIndex(script => script.filename === closingScript.fileName);
|
||||||
if (serverScriptIndex === -1 || savedScriptCode !== GetServer(closingScript.hostname)?.scripts[serverScriptIndex as number].code) {
|
if (serverScriptIndex === -1 || savedScriptCode !== GetServer(closingScript.hostname)?.scripts[serverScriptIndex as number].code) {
|
||||||
PromptEvent.emit({
|
PromptEvent.emit({
|
||||||
txt: 'Do you want to save changes to ' + closingScript.fileName + '?',
|
txt: 'Do you want to save changes to ' + closingScript.fileName + '?',
|
||||||
@ -578,7 +579,7 @@ export function Root(props: IProps): React.ReactElement {
|
|||||||
>
|
>
|
||||||
{openScripts.map(({ fileName, hostname }, index) => (
|
{openScripts.map(({ fileName, hostname }, index) => (
|
||||||
<Draggable key={fileName + hostname} draggableId={fileName + hostname} index={index} disableInteractiveElementBlocking={true}>
|
<Draggable key={fileName + hostname} draggableId={fileName + hostname} index={index} disableInteractiveElementBlocking={true}>
|
||||||
{(provided, snapshot) => (
|
{(provided) => (
|
||||||
<div
|
<div
|
||||||
ref={provided.innerRef}
|
ref={provided.innerRef}
|
||||||
{...provided.draggableProps}
|
{...provided.draggableProps}
|
||||||
|
Loading…
Reference in New Issue
Block a user