Skip to content

Commit ba01175

Browse files
committed
Refactor UiTextarea to use contentEditable instead of react-textarea-autosize and add support for action icons
- inspired by prompt field's 'clear' action, `packages/ui/src/components/editor/common/Textarea/Textarea.tsx` should accept 'on_action_click' and 'action_icon' (codicon). Use it for instructions original state restoration. - the textarea's action should use float right so that text nicely wraps around it. Inspire solution by the clear button present in the prompt field. - the action should be smaller, to not occupy two lines of text but one.
1 parent a2da14d commit ba01175

7 files changed

Lines changed: 203 additions & 134 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,6 @@
11461146
"path-browserify": "^1.0.1",
11471147
"react": "^18.2.0",
11481148
"react-dom": "^18.2.0",
1149-
"react-textarea-autosize": "^8.5.9",
11501149
"rimraf": "^6.0.1",
11511150
"simplebar-react": "^3.3.1",
11521151
"ui": "workspace:*",

‎apps/editor/src/views/settings/frontend/Home/sections/ApiConfigurationsSection.tsx‎

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -209,70 +209,52 @@ export const ApiConfigurationsSection = forwardRef<HTMLDivElement, Props>(
209209
<UiItem
210210
title={t('configurations.edit-context-system-instructions.title')}
211211
description={t('configurations.edit-context-system-instructions.description')}
212-
slot_right={
213-
props.edit_context_instructions !==
214-
props.default_edit_context_instructions && (
215-
<IconButton
216-
codicon_icon="discard"
217-
title={t('configurations.action.restore-default')}
218-
on_click={(e) => {
219-
e.stopPropagation()
220-
props.on_restore_edit_context_instructions()
221-
}}
222-
/>
223-
)
224-
}
225212
>
226213
<UiTextarea
227214
value={props.edit_context_instructions}
228215
on_change={props.set_edit_context_instructions}
229216
on_blur={props.on_edit_context_instructions_blur}
217+
action_icon={
218+
props.edit_context_instructions !== props.default_edit_context_instructions
219+
? 'discard'
220+
: undefined
221+
}
222+
action_title={t('configurations.action.restore-default')}
223+
on_action_click={props.on_restore_edit_context_instructions}
230224
/>
231225
</UiItem>
232226
<UiItem
233227
title={t('configurations.commit-message-instructions.title')}
234228
description={t('configurations.commit-message-instructions.description')}
235-
slot_right={
236-
props.commit_instructions !==
237-
props.default_commit_instructions && (
238-
<IconButton
239-
codicon_icon="discard"
240-
title={t('configurations.action.restore-default')}
241-
on_click={(e) => {
242-
e.stopPropagation()
243-
props.on_restore_commit_instructions()
244-
}}
245-
/>
246-
)
247-
}
248229
>
249230
<UiTextarea
250231
value={props.commit_instructions}
251232
on_change={props.set_commit_instructions}
252233
on_blur={props.on_commit_instructions_blur}
234+
action_icon={
235+
props.commit_instructions !== props.default_commit_instructions
236+
? 'discard'
237+
: undefined
238+
}
239+
action_title={t('configurations.action.restore-default')}
240+
on_action_click={props.on_restore_commit_instructions}
253241
/>
254242
</UiItem>
255243
<UiItem
256244
title={t('configurations.voice-input-instructions.title')}
257245
description={t('configurations.voice-input-instructions.description')}
258-
slot_right={
259-
props.voice_input_instructions !==
260-
props.default_voice_input_instructions && (
261-
<IconButton
262-
codicon_icon="discard"
263-
title={t('configurations.action.restore-default')}
264-
on_click={(e) => {
265-
e.stopPropagation()
266-
props.on_restore_voice_input_instructions()
267-
}}
268-
/>
269-
)
270-
}
271246
>
272247
<UiTextarea
273248
value={props.voice_input_instructions}
274249
on_change={props.set_voice_input_instructions}
275250
on_blur={props.on_voice_input_instructions_blur}
251+
action_icon={
252+
props.voice_input_instructions !== props.default_voice_input_instructions
253+
? 'discard'
254+
: undefined
255+
}
256+
action_title={t('configurations.action.restore-default')}
257+
on_action_click={props.on_restore_voice_input_instructions}
276258
/>
277259
</UiItem>
278260
</UiGroup>

‎apps/editor/src/views/settings/frontend/Home/sections/GeneralSection.tsx‎

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Item as UiItem } from '@ui/components/editor/settings/Item'
66
import { Group as UiGroup } from '@ui/components/editor/settings/Group/Group'
77
import { Section as UiSection } from '@ui/components/editor/settings/Section'
88
import { TextButton as UiTextButton } from '@ui/components/editor/settings/TextButton'
9-
import { IconButton as UiIconButton } from '@ui/components/editor/common/IconButton'
109
import { Textarea as UiTextarea } from '@ui/components/editor/common/Textarea'
1110
import { EditFormatInstructions } from '@/views/settings/types/messages'
1211
import {
@@ -384,19 +383,6 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
384383
expand: t('general.edit-format.expand'),
385384
collapse: t('general.edit-format.collapse')
386385
}}
387-
slot_right={
388-
instructions.whole != '' &&
389-
instructions.whole != EDIT_FORMAT_INSTRUCTIONS_WHOLE ? (
390-
<UiIconButton
391-
codicon_icon="discard"
392-
title={t('general.action.restore-default')}
393-
on_click={(e) => {
394-
e.stopPropagation()
395-
handle_reset_instruction('whole', EDIT_FORMAT_INSTRUCTIONS_WHOLE)
396-
}}
397-
/>
398-
) : undefined
399-
}
400386
>
401387
<UiTextarea
402388
value={instructions.whole}
@@ -405,6 +391,14 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
405391
set_instructions((prev) => ({ ...prev, whole: value }))
406392
}
407393
on_blur={handle_instructions_blur}
394+
action_icon={
395+
instructions.whole != '' &&
396+
instructions.whole != EDIT_FORMAT_INSTRUCTIONS_WHOLE
397+
? 'discard'
398+
: undefined
399+
}
400+
action_title={t('general.action.restore-default')}
401+
on_action_click={() => handle_reset_instruction('whole', EDIT_FORMAT_INSTRUCTIONS_WHOLE)}
408402
/>
409403
</UiItem>
410404

