Skip to content

Commit b9ed2f3

Browse files
committed
moved selection to cursorUtils
1 parent 9920366 commit b9ed2f3

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

src/MarkdownTextInput.web.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
174174
const pasteRef = useRef<boolean>(false);
175175
const divRef = useRef<HTMLDivElement | null>(null);
176176
const currentlyFocusedField = useRef<HTMLDivElement | null>(null);
177-
const contentSelection = useRef<ParseUtils.Selection | null>(null);
177+
const contentSelection = useRef<CursorUtils.Selection | null>(null);
178178
const className = `react-native-live-markdown-input-${multiline ? 'multiline' : 'singleline'}`;
179179
const history = useRef<InputHistory>();
180180
const dimensions = React.useRef<Dimensions | null>(null);
@@ -298,15 +298,15 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
298298
[onSelectionChange, setEventProps],
299299
);
300300

301-
const updateRefSelectionVariables = useCallback((newSelection: ParseUtils.Selection) => {
301+
const updateRefSelectionVariables = useCallback((newSelection: CursorUtils.Selection) => {
302302
const {start, end} = newSelection;
303303
const markdownHTMLInput = divRef.current as HTMLInputElement;
304304
markdownHTMLInput.selectionStart = start;
305305
markdownHTMLInput.selectionEnd = end;
306306
}, []);
307307

308308
const updateSelection = useCallback(
309-
(e: SyntheticEvent<HTMLDivElement> | null = null, predefinedSelection: ParseUtils.Selection | null = null) => {
309+
(e: SyntheticEvent<HTMLDivElement> | null = null, predefinedSelection: CursorUtils.Selection | null = null) => {
310310
if (!divRef.current) {
311311
return;
312312
}
@@ -636,7 +636,7 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
636636
return;
637637
}
638638

639-
const newSelection: ParseUtils.Selection = {start: selection.start, end: selection.end ?? selection.start};
639+
const newSelection: CursorUtils.Selection = {start: selection.start, end: selection.end ?? selection.start};
640640
contentSelection.current = newSelection;
641641
updateRefSelectionVariables(newSelection);
642642
CursorUtils.setCursorPosition(divRef.current, newSelection.start, newSelection.end);

src/web/cursorUtils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import * as BrowserUtils from './browserUtils';
22

3+
type Selection = {
4+
start: number;
5+
end: number;
6+
};
7+
38
let prevTextLength: number | undefined;
49

510
function getPrevTextLength() {
@@ -162,4 +167,4 @@ function scrollCursorIntoView(target: HTMLInputElement) {
162167
}
163168
}
164169

165-
export {getCurrentCursorPosition, moveCursorToEnd, setCursorPosition, setPrevText, removeSelection, scrollCursorIntoView, getPrevTextLength};
170+
export {getCurrentCursorPosition, moveCursorToEnd, setCursorPosition, setPrevText, removeSelection, scrollCursorIntoView, getPrevTextLength, Selection};

src/web/parserUtils.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ type TextChangeMetrics = {
3333
before: number;
3434
};
3535

36-
type Selection = {
37-
start: number;
38-
end: number;
39-
};
40-
4136
function addStyling(targetElement: HTMLElement, type: MarkdownType, markdownStyle: PartialMarkdownStyle) {
4237
const node = targetElement;
4338
switch (type) {
@@ -250,7 +245,7 @@ function parseText(target: HTMLElement, text: string, cursorPositionIndex: numbe
250245
* This is to align the onChange event with the native counter part:
251246
* - https://github.com/facebook/react-native/pull/45248
252247
*/
253-
function calculateInputMetrics(inputType: string, prevSelection: Selection, prevTextLength: number, normalizedText: string, cursorPosition: number | null): TextChangeMetrics {
248+
function calculateInputMetrics(inputType: string, prevSelection: CursorUtils.Selection, prevTextLength: number, normalizedText: string, cursorPosition: number | null): TextChangeMetrics {
254249
// The new text is between the prev start selection and the new end selection, can be empty
255250
const addedText = normalizedText.slice(prevSelection.start, cursorPosition ?? 0);
256251
// The length of the text that replaced the "before" text
@@ -282,4 +277,4 @@ function calculateInputMetrics(inputType: string, prevSelection: Selection, prev
282277

283278
export {parseText, parseRangesToHTMLNodes, calculateInputMetrics};
284279

285-
export type {MarkdownRange, MarkdownType, Selection};
280+
export type {MarkdownRange, MarkdownType};

0 commit comments

Comments
 (0)