Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A-11847: Fix long pressed accents on Chrome+MacOS #8

Draft
wants to merge 2 commits into
base: 0-47-merged-backports
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"to-snake-case": "^1.0.0",
"uglifyjs-webpack-plugin": "^1.1.8",
"webpack": "^3.11.0",
"webpack-cli": "^4.7.0",
"webpack-dev-server": "^3.1.11"
},
"peerDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions packages/slate-dev-environment/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ function getAndroidApiVersion() {
* @type {Boolean}
*/

console.log('index.js', { browser, os })

export const IS_CHROME = browser === 'chrome'
export const IS_OPERA = browser === 'opera'
export const IS_FIREFOX = browser === 'firefox'
Expand Down
37 changes: 35 additions & 2 deletions packages/slate-react/src/plugins/dom/after.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import Debug from 'debug'
import Hotkeys from 'slate-hotkeys'
import Plain from 'slate-plain-serializer'
import getWindow from 'get-window'
import { IS_IOS, IS_IE, IS_EDGE } from 'slate-dev-environment'
import {
IS_IOS,
IS_IE,
IS_EDGE,
IS_CHROME,
IS_MAC,
} from 'slate-dev-environment'

import cloneFragment from '../../utils/clone-fragment'
import getEventTransfer from '../../utils/get-event-transfer'
Expand Down Expand Up @@ -41,12 +47,39 @@ function AfterPlugin(options = {}) {
const { value } = editor
const isSynthetic = !!event.nativeEvent

console.log('isSynthetic', isSynthetic)

// If the event is synthetic, it's React's polyfill of `beforeinput` that
// isn't a true `beforeinput` event with meaningful information. It only
// gets triggered for character insertions, so we can just insert directly.
if (isSynthetic) {
event.preventDefault()
editor.insertText(event.data)

// COMPAT: On macOS, long press triggers an IME to insert an accent.
// Browsers that implements `beforeinput` event uses the
// `insertReplacementText` and a proper range. Chrome is instead selecting
// the character to replace, right when the input is triggered.
if (IS_MAC && IS_CHROME) {
const window = getWindow(event.target)
const domSelection = window.getSelection()
console.log('IS_MAC && IS_CHROME')
console.log('!domSelection.isCollapsed && value.selection.isCollapsed && !editor.isInCompositionMode()', { 'domSelection.isCollapsed': domSelection.isCollapsed, 'value.selection.isCollapsed': value.selection.isCollapsed, 'editor.isInCompositionMode()': editor.isInCompositionmode() })
if (
!domSelection.isCollapsed &&
value.selection.isCollapsed &&
!editor.isInCompositionMode()
) {
const range = editor.findRange(domSelection)
console.log({ range, 'event.data': event.data })
editor.insertTextAtRange(range, event.data)
} else {
console.log({ 'event.data': event.data })
editor.insertText(event.data)
}
} else {
editor.insertText(event.data)
}

return next()
}

Expand Down
6 changes: 5 additions & 1 deletion packages/slate-react/src/plugins/dom/before.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,10 @@ function BeforePlugin() {
return null
}

function isInCompositionMode() {
return isComposing
}

/**
* Return the plugin.
*
Expand All @@ -504,7 +508,7 @@ function BeforePlugin() {
onKeyDown,
onPaste,
onSelect,
queries: { userActionPerformed },
queries: { userActionPerformed, isInCompositionMode },
commands: { clearUserActionPerformed },
}
}
Expand Down
Loading