Skip to content

Commit 40dee61

Browse files
committed
Replace textarea with custom Notes component in prompt view
- textarea for 'notes' on Home (prompt view) should be a separte component, with the same border radius and border width as the prompt field ├── apps │ └── editor │ └── src │ └── views │ └── prompt │ └── frontend │ ├── Home │ │ ├── Home.module.scss │ │ ├── Home.tsx │ │ ├── hooks │ │ │ └── use-notes.ts │ │ └── index.ts │ ├── Prompt.tsx │ └── i18n │ └── translations │ └── home.ts └── packages └── ui └── src └── components └── editor ├── common │ ├── Textarea │ │ ├── Textarea.module.scss │ │ ├── Textarea.tsx │ │ └── index.ts │ └── prompts │ └── PromptField │ ├── PromptField.module.scss │ ├── PromptField.tsx │ └── index.ts └── prompt-view └── Notes ├── Notes.module.scss ├── Notes.tsx └── index.ts
1 parent e9b108d commit 40dee61

4 files changed

Lines changed: 231 additions & 4 deletions

File tree

‎apps/editor/src/views/prompt/frontend/Home/Home.tsx‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { Tasks as UiTasks } from '@ui/components/editor/prompt-view/Tasks'
1414
import { use_tasks } from './hooks/use-tasks'
1515
import { use_notes } from './hooks/use-notes'
1616
import { use_has_scrolled_past_target_button } from './hooks/use-has-scrolled-past-mode-button'
17-
import { Textarea as UiTextarea } from '@ui/components/editor/common/Textarea'
17+
import { Notes as UiNotes } from '@ui/components/editor/prompt-view/Notes'
1818
import { use_keyboard_shortcuts } from './hooks/use-keyboard-shortcuts'
1919
import {
2020
ApiPromptType,
@@ -253,12 +253,10 @@ export const Home: React.FC<Props> = (props) => {
253253

254254
const notes_section = (
255255
<div className={styles.inner__notes}>
256-
<UiTextarea
256+
<UiNotes
257257
value={notes_for_root}
258258
on_change={(v) => handle_note_change(active_root, v)}
259259
placeholder={t('home.notes.placeholder')}
260-
min_rows={4}
261-
blur_on_enter={false}
262260
/>
263261
</div>
264262
)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
.wrapper {
2+
position: relative;
3+
width: 100%;
4+
border: 0.5px solid var(--cwc-border-color);
5+
background-color: var(--vscode-input-background);
6+
color: var(--vscode-input-foreground);
7+
padding: 5px 7px;
8+
border-radius: 10px;
9+
cursor: text;
10+
box-sizing: border-box;
11+
12+
&:hover {
13+
border-color: var(--cwc-border-color-hover);
14+
}
15+
16+
&:focus-within {
17+
border-color: var(--vscode-focusBorder);
18+
}
19+
}
20+
21+
.textarea {
22+
outline: none;
23+
border: none;
24+
background: transparent;
25+
color: inherit;
26+
padding: 0;
27+
margin: 0;
28+
white-space: pre-wrap;
29+
overflow-wrap: break-word;
30+
word-break: break-word;
31+
line-height: 1.4;
32+
min-height: calc(4 * 1.4em);
33+
position: relative;
34+
35+
&--empty::before {
36+
content: attr(data-placeholder);
37+
position: absolute;
38+
color: var(--vscode-input-placeholderForeground);
39+
pointer-events: none;
40+
}
41+
}
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
import React from 'react'
2+
import cn from 'classnames'
3+
import styles from './Notes.module.scss'
4+
import {
5+
get_caret_position_from_div,
6+
set_caret_position_for_div
7+
} from '../../common/prompts/shared/symbols'
8+
9+
type Props = {
10+
value: string
11+
placeholder?: string
12+
on_change: (value: string) => void
13+
}
14+
15+
export const Notes: React.FC<Props> = (props) => {
16+
const internal_ref = React.useRef<HTMLDivElement | null>(null)
17+
const [undo_stack, set_undo_stack] = React.useState<
18+
{ value: string; caret_pos: number }[]
19+
>([])
20+
const [redo_stack, set_redo_stack] = React.useState<
21+
{ value: string; caret_pos: number }[]
22+
>([])
23+
const caret_pos_ref = React.useRef(0)
24+
25+
React.useEffect(() => {
26+
const on_selection_change = () => {
27+
if (
28+
document.activeElement === internal_ref.current &&
29+
internal_ref.current
30+
) {
31+
caret_pos_ref.current = get_caret_position_from_div(
32+
internal_ref.current
33+
)
34+
}
35+
}
36+
document.addEventListener('selectionchange', on_selection_change)
37+
return () =>
38+
document.removeEventListener('selectionchange', on_selection_change)
39+
}, [])
40+
41+
React.useEffect(() => {
42+
if (!internal_ref.current) return
43+
44+
const current_inner_text = internal_ref.current.innerText || ''
45+
if (
46+
current_inner_text !== props.value &&
47+
!(current_inner_text === '\n' && !props.value)
48+
) {
49+
internal_ref.current.innerText = props.value || ''
50+
}
51+
}, [props.value])
52+
53+
const update_value = (new_value: string) => {
54+
if (new_value === props.value) return
55+
set_undo_stack((prev) => [
56+
...prev,
57+
{ value: props.value || '', caret_pos: caret_pos_ref.current }
58+
])
59+
set_redo_stack([])
60+
props.on_change(new_value)
61+
}
62+
63+
const handle_input = (e: React.FormEvent<HTMLDivElement>) => {
64+
let new_value = e.currentTarget.innerText || ''
65+
if (new_value === '\n') {
66+
new_value = ''
67+
}
68+
update_value(new_value)
69+
}
70+
71+
const handle_key_down = (e: React.KeyboardEvent<HTMLDivElement>) => {
72+
if (
73+
(e.ctrlKey || e.metaKey) &&
74+
!e.shiftKey &&
75+
e.key.toLowerCase() === 'z'
76+
) {
77+
e.preventDefault()
78+
if (undo_stack.length > 0) {
79+
const prev_entry = undo_stack[undo_stack.length - 1]
80+
set_undo_stack((prev) => prev.slice(0, -1))
81+
set_redo_stack((prev) => [
82+
...prev,
83+
{ value: props.value || '', caret_pos: caret_pos_ref.current }
84+
])
85+
props.on_change(prev_entry.value)
86+
setTimeout(() => {
87+
if (internal_ref.current) {
88+
set_caret_position_for_div(
89+
internal_ref.current,
90+
prev_entry.caret_pos
91+
)
92+
}
93+
}, 0)
94+
}
95+
return
96+
}
97+
98+
if (
99+
((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'y') ||
100+
((e.ctrlKey || e.metaKey) && e.shiftKey && e.key.toLowerCase() === 'z')
101+
) {
102+
e.preventDefault()
103+
if (redo_stack.length > 0) {
104+
const next_entry = redo_stack[redo_stack.length - 1]
105+
set_redo_stack((prev) => prev.slice(0, -1))
106+
set_undo_stack((prev) => [
107+
...prev,
108+
{ value: props.value || '', caret_pos: caret_pos_ref.current }
109+
])
110+
props.on_change(next_entry.value)
111+
setTimeout(() => {
112+
if (internal_ref.current) {
113+
set_caret_position_for_div(
114+
internal_ref.current,
115+
next_entry.caret_pos
116+
)
117+
}
118+
}, 0)
119+
}
120+
return
121+
}
122+
123+
if (e.key === 'Escape' && !e.shiftKey) {
124+
e.preventDefault()
125+
e.currentTarget.blur()
126+
} else if (e.key === 'Enter') {
127+
e.preventDefault()
128+
if (!document.execCommand('insertText', false, '\n')) {
129+
const selection = window.getSelection()
130+
if (selection && selection.rangeCount > 0) {
131+
const range = selection.getRangeAt(0)
132+
range.deleteContents()
133+
const text_node = document.createTextNode('\n')
134+
range.insertNode(text_node)
135+
range.collapse(false)
136+
selection.removeAllRanges()
137+
selection.addRange(range)
138+
let new_value = e.currentTarget.innerText || ''
139+
if (new_value === '\n') new_value = ''
140+
update_value(new_value)
141+
}
142+
}
143+
}
144+
}
145+
146+
const handle_paste = (e: React.ClipboardEvent<HTMLDivElement>) => {
147+
e.preventDefault()
148+
const text = e.clipboardData.getData('text/plain')
149+
if (!document.execCommand('insertText', false, text)) {
150+
const selection = window.getSelection()
151+
if (selection && selection.rangeCount > 0) {
152+
const range = selection.getRangeAt(0)
153+
range.deleteContents()
154+
const text_node = document.createTextNode(text)
155+
range.insertNode(text_node)
156+
range.collapse(false)
157+
selection.removeAllRanges()
158+
selection.addRange(range)
159+
let new_value = e.currentTarget.innerText || ''
160+
if (new_value === '\n') new_value = ''
161+
update_value(new_value)
162+
}
163+
}
164+
}
165+
166+
return (
167+
<div
168+
className={styles.wrapper}
169+
onClick={() => internal_ref.current?.focus()}
170+
>
171+
<div
172+
ref={internal_ref}
173+
contentEditable={true}
174+
suppressContentEditableWarning={true}
175+
className={cn(styles.textarea, {
176+
[styles['textarea--empty']]: !props.value
177+
})}
178+
onInput={handle_input}
179+
onKeyDown={handle_key_down}
180+
onPaste={handle_paste}
181+
data-placeholder={props.placeholder}
182+
role="textbox"
183+
aria-multiline="true"
184+
/>
185+
</div>
186+
)
187+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './Notes'

0 commit comments

Comments
 (0)