RisingOrange / anki-enhanced-cloze

20 stars 4 forks source link

Keyboard shortcuts in ankidroid are invalid, I want to support userjs1-8 to use custom shortcuts in Android iOS #95

Closed hihvan closed 4 months ago

hihvan commented 8 months ago

Great plugin, thank you to the author.

It would be great if it supported keyboard shortcuts or userjs1-8 custom buttons on Android and iOS as well., it would be perfect.. Then I can use a game controller or custom gestures to quickly show/hide clozes.

I have tried adding the following content to the card template:

var userJs1 =revealOneClozeOfAType("genuine")
var userJs2 =revealOneClozeOfAType("pseudo")

However, it did not work. I also tried connecting a keyboard to my Android phone and pressing "j" and "k", but it did not work either.

sulisu commented 4 months ago

There is a related post in ankiweb forum: https://forums.ankiweb.net/t/how-can-i-use-user-action-by-configuring-javascript/31144

In desktop Anki, go to Tools--Manage Note Types, select "Enhanced Cloze 2.1 v2", press "Cards". Add two lines

        globalThis["userJs1"] = () => revealOneClozeOfAType("genuine");
        globalThis["userJs2"] = () => revealOneClozeOfAType("pseudo")

after following codes

        function revealOneClozeOfAType(clozeType) {
            if (!["genuine", "pseudo"].includes(clozeType)) {
                console.log(`clozeType has unexpected value: ${clozeType}`)
            }

            if (!$(`.${clozeType}-cloze[show-state="hint"]`).length) {
                $('#no-more-cloze').animate({
                    display: "toggle",
                }, 500);
                return
            }

            var hiddenClozes = $(`.${clozeType}-cloze[show-state="hint"]`)
            if (hiddenClozes.length != 0) {
                revealCloze(hiddenClozes[0]);
            }
        }

Save and sync to ankidroid, then you can map controller key to user action 1 and 2 to reveal cloze in cards.

hihvan commented 4 months ago

@sulisu 感谢您的分享,您的教程非常详细,我非常感谢你。:)爱来自瓷器❤️。极大的方便了我的生活。 谢谢。没有你的帮助,我无法解决这个困难。 我无法理解代码。经过我的尝试,隐藏卡片中的完形填空: 在代码:

function toggleAllClozesOfAType(clozeType) {
            if (!["genuine", "pseudo"].includes(clozeType)) {
                console.log(`clozeType has unexpected value: ${clozeType}`)
            }

            var allRevealed = !$(`.${clozeType}-cloze[show-state="hint"`).length
            $(`.${clozeType}-cloze`).each(function (index, elem) {
                toggleCloze(elem, allRevealed ? "hint" : "answer");
            })
        }

后继续增加2行:

        globalThis["userJs3"] = () => toggleAllClozesOfAType("genuine");
        globalThis["userJs4"] = () => toggleAllClozesOfAType("pseudo");