MCPcopy Create free account
hub / github.com/dailydotdev/apps / TextareaCommand

Class TextareaCommand

packages/shared/src/lib/textarea.ts:132–230  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

130 `![${filename}]()`;
131
132export class TextareaCommand {
133 private textareaRef: MutableRefObject<HTMLTextAreaElement>;
134
135 constructor(textareaRef: MutableRefObject<HTMLTextAreaElement>) {
136 this.textareaRef = textareaRef;
137 }
138
139 get textarea(): HTMLTextAreaElement {
140 return this.textareaRef.current;
141 }
142
143 private getIsolatedReplacement: TypeReplacementFn = (getReplacement) => {
144 const start = this.textarea.selectionStart;
145 const selection = [start, start];
146 const { replacement, offset } = getReplacement(CursorType.Isolated, {
147 word: '',
148 selection,
149 trailingChar: this.textarea.value[start - 1],
150 leadingChar: this.textarea.value[start + 1],
151 });
152 const result = concatReplacement(this.textarea, selection, replacement);
153 const index = start + replacement.length;
154 return { result, position: offset ?? [index, index], replacement };
155 };
156
157 private getAdjacentReplacement: TypeReplacementFn = (getReplacement) => {
158 const selection = [
159 this.textarea.selectionStart,
160 this.textarea.selectionEnd,
161 ];
162 const [word, start] = getCloseWord(this.textarea, selection);
163 const position = [start, start + word.length];
164 const { replacement, offset } = getReplacement(CursorType.Adjacent, {
165 word,
166 selection: position,
167 });
168 const defaultOffset = start + replacement.length;
169 const result = concatReplacement(this.textarea, position, replacement);
170 const finalPosition = offset ?? [defaultOffset, defaultOffset];
171 return { result, position: finalPosition, replacement };
172 };
173
174 private getHighlightedReplacement: TypeReplacementFn = (getReplacement) => {
175 const start = this.textarea.selectionStart;
176 const selection = [start, this.textarea.selectionEnd];
177 const [highlighted, trailingChar, leadingChar] = getSelectedString(
178 this.textarea,
179 );
180 const { replacement, offset } = getReplacement(CursorType.Highlighted, {
181 word: highlighted,
182 trailingChar,
183 leadingChar,
184 selection,
185 });
186 const result = concatReplacement(this.textarea, selection, replacement);
187 const end = start + replacement.length;
188 return { result, position: offset ?? [end, end], replacement };
189 };

Callers

nothing calls this directly

Calls 4

getReplacementFunction · 0.85
concatReplacementFunction · 0.85
getCloseWordFunction · 0.85
getSelectedStringFunction · 0.85

Tested by

no test coverage detected