Skip to content

Commit

Permalink
ELEMENTS-1372: fix caret when inserting first row char
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabez0r committed Jun 1, 2021
1 parent 8936de0 commit 8e94dc8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions core/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import equal from 'deep-equal';
import Emitter from './emitter';
import logger from './logger';
import './shadow-selection-polyfill';
import { ShadowSelection } from './shadow-selection-polyfill';

const debug = logger('quill:selection');

Expand Down Expand Up @@ -33,9 +34,13 @@ class Selection {
setTimeout(this.update.bind(this, Emitter.sources.USER), 1);
}
});
this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, () => {
this.emitter.on(Emitter.events.SCROLL_BEFORE_UPDATE, async (_, mutations) => {
if (!this.hasFocus()) return;
const native = this.getNativeRange();
// when need to hack the offset on Safari, when we are dealing with the first character of a row
const hackOffset = this.rootDocument.getSelection() instanceof ShadowSelection &&
mutations.some((a) => a.type === 'characterData' && a.oldValue === '') &&
native.start.offset === native.end.offset;
if (native == null) return;
if (native.start.node === this.cursor.textNode) return; // cursor.restore() will handle
this.emitter.once(Emitter.events.SCROLL_UPDATE, () => {
Expand All @@ -46,9 +51,9 @@ class Selection {
) {
this.setNativeRange(
native.start.node,
native.start.offset,
native.start.offset + (hackOffset ? 1 : 0),
native.end.node,
native.end.offset,
native.end.offset + (hackOffset ? 1 : 0),
);
}
this.update(Emitter.sources.SILENT);
Expand Down
2 changes: 1 addition & 1 deletion core/shadow-selection-polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const SUPPORTS_BEFORE_INPUT = typeof window.InputEvent.prototype.getTargetRanges
const IS_FIREFOX = window.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

let processing = false;
class ShadowSelection {
export class ShadowSelection {
constructor() {
this._ranges = [];
}
Expand Down

0 comments on commit 8e94dc8

Please sign in to comment.