From 232097d99952a76f8b76f05e7119d897005f9a39 Mon Sep 17 00:00:00 2001 From: pantonshire Date: Thu, 15 Jun 2023 17:04:58 +0100 Subject: [PATCH] option to transpose table --- src/app/globals.css | 1 + src/app/page.module.css | 25 ++++- src/components/inspector.tsx | 24 ++++- src/components/output_table.tsx | 183 +++++++++++++++++++++++--------- 4 files changed, 177 insertions(+), 56 deletions(-) diff --git a/src/app/globals.css b/src/app/globals.css index 802067d..319c6f1 100644 --- a/src/app/globals.css +++ b/src/app/globals.css @@ -1,4 +1,5 @@ :root { + --base_max_width: 1000px; --main_1: #735cd6; --main_2: #db52c9; --light_1: #a598e0; diff --git a/src/app/page.module.css b/src/app/page.module.css index 9b0b636..ad1166e 100644 --- a/src/app/page.module.css +++ b/src/app/page.module.css @@ -3,7 +3,7 @@ } .title_section { - max-width: 1000px; + max-width: var(--base_max_width); margin: 0 auto 2rem auto; padding: 0 3rem; } @@ -20,7 +20,7 @@ } .input_section { - max-width: 1000px; + max-width: var(--base_max_width); margin: 0 auto 2rem auto; } @@ -55,17 +55,32 @@ #output_table { margin: 0 auto; + font-size: 0.9rem; } -#output_table tbody th:nth-child(1) { +.output_table_regular tbody th:nth-child(1) { text-align: center; } -#output_table td, -#output_table th { +.output_table_regular { white-space: nowrap; } +.output_table_transposed th, +.output_table_transposed td { + vertical-align: top; +} + +.button_container { + max-width: var(--base_max_width); + margin: 1rem auto 1rem auto; +} + +.button { + padding: 0.25rem 2rem; + display: block; +} + @media screen and (min-width: 40rem) { .main { padding: 2rem 3rem; diff --git a/src/components/inspector.tsx b/src/components/inspector.tsx index 747598b..c54b023 100644 --- a/src/components/inspector.tsx +++ b/src/components/inspector.tsx @@ -1,17 +1,33 @@ 'use client'; -import { useState } from 'react'; +import { useEffect, useState } from 'react'; import styles from '@/app/page.module.css' import { TextField } from './textfield'; import { UtfdumpContextProvider } from '@/context/utfdump'; import { OutputTable } from './output_table'; +const lsTransposeKey = 'transpose'; +const lsTrueStr = 't'; +const lsFalseStr = 'f'; + type InspectorProps = { sourceUrl: string, }; export function Inspector(props: InspectorProps) { const [currentString, setCurrentString] = useState(''); + const [transpose, setTranspose] = useState(true); + + useEffect(() => { + const lsTranspose = localStorage.getItem(lsTransposeKey) !== lsFalseStr; + setTranspose(lsTranspose); + }, []); + + function toggleTranspose() { + const newTranspose = !transpose; + setTranspose(newTranspose); + localStorage.setItem(lsTransposeKey, newTranspose ? lsTrueStr : lsFalseStr); + } return ( @@ -30,7 +46,11 @@ export function Inspector(props: InspectorProps) { - + ); diff --git a/src/components/output_table.tsx b/src/components/output_table.tsx index fb4b4ef..a54538e 100644 --- a/src/components/output_table.tsx +++ b/src/components/output_table.tsx @@ -3,10 +3,16 @@ import styles from '@/app/page.module.css' import { UtfdumpContext } from '@/context/utfdump'; import { EncodedCodepoint } from 'utfdump_wasm'; -export function OutputTable(props: { currentString: string }) { +export function OutputTable(props: { + currentString: string, + transpose: boolean, + toggleTranspose: () => void +}) { const ctx = useContext(UtfdumpContext); - + + let nonEmpty = false; let rows = []; + let transposedData: React.JSX.Element[][] = [[], [], [], [], [], [], [], [], [], [], [], [], [], []]; if (ctx.utfdump) { for (const codepointStr of props.currentString) { @@ -38,24 +44,60 @@ export function OutputTable(props: { currentString: string }) { charDisplayStr = '\u25cc' + charDisplayStr; } - rows.push(( - - {charDisplayStr} - {charData.name()} - U+{codepoint.toString(16).padStart(4, '0')} - {encodedUtf8Str} - {encodedUtf16Str} - {charData.category_full()} - {combiningClassName || combiningClassNum} - {charData.bidi_full()} - {charData.mirrored() ? 'Yes' : 'No'} - {optCharCodes(charData.decomp_string())} - {optCharCodes(charData.uppercase_string())} - {optCharCodes(charData.lowercase_string())} - {optCharCodes(charData.titlecase_string())} - {charData.numeric_value()} - - )); + const elemChar = {charDisplayStr}; + const elemName = {charData.name()}; + const elemCode = U+{codepoint.toString(16).padStart(4, '0')}; + const elemUtf8 = {encodedUtf8Str}; + const elemUtf16 = {encodedUtf16Str}; + const elemCategory = {charData.category_full()}; + const elemCombining = {combiningClassName || combiningClassNum}; + const elemBidi = {charData.bidi_full()}; + const elemMirrored = {charData.mirrored() ? 'Yes' : 'No'}; + const elemDecomp = {optCharCodes(charData.decomp_string())}; + const elemUpper = {optCharCodes(charData.uppercase_string())}; + const elemLower = {optCharCodes(charData.lowercase_string())}; + const elemTitle = {optCharCodes(charData.titlecase_string())}; + const elemNumeric = {charData.numeric_value()}; + + if (props.transpose) { + transposedData[0].push(elemChar); + transposedData[1].push(elemName); + transposedData[2].push(elemCode); + transposedData[3].push(elemUtf8); + transposedData[4].push(elemUtf16); + transposedData[5].push(elemCategory); + transposedData[6].push(elemCombining); + transposedData[7].push(elemBidi); + transposedData[8].push(elemMirrored); + transposedData[9].push(elemDecomp); + transposedData[10].push(elemUpper); + transposedData[11].push(elemLower); + transposedData[12].push(elemTitle); + transposedData[13].push(elemNumeric); + } + + else { + rows.push(( + + {elemChar} + {elemName} + {elemCode} + {elemUtf8} + {elemUtf16} + {elemCategory} + {elemCombining} + {elemBidi} + {elemMirrored} + {elemDecomp} + {elemUpper} + {elemLower} + {elemTitle} + {elemNumeric} + + )); + } + + nonEmpty = true; charData.free(); } @@ -69,40 +111,83 @@ export function OutputTable(props: { currentString: string }) { let table; - if (rows.length > 0) { - table = ( - - - - - - - - - - - - - - - - - - - - - {rows} - -
NameCodeUTF-8UTF-16LECategoryCombiningBidirectionalMirroredDecompUpperLowerTitleNumeric
- ); + if (nonEmpty) { + if (props.transpose) { + table = ( + + + {transposedData[0]} + + + {transposedData[1]} + {transposedData[2]} + {transposedData[3]} + {transposedData[4]} + {transposedData[5]} + {transposedData[6]} + {transposedData[7]} + {transposedData[8]} + {transposedData[9]} + {transposedData[10]} + {transposedData[11]} + {transposedData[12]} + {transposedData[13]} + +
Name
Code
UTF-8
UTF-16LE
Category
Combining
Bidirectional
Mirrored
Decomp
Upper
Lower
Title
Numeric
+ ); + } + + else { + table = ( + + + + + + + + + + + + + + + + + + + + + {rows} + +
NameCodeUTF-8UTF-16LECategoryCombiningBidirectionalMirroredDecompUpperLowerTitleNumeric
+ ); + } } else { table = (<>); } + let transposeButton; + if (nonEmpty) { + transposeButton = ( + + ); + } else { + transposeButton = (<>); + } + return ( -
-
- {table} +
+
+ {transposeButton} +
+
+
+ {table} +
);