Skip to content

Commit c181fdd

Browse files
committed
feat: add shortenTextAfterLength props. (#12)
1 parent b37638c commit c181fdd

File tree

5 files changed

+80
-16
lines changed

5 files changed

+80
-16
lines changed

core/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,8 @@ export interface JsonViewProps<T> extends React.DetailedHTMLProps<React.HTMLAttr
666666
displayDataTypes?: boolean;
667667
/** When set to `true`, `objects` and `arrays` are labeled with size @default true */
668668
displayObjectSize?: boolean;
669+
/** Shorten long JSON strings, Set to `0` to disable this feature @default 20 */
670+
shortenTextAfterLength?: number;
669671
/** Define the root node name. @default undefined */
670672
keyName?: string | number;
671673
/** The user can copy objects and arrays to clipboard by clicking on the clipboard icon. @default true */
@@ -724,7 +726,7 @@ export interface CountInfoProps {
724726
```ts
725727
import { JsonViewProps } from '@uiw/react-json-view';
726728
import type { CountInfoExtraProps } from '@uiw/react-json-view/cjs/editor/countInfoExtra';
727-
export interface JsonViewEditorProps<T extends object> extends JsonViewProps<T> {
729+
export interface JsonViewEditorProps<T extends object> extends Omit<JsonViewProps<T>, 'shortenTextAfterLength'> {
728730
/**
729731
* When a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed.
730732
* @returns {boolean} Returning false from onEdit will prevent the change from being made.

core/src/editor/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ReValue } from './value';
66
import { CountInfoExtra } from './countInfoExtra';
77
import type { CountInfoExtraProps } from './countInfoExtra';
88

9-
export interface JsonViewEditorProps<T extends object> extends JsonViewProps<T> {
9+
export interface JsonViewEditorProps<T extends object> extends Omit<JsonViewProps<T>, 'shortenTextAfterLength'> {
1010
/**
1111
* When a callback function is passed in, edit functionality is enabled. The callback is invoked before edits are completed.
1212
* @returns {boolean} Returning false from onEdit will prevent the change from being made.

core/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export interface JsonViewProps<T extends object>
4040
displayDataTypes?: boolean;
4141
/** When set to `true`, `objects` and `arrays` are labeled with size @default true */
4242
displayObjectSize?: boolean;
43+
/** Shorten long JSON strings, Set to `0` to disable this feature @default 20 */
44+
shortenTextAfterLength?: number;
4345
/** Define the root node name. @default undefined */
4446
keyName?: string | number;
4547
/** The user can copy objects and arrays to clipboard by clicking on the clipboard icon. @default true */

core/src/node.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const RootNode = forwardRef(
5050
highlightUpdates = true,
5151
objectSortKeys = false,
5252
indentWidth = 15,
53+
shortenTextAfterLength = 20,
5354
collapsed,
5455
level = 1,
5556
keyid = 'root',
@@ -92,6 +93,7 @@ export const RootNode = forwardRef(
9293
displayDataTypes,
9394
displayObjectSize,
9495
enableClipboard,
96+
shortenTextAfterLength,
9597
level: level + 1,
9698
parentValue: value as T,
9799
indentWidth,

core/src/value.tsx

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export interface ValueViewProps<T extends object>
7373
enableClipboard: boolean;
7474
indentWidth: number;
7575
level?: number;
76+
shortenTextAfterLength?: JsonViewProps<T>['shortenTextAfterLength'];
7677
namespace?: Array<string | number>;
7778
quotes?: JsonViewProps<T>['quotes'];
7879
components?: JsonViewProps<T>['components'];
@@ -119,6 +120,58 @@ export function getValueString<T>(value: T) {
119120
return { type, content };
120121
}
121122

123+
interface RenderStringValueProps extends React.LabelHTMLAttributes<HTMLLabelElement> {
124+
color?: string;
125+
href?: string;
126+
style?: React.CSSProperties;
127+
isURL?: boolean;
128+
length?: number;
129+
}
130+
131+
const RenderShortenTextValue: FC<PropsWithChildren<RenderStringValueProps>> = ({
132+
children,
133+
length,
134+
style,
135+
...rest
136+
}) => {
137+
const childrenStr = children as string;
138+
const [shorten, setShorten] = useState(length && childrenStr.length >= length);
139+
const click = () => {
140+
console.log(shorten);
141+
if (length && childrenStr.length <= length) return setShorten(false);
142+
setShorten(!shorten);
143+
};
144+
const text = shorten ? `${childrenStr.slice(0, length)}...` : childrenStr;
145+
return (
146+
<RenderStringValue {...rest} style={{ ...style, cursor: 'pointer' }} onClick={click}>
147+
{text}
148+
</RenderStringValue>
149+
);
150+
};
151+
RenderShortenTextValue.displayName = 'JVR.RenderShortenTextValue';
152+
153+
const RenderStringValue: FC<PropsWithChildren<RenderStringValueProps>> = ({
154+
color,
155+
style,
156+
isURL,
157+
href,
158+
children,
159+
...rest
160+
}) => {
161+
return (
162+
<Label color={color} style={style} {...rest} className="w-rjv-value">
163+
{isURL && (
164+
<a href={href} style={{ color }} target="_blank" rel="noopener noreferrer">
165+
{children}
166+
</a>
167+
)}
168+
{!isURL && children}
169+
</Label>
170+
);
171+
};
172+
173+
RenderStringValue.displayName = 'JVR.RenderStringValue';
174+
122175
export function ValueView<T extends object>(props: ValueViewProps<T>) {
123176
const {
124177
value,
@@ -136,6 +189,7 @@ export function ValueView<T extends object>(props: ValueViewProps<T>) {
136189
enableClipboard,
137190
displayObjectSize,
138191
displayDataTypes,
192+
shortenTextAfterLength,
139193
...reset
140194
} = props;
141195

@@ -192,26 +246,30 @@ export function ValueView<T extends object>(props: ValueViewProps<T>) {
192246
content,
193247
children: content,
194248
});
195-
const valueView = reView ? (
196-
reView
197-
) : (
198-
<Label color={color} style={style} className="w-rjv-value">
199-
{isURL ? (
200-
<a href={value.href} style={{ color }} target="_blank" rel="noopener noreferrer">
201-
{content}
202-
</a>
203-
) : (
204-
content
205-
)}
206-
</Label>
207-
);
249+
250+
const valueView =
251+
shortenTextAfterLength && type === 'string' ? (
252+
<RenderShortenTextValue
253+
color={color}
254+
href={isURL ? value.href : ''}
255+
style={style}
256+
isURL={isURL}
257+
length={shortenTextAfterLength}
258+
>
259+
{content}
260+
</RenderShortenTextValue>
261+
) : (
262+
<RenderStringValue color={color} href={isURL ? value.href : ''} style={style} isURL={isURL}>
263+
{content}
264+
</RenderStringValue>
265+
);
208266
return (
209267
<Line {...eventProps}>
210268
<Label {...reset} ref={null}>
211269
{renderKey}
212270
<Colon />
213271
{typeView}
214-
{valueView}
272+
{reView ? reView : valueView}
215273
{tools}
216274
</Label>
217275
</Line>

0 commit comments

Comments
 (0)