Fix 'same cloze' shortcut on macOS

https://forums.ankiweb.net/t/mac-shortcut-for-cloze-deletion-same-card/63785
This commit is contained in:
Damien Elmes 2025-07-18 18:12:30 +07:00
parent 0b30155c90
commit 278a84f8d2
2 changed files with 25 additions and 4 deletions

View file

@ -4,7 +4,7 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
--> -->
<script lang="ts"> <script lang="ts">
import * as tr from "@generated/ftl"; import * as tr from "@generated/ftl";
import { isApplePlatform } from "@tslib/platform"; import { chromiumVersion, isApplePlatform } from "@tslib/platform";
import { getPlatformString } from "@tslib/shortcuts"; import { getPlatformString } from "@tslib/shortcuts";
import { createEventDispatcher } from "svelte"; import { createEventDispatcher } from "svelte";
import { get } from "svelte/store"; import { get } from "svelte/store";
@ -22,9 +22,13 @@ License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
const { focusedInput, fields } = noteEditorContext.get(); const { focusedInput, fields } = noteEditorContext.get();
// Workaround for Cmd+Option+Shift+C not working on macOS. The keyup approach works // Workaround for Cmd+Option+Shift+C not working on macOS on older Chromium
// on Linux as well, but fails on Windows. // versions.
const event = isApplePlatform() ? "keyup" : "keydown"; const chromiumVer = chromiumVersion();
const event =
isApplePlatform() && chromiumVer != null && chromiumVer <= 112
? "keyup"
: "keydown";
const clozePattern = /\{\{c(\d+)::/gu; const clozePattern = /\{\{c(\d+)::/gu;
function getCurrentHighestCloze(increment: boolean): number { function getCurrentHighestCloze(increment: boolean): number {

View file

@ -13,3 +13,20 @@ export function isApplePlatform(): boolean {
export function isDesktop(): boolean { export function isDesktop(): boolean {
return !(/iphone|ipad|ipod|android/i.test(window.navigator.userAgent)); return !(/iphone|ipad|ipod|android/i.test(window.navigator.userAgent));
} }
export function chromiumVersion(): number | null {
const userAgent = window.navigator.userAgent;
// Check if it's a Chromium-based browser (Chrome, Edge, Opera, etc.)
// but exclude Safari which also contains "Chrome" in its user agent
if (userAgent.includes("Safari") && !userAgent.includes("Chrome")) {
return null; // Safari
}
const chromeMatch = userAgent.match(/Chrome\/(\d+)/);
if (chromeMatch) {
return parseInt(chromeMatch[1], 10);
}
return null; // Not a Chromium-based browser
}