Skip to content

Commit

Permalink
Try without transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ocavue committed Jul 18, 2024
1 parent 6d92d24 commit 0e7cbc2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 1 addition & 1 deletion demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { schema as baseSchema } from 'prosemirror-schema-basic';
import { keymap } from 'prosemirror-keymap';
import { exampleSetup, buildMenuItems } from 'prosemirror-example-setup';
import { MenuItem, Dropdown } from 'prosemirror-menu';
import { imeSpan } from 'prosemirror-safari-ime-span';
import { imeSpan } from '../src/ime';
import {
addColumnAfter,
addColumnBefore,
Expand Down
12 changes: 12 additions & 0 deletions src/ime/browser.ts
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)
55 changes: 55 additions & 0 deletions src/ime/index.ts
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> = {

Check warning on line 15 in src/ime/index.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
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 });

0 comments on commit 0e7cbc2

Please sign in to comment.