-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copied from https://github.com/prosemirror/prosemirror-view/blob/1.33.8/src/browser.ts | ||
|
||
const nav = typeof navigator != 'undefined' ? navigator : null | ||
const agent = (nav && nav.userAgent) || '' | ||
|
||
const ie_edge = /Edge\/(\d+)/.exec(agent) | ||
const ie_upto10 = /MSIE \d/.exec(agent) | ||
const ie_11up = /Trident\/(?:[7-9]|\d{2,})\..*rv:(\d+)/.exec(agent) | ||
|
||
const ie = !!(ie_upto10 || ie_11up || ie_edge) | ||
|
||
export const safari = !ie && !!nav && /Apple Computer/.test(nav.vendor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
Plugin, | ||
PluginKey, | ||
type PluginSpec, | ||
type EditorState, | ||
} from 'prosemirror-state'; | ||
import { Decoration, DecorationSet, type EditorView } from 'prosemirror-view'; | ||
|
||
import { safari } from './browser'; | ||
|
||
const key = new PluginKey<boolean>('safari-ime-span'); | ||
|
||
let isComposing = false; | ||
|
||
const spec: PluginSpec<any> = { | ||
key, | ||
props: { | ||
decorations: createDecorations, | ||
handleDOMEvents: { | ||
compositionstart: () => { | ||
isComposing = true; | ||
}, | ||
compositionend: () => { | ||
isComposing = false; | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
function createDecorations(state: EditorState): DecorationSet | undefined { | ||
const { $from, $to, to } = state.selection; | ||
if (isComposing && $from.sameParent($to)) { | ||
const deco = Decoration.widget(to, createSpan, { | ||
ignoreSelection: true, | ||
key: 'safari-ime-span', | ||
}); | ||
return DecorationSet.create(state.doc, [deco]); | ||
} | ||
} | ||
|
||
function createSpan(view: EditorView): HTMLSpanElement { | ||
const span = view.dom.ownerDocument.createElement('span'); | ||
span.className = 'ProseMirror-safari-ime-span'; | ||
return span; | ||
} | ||
|
||
/** | ||
* A plugin as a workaround for a bug in Safari that causes the composition | ||
* based IME to remove the empty HTML element with CSS `position: relative`. | ||
* | ||
* See also https://github.com/ProseMirror/prosemirror/issues/934 | ||
* | ||
* @public @group Plugins | ||
*/ | ||
export const imeSpan = new Plugin(safari ? spec : { key }); |