Skip to content

Commit 94df350

Browse files
committed
Make compatible with Closet
1 parent ab8b2cf commit 94df350

33 files changed

+324
-328
lines changed

src/anking_notetypes/editor.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def make_insertion_js(field_index: int, text: str) -> str:
9494

9595
cmd = (
9696
f"pycmd(`key:{field_index}:${{getNoteId()}}:{escaped}`); "
97-
f"EditorCloset.setFieldHTML({field_index}, `{escaped}`); "
97+
f"EditorIO.setFieldHTML({field_index}, `{escaped}`); "
9898
)
9999
return cmd
100100

@@ -106,7 +106,7 @@ def insert_into_zero_indexed(editor, text: str) -> None:
106106
if not match or int(match[0]) != 0:
107107
continue
108108

109-
editor.web.eval(f"EditorCloset.insertIntoZeroIndexed(`{text}`, {index}); ")
109+
editor.web.eval(f"EditorIO.insertIntoZeroIndexed(`{text}`, {index}); ")
110110
break
111111

112112

@@ -157,13 +157,13 @@ def add_occlusion_messages(
157157
if isinstance(context, Editor):
158158
editor: Editor = context
159159
global old_occlusion_indices, occlusion_editor_active # pylint: disable=global-statement
160-
if message.startswith("oldOcclusions"):
160+
if message.startswith("ankingOldOcclusions"):
161161
_, _src, index_text = message.split(":", 2)
162162
old_occlusion_indices = process_occlusion_index_text(index_text)
163163

164164
return (True, None)
165165

166-
elif message.startswith("newOcclusions"):
166+
elif message.startswith("ankingNewOcclusions"):
167167
_, _src, index_text = message.split(":", 2)
168168
indices = process_occlusion_index_text(index_text)
169169

@@ -172,7 +172,7 @@ def add_occlusion_messages(
172172

173173
return (True, could_fill)
174174

175-
elif message.startswith("occlusionText"):
175+
elif message.startswith("ankingOcclusionText"):
176176
text = message.split(":", 1)[1]
177177

178178
if occlusion_behavior == "autopaste":
@@ -182,19 +182,19 @@ def add_occlusion_messages(
182182

183183
return (True, None)
184184

185-
elif message == "occlusionEditorActive":
185+
elif message == "ankingOcclusionEditorActive":
186186
occlusion_editor_active = True
187187
return (True, None)
188188

189-
elif message == "occlusionEditorInactive":
189+
elif message == "ankingOcclusionEditorInactive":
190190
occlusion_editor_active = False
191191
return (True, None)
192192

193-
elif message.startswith("closetRefocus"):
193+
elif message.startswith("ankingClosetRefocus"):
194194
refocus(editor)
195195
return (True, None)
196196

197-
elif message.startswith("closetMultipleImages"):
197+
elif message.startswith("ankingClosetMultipleImages"):
198198
showInfo("Cannot start occlusion editor if field contains multiple images.")
199199
return (True, None)
200200

@@ -235,7 +235,7 @@ def toggle_occlusion_mode(editor):
235235

236236
if not is_io_note_type(model["name"]):
237237
showInfo("Please choose an AnKing image occlusion note type")
238-
editor.web.eval("EditorCloset.setInactive()")
238+
editor.web.eval("EditorIO.setInactive()")
239239
return
240240

241241
addon_package = mw.addonManager.addonFromModule(__name__)
@@ -245,7 +245,7 @@ def toggle_occlusion_mode(editor):
245245
if not mw.focusWidget() is editor:
246246
refocus(editor)
247247

248-
editor.web.eval(f"EditorCloset.toggleOcclusionMode({js_path}, {max_code_fields})")
248+
editor.web.eval(f"EditorIO.toggleOcclusionMode({js_path}, {max_code_fields})")
249249

250250

251251
def add_occlusion_button(buttons, editor):
@@ -256,13 +256,15 @@ def add_occlusion_button(buttons, editor):
256256

257257
occlusion_button = editor._addButton( # pylint: disable=protected-access
258258
str(icon_path.absolute()),
259-
"occlude",
259+
"anking_occlude",
260260
f"Put all fields into occlusion mode ({shortcut_as_text})",
261-
id="closetOcclude",
261+
id="ankingOcclude",
262262
disables=False,
263263
)
264264

265-
editor._links["occlude"] = toggle_occlusion_mode # pylint: disable=protected-access
265+
editor._links[ # pylint: disable=protected-access
266+
"anking_occlude"
267+
] = toggle_occlusion_mode
266268
buttons.insert(-1, occlusion_button)
267269

268270

@@ -271,7 +273,8 @@ def wrap_as_option(name: str, code: str, tooltip: str, shortcut_text: str) -> st
271273

272274

273275
def add_buttons(buttons, editor):
274-
editor.occlusion_editor_active = False
276+
global occlusion_editor_active # pylint: disable=global-statement
277+
occlusion_editor_active = False
275278

276279
add_occlusion_button(buttons, editor)
277280

@@ -282,7 +285,7 @@ def add_occlusion_shortcut(cuts, editor):
282285

283286
occlusion_container_pattern = re.compile(
284287
# remove trailing <br> to prevent accumulation
285-
r'<div class="closet-occlusion-container">.*?(<img.*?>).*?</div>(<br>)*'
288+
r'<div class="anking-occlusion-container">.*?(<img.*?>).*?</div>(<br>)*'
286289
)
287290

288291

@@ -296,16 +299,16 @@ def remove_occlusion_code(txt: str, _editor) -> str:
296299

297300

298301
def clear_occlusion_mode(js, _note, _editor):
299-
return f"EditorCloset.clearOcclusionMode().then(() => {{ {js} }}); "
302+
return f"EditorIO.clearOcclusionMode().then(() => {{ {js} }}); "
300303

301304

302305
def refocus(editor):
303306
editor.web.setFocus()
304-
editor.web.eval("EditorCloset.refocus(); ")
307+
editor.web.eval("EditorIO.refocus(); ")
305308

306309

307310
def maybe_refocus(editor):
308-
editor.web.eval("EditorCloset.maybeRefocus(); ")
311+
editor.web.eval("EditorIO.maybeRefocus(); ")
309312

310313

311314
def init_webview():

src/anking_notetypes/note_types/IO-one by one/Back Template.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,11 @@
319319
toggleAllButton.title = `Shortcut: ${window.toggleAllShortcut}`;
320320
revealNextButton.title = `Shortcut: ${window.revealNextShortcut}`;
321321

322-
let rect = document.querySelector(".closet-rect.is-active")
322+
let rect = document.querySelector(".anking-rect.is-active")
323323
if (rect) {
324324
activate(rect)
325325
observer.disconnect()
326-
globalThis.AnkingIORects = document.querySelectorAll(".closet-rect.is-active")
326+
globalThis.AnkingIORects = document.querySelectorAll(".anking-rect.is-active")
327327
for (let rect of globalThis.AnkingIORects) {
328328
rect.addEventListener("click", reveal)
329329
}
@@ -366,7 +366,7 @@
366366

367367

368368
window.toggleNext = function() {
369-
let active = document.querySelector(".closet-rect.is-highlighted")
369+
let active = document.querySelector(".anking-rect.is-highlighted")
370370
if (active) incrementalReveal.call(active)
371371
}
372372

@@ -379,7 +379,7 @@
379379
for (let rect of globalThis.AnkingIORects) {
380380
hide.call(rect)
381381
}
382-
let newActiveRect = document.querySelector(".closet-rect.is-active")
382+
let newActiveRect = document.querySelector(".anking-rect.is-active")
383383
activate(newActiveRect)
384384
} else {
385385
for (let rect of globalThis.AnkingIORects) {
@@ -421,7 +421,7 @@
421421
setupIncrementalIO()
422422
</script>
423423

424-
<script data-name="Closet Setup" data-version="v0.1">
424+
<script data-name="AnKing Closet Setup" data-version="v0.1">
425425
function closetUserLogic(closet, preset, chooseMemory) {
426426
const elements = closet.template.anki.getQaChildNodes();
427427
const memory = chooseMemory("closet__1");
@@ -455,13 +455,13 @@
455455

456456
var getAnkiPrefix = () => globalThis.ankiPlatform === "desktop" ? "" : globalThis.AnkiDroidJS ? "https://appassets.androidplatform.net" : ".";
457457

458-
var closetFilename = "__closet-0.6.2.js";
458+
var closetFilename = "__ankingio-0.6.2.js";
459459
var closetPromise = import(`${getAnkiPrefix()}/${closetFilename}`);
460460
closetPromise
461461
.then(
462462
({ closet }) => closet.template.anki.initialize(closet, closetUserLogic, "{{Card}}", "{{Tags}}", "back"),
463-
(error) => console.log("An error occured while loading Closet:", error))
464-
.catch((error) => console.log("An error occured while executing Closet:", error));
463+
(error) => console.log("An error occured while loading AnKing Closet:", error))
464+
.catch((error) => console.log("An error occured while executing AnKing Closet:", error));
465465

466466
if (globalThis.onUpdateHook) {
467467
onUpdateHook.push(() => closetPromise);

src/anking_notetypes/note_types/IO-one by one/IO-one by one.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@
9999
]
100100
]
101101
],
102-
"closetVersion": "0.6.1",
103102
"vers": [],
104103
"assetManagerHtml": {
105104
"enabled": true,
@@ -149,7 +148,7 @@
149148
"version": "v0.1"
150149
},
151150
{
152-
"code": "/* Script by Matthias Metelka @kleinerpirat */\n\n(function () {\n var observer = new MutationObserver(() => {\n let rect = document.querySelector(\".closet-rect.is-active\")\n if (rect) {\n activate(rect)\n observer.disconnect()\n let rects = document.querySelectorAll(\".closet-rect.is-active\")\n for (let rect of rects) {\n rect.addEventListener(\"click\", reveal)\n }\n if (!globalThis.AnKingIOListening) {\n document.addEventListener(\"keydown\", (event) => {\n if (event.key == \"n\") {\n let active = document.querySelector(\".closet-rect.is-highlighted\")\n if (active) incrementalReveal.call(active)\n }\n else if (event.key == \"g\") {\n for (let rect of rects) {\n incrementalReveal.call(rect)\n }\n }\n })\n globalThis.AnKingIOListening = true\n }\n let buttons = document.getElementsByClassName(\"extra-btn\")\n\n function toggle() {\n if (!this.classList.contains(\"uncollapsed\")) {\n this.nextElementSibling.classList.remove(\"hidden\")\n this.classList.add(\"uncollapsed\")\n }\n else {\n this.nextElementSibling.classList.add(\"hidden\")\n this.classList.remove(\"uncollapsed\")\n }\n }\n for (let button of buttons) {\n button.addEventListener(\"click\", toggle)\n }\n }\n })\n observer.observe(document.getElementById(\"qa\"), {\n childList: true,\n subtree: true\n })\n\n function activate(rect) {\n rect.classList.add(\"is-highlighted\")\n rect.addEventListener(\"click\", incrementalReveal)\n }\n\n function incrementalReveal() {\n reveal.call(this)\n let next = this.nextElementSibling\n if (next) {\n while (next.classList.contains(\"revealed\") && next.nextElementSibling) {\n next = next.nextElementSibling\n }\n if (!next.classList.contains(\"revealed\")) activate(next)\n }\n else {\n document.getElementById(\"extra-content\").classList.remove(\"hidden\")\n }\n }\n\n function reveal() {\n this.classList.remove(\"is-highlighted\")\n this.classList.add(\"revealed\")\n }\n})()",
151+
"code": "/* Script by Matthias Metelka @kleinerpirat */\n\n(function () {\n var observer = new MutationObserver(() => {\n let rect = document.querySelector(\".anking-rect.is-active\")\n if (rect) {\n activate(rect)\n observer.disconnect()\n let rects = document.querySelectorAll(\".anking-rect.is-active\")\n for (let rect of rects) {\n rect.addEventListener(\"click\", reveal)\n }\n if (!globalThis.AnKingIOListening) {\n document.addEventListener(\"keydown\", (event) => {\n if (event.key == \"n\") {\n let active = document.querySelector(\".anking-rect.is-highlighted\")\n if (active) incrementalReveal.call(active)\n }\n else if (event.key == \"g\") {\n for (let rect of rects) {\n incrementalReveal.call(rect)\n }\n }\n })\n globalThis.AnKingIOListening = true\n }\n let buttons = document.getElementsByClassName(\"extra-btn\")\n\n function toggle() {\n if (!this.classList.contains(\"uncollapsed\")) {\n this.nextElementSibling.classList.remove(\"hidden\")\n this.classList.add(\"uncollapsed\")\n }\n else {\n this.nextElementSibling.classList.add(\"hidden\")\n this.classList.remove(\"uncollapsed\")\n }\n }\n for (let button of buttons) {\n button.addEventListener(\"click\", toggle)\n }\n }\n })\n observer.observe(document.getElementById(\"qa\"), {\n childList: true,\n subtree: true\n })\n\n function activate(rect) {\n rect.classList.add(\"is-highlighted\")\n rect.addEventListener(\"click\", incrementalReveal)\n }\n\n function incrementalReveal() {\n reveal.call(this)\n let next = this.nextElementSibling\n if (next) {\n while (next.classList.contains(\"revealed\") && next.nextElementSibling) {\n next = next.nextElementSibling\n }\n if (!next.classList.contains(\"revealed\")) activate(next)\n }\n else {\n document.getElementById(\"extra-content\").classList.remove(\"hidden\")\n }\n }\n\n function reveal() {\n this.classList.remove(\"is-highlighted\")\n this.classList.add(\"revealed\")\n }\n})()",
153152
"conditions": [
154153
"side",
155154
"=",

src/anking_notetypes/note_types/IO-one by one/Styling.css

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@import url("__closet-0.6.1.css");
1+
@import url("__ankingio-0.6.2.css");
22

33

44
/*#########################################################
@@ -441,18 +441,18 @@ ul ul, table ul, ol ol, table ol {
441441

442442

443443
/* ~~~~~~INCREMENTAL REVEAL: Closet rects~~~~~~ */
444-
.closet-rect.is-active.is-back rect {
444+
.anking-rect.is-active.is-back rect {
445445
fill: var(--rect-bg);
446446
stroke: var(--rect-border);
447447
cursor: pointer;
448448
}
449449

450-
.closet-rect.is-active.is-back.is-highlighted rect {
450+
.anking-rect.is-active.is-back.is-highlighted rect {
451451
fill: var(--active-rect-bg);
452452
stroke: var(--active-rect-border);
453453
}
454454

455-
.closet-rect.revealed rect {
455+
.anking-rect.revealed rect {
456456
visibility: hidden;
457457
}
458458

src/anking_notetypes/note_types/Physeo-IO one by one/Back Template.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -318,11 +318,11 @@
318318
toggleAllButton.title = `Shortcut: ${window.toggleAllShortcut}`;
319319
revealNextButton.title = `Shortcut: ${window.revealNextShortcut}`;
320320

321-
let rect = document.querySelector(".closet-rect.is-active")
321+
let rect = document.querySelector(".anking-rect.is-active")
322322
if (rect) {
323323
activate(rect)
324324
observer.disconnect()
325-
globalThis.AnkingIORects = document.querySelectorAll(".closet-rect.is-active")
325+
globalThis.AnkingIORects = document.querySelectorAll(".anking-rect.is-active")
326326
for (let rect of globalThis.AnkingIORects) {
327327
rect.addEventListener("click", reveal)
328328
}
@@ -365,7 +365,7 @@
365365

366366

367367
window.toggleNext = function() {
368-
let active = document.querySelector(".closet-rect.is-highlighted")
368+
let active = document.querySelector(".anking-rect.is-highlighted")
369369
if (active) incrementalReveal.call(active)
370370
}
371371

@@ -378,7 +378,7 @@
378378
for (let rect of globalThis.AnkingIORects) {
379379
hide.call(rect)
380380
}
381-
let newActiveRect = document.querySelector(".closet-rect.is-active")
381+
let newActiveRect = document.querySelector(".anking-rect.is-active")
382382
activate(newActiveRect)
383383
} else {
384384
for (let rect of globalThis.AnkingIORects) {
@@ -420,7 +420,7 @@
420420
setupIncrementalIO()
421421
</script>
422422

423-
<script data-name="Closet Setup" data-version="v0.1">
423+
<script data-name="AnKing Closet Setup" data-version="v0.1">
424424
function closetUserLogic(closet, preset, chooseMemory) {
425425
const elements = closet.template.anki.getQaChildNodes();
426426
const memory = chooseMemory("closet__1");
@@ -454,13 +454,13 @@
454454

455455
var getAnkiPrefix = () => globalThis.ankiPlatform === "desktop" ? "" : globalThis.AnkiDroidJS ? "https://appassets.androidplatform.net" : ".";
456456

457-
var closetFilename = "__closet-0.6.2.js";
457+
var closetFilename = "__ankingio-0.6.2.js";
458458
var closetPromise = import(`${getAnkiPrefix()}/${closetFilename}`);
459459
closetPromise
460460
.then(
461461
({ closet }) => closet.template.anki.initialize(closet, closetUserLogic, "{{Card}}", "{{Tags}}", "back"),
462-
(error) => console.log("An error occured while loading Closet:", error))
463-
.catch((error) => console.log("An error occured while executing Closet:", error));
462+
(error) => console.log("An error occured while loading AnKing Closet:", error))
463+
.catch((error) => console.log("An error occured while executing AnKing Closet:", error));
464464

465465
if (globalThis.onUpdateHook) {
466466
onUpdateHook.push(() => closetPromise);

0 commit comments

Comments
 (0)