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

Function getCloseWord

packages/shared/src/lib/textarea.ts:45–73  ·  view source on GitHub ↗
(
  textarea: HTMLTextAreaElement,
  selected: number[],
)

Source from the content-addressed store, hash-verified

43};
44
45export const getCloseWord = (
46 textarea: HTMLTextAreaElement,
47 selected: number[],
48): [string, number] => {
49 const [, end] = selected;
50 const index = textarea.value.substring(0, end).split('\n').length - 1;
51 const lines = textarea.value.split('\n');
52 const line = lines[index];
53 const preLines = lines.slice(0, index).join('\n');
54 const offset = index === 0 ? 0 : 1;
55 const base = end - preLines.length - offset;
56 let lastIndex = 0;
57 let startIndex = 0;
58
59 const found = line.split(' ').find((word) => {
60 startIndex = lastIndex;
61 const current = lastIndex + word.length;
62
63 if (current >= base) {
64 return true;
65 }
66
67 lastIndex = current + 1;
68
69 return false;
70 });
71
72 return [found ?? '', preLines.length + startIndex + offset];
73};
74
75/**
76 * Enum to describe the current cursor's state.

Callers 3

checkMentionFunction · 0.90
checkEmojiFunction · 0.90
TextareaCommandClass · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected