-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
index.js
45 lines (41 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import getNumberOfLines from './getNumberOfLines';
import updateNumberOfLines from './updateNumberOfLines';
import * as DeviceCapabilities from '../DeviceCapabilities';
/**
* Replace substring between selection with a text.
* @param {String} text
* @param {Object} selection
* @param {String} textToInsert
* @returns {String}
*/
function insertText(text, selection, textToInsert) {
return text.slice(0, selection.start) + textToInsert + text.slice(selection.end, text.length);
}
/**
* Check whether we can skip trigger hotkeys on some specific devices.
* @param {Boolean} isSmallScreenWidth
* @param {Boolean} isKeyboardShown
* @returns {Boolean}
*/
function canSkipTriggerHotkeys(isSmallScreenWidth, isKeyboardShown) {
// Do not trigger actions for mobileWeb or native clients that have the keyboard open
// because for those devices, we want the return key to insert newlines rather than submit the form
return (isSmallScreenWidth && DeviceCapabilities.canUseTouchScreen()) || isKeyboardShown;
}
/**
* Returns the length of the common suffix between two input strings.
* The common suffix is the number of characters shared by both strings
* at the end (suffix) until a mismatch is encountered.
*
* @param {string} str1
* @param {string} str2
* @returns {number} The length of the common suffix between the strings.
*/
function getCommonSuffixLength(str1, str2) {
let i = 0;
while (str1[str1.length - 1 - i] === str2[str2.length - 1 - i]) {
i++;
}
return i;
}
export {getNumberOfLines, updateNumberOfLines, insertText, canSkipTriggerHotkeys, getCommonSuffixLength};