From 4d60e21b2ab685040a87c169c621b01f06d3c355 Mon Sep 17 00:00:00 2001 From: David Whitaker Date: Wed, 8 Jun 2022 20:03:05 -0400 Subject: [PATCH] Add fix for contenteditable in safari (#354) --- .../src/ContenteditableEditor.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/textcomplete-contenteditable/src/ContenteditableEditor.ts b/packages/textcomplete-contenteditable/src/ContenteditableEditor.ts index d83b096e..21099f6f 100644 --- a/packages/textcomplete-contenteditable/src/ContenteditableEditor.ts +++ b/packages/textcomplete-contenteditable/src/ContenteditableEditor.ts @@ -4,7 +4,6 @@ import { isSafari, getLineHeightPx } from "@textcomplete/utils" export class ContenteditableEditor extends Editor { constructor(public readonly el: HTMLElement) { super() - if (isSafari()) return this.startListening() } @@ -23,16 +22,18 @@ export class ContenteditableEditor extends Editor { if (before != null && after != null) { const replace = searchResult.replace(before, after) if (Array.isArray(replace)) { + let beforeCursor = replace[0] + if (isSafari()) beforeCursor = beforeCursor.replace(before, "") const range = this.getRange() range.selectNode(range.startContainer) this.el.ownerDocument.execCommand( "insertText", false, - replace[0] + replace[1] + beforeCursor + replace[1] ) range.detach() const newRange = this.getRange() - newRange.setStart(newRange.startContainer, replace[0].length) + newRange.setStart(newRange.startContainer, beforeCursor.length) newRange.collapse(true) } }