Skip to content

Commit

Permalink
Merge pull request #58 from suyizhang/feature/custom-operation
Browse files Browse the repository at this point in the history
新增  CustomOperation
  • Loading branch information
YYsuni authored Aug 26, 2024
2 parents cc87705 + 65a0e61 commit 70f8e02
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import 'react18-json-view/src/style.css'
| `customizeCopy` | `(node: any) => any` | internal stringify | Customize copy behavior, only the returned non-empty string will be written to clipboard. |
| `CopyComponent` \/ `DoneComponent` \/ `CancelComponent` (canary) | `React.FC` \/ `React.Component` `<{ onClick: (event: React.MouseEvent) => void; className: string ; style: React.CSSProperties}>` | - | Customize copy icon. |
| `CopiedComponent` (canary) | `React.FC` \/ `React.Component` `<{ className: string; style: React.CSSProperties }>` | - | Customize copied icon. |
| `CustomOperation` | `React.FC` \/ `React.Component` `<{ node: any }>` | - | Custom Operation |

### Collapsed function
```ts
Expand Down
5 changes: 2 additions & 3 deletions src/components/json-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ interface Props {

export default function JsonNode({ node, depth, deleteHandle: _deleteHandle, indexOrName, parent, editHandle }: Props) {
// prettier-ignore
const { collapseStringsAfterLength, enableClipboard, editable, src, onDelete, onChange, customizeNode, matchesURL, urlRegExp, EditComponent, DoneComponent, CancelComponent } = useContext(JsonViewContext)
const { collapseStringsAfterLength, enableClipboard, editable, src, onDelete, onChange, customizeNode, matchesURL, urlRegExp, EditComponent, DoneComponent, CancelComponent, CustomOperation } = useContext(JsonViewContext)

let customReturn: ReturnType<CustomizeNode> | undefined
if (typeof customizeNode === 'function') customReturn = safeCall(customizeNode, [{ node, depth, indexOrName }])
Expand Down Expand Up @@ -124,14 +124,12 @@ export default function JsonNode({ node, depth, deleteHandle: _deleteHandle, ind
)

const isEditing = editing || deleting

const ctrlClick =
!isEditing && editableEdit(editable) && customEdit(customReturn as CustomizeOptions | undefined) && editHandle
? (event: React.MouseEvent) => {
if (event.ctrlKey || event.metaKey) edit()
}
: undefined

const Icons = (
<>
{isEditing &&
Expand Down Expand Up @@ -166,6 +164,7 @@ export default function JsonNode({ node, depth, deleteHandle: _deleteHandle, ind
{!isEditing && editableDelete(editable) && customDelete(customReturn as CustomizeOptions | undefined) && _deleteHandle && (
<DeleteSVG className='json-view--edit' onClick={() => setDeleting(true)} />
)}
{ typeof CustomOperation === 'function' ? <CustomOperation node={node} /> : null }
</>
)

Expand Down
15 changes: 12 additions & 3 deletions src/components/json-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export const JsonViewContext = createContext({
| React.FC<{ onClick: (event: React.MouseEvent) => void; className: string; style: React.CSSProperties }>
| React.Component<{ onClick: (event: React.MouseEvent) => void; className: string; style: React.CSSProperties }>
| undefined,
CustomOperation: undefined as
| React.FC<{ node: any }>
| React.Component<{ node: any }>
| undefined,
})

export interface JsonViewProps {
Expand Down Expand Up @@ -122,6 +126,10 @@ export interface JsonViewProps {
DoneComponent?:
| React.FC<{ onClick: (event: React.MouseEvent) => void; className: string; style: React.CSSProperties }>
| React.Component<{ onClick: (event: React.MouseEvent) => void; className: string; style: React.CSSProperties }>

CustomOperation?:
| React.FC<{ node: any }>
| React.Component<{ node: any }>
}

export default function JsonView({
Expand Down Expand Up @@ -165,13 +173,13 @@ export default function JsonView({

EditComponent,
CancelComponent,
DoneComponent
DoneComponent,
CustomOperation,
}: JsonViewProps) {
const [_, update] = useState(0)
const forceUpdate = useCallback(() => update(state => ++state), [])
const [src, setSrc] = useState(_src)
useEffect(() => setSrc(_src), [_src])

return (
<JsonViewContext.Provider
value={{
Expand Down Expand Up @@ -210,7 +218,8 @@ export default function JsonView({
CopiedComponent,
EditComponent,
CancelComponent,
DoneComponent
DoneComponent,
CustomOperation,
}}>
<code
className={'json-view' + (dark ? ' dark' : '') + (theme && theme !== 'default' ? ' json-view_' + theme : '') + (className ? ' ' + className : '')}
Expand Down
3 changes: 2 additions & 1 deletion src/components/large-array-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface Props {
}

export default function LargeArrayNode({ originNode, node, depth, index, deleteHandle: _deleteSelf, customOptions, startIndex }: Props) {
const { enableClipboard, src, onEdit, onChange, forceUpdate, displaySize } = useContext(JsonViewContext)
const { enableClipboard, src, onEdit, onChange, forceUpdate, displaySize, CustomOperation } = useContext(JsonViewContext)

const [fold, setFold] = useState(true)

Expand Down Expand Up @@ -57,6 +57,7 @@ export default function LargeArrayNode({ originNode, node, depth, index, deleteH
)}

{!fold && enableClipboard && customCopy(customOptions) && <CopyButton node={node} />}
{ typeof CustomOperation === 'function' ? <CustomOperation node={node} /> : null }
</>
)

Expand Down
3 changes: 2 additions & 1 deletion src/components/large-array.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default function LargeArray({ node, depth, deleteHandle: _deleteSelf, ind
nestCollapsedArray.push(node.slice(i, i + 100))
}

const { collapsed, enableClipboard, collapseObjectsAfterLength, editable, onDelete, src, onAdd, onEdit, onChange, forceUpdate, displaySize } =
const { collapsed, enableClipboard, collapseObjectsAfterLength, editable, onDelete, src, onAdd, CustomOperation, onChange, forceUpdate, displaySize } =
useContext(JsonViewContext)

const [fold, setFold] = useState(isCollapsed_largeArray(node, depth, indexOrName, collapsed, collapseObjectsAfterLength, customOptions))
Expand Down Expand Up @@ -89,6 +89,7 @@ export default function LargeArray({ node, depth, deleteHandle: _deleteSelf, ind
{!fold && !isEditing && editableDelete(editable) && customDelete(customOptions) && _deleteSelf && (
<DeleteSVG className='json-view--edit' onClick={() => setDeleting(true)} />
)}
{ typeof CustomOperation === 'function' ? <CustomOperation node={node} /> : null }
</>
)

Expand Down
4 changes: 3 additions & 1 deletion src/components/object-node.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export default function ObjectNode({ node, depth, indexOrName, deleteHandle: _de
onEdit,
onChange,
forceUpdate,
displaySize
displaySize,
CustomOperation,
} = useContext(JsonViewContext)

if (!ignoreLargeArray && Array.isArray(node) && node.length > 100) {
Expand Down Expand Up @@ -173,6 +174,7 @@ export default function ObjectNode({ node, depth, indexOrName, deleteHandle: _de
{!fold && !isEditing && editableDelete(editable) && customDelete(customOptions) && _deleteSelf && (
<DeleteSVG className='json-view--edit' onClick={() => setDeleting(true)} />
)}
{ typeof CustomOperation === 'function' ? <CustomOperation node={node} /> : null }
</>
)

Expand Down

0 comments on commit 70f8e02

Please sign in to comment.