Skip to content

Commit

Permalink
Fix language auto detection on Safari Webkit which was broken
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Dec 25, 2023
1 parent 594e23c commit fcf2c63
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/editor/language-detection/autodetect.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import { LANGUAGE_CHANGE } from "../annotation";

const GUESSLANG_TO_TOKEN = Object.fromEntries(LANGUAGES.map(l => [l.guesslang,l.token]))

function requestIdleCallbackCompat(cb) {
if (window.requestIdleCallback) {
return window.requestIdleCallback(cb)
} else {
return setTimeout(cb, 0)
}
}

function cancelIdleCallbackCompat(id) {
if (window.cancelIdleCallback) {
window.cancelIdleCallback(id)
} else {
clearTimeout(id)
}
}

export function languageDetection(getView) {
const previousBlockContent = {}
Expand Down Expand Up @@ -45,11 +60,11 @@ export function languageDetection(getView) {
const plugin = EditorView.updateListener.of(update => {
if (update.docChanged) {
if (idleCallbackId !== null) {
cancelIdleCallback(idleCallbackId)
cancelIdleCallbackCompat(idleCallbackId)
idleCallbackId = null
}

idleCallbackId = requestIdleCallback(() => {
idleCallbackId = requestIdleCallbackCompat(() => {
idleCallbackId = null

const range = update.state.selection.asSingle().ranges[0]
Expand Down

0 comments on commit fcf2c63

Please sign in to comment.