forked from nervosnetwork/ckb-explorer-frontend
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move selection parser to app root
- Loading branch information
1 parent
95f2edf
commit d241c91
Showing
7 changed files
with
191 additions
and
102 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
3 changes: 0 additions & 3 deletions
3
...nsaction/TransactionCellScript/Select.tsx → src/components/SelectionParser/Select.tsx
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,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
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; | ||
} |
90 changes: 0 additions & 90 deletions
90
src/pages/Transaction/TransactionCellScript/CellInfoDataView.tsx
This file was deleted.
Oops, something went wrong.
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