Skip to content

Commit

Permalink
Multiline scroll fix (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszGrajdek authored Mar 8, 2024
1 parent 1132fcd commit 1171452
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/MarkdownTextInput.web.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {StyleSheet} from 'react-native';
import * as ParseUtils from './web/parserUtils';
import * as CursorUtils from './web/cursorUtils';
import * as StyleUtils from './styleUtils';
import * as BrowserUtils from './web/browserUtils';
import type * as MarkdownTextInputDecoratorViewNativeComponent from './MarkdownTextInputDecoratorViewNativeComponent';
import './web/MarkdownTextInput.css';
import InputHistory from './web/InputHistory';
Expand Down Expand Up @@ -380,6 +381,14 @@ const MarkdownTextInput = React.forwardRef<TextInput, MarkdownTextInputProps>(
// We need to change normal behavior of "Enter" key to insert a line breaks, to prevent wrapping contentEditable text in <div> tags.
// Thanks to that in every situation we have proper amount of new lines in our parsed text. Without it pressing enter in empty lines will add 2 more new lines.
document.execCommand('insertLineBreak');

const range = window.getSelection();
if (range && !BrowserUtils.isFirefox) {
const scrollMarkerNode = document.createElement('div');
range.getRangeAt(0).insertNode(scrollMarkerNode);
scrollMarkerNode.scrollIntoView();
scrollMarkerNode.remove();
}
}

if (!e.shiftKey && ((shouldBlurOnSubmit && hostNode !== null) || !multiline)) {
Expand Down
4 changes: 4 additions & 0 deletions src/web/browserUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const isFirefox = navigator.userAgent.toLowerCase().includes('firefox');
const isChromium = 'chrome' in window;

export {isFirefox, isChromium};
7 changes: 3 additions & 4 deletions src/web/parserUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as CursorUtils from './cursorUtils';
import type * as StyleUtilsTypes from '../styleUtils';
import * as BrowserUtils from './browserUtils';

type PartialMarkdownStyle = StyleUtilsTypes.PartialMarkdownStyle;

Expand Down Expand Up @@ -172,8 +173,6 @@ function moveCursor(isFocused: boolean, alwaysMoveCursorToTheEnd: boolean, curso
}
}

const isChromium = 'chrome' in window;

function parseText(target: HTMLElement, text: string, curosrPositionIndex: number | null, markdownStyle: PartialMarkdownStyle = {}, alwaysMoveCursorToTheEnd = false) {
const targetElement = target;

Expand Down Expand Up @@ -202,12 +201,12 @@ function parseText(target: HTMLElement, text: string, curosrPositionIndex: numbe
targetElement.innerText = '';
target.appendChild(dom);

if (isChromium) {
if (BrowserUtils.isChromium) {
moveCursor(isFocused, alwaysMoveCursorToTheEnd, cursorPosition, target);
}
}

if (!isChromium) {
if (!BrowserUtils.isChromium) {
moveCursor(isFocused, alwaysMoveCursorToTheEnd, cursorPosition, target);
}
}
Expand Down

0 comments on commit 1171452

Please sign in to comment.