format code
This commit is contained in:
1 parent
d32c457990
commit
8b9e5b963f
6 files changed
+61
-52
No files matched your search
+31
-28
@@ -11,6 +11,7 @@ import { message } from "@/misc/hooks";
|
|||||||
import i18n from "@/misc/i18n";
|
import i18n from "@/misc/i18n";
|
||||||
import { Hotkey, isHotkeyPressed, isMacos, useHotkeys } from "@/misc/keys";
|
import { Hotkey, isHotkeyPressed, isMacos, useHotkeys } from "@/misc/keys";
|
||||||
import Path from "@/misc/path";
|
import Path from "@/misc/path";
|
||||||
|
import { mergeClassNames } from "@/misc/util";
|
||||||
import { ArrowDownOutlined, ArrowUpOutlined, CloseOutlined } from "@ant-design/icons";
|
import { ArrowDownOutlined, ArrowUpOutlined, CloseOutlined } from "@ant-design/icons";
|
||||||
import G6, { G6GraphEvent, Item, TreeGraph } from "@antv/g6";
|
import G6, { G6GraphEvent, Item, TreeGraph } from "@antv/g6";
|
||||||
import { dialog } from "@electron/remote";
|
import { dialog } from "@electron/remote";
|
||||||
@@ -18,7 +19,7 @@ import { useSize } from "ahooks";
|
|||||||
import { Button, Dropdown, Flex, FlexProps, Input, InputRef, MenuProps } from "antd";
|
import { Button, Dropdown, Flex, FlexProps, Input, InputRef, MenuProps } from "antd";
|
||||||
import { clipboard } from "electron";
|
import { clipboard } from "electron";
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
import React, { FC, KeyboardEvent, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
import React, { FC, KeyboardEvent, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { FiDelete } from "react-icons/fi";
|
import { FiDelete } from "react-icons/fi";
|
||||||
import { IoMdReturnLeft } from "react-icons/io";
|
import { IoMdReturnLeft } from "react-icons/io";
|
||||||
@@ -27,7 +28,6 @@ import { VscCaseSensitive } from "react-icons/vsc";
|
|||||||
import { mergeRefs } from "react-merge-refs";
|
import { mergeRefs } from "react-merge-refs";
|
||||||
import { useDebounceCallback } from "usehooks-ts";
|
import { useDebounceCallback } from "usehooks-ts";
|
||||||
import "./register-node";
|
import "./register-node";
|
||||||
import { mergeClassNames } from "@/misc/util";
|
|
||||||
|
|
||||||
export interface EditorProps extends React.HTMLAttributes<HTMLElement> {
|
export interface EditorProps extends React.HTMLAttributes<HTMLElement> {
|
||||||
data: EditorStore;
|
data: EditorStore;
|
||||||
@@ -148,7 +148,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
filterCase: false,
|
filterCase: false,
|
||||||
filterFocus: false,
|
filterFocus: false,
|
||||||
filterType: "",
|
filterType: "",
|
||||||
placeholder: ""
|
placeholder: "",
|
||||||
});
|
});
|
||||||
|
|
||||||
const onSearchChange = (option: FilterOption) => {
|
const onSearchChange = (option: FilterOption) => {
|
||||||
@@ -238,10 +238,13 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
let found = false;
|
let found = false;
|
||||||
if (option.filterType === "nodeId") {
|
if (option.filterType === "nodeId") {
|
||||||
if (option.filterStr === node.id) {
|
if (option.filterStr === node.id) {
|
||||||
found = true
|
found = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (includeString(node.name, option) || includeString(node.desc || node.def.desc, option)) {
|
if (
|
||||||
|
includeString(node.name, option) ||
|
||||||
|
includeString(node.desc || node.def.desc, option)
|
||||||
|
) {
|
||||||
found = true;
|
found = true;
|
||||||
} else if (node.input) {
|
} else if (node.input) {
|
||||||
for (const str of node.input) {
|
for (const str of node.input) {
|
||||||
@@ -1091,40 +1094,40 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
};
|
};
|
||||||
|
|
||||||
const searchByType = (type: string) => {
|
const searchByType = (type: string) => {
|
||||||
let placeholder = ""
|
let placeholder = "";
|
||||||
const filterType = type
|
const filterType = type;
|
||||||
// todo multiple parameter format judgment
|
// todo multiple parameter format judgment
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "nodeId":
|
case "nodeId":
|
||||||
placeholder = t("jumpNode")
|
placeholder = t("jumpNode");
|
||||||
break
|
break;
|
||||||
default:
|
default:
|
||||||
placeholder = t("searchNode")
|
placeholder = t("searchNode");
|
||||||
break
|
break;
|
||||||
}
|
}
|
||||||
if (!showingSearch) {
|
if (!showingSearch) {
|
||||||
setFilterOption({ ...filterOption, placeholder, filterType })
|
setFilterOption({ ...filterOption, placeholder, filterType });
|
||||||
setShowingSearch(true)
|
setShowingSearch(true);
|
||||||
return
|
return;
|
||||||
}
|
}
|
||||||
if (filterOption.filterType === type) return searchIptRef.current?.focus()
|
if (filterOption.filterType === type) return searchIptRef.current?.focus();
|
||||||
setShowingSearch(false)
|
setShowingSearch(false);
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
setShowingSearch(true)
|
setShowingSearch(true);
|
||||||
setFilterOption({ ...filterOption, placeholder, filterType })
|
setFilterOption({ ...filterOption, placeholder, filterType });
|
||||||
searchIptRef.current?.focus()
|
searchIptRef.current?.focus();
|
||||||
}, 50)
|
}, 50);
|
||||||
}
|
};
|
||||||
|
|
||||||
const handleKeyDown = (e: KeyboardEvent) => {
|
const handleKeyDown = (e: KeyboardEvent) => {
|
||||||
if (e.code === Hotkey.Enter) {
|
if (e.code === Hotkey.Enter) {
|
||||||
nextResult()
|
nextResult();
|
||||||
} else if ((e.ctrlKey || e.metaKey) && e.code === "KeyF") {
|
} else if ((e.ctrlKey || e.metaKey) && e.code === "KeyF") {
|
||||||
searchByType("")
|
searchByType("");
|
||||||
} else if ((e.ctrlKey || e.metaKey) && e.code === "KeyG") {
|
} else if ((e.ctrlKey || e.metaKey) && e.code === "KeyG") {
|
||||||
searchByType("nodeId")
|
searchByType("nodeId");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
@@ -1156,7 +1159,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
>
|
>
|
||||||
<Input
|
<Input
|
||||||
ref={searchIptRef}
|
ref={searchIptRef}
|
||||||
placeholder={ filterOption.placeholder }
|
placeholder={filterOption.placeholder}
|
||||||
autoFocus
|
autoFocus
|
||||||
size="small"
|
size="small"
|
||||||
style={{
|
style={{
|
||||||
@@ -1172,7 +1175,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
index: 0,
|
index: 0,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
onKeyDown={ handleKeyDown }
|
onKeyDown={handleKeyDown}
|
||||||
suffix={
|
suffix={
|
||||||
<Flex gap="2px" style={{ alignItems: "center" }}>
|
<Flex gap="2px" style={{ alignItems: "center" }}>
|
||||||
{filterOption.filterType !== "nodeId" && (
|
{filterOption.filterType !== "nodeId" && (
|
||||||
@@ -1249,7 +1252,7 @@ export const Editor: FC<EditorProps> = ({ onUpdate: updateState, data: editor, .
|
|||||||
filterFocus: false,
|
filterFocus: false,
|
||||||
filterStr: "",
|
filterStr: "",
|
||||||
filterType: "",
|
filterType: "",
|
||||||
placeholder: ""
|
placeholder: "",
|
||||||
});
|
});
|
||||||
keysRef.current?.focus();
|
keysRef.current?.focus();
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { EditNode, EditTree, FileTreeType, useWorkspace } from "@/contexts/workspace-context";
|
import { EditNode, EditTree, useWorkspace } from "@/contexts/workspace-context";
|
||||||
import { NodeArgType, NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type";
|
import { NodeArgType, NodeModel, TreeGraphData, TreeModel } from "@/misc/b3type";
|
||||||
import { Hotkey, isMacos } from "@/misc/keys";
|
import { Hotkey, isMacos } from "@/misc/keys";
|
||||||
import Path from "@/misc/path";
|
import Path from "@/misc/path";
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import { app } from "@electron/remote";
|
|||||||
import { Button, Flex, Layout, Space, Tabs, Tag, Tooltip } from "antd";
|
import { Button, Flex, Layout, Space, Tabs, Tag, Tooltip } from "antd";
|
||||||
import { FC, useEffect, useState } from "react";
|
import { FC, useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { FaExclamationTriangle, FaRegFolderOpen } from "react-icons/fa";
|
import { FaExclamationTriangle } from "react-icons/fa";
|
||||||
import { PiTreeStructureFill } from "react-icons/pi";
|
import { PiTreeStructureFill } from "react-icons/pi";
|
||||||
import { VscNewFolder, VscRepo } from "react-icons/vsc";
|
import { VscNewFolder, VscRepo } from "react-icons/vsc";
|
||||||
import useForceUpdate from "use-force-update";
|
import useForceUpdate from "use-force-update";
|
||||||
@@ -44,7 +44,14 @@ export const Workspace: FC = () => {
|
|||||||
const { width = 0, height = 0 } = useWindowSize();
|
const { width = 0, height = 0 } = useWindowSize();
|
||||||
|
|
||||||
const keysRef = useHotkeys<HTMLDivElement>(
|
const keysRef = useHotkeys<HTMLDivElement>(
|
||||||
[Hotkey.Save, Hotkey.CloseEditor, Hotkey.SearchTree, Hotkey.Build, Hotkey.SearchNode, Hotkey.JumpNode],
|
[
|
||||||
|
Hotkey.Save,
|
||||||
|
Hotkey.CloseEditor,
|
||||||
|
Hotkey.SearchTree,
|
||||||
|
Hotkey.Build,
|
||||||
|
Hotkey.SearchNode,
|
||||||
|
Hotkey.JumpNode,
|
||||||
|
],
|
||||||
(event) => {
|
(event) => {
|
||||||
if (isHotkeyPressed(Hotkey.Save)) {
|
if (isHotkeyPressed(Hotkey.Save)) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -68,8 +75,8 @@ export const Workspace: FC = () => {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
workspace.editing?.dispatch("searchNode");
|
workspace.editing?.dispatch("searchNode");
|
||||||
} else if (isHotkeyPressed(Hotkey.JumpNode)) {
|
} else if (isHotkeyPressed(Hotkey.JumpNode)) {
|
||||||
event.preventDefault()
|
event.preventDefault();
|
||||||
workspace.editing?.dispatch("jumpNode")
|
workspace.editing?.dispatch("jumpNode");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { readJson, writeJson } from "@/misc/util";
|
import { readJson, writeJson } from "@/misc/util";
|
||||||
import { app } from "@electron/remote";
|
import { app } from "@electron/remote";
|
||||||
import { create } from "zustand";
|
|
||||||
import * as fs from "fs";
|
import * as fs from "fs";
|
||||||
|
import { create } from "zustand";
|
||||||
|
|
||||||
const settingPath = app.getPath("userData") + "/settings.json";
|
const settingPath = app.getPath("userData") + "/settings.json";
|
||||||
|
|
||||||
|
|||||||
@@ -145,13 +145,13 @@ export type WorkspaceStore = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const loadFileTree = (workdir: string, filename: string) => {
|
const loadFileTree = (workdir: string, filename: string) => {
|
||||||
const fullpath = fs.realpathSync(`${ workdir }/${ filename }`);
|
const fullpath = fs.realpathSync(`${workdir}/${filename}`);
|
||||||
if (!fs.existsSync(fullpath)) {
|
if (!fs.existsSync(fullpath)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stat = fs.statSync(fullpath);
|
const stat = fs.statSync(fullpath);
|
||||||
if (!( stat.isDirectory() || fullpath.endsWith(".json") )) {
|
if (!(stat.isDirectory() || fullpath.endsWith(".json"))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,13 +164,13 @@ const loadFileTree = (workdir: string, filename: string) => {
|
|||||||
data.children = [];
|
data.children = [];
|
||||||
const files = fs.readdirSync(data.path);
|
const files = fs.readdirSync(data.path);
|
||||||
files.forEach((v) => {
|
files.forEach((v) => {
|
||||||
const child = loadFileTree(workdir, `${ filename }/${ v }`);
|
const child = loadFileTree(workdir, `${filename}/${v}`);
|
||||||
if (child) {
|
if (child) {
|
||||||
data.children?.push(child);
|
data.children?.push(child);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
data.children.sort((a, b) => {
|
data.children.sort((a, b) => {
|
||||||
if (( a.children && b.children ) || ( !a.children && !b.children )) {
|
if ((a.children && b.children) || (!a.children && !b.children)) {
|
||||||
return a.title.localeCompare(b.title);
|
return a.title.localeCompare(b.title);
|
||||||
} else {
|
} else {
|
||||||
return a.children ? -1 : 1;
|
return a.children ? -1 : 1;
|
||||||
@@ -188,7 +188,7 @@ const saveFile = (editor?: EditorStore) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
export const useWorkspace = create<WorkspaceStore>((set, get) => ({
|
||||||
allFiles: [],
|
allFiles: [],
|
||||||
fileTree: undefined,
|
fileTree: undefined,
|
||||||
editors: [],
|
editors: [],
|
||||||
@@ -210,15 +210,15 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
if (!fs.existsSync(path)) {
|
if (!fs.existsSync(path)) {
|
||||||
useSetting.getState().removeRecent(path);
|
useSetting.getState().removeRecent(path);
|
||||||
}
|
}
|
||||||
message.error(`load workspace error: ${ path }`);
|
message.error(`load workspace error: ${path}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
createProject: () => {
|
createProject: () => {
|
||||||
const path = dialog.showSaveDialogSync({
|
const path = dialog.showSaveDialogSync({
|
||||||
properties: [ "showOverwriteConfirmation", "createDirectory" ],
|
properties: ["showOverwriteConfirmation", "createDirectory"],
|
||||||
filters: [ { name: "Behavior3 Workspace", extensions: [ "b3-workspace" ] } ],
|
filters: [{ name: "Behavior3 Workspace", extensions: ["b3-workspace"] }],
|
||||||
});
|
});
|
||||||
if (path) {
|
if (path) {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
@@ -234,7 +234,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
const win = BrowserWindow.getFocusedWindow();
|
const win = BrowserWindow.getFocusedWindow();
|
||||||
if (win) {
|
if (win) {
|
||||||
const path = dialog.showOpenDialogSync(win, {
|
const path = dialog.showOpenDialogSync(win, {
|
||||||
filters: [ { name: "workspace", extensions: [ "b3-workspace" ] } ],
|
filters: [{ name: "workspace", extensions: ["b3-workspace"] }],
|
||||||
});
|
});
|
||||||
if (path?.length) {
|
if (path?.length) {
|
||||||
ipcRenderer.invoke("open-win", path[0]);
|
ipcRenderer.invoke("open-win", path[0]);
|
||||||
@@ -248,7 +248,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
if (workspace.path)
|
if (workspace.path)
|
||||||
if (!buildDir) {
|
if (!buildDir) {
|
||||||
buildDir = dialog.showOpenDialogSync({
|
buildDir = dialog.showOpenDialogSync({
|
||||||
properties: [ "openDirectory", "createDirectory" ],
|
properties: ["openDirectory", "createDirectory"],
|
||||||
})?.[0];
|
})?.[0];
|
||||||
}
|
}
|
||||||
if (buildDir) {
|
if (buildDir) {
|
||||||
@@ -281,9 +281,9 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
batchProject: () => {
|
batchProject: () => {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
const scriptPath = dialog.showOpenDialogSync({
|
const scriptPath = dialog.showOpenDialogSync({
|
||||||
properties: [ "openFile" ],
|
properties: ["openFile"],
|
||||||
defaultPath: workspace.workdir,
|
defaultPath: workspace.workdir,
|
||||||
filters: [ { name: "Javascript", extensions: [ "js" ] } ],
|
filters: [{ name: "Javascript", extensions: ["js"] }],
|
||||||
})?.[0];
|
})?.[0];
|
||||||
if (scriptPath) {
|
if (scriptPath) {
|
||||||
try {
|
try {
|
||||||
@@ -329,7 +329,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
workspace.edit(editor.path);
|
workspace.edit(editor.path);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
message.error(`invalid file: ${ path }`);
|
message.error(`invalid file: ${path}`);
|
||||||
}
|
}
|
||||||
} else if (workspace.editing !== editor) {
|
} else if (workspace.editing !== editor) {
|
||||||
workspace.edit(editor.path);
|
workspace.edit(editor.path);
|
||||||
@@ -389,8 +389,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
saveFile(workspace.editing);
|
saveFile(workspace.editing);
|
||||||
},
|
},
|
||||||
|
|
||||||
saveAs: () => {
|
saveAs: () => {},
|
||||||
},
|
|
||||||
|
|
||||||
saveAll: () => {
|
saveAll: () => {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
@@ -445,7 +444,7 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
|
|
||||||
loadNodeDefs: () => {
|
loadNodeDefs: () => {
|
||||||
const workspace = get();
|
const workspace = get();
|
||||||
const nodeDefData = readJson(`${ workspace.workdir }/node-config.b3-setting`) as NodeDef[];
|
const nodeDefData = readJson(`${workspace.workdir}/node-config.b3-setting`) as NodeDef[];
|
||||||
const nodeDefs: Map<string, NodeDef> = new Map();
|
const nodeDefs: Map<string, NodeDef> = new Map();
|
||||||
for (const v of nodeDefData) {
|
for (const v of nodeDefData) {
|
||||||
nodeDefs.set(v.name, v);
|
nodeDefs.set(v.name, v);
|
||||||
@@ -479,4 +478,4 @@ export const useWorkspace = create<WorkspaceStore>((set, get) => ( {
|
|||||||
const workspace = get();
|
const workspace = get();
|
||||||
return workspace.nodeDefs.has(name);
|
return workspace.nodeDefs.has(name);
|
||||||
},
|
},
|
||||||
} ));
|
}));
|
||||||
+1
-1
@@ -33,7 +33,7 @@ export function useHotkeys<T extends HTMLElement>(
|
|||||||
keys: Keys,
|
keys: Keys,
|
||||||
callback: HotkeyCallback,
|
callback: HotkeyCallback,
|
||||||
options?: OptionsOrDependencyArray,
|
options?: OptionsOrDependencyArray,
|
||||||
dependencies?: OptionsOrDependencyArray,
|
dependencies?: OptionsOrDependencyArray
|
||||||
): MutableRefObject<RefType<T>> {
|
): MutableRefObject<RefType<T>> {
|
||||||
if (keys instanceof Array) {
|
if (keys instanceof Array) {
|
||||||
keys = keys.filter((v) => !!v);
|
keys = keys.filter((v) => !!v);
|
||||||
|
|||||||
Reference in new issue
Block a user