mirror of
https://github.com/AykutSarac/jsoncrack.com.git
synced 2025-01-27 15:22:56 +08:00
refactor
This commit is contained in:
parent
73436b8913
commit
b1e7361aa4
@ -1,9 +0,0 @@
|
||||
import { altogic, AltogicResponse } from "src/api/altogic";
|
||||
|
||||
const getPartnerStatus = async (
|
||||
parentURL: string
|
||||
): Promise<AltogicResponse<{ premium: boolean }>> => {
|
||||
return await altogic.endpoint.get("/partner", {}, { parentURL });
|
||||
};
|
||||
|
||||
export { getPartnerStatus };
|
@ -1,11 +1,11 @@
|
||||
import { Graph } from "../json/jsonParser";
|
||||
|
||||
export const addEdgeToGraph = (graph: Graph, from: string, to: string) => {
|
||||
graph.edges = graph.edges.concat([
|
||||
{
|
||||
id: `e${from}-${to}`,
|
||||
from: from,
|
||||
to: to,
|
||||
},
|
||||
]);
|
||||
const newEdge = {
|
||||
id: `e${from}-${to}`,
|
||||
from: from,
|
||||
to: to,
|
||||
};
|
||||
|
||||
graph.edges.push(newEdge);
|
||||
};
|
||||
|
@ -9,7 +9,7 @@ type Props = {
|
||||
};
|
||||
|
||||
export const addNodeToGraph = ({ graph, text, type = "null", isEmpty = false }: Props) => {
|
||||
let id = String(graph.nodes.length + 1);
|
||||
const id = String(graph.nodes.length + 1);
|
||||
const isParent = type === "array" || type === "object";
|
||||
const { width, height } = calculateNodeSize(text, isParent);
|
||||
|
||||
@ -26,7 +26,7 @@ export const addNodeToGraph = ({ graph, text, type = "null", isEmpty = false }:
|
||||
},
|
||||
};
|
||||
|
||||
graph.nodes = graph.nodes.concat([node]);
|
||||
graph.nodes.push(node);
|
||||
|
||||
return id;
|
||||
};
|
||||
|
@ -17,7 +17,7 @@ export const isContentImage = (value: string | [string, string][]) => {
|
||||
|
||||
const sizeCache = new Map<string | [string, string][], { width: number; height: number }>();
|
||||
|
||||
function calculateWidthAndHeight(str: string, single = false) {
|
||||
const calculateWidthAndHeight = (str: string, single = false) => {
|
||||
if (!str) return { width: 45, height: 45 };
|
||||
|
||||
const dummyElement = document.createElement("div");
|
||||
@ -37,14 +37,14 @@ function calculateWidthAndHeight(str: string, single = false) {
|
||||
document.body.removeChild(dummyElement);
|
||||
|
||||
return { width, height };
|
||||
}
|
||||
};
|
||||
|
||||
export const calculateNodeSize = (text: string | [string, string][], isParent = false) => {
|
||||
let lines = "";
|
||||
const isFolded = useGraph.getState().foldNodes;
|
||||
const isImagePreview = useStored.getState().imagePreview;
|
||||
const isImage = isContentImage(text) && isImagePreview;
|
||||
const cacheKey = [text, isParent, isFolded].toString();
|
||||
const { foldNodes } = useGraph.getState();
|
||||
const { imagePreview } = useStored.getState();
|
||||
const isImage = isContentImage(text) && imagePreview;
|
||||
const cacheKey = [text, isParent, foldNodes].toString();
|
||||
|
||||
// check cache
|
||||
if (sizeCache.has(cacheKey)) {
|
||||
@ -60,8 +60,8 @@ export const calculateNodeSize = (text: string | [string, string][], isParent =
|
||||
|
||||
let sizes = calculateWidthAndHeight(lines, typeof text === "string");
|
||||
if (isImage) sizes = { width: 80, height: 80 };
|
||||
if (isFolded) sizes.width = 300;
|
||||
if (isParent && isFolded) sizes.width = 170;
|
||||
if (foldNodes) sizes.width = 300;
|
||||
if (isParent && foldNodes) sizes.width = 170;
|
||||
if (isParent) sizes.width += 100;
|
||||
if (sizes.width > 700) sizes.width = 700;
|
||||
|
||||
|
@ -1,15 +1,6 @@
|
||||
export function getNextDirection(direction: "LEFT" | "RIGHT" | "DOWN" | "UP") {
|
||||
switch (direction) {
|
||||
case "RIGHT":
|
||||
return "DOWN";
|
||||
|
||||
case "DOWN":
|
||||
return "LEFT";
|
||||
|
||||
case "LEFT":
|
||||
return "UP";
|
||||
|
||||
default:
|
||||
return "RIGHT";
|
||||
}
|
||||
if (direction === "RIGHT") return "DOWN";
|
||||
if (direction === "DOWN") return "LEFT";
|
||||
if (direction === "LEFT") return "UP";
|
||||
return "RIGHT";
|
||||
}
|
||||
|
@ -12,17 +12,17 @@ export const getOutgoers = (
|
||||
if (initialParentNode) outgoerNodes.push(initialParentNode);
|
||||
}
|
||||
|
||||
const runner = (nodeId: string) => {
|
||||
const outgoerIds = edges.filter(e => e.from === nodeId).map(e => e.to);
|
||||
const findOutgoers = (currentNodeId: string) => {
|
||||
const outgoerIds = edges.filter(e => e.from === currentNodeId).map(e => e.to);
|
||||
const nodeList = nodes.filter(n => {
|
||||
if (parent.includes(n.id) && !matchingNodes.includes(n.id)) matchingNodes.push(n.id);
|
||||
return outgoerIds.includes(n.id) && !parent.includes(n.id);
|
||||
});
|
||||
|
||||
outgoerNodes.push(...nodeList);
|
||||
nodeList.forEach(node => runner(node.id));
|
||||
nodeList.forEach(node => findOutgoers(node.id));
|
||||
};
|
||||
|
||||
runner(nodeId);
|
||||
findOutgoers(nodeId);
|
||||
return [outgoerNodes, matchingNodes];
|
||||
};
|
||||
|
@ -1,19 +1,21 @@
|
||||
export const searchQuery = (param: string) => {
|
||||
return document.querySelectorAll(`${param}`);
|
||||
return document.querySelectorAll(param);
|
||||
};
|
||||
|
||||
export const cleanupHighlight = () => {
|
||||
const nodes = document.querySelectorAll("foreignObject.searched, .highlight");
|
||||
|
||||
nodes?.forEach(node => {
|
||||
node.classList.remove("highlight");
|
||||
node.classList.remove("searched");
|
||||
nodes.forEach(node => {
|
||||
node.classList.remove("highlight", "searched");
|
||||
});
|
||||
};
|
||||
|
||||
export const highlightMatchedNodes = (nodes: NodeListOf<Element>, selectedNode: number) => {
|
||||
nodes?.forEach(node => {
|
||||
node.parentElement?.closest("foreignObject")?.classList.add("searched");
|
||||
nodes.forEach(node => {
|
||||
const foreignObject = node.parentElement?.closest("foreignObject");
|
||||
if (foreignObject) {
|
||||
foreignObject.classList.add("searched");
|
||||
}
|
||||
});
|
||||
|
||||
nodes[selectedNode].classList.add("highlight");
|
||||
|
@ -1,21 +0,0 @@
|
||||
export function getAllPaths(jsonObj: string) {
|
||||
if (!jsonObj) return [];
|
||||
|
||||
jsonObj = JSON.parse(jsonObj);
|
||||
let paths = [] as string[];
|
||||
|
||||
function traverse(obj: string | object, path: string[]) {
|
||||
if (typeof obj !== "object") {
|
||||
paths.push(path.join("."));
|
||||
return;
|
||||
}
|
||||
|
||||
for (let key in obj) {
|
||||
traverse(obj[key], path.concat(key));
|
||||
}
|
||||
}
|
||||
|
||||
traverse(jsonObj, []);
|
||||
|
||||
return paths;
|
||||
}
|
@ -1,14 +0,0 @@
|
||||
export function getObjectValues(obj: object) {
|
||||
if (!obj) return [];
|
||||
let values = [] as string[];
|
||||
|
||||
for (const key in obj) {
|
||||
if (typeof obj[key] === "object") {
|
||||
values = values.concat(getObjectValues(obj[key]));
|
||||
} else {
|
||||
values.push(obj[key]);
|
||||
}
|
||||
}
|
||||
|
||||
return [...new Set(values)];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user