mirror of https://github.com/requarks/wiki
parent
f0f71536ab
commit
40ed77b5b8
@ -0,0 +1,21 @@
|
||||
const DEFAULT_CODEMIRROR_INPUT_STYLE = 'contenteditable'
|
||||
|
||||
export function isIOSDevice({ userAgent = '', platform = '', maxTouchPoints = 0 } = {}) {
|
||||
return /iP(?:ad|hone|od)/.test(userAgent) || (platform === 'MacIntel' && maxTouchPoints > 1)
|
||||
}
|
||||
|
||||
export function getCodeMirrorInputStyle(nav = (typeof navigator !== 'undefined' ? navigator : null)) {
|
||||
if (!nav) {
|
||||
return DEFAULT_CODEMIRROR_INPUT_STYLE
|
||||
}
|
||||
|
||||
// iOS Korean IME uses a fragile contenteditable/beforeinput path in Safari/WebKit.
|
||||
// Falling back to the textarea input style avoids broken Hangul composition.
|
||||
return isIOSDevice({
|
||||
userAgent: nav.userAgent,
|
||||
platform: nav.platform,
|
||||
maxTouchPoints: nav.maxTouchPoints || 0
|
||||
}) ? 'textarea' : DEFAULT_CODEMIRROR_INPUT_STYLE
|
||||
}
|
||||
|
||||
export default getCodeMirrorInputStyle
|
||||
@ -0,0 +1,27 @@
|
||||
import { getCodeMirrorInputStyle, isIOSDevice } from './codemirror'
|
||||
|
||||
describe('helpers/codemirror', () => {
|
||||
it('detects classic iOS user agents', () => {
|
||||
expect(isIOSDevice({
|
||||
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
|
||||
platform: 'iPhone',
|
||||
maxTouchPoints: 5
|
||||
})).toBe(true)
|
||||
})
|
||||
|
||||
it('detects iPadOS devices that expose a desktop platform', () => {
|
||||
expect(getCodeMirrorInputStyle({
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
|
||||
platform: 'MacIntel',
|
||||
maxTouchPoints: 5
|
||||
})).toBe('textarea')
|
||||
})
|
||||
|
||||
it('keeps the existing contenteditable path on desktop browsers', () => {
|
||||
expect(getCodeMirrorInputStyle({
|
||||
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36',
|
||||
platform: 'MacIntel',
|
||||
maxTouchPoints: 0
|
||||
})).toBe('contenteditable')
|
||||
})
|
||||
})
|
||||
Loading…
Reference in new issue