-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add APIs for IO card rendering #2739
Conversation
We currently use "image occlusion" in most places, but some references to "image cloze" still remain. For consistency's sake and to make it easier to quickly find IO-related code, this commit replaces all remaining references to "image cloze", only maintaining those required for backwards compatibility with existing note types.
Mutating shapes would be a recipe for trouble when combined with IO API use by external consumers. (makeNormal(makeAbsolute(makeNormal())) is not idempotent, and keeping track of the original state would introduce additional complexity with no discernible performance benefit or otherwise.)
For consistency with previous implementation
export { extractShapesFromRenderedClozes } from "./from-cloze"; | ||
export { Polygon } from "./polygon"; | ||
export { Rectangle } from "./rectangle"; | ||
export { Text } from "./text"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some people consider this an anti-pattern, and I've started to feel the same way. I know we have other examples in the codebase already that do this, but I'd suggest going forward, we limit this to public API exports, and avoid making these for our own use.
https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can definitely see that. I would also agree that the convenience factor in internal use really isn't that high.
However, given that third-party code consuming these APIs will likely also want to have access to their corresponding types, I'm wondering how we should best handle this here.
E.g., with the current state I can just import the various Shape
types from this barrel file without having to worry about the internal structure of the shapes
package (annotating shape types with e.g. typeof globalThis.anki.imageOcclusion.Shape
doesn't work without some major TS acrobatics which are likely a bad practice of their own).
Perhaps just bundling type exports in barrel files like these could be an option? Or splitting out type exports in a different way like d.ts files?
FWIW, I don't feel particularly strong about this from a stylistic standpoint, just want to make sure that we go with as defensive a solution as possible, so that using anki
types is not too brittle long-term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're consuming the types then it's arguably public API, and I remove my objection.
I'd like to return to the question of the best way to expose these to add-on authors in the future - some previous discussion about it can be found at #1764.
Huh, that's a weird one, good catch! But it looks like it's not specific to this PR – I can also reproduce with main (0e24532): Screencast-2023-10-19_17.30.34.mp4So I would propose we split it off into a separate issue. |
Sorry, I did briefly test this on main before reporting it; not sure why I didn't spot the issue then. |
Extends IO with a number of JS APIs that allow altering IO card rendering:
onWillDrawShapes
andonDidDrawShapes
callback parameters to IO setup function, allowing consumers to modify rendering both before and after the occlusion masks are drawnShape
objectsdrawShape
and shape classesRefactoring changes as a result of these additions:
globalThis.anki.imageOcclusion
anki.setupImagecloze()
toanki.imageOcclusion.setup()
while retaining backwards compatibility with old templatesI was a bit unsure on the best way to add these APIs, walking through multiple different implementations and e.g. also experimenting with an extended hook system, but I eventually ended up settling on this template-centric approach with callback parameters.
(FWIW, pushed the extended hook system here, in case it could still come in handy in the future)
One point still has me a bit confused, however. We currently expose globals in the reviewer context through a number of means:
_drawMark
), weexport
the corresponding symbols inreviewer/index.ts
mutateNextCardStates
andsetupImageCloze
, we register them to aglobalThis.anki
object. We do so in two places,reviewer/index.ts
(for desktop) andreviewer/reviewer_extras.ts
(I assume for AnkiMobile, AnkiWeb and maybe AnkiDroid?)onUpdateHook
that are both exposed viaexport
s andregisterPackage
, but do not seem to be exposed inreviewer/reviewer_extras.ts
, while still being available on mobileWhich option, or combination thereof would you recommend using here? Given that we were already using the
anki
global forsetupImageCloze
, that's what I ended up going with. But I was wondering if we have started migrating more towards theregisterPackage
approach (or started and then stopped, givenmutateNextCardStates
andsetupImageCloze
?).