From 61eb907c96a43a9d0cc60e514b5ea71627276161 Mon Sep 17 00:00:00 2001 From: yuanyxh <15766118362@139.com> Date: Thu, 15 Aug 2024 23:15:48 +0800 Subject: [PATCH] feat: remove js2ts --- src/tools/json2typescript/Index.module.less | 25 --- src/tools/json2typescript/Index.tsx | 67 -------- src/tools/json2typescript/JSON2TypeScript.ts | 155 ------------------- src/viewer/Tools.tsx | 4 +- 4 files changed, 3 insertions(+), 248 deletions(-) delete mode 100644 src/tools/json2typescript/Index.module.less delete mode 100644 src/tools/json2typescript/Index.tsx delete mode 100644 src/tools/json2typescript/JSON2TypeScript.ts diff --git a/src/tools/json2typescript/Index.module.less b/src/tools/json2typescript/Index.module.less deleted file mode 100644 index 34dd678..0000000 --- a/src/tools/json2typescript/Index.module.less +++ /dev/null @@ -1,25 +0,0 @@ -.container { - height: 100%; - display: flex; - justify-content: space-between; - align-items: center; -} - -.json, -.typescript { - flex: 1; - height: 100%; -} - -.json { - border-inline: 1px solid #d9d9d9; -} - -.toolbar { - margin-bottom: 12px; - padding-left: 20px; - height: 48px; - line-height: 48px; - font-size: 18px; - border-bottom: 1px solid #d9d9d9; -} diff --git a/src/tools/json2typescript/Index.tsx b/src/tools/json2typescript/Index.tsx deleted file mode 100644 index 8c1540b..0000000 --- a/src/tools/json2typescript/Index.tsx +++ /dev/null @@ -1,67 +0,0 @@ -import { useEffect, useState } from 'react'; - -import style from './Index.module.less'; -import { json2ts } from './JSON2TypeScript'; - -import Editor from '@monaco-editor/react'; - -const JSON2TypeScript: React.FC = () => { - const [typescript, setTypeScript] = useState(''); - - useEffect(() => { - handleChange(`{ - "userId": 1, - "id": 1, - "title": "delectus aut autem", - "completed": false -}`); - }, []); - - const handleChange = (e: string | undefined) => { - try { - const obj = JSON.parse(e || ''); - - const ts = json2ts(obj); - - // console.log(ts); - - if (typeof ts === 'string') { - setTypeScript(ts); - } - } catch (err) { - // console.log(err); - } - }; - - return ( -
-
-
JSON
- -
- -
-
TypeScript
- -
-
- ); -}; - -export default JSON2TypeScript; diff --git a/src/tools/json2typescript/JSON2TypeScript.ts b/src/tools/json2typescript/JSON2TypeScript.ts deleted file mode 100644 index 16e227d..0000000 --- a/src/tools/json2typescript/JSON2TypeScript.ts +++ /dev/null @@ -1,155 +0,0 @@ -import { isArray, isNull, isObject } from 'lodash-es'; - -type BaseType = 'string' | 'number' | 'boolean' | 'null' | 'any' | JointType; - -type JointType = Record; - -type Type = { - type: BaseType; - required: boolean; - count?: number; -}; - -const createStringType = (): Type => { - return { type: 'string', required: true }; -}; - -const createNumberType = (): Type => { - return { type: 'number', required: true }; -}; - -const createBooleanType = (): Type => { - return { type: 'boolean', required: true }; -}; - -const createNullType = (): Type => { - return { type: 'null', required: true }; -}; - -/** - * - * @param obj 需要检索类型的对象 - * @param sibling 可选的同级类型 - * @returns 返回对象类型 - */ -function createObjectType(obj: Record, sibling?: BaseType): Type { - // 如果有同级类型,且同级类型不是对象,则返回 any - if (sibling && (typeof sibling === 'string' || isArray(sibling))) { - return { type: 'any', required: true }; - } - - const names = Object.getOwnPropertyNames(obj); - const oldNames = sibling ? Object.getOwnPropertyNames(sibling) : []; - - const typeObj: JointType = sibling || {}; - - const merge = (t: Type, name: string) => { - // 没有同级类型,不做特殊处理 - if (!sibling) { - return (typeObj[name] = t); - } - - // 存在同级类型,但没有相同属性,标记为可选 - const oldType = typeObj[name]; - if (!oldType) { - t.required = false; - return (typeObj[name] = t); - } - - // 类型不一致,标记为 any - if (oldType.type !== t.type) { - oldType.type = 'any'; - } - }; - - for (let i = 0; i < names.length; i++) { - const name = names[i]; - - const value = obj[name]; - const type = typeof value; - - switch (type) { - case 'string': - merge(createStringType(), name); - break; - case 'number': - merge(createNumberType(), name); - break; - case 'boolean': - merge(createBooleanType(), name); - break; - default: - if (isNull(value)) { - merge(createNullType(), name); - } else if (isArray(value)) { - merge(createArrayType(value), name); - } else if (isObject(value)) { - // 如果存在同级元素,将它向下传递 - merge(createObjectType(value, typeObj[name]?.type), name); - } - } - } - - // obj 中如果没有 sibling 的属性,需要标记为可选 - oldNames - .filter((o) => !names.includes(o)) - .forEach((o) => { - typeObj[o].required = false; - }); - - return { type: typeObj, required: true }; -} - -function createArrayType(array: any[]): Type { - let type!: Type; - - for (let i = 0; i < array.length; i++) { - const ele = array[i]; - - const eleType = typeof ele; - - switch (eleType) { - case 'string': - createStringType(); - break; - case 'number': - createNumberType(); - break; - case 'boolean': - createBooleanType(); - break; - default: - if (isNull(ele)) { - createNullType(); - } else if (isArray(ele)) { - createArrayType(ele); - } else if (isObject(ele)) { - createObjectType(ele); - } - } - } - - return type; -} - -export function transform(json: Record | any[]) { - if (isArray(json)) { - return createArrayType(json); - } else if (isObject(json)) { - return createObjectType(json); - } - - throw new Error('Invail type of the json.'); -} - -export const getTag = (count: number) => { - return '[]'.repeat(count); -}; - -export function json2ts(json: Record | any[]) { - const tree = transform(json); - - console.log(tree); - - return ''; -} diff --git a/src/viewer/Tools.tsx b/src/viewer/Tools.tsx index a03919f..e5b11d3 100644 --- a/src/viewer/Tools.tsx +++ b/src/viewer/Tools.tsx @@ -2,7 +2,7 @@ import type { MenuProps } from 'antd'; import { Layout, Menu } from 'antd'; import type { ResolveRouteObject } from '@/router'; -import { Outlet, useHistory } from '@/router'; +import { Outlet, useHistory, useLocation } from '@/router'; import { useTools } from './hooks/useTools'; @@ -27,12 +27,14 @@ const Tools: React.FC = () => { const tools = useTools(); const router = useHistory(); + const location = useLocation(); return (
{ router.push(e.key);