Skip to content

Commit

Permalink
Add fix for contenteditable in safari (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
damwhit authored Jun 9, 2022
1 parent 16c6684 commit 4d60e21
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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)
}
}
Expand Down

0 comments on commit 4d60e21

Please sign in to comment.