|
1 |
| -import React, { useEffect, useRef } from 'react' |
| 1 | +import React, { useEffect, useRef, useState } from 'react' |
2 | 2 | import { AutogrowTextArea } from './AutogrowTextArea'
|
3 |
| -import { insertCharInTextArea, toPathString, truncate } from './helpers' |
| 3 | +import { insertCharInTextArea, toPathString } from './helpers' |
4 | 4 | import { useTheme } from './contexts'
|
5 |
| -import { type InputProps } from './types' |
| 5 | +import { type NodeData, type InputProps } from './types' |
| 6 | +import { type TranslateFunction } from './localisation' |
6 | 7 |
|
7 | 8 | export const INVALID_FUNCTION_STRING = '**INVALID_FUNCTION**'
|
8 | 9 |
|
| 10 | +interface StringDisplayProps { |
| 11 | + nodeData: NodeData |
| 12 | + styles: React.CSSProperties |
| 13 | + pathString: string |
| 14 | + showStringQuotes?: boolean |
| 15 | + stringTruncate?: number |
| 16 | + canEdit: boolean |
| 17 | + setIsEditing: (value: React.SetStateAction<boolean>) => void |
| 18 | + translate: TranslateFunction |
| 19 | +} |
| 20 | +export const StringDisplay: React.FC<StringDisplayProps> = ({ |
| 21 | + nodeData, |
| 22 | + showStringQuotes = true, |
| 23 | + stringTruncate = 200, |
| 24 | + pathString, |
| 25 | + canEdit, |
| 26 | + setIsEditing, |
| 27 | + styles, |
| 28 | + translate, |
| 29 | +}) => { |
| 30 | + const value = nodeData.value as string |
| 31 | + const [isExpanded, setIsExpanded] = useState(false) |
| 32 | + |
| 33 | + const quoteChar = showStringQuotes ? '"' : '' |
| 34 | + |
| 35 | + const requiresTruncation = value.length > stringTruncate |
| 36 | + |
| 37 | + const handleMaybeEdit = () => { |
| 38 | + canEdit ? setIsEditing(true) : setIsExpanded(!isExpanded) |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <div |
| 43 | + id={`${pathString}_display`} |
| 44 | + onDoubleClick={handleMaybeEdit} |
| 45 | + onClick={(e) => { |
| 46 | + if (e.getModifierState('Control') || e.getModifierState('Meta')) handleMaybeEdit() |
| 47 | + }} |
| 48 | + className="jer-value-string" |
| 49 | + style={styles} |
| 50 | + > |
| 51 | + {quoteChar} |
| 52 | + {!requiresTruncation ? ( |
| 53 | + `${value}${quoteChar}` |
| 54 | + ) : isExpanded ? ( |
| 55 | + <> |
| 56 | + <span> |
| 57 | + {value} |
| 58 | + {quoteChar} |
| 59 | + </span> |
| 60 | + <span className="jer-string-expansion jer-show-less" onClick={() => setIsExpanded(false)}> |
| 61 | + {' '} |
| 62 | + {translate('SHOW_LESS', nodeData)} |
| 63 | + </span> |
| 64 | + </> |
| 65 | + ) : ( |
| 66 | + <> |
| 67 | + <span>{value.slice(0, stringTruncate - 2).trimEnd()}</span> |
| 68 | + <span className="jer-string-expansion jer-ellipsis" onClick={() => setIsExpanded(true)}> |
| 69 | + ... |
| 70 | + </span> |
| 71 | + {quoteChar} |
| 72 | + </> |
| 73 | + )} |
| 74 | + </div> |
| 75 | + ) |
| 76 | +} |
| 77 | + |
9 | 78 | export const StringValue: React.FC<InputProps & { value: string }> = ({
|
10 | 79 | value,
|
11 | 80 | setValue,
|
12 | 81 | isEditing,
|
13 | 82 | path,
|
14 |
| - setIsEditing, |
15 | 83 | handleEdit,
|
16 |
| - stringTruncate, |
17 |
| - showStringQuotes, |
18 | 84 | nodeData,
|
19 | 85 | handleKeyboard,
|
20 | 86 | keyboardCommon,
|
| 87 | + ...props |
21 | 88 | }) => {
|
22 | 89 | const { getStyles } = useTheme()
|
23 | 90 |
|
24 | 91 | const textAreaRef = useRef<HTMLTextAreaElement>(null)
|
25 | 92 |
|
26 | 93 | const pathString = toPathString(path)
|
27 | 94 |
|
28 |
| - const quoteChar = showStringQuotes ? '"' : '' |
29 |
| - |
30 | 95 | return isEditing ? (
|
31 | 96 | <AutogrowTextArea
|
32 | 97 | className="jer-input-text"
|
@@ -54,19 +119,12 @@ export const StringValue: React.FC<InputProps & { value: string }> = ({
|
54 | 119 | styles={getStyles('input', nodeData)}
|
55 | 120 | />
|
56 | 121 | ) : (
|
57 |
| - <div |
58 |
| - id={`${pathString}_display`} |
59 |
| - onDoubleClick={() => setIsEditing(true)} |
60 |
| - onClick={(e) => { |
61 |
| - if (e.getModifierState('Control') || e.getModifierState('Meta')) setIsEditing(true) |
62 |
| - }} |
63 |
| - className="jer-value-string" |
64 |
| - style={getStyles('string', nodeData)} |
65 |
| - > |
66 |
| - {quoteChar} |
67 |
| - {truncate(value, stringTruncate)} |
68 |
| - {quoteChar} |
69 |
| - </div> |
| 122 | + <StringDisplay |
| 123 | + nodeData={nodeData} |
| 124 | + pathString={pathString} |
| 125 | + styles={getStyles('string', nodeData)} |
| 126 | + {...props} |
| 127 | + /> |
70 | 128 | )
|
71 | 129 | }
|
72 | 130 |
|
|
0 commit comments