MM-26722 check for switchKeyboardForCodeBlocks only on iOS 12+ (#4546)

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
This commit is contained in:
Mattermost Build 2020-07-07 19:13:53 +02:00 committed by GitHub
parent 457df4bb4d
commit f4422db6d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 15 deletions

View file

@ -184,22 +184,25 @@ export function escapeRegex(text) {
}
export function switchKeyboardForCodeBlocks(value, cursorPosition) {
const regexForCodeBlock = /^```$(.*?)^```$|^```$(.*)/gms;
if (Platform.OS === 'ios' && parseInt(Platform.Version, 10) >= 12) {
const regexForCodeBlock = /^```$(.*?)^```$|^```$(.*)/gms;
const matches = [];
let nextMatch;
while ((nextMatch = regexForCodeBlock.exec(value)) !== null) {
matches.push({
startOfMatch: regexForCodeBlock.lastIndex - nextMatch[0].length,
endOfMatch: regexForCodeBlock.lastIndex + 1,
});
const matches = [];
let nextMatch;
while ((nextMatch = regexForCodeBlock.exec(value)) !== null) {
matches.push({
startOfMatch: regexForCodeBlock.lastIndex - nextMatch[0].length,
endOfMatch: regexForCodeBlock.lastIndex + 1,
});
}
const cursorIsInsideCodeBlock = matches.some((match) => cursorPosition >= match.startOfMatch && cursorPosition <= match.endOfMatch);
// 'email-address' keyboardType prevents iOS emdash autocorrect
if (cursorIsInsideCodeBlock) {
return 'email-address';
}
}
const cursorIsInsideCodeBlock = matches.some((match) => cursorPosition >= match.startOfMatch && cursorPosition <= match.endOfMatch);
// 'email-address' keyboardType prevents iOS emdash autocorrect
if (cursorIsInsideCodeBlock) {
return 'email-address';
}
return 'default';
}

View file

@ -1,6 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {Platform} from 'react-native';
import {switchKeyboardForCodeBlocks} from './markdown';
describe('switchKeyboardForCodeBlocks', () => {
@ -67,3 +68,11 @@ describe('switchKeyboardForCodeBlocks', () => {
});
}
});
describe('switchKeyboardForCodeBlocks for iOS 11', () => {
it('Should return default keyboard', () => {
Platform.Version = 11;
expect(switchKeyboardForCodeBlocks('```\ntest\n```test', 12)).toEqual('default');
Platform.Version = 12;
});
});

View file

@ -94,7 +94,11 @@ jest.doMock('react-native', () => {
};
return Object.setPrototypeOf({
Platform,
Platform: {
...Platform,
OS: 'ios',
Version: 12,
},
StyleSheet,
ViewPropTypes,
PermissionsAndroid,