From fcf2c630a6196e6367e1768e6e393ab85d7528f0 Mon Sep 17 00:00:00 2001 From: Jonatan Heyman Date: Tue, 26 Dec 2023 00:19:15 +0100 Subject: [PATCH] Fix language auto detection on Safari Webkit which was broken --- src/editor/language-detection/autodetect.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/editor/language-detection/autodetect.js b/src/editor/language-detection/autodetect.js index d39dcd4..92ad144 100644 --- a/src/editor/language-detection/autodetect.js +++ b/src/editor/language-detection/autodetect.js @@ -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 = {} @@ -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]