Skip to content

Commit

Permalink
Merge pull request #7 from miroapp/MDOCS-1385
Browse files Browse the repository at this point in the history
MDOCS-1385 hide cursor on compositionupdate events
  • Loading branch information
bmakuh authored Nov 14, 2024
2 parents 2f1efb8 + 16a1cd0 commit d684abf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/quill/src/blots/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Cursor extends EmbedBlot {
}

restore(): EmbedContextRange | null {
if (this.selection.composing || this.parent == null) return null;
if (this.parent == null) return null;
const range = this.selection.getNativeRange();
// Browser may push down styles/nodes inside the cursor blot.
// https://dvcs.w3.org/hg/editing/raw-file/tip/editing.html#push-down-values
Expand Down
10 changes: 10 additions & 0 deletions packages/quill/src/core/composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ class Composition {
}
});

this.scroll.domNode.addEventListener('compositionupdate', () => {
if (!this.scroll.batch) {
this.handleCompositionUpdate();
}
});

this.scroll.domNode.addEventListener('compositionend', (event) => {
if (this.isComposing) {
// Webkit makes DOM changes after compositionend, so we use microtask to
Expand All @@ -45,6 +51,10 @@ class Composition {
}
}

private handleCompositionUpdate() {
this.emitter.emit(Emitter.events.COMPOSITION_UPDATE);
}

private handleCompositionEnd(event: CompositionEvent) {
this.emitter.emit(Emitter.events.COMPOSITION_BEFORE_END, event);
// HACK: There is a bug in the safari browser in mobile devices and when we finish typing
Expand Down
1 change: 1 addition & 0 deletions packages/quill/src/core/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Emitter extends EventEmitter<string> {
TEXT_CHANGE: 'text-change',
COMPOSITION_BEFORE_START: 'composition-before-start',
COMPOSITION_START: 'composition-start',
COMPOSITION_UPDATE: 'composition-update',
COMPOSITION_BEFORE_END: 'composition-before-end',
COMPOSITION_END: 'composition-end',
} as const;
Expand Down
12 changes: 12 additions & 0 deletions packages/quill/src/core/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ class Selection {
this.emitter.on(Emitter.events.COMPOSITION_BEFORE_START, () => {
this.composing = true;
});
this.emitter.on(Emitter.events.COMPOSITION_UPDATE, () => {
if (this.cursor.parent) {
const range = this.cursor.restore();
if (!range) return;
this.setNativeRange(
range.startNode,
range.startOffset,
range.endNode,
range.endOffset,
);
}
});
this.emitter.on(Emitter.events.COMPOSITION_END, () => {
this.composing = false;
if (this.cursor.parent) {
Expand Down
4 changes: 2 additions & 2 deletions packages/quill/test/unit/ui/picker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ describe('Picker', () => {
).toEqual('false');
});

test('aria attributes toggle correctly when the picker is opened via mousedown', () => {
test('aria attributes toggle correctly when the picker is opened via pointerdown', () => {
const { pickerSelector } = setup();
const pickerLabel = pickerSelector.querySelector('.ql-picker-label');
pickerLabel?.dispatchEvent(
new Event('mousedown', {
new Event('pointerdown', {
bubbles: true,
cancelable: true,
}),
Expand Down

0 comments on commit d684abf

Please sign in to comment.