@@ -416,22 +410,6 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
416410
expand: t('general.edit-format.expand'),
417411
collapse: t('general.edit-format.collapse')
418412
}}
419-
slot_right={
420-
instructions.truncated != '' &&
421-
instructions.truncated != EDIT_FORMAT_INSTRUCTIONS_TRUNCATED ? (
422-
<UiIconButton
423-
codicon_icon="discard"
424-
title={t('general.action.restore-default')}
425-
on_click={(e) => {
426-
e.stopPropagation()
427-
handle_reset_instruction(
428-
'truncated',
429-
EDIT_FORMAT_INSTRUCTIONS_TRUNCATED
430-
)
431-
}}
432-
/>
433-
) : undefined
434-
}
435413
>
436414
<UiTextarea
437415
value={instructions.truncated}
@@ -443,6 +421,14 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
443421
}))
444422
}
445423
on_blur={handle_instructions_blur}
424+
action_icon={
425+
instructions.truncated != '' &&
426+
instructions.truncated != EDIT_FORMAT_INSTRUCTIONS_TRUNCATED
427+
? 'discard'
428+
: undefined
429+
}
430+
action_title={t('general.action.restore-default')}
431+
on_action_click={() => handle_reset_instruction('truncated', EDIT_FORMAT_INSTRUCTIONS_TRUNCATED)}
446432
/>
447433
</UiItem>
448434

@@ -454,23 +440,6 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
454440
expand: t('general.edit-format.expand'),
455441
collapse: t('general.edit-format.collapse')
456442
}}
457-
slot_right={
458-
instructions.search_replace != '' &&
459-
instructions.search_replace !=
460-
EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE ? (
461-
<UiIconButton
462-
codicon_icon="discard"
463-
title={t('general.action.restore-default')}
464-
on_click={(e) => {
465-
e.stopPropagation()
466-
handle_reset_instruction(
467-
'search_replace',
468-
EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE
469-
)
470-
}}
471-
/>
472-
) : undefined
473-
}
474443
>
475444
<UiTextarea
476445
value={instructions.search_replace}
@@ -482,6 +451,14 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
482451
}))
483452
}
484453
on_blur={handle_instructions_blur}
454+
action_icon={
455+
instructions.search_replace != '' &&
456+
instructions.search_replace != EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE
457+
? 'discard'
458+
: undefined
459+
}
460+
action_title={t('general.action.restore-default')}
461+
on_action_click={() => handle_reset_instruction('search_replace', EDIT_FORMAT_INSTRUCTIONS_SEARCH_REPLACE)}
485462
/>
486463
</UiItem>
487464

@@ -493,19 +470,6 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
493470
expand: t('general.edit-format.expand'),
494471
collapse: t('general.edit-format.collapse')
495472
}}
496-
slot_right={
497-
instructions.diff != '' &&
498-
instructions.diff != EDIT_FORMAT_INSTRUCTIONS_DIFF ? (
499-
<UiIconButton
500-
codicon_icon="discard"
501-
title={t('general.action.restore-default')}
502-
on_click={(e) => {
503-
e.stopPropagation()
504-
handle_reset_instruction('diff', EDIT_FORMAT_INSTRUCTIONS_DIFF)
505-
}}
506-
/>
507-
) : undefined
508-
}
509473
>
510474
<UiTextarea
511475
value={instructions.diff}
@@ -514,6 +478,14 @@ export const GeneralSection = forwardRef<HTMLDivElement, Props>(
514478
set_instructions((prev) => ({ ...prev, diff: value }))
515479
}
516480
on_blur={handle_instructions_blur}
481+
action_icon={
482+
instructions.diff != '' &&
483+
instructions.diff != EDIT_FORMAT_INSTRUCTIONS_DIFF
484+
? 'discard'
485+
: undefined
486+
}
487+
action_title={t('general.action.restore-default')}
488+
on_action_click={() => handle_reset_instruction('diff', EDIT_FORMAT_INSTRUCTIONS_DIFF)}
517489
/>
518490
</UiItem>
519491
</UiGroup>

‎packages/ui/package.json‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"react-markdown": "^10.1.0",
3131
"react-qr-code": "^2.0.18",
3232
"react-sortablejs": "^6.1.4",
33-
"react-textarea-autosize": "^8.5.9",
3433
"simplebar-react": "^3.3.1",
3534
"vite-plugin-svgr": "^3.2.0"
3635
}
Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,64 @@
1-
.textarea {
1+
.wrapper {
2+
position: relative;
3+
width: 100%;
24
border: 1px solid var(--cwc-border-color);
35
background: var(--vscode-input-background);
46
color: var(--vscode-input-foreground);
57
padding: 4px 6px;
68
border-radius: 4px;
7-
width: 100%;
8-
resize: none;
9-
overflow: hidden;
9+
cursor: text;
10+
box-sizing: border-box;
1011

1112
&:hover {
1213
border-color: var(--cwc-border-color-hover);
1314
}
1415

15-
&:focus {
16-
outline: none;
16+
&:focus-within {
1717
border-color: var(--vscode-focusBorder);
1818
}
1919
}
20+
21+
.action {
22+
float: right;
23+
position: relative;
24+
z-index: 1;
25+
width: 14px;
26+
height: 14px;
27+
display: flex;
28+
align-items: center;
29+
justify-content: center;
30+
cursor: pointer;
31+
color: var(--vscode-icon-foreground);
32+
opacity: 0.5;
33+
margin-left: 6px;
34+
margin-top: -1px;
35+
36+
&::before {
37+
font-size: 13px;
38+
}
39+
40+
&:hover {
41+
opacity: 1;
42+
}
43+
}
44+
45+
.textarea {
46+
outline: none;
47+
border: none;
48+
background: transparent;
49+
color: inherit;
50+
padding: 0;
51+
margin: 0;
52+
white-space: pre-wrap;
53+
overflow-wrap: break-word;
54+
word-break: break-word;
55+
line-height: 1.4;
56+
position: relative;
57+
58+
&--empty::before {
59+
content: attr(data-placeholder);
60+
position: absolute;
61+
color: var(--vscode-input-placeholderForeground);
62+
pointer-events: none;
63+
}
64+
}

‎packages/ui/src/components/editor/common/Textarea/Textarea.stories.tsx‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ export const WithValue = () => {
3232
/>
3333
)
3434
}
35+
36+
export const WithAction = () => {
37+
const [value, set_value] = useState('Some text with an action')
38+
39+
return (
40+
<Textarea
41+
value={value}
42+
on_change={set_value}
43+
placeholder="Text area with action"
44+
min_rows={3}
45+
action_icon="clear-all"
46+
action_title="Clear text"
47+
on_action_click={() => set_value('')}
48+
/>
49+
)
50+
}

0 commit comments

Comments
 (0)