forked from nervosnetwork/ckb-explorer-frontend
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support cell data decoding #267
Closed
PainterPuppets
wants to merge
4
commits into
Magickbase:develop
from
PainterPuppets:cell_data_support_decoding
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cbc6f7c
feat: cell data support decoding
PainterPuppets 95f2edf
feat: change select components to redix & support Real-time parsing o…
PainterPuppets d241c91
chore: move selection parser to app root
PainterPuppets 69c1d26
Merge branch 'develop' into cell_data_support_decoding
Keith-CY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from 'react' | ||
import * as Select from '@radix-ui/react-select' | ||
import classnames from 'classnames' | ||
|
||
import styles from './select.module.scss' | ||
|
||
interface MySelectProps extends Select.SelectProps { | ||
options: { value: string; label: string }[] | ||
placeholder?: string | ||
container?: Select.PortalProps['container'] | ||
} | ||
|
||
const MySelect = ({ placeholder, options, container, ...props }: MySelectProps) => ( | ||
<Select.Root {...props}> | ||
<Select.Trigger className={styles.selectTrigger} aria-label="Food"> | ||
<Select.Value placeholder={placeholder} /> | ||
<Select.Icon className={styles.selectIcon} /> | ||
</Select.Trigger> | ||
<Select.Portal container={container}> | ||
<Select.Content position="popper" className={styles.selectContent}> | ||
<Select.ScrollUpButton className={styles.SelectScrollButton} /> | ||
<Select.Viewport className={styles.selectViewport}> | ||
{options.map(option => ( | ||
<SelectItem key={option.value} value={option.value}> | ||
{option.label} | ||
</SelectItem> | ||
))} | ||
</Select.Viewport> | ||
<Select.ScrollDownButton className={styles.selectScrollButton} /> | ||
</Select.Content> | ||
</Select.Portal> | ||
</Select.Root> | ||
) | ||
|
||
const SelectItem: React.FC<React.PropsWithChildren<Select.SelectItemProps>> = ({ children, className, ...props }) => { | ||
return ( | ||
<Select.Item className={classnames(styles.selectItem, className)} {...props}> | ||
<Select.ItemText>{children}</Select.ItemText> | ||
</Select.Item> | ||
) | ||
} | ||
|
||
export default MySelect |
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,139 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import React, { ComponentProps, useEffect, useMemo, useRef, useState, createContext } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { Root, Trigger, Portal, Content } from 'selection-popover' | ||
import Select from './Select' | ||
import styles from './styles.module.scss' | ||
|
||
const DECODER = { | ||
utf8: 'UTF-8', | ||
number: 'Number', | ||
// todo: support decode address & Big-Endian | ||
// bigend: 'Big-Endian', | ||
} | ||
|
||
type ContextValue = { | ||
container?: HTMLElement | null | ||
} | ||
|
||
const SelectionParserContext = createContext<ContextValue>({ | ||
container: undefined, | ||
}) | ||
|
||
export const SelectionParserProvider = SelectionParserContext.Provider | ||
|
||
interface Props extends ComponentProps<'div'> {} | ||
|
||
export const SelectionParser: React.FC<React.PropsWithChildren<Props>> = ({ children, ...props }) => { | ||
const { t } = useTranslation() | ||
const ref = useRef(null) | ||
const wrapperRef = useRef<HTMLDivElement>(null) | ||
const contentRef = useRef<HTMLDivElement>(null) | ||
const [decoderOpen, setDecoderOpen] = useState(false) | ||
const [selectedText, setSelectedText] = useState('') | ||
const [anchorElement, setAnchorElement] = useState<HTMLElement | undefined>(undefined) | ||
const [decoder, setDecoder] = useState<keyof typeof DECODER>(Object.keys(DECODER)[0] as keyof typeof DECODER) | ||
|
||
const prefix0x = (hex: string) => { | ||
if (hex.startsWith('0x')) { | ||
return hex | ||
} | ||
|
||
if (hex.startsWith('x')) { | ||
return `0${hex}` | ||
} | ||
|
||
return `0x${hex}` | ||
} | ||
|
||
const onSelectionChanged = (e: Event) => { | ||
e.stopPropagation() | ||
e.preventDefault() | ||
|
||
const selection = window.getSelection() | ||
|
||
if (!selection) { | ||
setDecoderOpen(false) | ||
return | ||
} | ||
|
||
if (selection.anchorNode !== selection.focusNode) { | ||
setDecoderOpen(false) | ||
return | ||
} | ||
|
||
const min = Math.min(selection.anchorOffset ?? 0, selection.focusOffset ?? 0) | ||
const max = Math.max(selection.anchorOffset ?? 0, selection.focusOffset ?? 0) | ||
const text = (selection.anchorNode?.textContent ?? '').slice(min, max) | ||
|
||
setSelectedText(text) | ||
setAnchorElement(selection.anchorNode?.parentElement ?? undefined) | ||
} | ||
|
||
useEffect(() => { | ||
window.document.addEventListener('selectionchange', onSelectionChanged) | ||
return () => window.document.removeEventListener('selectionchange', onSelectionChanged) | ||
}, []) | ||
|
||
const handleOpenChange = (open: boolean) => { | ||
if (!open) { | ||
setDecoderOpen(false) | ||
return false | ||
} | ||
|
||
const selection = window.getSelection() | ||
if (!selection) { | ||
setDecoderOpen(false) | ||
return false | ||
} | ||
|
||
if (selection.anchorNode !== selection.focusNode) { | ||
setDecoderOpen(false) | ||
return false | ||
} | ||
|
||
setDecoderOpen(true) | ||
return true | ||
} | ||
|
||
const decodedText = useMemo(() => { | ||
try { | ||
if (decoder === 'utf8') { | ||
const bytes = [...selectedText.matchAll(/[0-9a-f]{2}/g)].map(a => parseInt(a[0], 16)) | ||
return new TextDecoder().decode(new Uint8Array(bytes)) | ||
} | ||
|
||
if (decoder === 'number') { | ||
if (!selectedText) return '' | ||
return BigInt(prefix0x(selectedText)).toString() | ||
} | ||
} catch { | ||
return `Unable to parse ${selectedText} to ${decoder}` | ||
} | ||
}, [decoder, selectedText]) | ||
|
||
return ( | ||
<div ref={wrapperRef} {...props}> | ||
<Root whileSelect openDelay={100} open={decoderOpen} onOpenChange={handleOpenChange}> | ||
<Trigger ref={ref}>{children}</Trigger> | ||
<Portal container={anchorElement}> | ||
<Content ref={contentRef} side="bottom" className={styles.selectionPopover}> | ||
<div className={styles.selectionPopoverHeader}> | ||
{t('transaction.view_data_as')} | ||
<Select | ||
container={contentRef.current} | ||
value={decoder} | ||
onValueChange={e => setDecoder(e as keyof typeof DECODER)} | ||
options={Object.keys(DECODER).map(key => ({ | ||
value: key, | ||
label: DECODER[key as keyof typeof DECODER], | ||
}))} | ||
/> | ||
</div> | ||
<div className={styles.selectionPopoverContent}>{decodedText}</div> | ||
</Content> | ||
</Portal> | ||
</Root> | ||
</div> | ||
) | ||
} |
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,63 @@ | ||
button { | ||
all: unset; | ||
} | ||
|
||
.selectTrigger { | ||
display: inline-flex; | ||
cursor: pointer; | ||
align-items: center; | ||
justify-content: center; | ||
border-radius: 4px; | ||
padding: 0 15px; | ||
line-height: 1; | ||
gap: 5px; | ||
background-color: white; | ||
color: var(--primary-color); | ||
} | ||
|
||
.selectIcon { | ||
font-size: 12px; | ||
} | ||
|
||
.selectContent { | ||
width: 100%; | ||
overflow: hidden; | ||
background-color: white; | ||
border-radius: 6px; | ||
box-shadow: 0 10px 38px -10px rgb(22 23 24 / 35%), 0 10px 20px -15px rgb(22 23 24 / 20%); | ||
z-index: 3; | ||
} | ||
|
||
.selectViewport { | ||
padding: 12px 8px; | ||
gap: 8px; | ||
display: flex; | ||
flex-direction: column; | ||
} | ||
|
||
.selectItem { | ||
cursor: pointer; | ||
border-radius: 3px; | ||
display: flex; | ||
align-items: center; | ||
|
||
// padding: 0 35px 0 25px; | ||
position: relative; | ||
user-select: none; | ||
font-weight: 500; | ||
font-size: 1rem; | ||
line-height: 1.5rem; | ||
|
||
&:hover { | ||
color: var(--primary-color); | ||
} | ||
} | ||
|
||
.selectScrollButton { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
height: 25px; | ||
background-color: white; | ||
cursor: default; | ||
} |
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,45 @@ | ||
@import '../../styles/variables.module'; | ||
|
||
.selectionPopover { | ||
background-color: #fff; | ||
border-radius: 4px; | ||
box-shadow: 0 2px 10px #eee; | ||
transform-origin: var(--selection-popover-content-transform-origin); | ||
animation: scale-in 500ms cubic-bezier(0.16, 1, 0.3, 1); | ||
max-width: 400px; | ||
word-wrap: break-word; | ||
z-index: 2; | ||
} | ||
|
||
.selectionPopoverHeader { | ||
padding: 16px; | ||
border-bottom: 1px solid #e5e5e5; | ||
font-size: 1rem; | ||
font-weight: 500; | ||
text-align: start; | ||
} | ||
|
||
.selectionPopoverContent { | ||
padding: 16px; | ||
font-size: 0.875rem; | ||
font-weight: 600; | ||
color: #666; | ||
line-height: 1.5rem; | ||
text-align: start; | ||
} | ||
|
||
@keyframes scale-in { | ||
from { | ||
opacity: 0; | ||
transform: scale(0); | ||
} | ||
|
||
to { | ||
opacity: 1; | ||
transform: scale(1); | ||
} | ||
} | ||
|
||
.notSelect { | ||
user-select: none; | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these update still necessary when the selection is listened globally
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These code is to make the component look the same as the design, I'll remove it if our plan to update it later.