generated from zxch3n/ts-rust-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: align partial layout with full layout
- Loading branch information
Showing
9 changed files
with
205 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { useCallback, useEffect, useRef, useState } from 'react'; | ||
import { Renderer } from '../renderer'; | ||
import { Node } from '../tidy'; | ||
import { LayoutTypeStr } from '../TidyComponent'; | ||
import { createNode, createTree, visit } from '../utils'; | ||
import { debugStrToTree } from './debugToTree'; | ||
|
||
export default { | ||
title: 'DebugTree', | ||
component: DebugTree, | ||
}; | ||
|
||
interface Props { | ||
input: string; | ||
} | ||
|
||
function DebugTree({ input }: Props) { | ||
const root = debugStrToTree(input); | ||
const renderRef = useRef<Renderer>(); | ||
const containerRef = useRef<HTMLDivElement>(null); | ||
useEffect(() => { | ||
const func = async () => { | ||
renderRef.current = new Renderer(containerRef.current!); | ||
renderRef.current.init(root); | ||
}; | ||
|
||
func(); | ||
return () => { | ||
renderRef.current?.dispose(); | ||
}; | ||
}, []); | ||
|
||
return <div ref={containerRef} style={{ width: '100%', minHeight: 500 }} />; | ||
} | ||
|
||
/** | ||
* Primary UI component for user interaction | ||
*/ | ||
export const Debug = () => { | ||
const partial = `x: 0, y: 0, width: 6, height: 34, rx: 78.6875, mod: -78.6875 | ||
x: -78.6875, y: 44, width: 29, height: 11, rx: 21.25, mod: -21.25 | ||
x: -99.9375, y: 65, width: 24, height: 34, rx: 30, mod: -30 | ||
x: -129.9375, y: 109, width: 6, height: 20, rx: 0, mod: 0 | ||
x: -129.9375, y: 139, width: 22, height: 48, rx: 0, mod: 0 | ||
x: -102.9375, y: 109, width: 12, height: 49, rx: 0, mod: 27 | ||
x: -69.9375, y: 109, width: 34, height: 27, rx: 0, mod: 60 | ||
x: -57.4375, y: 65, width: 41, height: 26, rx: 0, mod: 42.5 | ||
x: 25.40625, y: 44, width: 16, height: 15, rx: 0, mod: 104.09375 | ||
x: 78.6875, y: 44, width: 41, height: 37, rx: 61.875, mod: 95.5 | ||
x: 16.8125, y: 91, width: 45, height: 23, rx: 45.75, mod: -45.75 | ||
x: -28.9375, y: 124, width: 28, height: 39, rx: 0, mod: 0 | ||
x: -28.9375, y: 173, width: 19, height: 14, rx: 0, mod: 0 | ||
x: 19.5625, y: 124, width: 49, height: 42, rx: 0, mod: 48.5 | ||
x: 62.5625, y: 124, width: 17, height: 30, rx: 0, mod: 91.5 | ||
x: 96.5625, y: 91, width: 25, height: 10, rx: 0, mod: 79.75 | ||
x: 96.5625, y: 111, width: 31, height: 15, rx: 0, mod: 0 | ||
x: 140.5625, y: 91, width: 37, height: 15, rx: 0, mod: 123.75`; | ||
const full = `x: 0, y: 0, width: 6, height: 34, rx: 78.6875, mod: -78.6875 | ||
x: -78.6875, y: 44, width: 29, height: 11, rx: 21.25, mod: -21.25 | ||
x: -99.9375, y: 65, width: 24, height: 34, rx: 30, mod: -30 | ||
x: -129.9375, y: 109, width: 6, height: 20, rx: 0, mod: 0 | ||
x: -129.9375, y: 139, width: 22, height: 48, rx: 0, mod: 0 | ||
x: -102.9375, y: 109, width: 12, height: 49, rx: 0, mod: 27 | ||
x: -69.9375, y: 109, width: 34, height: 27, rx: 0, mod: 60 | ||
x: -57.4375, y: 65, width: 41, height: 26, rx: 0, mod: 42.5 | ||
x: 10.625, y: 44, width: 16, height: 15, rx: 0, mod: 89.3125 | ||
x: 78.6875, y: 44, width: 41, height: 37, rx: 61.875, mod: 95.5 | ||
x: 16.8125, y: 91, width: 45, height: 23, rx: 45.75, mod: -45.75 | ||
x: -28.9375, y: 124, width: 28, height: 39, rx: 0, mod: 0 | ||
x: -28.9375, y: 173, width: 19, height: 14, rx: 0, mod: 0 | ||
x: 19.5625, y: 124, width: 49, height: 42, rx: 0, mod: 48.5 | ||
x: 62.5625, y: 124, width: 17, height: 30, rx: 0, mod: 91.5 | ||
x: 96.5625, y: 91, width: 25, height: 10, rx: 0, mod: 79.75 | ||
x: 96.5625, y: 111, width: 31, height: 15, rx: 0, mod: 0 | ||
x: 140.5625, y: 91, width: 37, height: 15, rx: 0, mod: 123.75`; | ||
return ( | ||
<div> | ||
<h2>Partial Layout</h2> | ||
<DebugTree input={partial} /> | ||
<h2>Full Layout</h2> | ||
<DebugTree input={full} /> | ||
</div> | ||
); | ||
}; | ||
|
||
Debug.args = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { InnerNode } from '../tidy'; | ||
export function debugStrToTree(input: string): InnerNode { | ||
const lines = input.split('\n'); | ||
return linesToNode(lines.map((line) => ({ line, indent: getIndent(line) }))); | ||
} | ||
|
||
function linesToNode(lines: { line: string; indent: number }[]): InnerNode { | ||
const currentIndent = lines[0].indent; | ||
const node = lineToNode(lines[0].line); | ||
for (let i = 1; i < lines.length; i++) { | ||
const { line, indent } = lines[i]; | ||
if (indent === currentIndent + 1) { | ||
const start = i; | ||
let childIndent = lines[i + 1]?.indent; | ||
while (childIndent != null && childIndent >= currentIndent + 2) { | ||
i += 1; | ||
childIndent = lines[i + 1]?.indent; | ||
} | ||
|
||
const child = linesToNode(lines.slice(start, i + 1)); | ||
node.children.push(child); | ||
} else { | ||
throw new Error(); | ||
} | ||
} | ||
|
||
return node; | ||
} | ||
|
||
function lineToNode(line: string): InnerNode { | ||
const ans = line.match( | ||
/x: ([-\d\.]+), y: ([-\d\.]+), width: ([\d\.]+), height: ([\d\.]+)/, | ||
)!; | ||
return { | ||
id: (Math.random() * 1e9) | 0, | ||
x: parseFloat(ans[1]), | ||
y: parseFloat(ans[2]), | ||
width: parseFloat(ans[3]), | ||
height: parseFloat(ans[4]), | ||
children: [], | ||
}; | ||
} | ||
|
||
function getIndent(line: string) { | ||
return (line.match(/^ */s)![0].length / 4) | 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters