From 12dbc8ac5f8fe7576759640d64060a5ff93afadf Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Fri, 13 Oct 2017 22:42:54 +0200 Subject: [PATCH] Add array paging --- app/ui/components/json-viewer.js | 69 +++++++++++++++++--------- app/ui/css/components/json-viewer.less | 23 ++++++++- 2 files changed, 68 insertions(+), 24 deletions(-) diff --git a/app/ui/components/json-viewer.js b/app/ui/components/json-viewer.js index 3998a65a74c9..22a64cee6d94 100644 --- a/app/ui/components/json-viewer.js +++ b/app/ui/components/json-viewer.js @@ -3,8 +3,8 @@ import React from 'react'; import classnames from 'classnames'; import autobind from 'autobind-decorator'; -// const PAGE_SIZE = 10; -const MAX_VALUE_LENGTH = 50; +const PAGE_SIZE = 25; +const MAX_VALUE_LENGTH = 100; type Props = { body: Buffer, @@ -83,7 +83,8 @@ type Props2 = { type State2 = { expanded: boolean, - hasBeenExpanded: boolean + hasBeenExpanded: boolean, + page: number }; @autobind @@ -93,7 +94,8 @@ class JSONViewerObj extends React.PureComponent { const {paths} = props; this.state = { expanded: paths.length === 0, - hasBeenExpanded: false + hasBeenExpanded: false, + page: 0 }; } @@ -153,9 +155,15 @@ class JSONViewerObj extends React.PureComponent { return abbr; } - return collapsed - ? {abbr} {comment} - : null; + if (collapsed) { + return ( + + {abbr} {comment} + + ); + } else { + return null; + } } const strObj: string = `${obj}`; @@ -170,9 +178,9 @@ class JSONViewerObj extends React.PureComponent { } return ( -
+      
         {displayValue}
-      
+ ); } @@ -187,13 +195,18 @@ class JSONViewerObj extends React.PureComponent { })); } + handleNextPage () { + this.setState(({page}) => ({page: page + 1})); + } + render () { const {label, value, paths, hide, onExpand} = this.props; - const {expanded, hasBeenExpanded} = this.state; + const {expanded, hasBeenExpanded, page} = this.state; const collapsable = this.isCollapsable(value); const collapsed = !expanded; const indentStyles = {paddingLeft: `${(paths.length - 1) * 1.3}em`}; + const nextIndentStyles = {paddingLeft: `${(paths.length) * 1.3}em`}; const rowClasses = classnames({ 'hide': hide, @@ -211,19 +224,16 @@ class JSONViewerObj extends React.PureComponent { className="json-viewer__key-container" onClick={collapsable ? this.handleClickKey : null}> - - {label} - - - - {this.getValue(value, collapsed)} + {label} + {this.getValue(value, collapsed)} )); } if (!collapsed || hasBeenExpanded) { - for (let key = 0; key < value.length; key++) { + const maxItem = page >= 0 ? PAGE_SIZE * (page + 1) : Infinity; + for (let key = 0; key < value.length && key < maxItem; key++) { const newPaths = [...paths, `[${key}]`]; rows.push(( { /> )); } + + if (value.length > maxItem) { + const numLeft = Math.min(value.length - maxItem, PAGE_SIZE); + rows.push( + + + + + {maxItem}…{value.length - 1} + + + + + + + ); + } } } else if (value && typeof value === 'object') { if (label !== undefined) { @@ -249,9 +276,7 @@ class JSONViewerObj extends React.PureComponent { {label} - - {this.getValue(value, collapsed)} - + {this.getValue(value, collapsed)} )); } @@ -284,9 +309,7 @@ class JSONViewerObj extends React.PureComponent { ) : null} - - {this.getValue(value, collapsed)} - + {this.getValue(value, collapsed)} )); } diff --git a/app/ui/css/components/json-viewer.less b/app/ui/css/components/json-viewer.less index bb2f19ada41f..55ce410404f0 100644 --- a/app/ui/css/components/json-viewer.less +++ b/app/ui/css/components/json-viewer.less @@ -19,13 +19,29 @@ padding-right: @padding-sm; } - .json-viewer__key { + .json-viewer__key--array, + .json-viewer__key--object { user-select: text; &::after { content: ':'; } } + .json-viewer__value--next-page { + button { + padding: @padding-xxs @padding-xs; + border-radius: @radius-sm; + margin: @padding-sm 0 @padding-xs 0; + border: 1px solid var(--hl); + display: inline-block; + opacity: @opacity-subtle; + + &:hover { + opacity: 1; + } + } + } + .json-viewer__key--object { color: var(--color-success); } @@ -57,6 +73,7 @@ &.json-viewer__row--collapsed .json-viewer__icon::before { content: '▸'; } + &:not(.json-viewer__row--collapsed) .json-viewer__icon::before { content: '▾'; } @@ -65,10 +82,14 @@ .json-viewer__value { user-select: text; cursor: text; + white-space: pre-wrap; } .json-viewer__type-string { color: var(--color-notice); + &::before, &::after { + content: '"'; + } } .json-viewer__type-null,