Skip to content
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

Created the AnkiMediaQueue for JavaScript media support #604

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
90 commits
Select commit Hold shift + click to select a range
e91482d
Created the AnkiMediaQueue for JavaScript media support
evandrocoan Apr 25, 2020
ad3afca
Renamed single_audio_file.html to main_webview.html
evandrocoan May 5, 2020
ef50afc
Added missing ankimedia prefix on error message
evandrocoan May 4, 2020
1c4b160
Fixed card previewing playing the audio multiple times
evandrocoan May 4, 2020
ac2bc91
Removed duplicated code from reviewer.test.ts
evandrocoan May 5, 2020
38bd3d9
Set to reviewer.test.ts also check all medias are paused
evandrocoan May 5, 2020
469836d
Renamed silence1.mp3 to silence 1.mp3 to cover file names with
evandrocoan May 9, 2020
9218221
The setting PlaybackRequiresUserGesture is only available on newer
evandrocoan May 18, 2020
2b07b92
Set to display the media name when it is failed to run play()
evandrocoan May 20, 2020
0f3ff29
Set to reviewer.test.ts getPausedMedias() return the playing medias
evandrocoan May 20, 2020
a2dc2bc
Fixed card layout back window title to card layout after recent
evandrocoan May 20, 2020
ab82bdb
Fixed the media element pausing when the page is updated twice
evandrocoan May 20, 2020
ada31dd
Fixed previewer.py window not playing JavaScript due to missing
evandrocoan May 22, 2020
90fb8b8
Fixed ankimedia.ts not playing audios without HTML media tag
evandrocoan May 22, 2020
120065c
Fixed ankimedia not replaying when switching clayout.py front/back
evandrocoan May 22, 2020
13bcce4
Fixed clayout.py playing the front media when showing the card back
evandrocoan May 22, 2020
12d2bbd
Fixed JavaScript playing audio when disabled on decks settings
evandrocoan May 23, 2020
a2f254a
Fixed medias passed not being saved between ankimedia.setup() calls
evandrocoan May 23, 2020
cecf824
Fixed reviewer.test.ts usage of toBeEqual instead of toBeTruthy,
evandrocoan May 23, 2020
360cba7
Inverted the ankimedia.add to filename, where parameter ordering
evandrocoan May 23, 2020
2e2bd04
Moved ankimedia.ts duplicate media checking into _getMediaElement()
evandrocoan May 23, 2020
902f424
Renamed _fixDuplicates() duplicates local variable to selected
evandrocoan May 24, 2020
c3c8e26
Fixed puppeteer evaluate functions not stopping on break points
evandrocoan May 24, 2020
5cfb4eb
Fixed formatting on qt/ts/globalSetup.js
evandrocoan May 24, 2020
8840c68
Documented the usage of puppeteer setDefaultTimeout
evandrocoan May 24, 2020
ffc48e1
Cached _getMediaElement() medias into a dictionary
evandrocoan May 25, 2020
d54146a
Fixed _validateWhere() always showing add instead of add or addall
evandrocoan May 25, 2020
7769047
Fixed ankimedia.addall() being balled twice when showing the back
evandrocoan May 25, 2020
23194ef
Fixed ankimedia.addall calls not skipping the front audio
evandrocoan May 25, 2020
82e01e8
Fixed the wrong playing when there are duplicates
evandrocoan May 25, 2020
729d8dd
Fixed Unhandled promise rejections when skipping reviewer.test.ts
evandrocoan May 25, 2020
9ca1644
Replaced reviewer-exceptions.test.ts single quotes by backticks
evandrocoan May 25, 2020
11c4a8d
Added support to ankimedia.add() without the where parameter
evandrocoan May 25, 2020
0b2fcdd
Removed strict comparisons === and !== from ankimedia.ts to
evandrocoan May 25, 2020
26ffe8a
Extended reviewer.test.ts with/without "front"/"back"
evandrocoan May 25, 2020
1ff4d69
Created the ankimedia.replay() function to replay the card media
evandrocoan May 25, 2020
d0910bd
Fixed reviewer.py replaying ankimedia.ts front when it is disabled
evandrocoan May 25, 2020
8ba906d
Fixed ankimedia.ts data-file attributes not being trimmed
evandrocoan May 25, 2020
7651de3
Fixed ankimidia.replay() pausing randomly one of the medias by
evandrocoan May 26, 2020
c5a7644
Increased ANKI_MEDIA_QUEUE_PREVIEW_TIMEOUT to 1000 to keep the
evandrocoan May 26, 2020
68ec2b7
Set to always reenable the is_autoseek attributing even on failture
evandrocoan May 26, 2020
4e7bb16
Created the ankimedia.setup(skip) parameter to replace skip_front
evandrocoan May 26, 2020
bb88934
Created the ankimedia._getMediaInfo() to show more complete error
evandrocoan May 26, 2020
092164d
Created the ankimedia._reset(skip_src_reset) fixing clayout.py
evandrocoan May 26, 2020
f4c5bc4
Created the ankimedia._debug function as wrapper around
evandrocoan May 27, 2020
5656fcc
Fixed front audio being paused by call to ankimedia._playnext when
evandrocoan May 27, 2020
beaf047
Also fixed replay being paused by call to ankimedia._playnext when
evandrocoan May 27, 2020
ad50bbf
Fixed previewer.py not playing ankimedia correctly
evandrocoan May 26, 2020
a6c466d
Fixed the first audio having a delay applied instead of the others
evandrocoan May 28, 2020
0c80f0e
Fixed ankimedia._checkPreviewPage() not resetting running timers
evandrocoan May 28, 2020
40ffdde
Renamed _playing_element_timeout to _playing_element_timer
evandrocoan May 28, 2020
02056f5
Fixed QWebEngineSettings error: unused 'type: ignore' comment
evandrocoan May 28, 2020
9505064
Fixed ankimedia.play_duplicates not selecting the back card indexes
evandrocoan May 28, 2020
b068a6b
Fixed ankimedia.setup() being called by previewer.py when setup
evandrocoan May 28, 2020
f6c5e7d
Deprecated the ankimedia._reset() media.src = "" because this
evandrocoan May 29, 2020
31029ec
The ankimedia.setup() function must never be called by Anki, only
evandrocoan May 29, 2020
8508dc9
Fixed ankimedia.play_duplicates queue not being cleaned
evandrocoan May 30, 2020
4f67704
Removed unrequired array size check before cleaning it up
evandrocoan May 30, 2020
3f01403
Fixed skip_front check breaking the correctly media on replay
evandrocoan May 30, 2020
7a55a4d
Fixed ankimedia.is_autoseek not being restored when replaying the
evandrocoan May 30, 2020
805fe32
Fixed _checkPreviewPage() not binding to the this object
evandrocoan May 30, 2020
dc213a4
Removed unrequired front %s formatting on reviewer.test.ts
evandrocoan Jun 5, 2020
62aac6b
Replaced the data-file attribute by src attribute to allow user
evandrocoan Jun 5, 2020
a2d8e8f
Ensured ankimedia.is_playing is set to false reviewer.test.ts
evandrocoan Jun 5, 2020
e58c695
Deprecated the usage of ankimedia.is_first attribute as it does
evandrocoan Jun 5, 2020
d64e89b
Announce media files are added with leading/trailing whitespaces
evandrocoan Jun 5, 2020
05e7036
Test ankimedia.setup{delay} creates an delay between medias
evandrocoan Jun 5, 2020
e732f73
Replaced reviewer.test.ts audio.src by audio.getAttribute,
evandrocoan Jun 5, 2020
3dc117c
Added support to media nested source child src attributes
evandrocoan Jun 5, 2020
d687b94
Set ankimedia to use HTMLMediaElement instead of HTMLAudioElement
evandrocoan Jun 5, 2020
4102d3c
Deprecated the usage of qt/ts/gulpfile.js as reviewer.js does not
evandrocoan Jun 5, 2020
3c806b6
Set to ankimedia._getMediaInfo show all source children if existent
evandrocoan Jun 5, 2020
5129340
Fix jest unit tests after merge
evandrocoan Jul 2, 2020
9923ba7
Also check whether media.firstChild getAttribute() method exists
evandrocoan Jul 3, 2020
82526b1
Added some empty line spacing on ankimedia.ts
evandrocoan Jul 4, 2020
390748f
Fixed empty filename throwing invalid where argument
evandrocoan Aug 14, 2020
c604db3
Fixed answer detection not working when disabling builtin
evandrocoan Jun 21, 2021
91bd54e
Replaced ankimedia.ts by ankimedia.js because
evandrocoan Sep 8, 2021
9d0e9c1
Fixed ankimedia.js not being copied to the run directory
evandrocoan Oct 13, 2021
1464113
Fix qt 6 support
evandrocoan Nov 23, 2021
700b9ee
Fixed ankimedia.js formatting with bazel run //ts:format
evandrocoan Nov 23, 2021
644e8a1
Moved javascript audio code into utils
evandrocoan Nov 23, 2021
5d302fa
Fixed javascript media queue not being cleaned when hot reloading
evandrocoan Mar 24, 2022
5cc7628
Fixed Javascript variables not being cleaned and fixed anki media
evandrocoan Apr 28, 2022
6d1bc2d
Updated ankimedia.js with togglePause implementation and add support
evandrocoan Jan 3, 2023
f5debeb
Fixed pausing between playing two audios should play the next audio
evandrocoan Jan 3, 2023
9127903
Fixed togglePause not resuming with the next media on the queues
evandrocoan Jan 3, 2023
485896d
Added support for adding audios from html strings
evandrocoan Jan 8, 2023
c6c7991
Fix HTML media not pausing when showing next question
evandrocoan Jan 14, 2023
0e5ff2d
Fix ankimedia documentation misspellings
evandrocoan Jan 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion build/configure/src/aqt.rs
Expand Up @@ -202,7 +202,8 @@ fn build_vendor_js(build: &mut Build) -> Result<()> {
":node_modules:jquery-ui",
":node_modules:css-browser-selector",
":node_modules:bootstrap-dist",
"qt/aqt/data/web/js/vendor/plot.js"
"qt/aqt/data/web/js/vendor/plot.js",
"qt/aqt/data/web/js/vendor/ankimedia.js"
],
output_folder: "qt/_aqt/data/web/js/vendor",
},
Expand Down
23 changes: 22 additions & 1 deletion qt/aqt/browser/previewer.py
Expand Up @@ -28,7 +28,14 @@
from aqt.reviewer import replay_audio
from aqt.sound import av_player, play_clicked_audio
from aqt.theme import theme_manager
from aqt.utils import disable_help_button, restoreGeom, saveGeom, setWindowIcon, tr
from aqt.utils import (
disable_help_button,
enable_javascript_playback,
restoreGeom,
saveGeom,
setWindowIcon,
tr,
)
from aqt.webview import AnkiWebView

LastStateAndMod = tuple[str, int, int]
Expand Down Expand Up @@ -83,6 +90,8 @@ def _create_gui(self) -> None:
self.bbox = QDialogButtonBox()
self.bbox.setLayoutDirection(Qt.LayoutDirection.LeftToRight)

enable_javascript_playback(self._web._page.settings())

self._replay = self.bbox.addButton(
tr.actions_replay_audio(), QDialogButtonBox.ButtonRole.ActionRole
)
Expand Down Expand Up @@ -110,6 +119,7 @@ def _on_finished(self, ok: int) -> None:
self._on_close()

def _on_replay_audio(self) -> None:
self._web.eval("ankimedia.replay();")
if self._state == "question":
replay_audio(self.card(), True)
elif self._state == "answer":
Expand All @@ -128,6 +138,7 @@ def _setup_web_view(self) -> None:
js=[
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/vendor/ankimedia.js",
"js/reviewer.js",
],
context=self,
Expand Down Expand Up @@ -189,6 +200,10 @@ def _render_scheduled(self) -> None:
elif self._card_changed:
self._state = "question"

if not self._show_both_sides and self._state == "answer":
self._web.eval("ankimedia.skip_front = true;")
self._web.eval("ankimedia._reset({skip_front_reset: true});")

currentState = self._state_and_mod()
if currentState == self._last_state:
# nothing has changed, avoid refreshing
Expand Down Expand Up @@ -222,6 +237,8 @@ def _render_scheduled(self) -> None:
else:
audio = []
self._web.setPlaybackRequiresGesture(True)
self._web.eval("ankimedia.autoplay = false;")

gui_hooks.av_player_will_play_tags(audio, self._state, self)
av_player.play_tags(audio)
txt = self.mw.prepare_card_text_for_display(txt)
Expand All @@ -236,10 +253,14 @@ def _render_scheduled(self) -> None:
js = f"{func}({json.dumps(txt)}, '{bodyclass}');"
self._web.eval(js)
self._card_changed = False
self._web.eval("ankimedia.skip_front = false;")

def _on_show_both_sides(self, toggle: bool) -> None:
self._show_both_sides = toggle
self.mw.col.set_config_bool(Config.Bool.PREVIEW_BOTH_SIDES, toggle)
self._web.eval("ankimedia._reset();")
if self._state == "question" and toggle:
self._web.eval("ankimedia.skip_front = true;")
if self._state == "answer" and not toggle:
self._state = "question"
self.render_card()
Expand Down
11 changes: 11 additions & 0 deletions qt/aqt/clayout.py
Expand Up @@ -24,6 +24,7 @@
askUser,
disable_help_button,
downArrow,
enable_javascript_playback,
getOnlyText,
openHelp,
restoreGeom,
Expand Down Expand Up @@ -351,6 +352,7 @@ def setup_preview(self) -> None:
js=[
"js/mathjax.js",
"js/vendor/mathjax/tex-chtml.js",
"js/vendor/ankimedia.js",
"js/reviewer.js",
],
context=self,
Expand All @@ -359,6 +361,8 @@ def setup_preview(self) -> None:
self.preview_web.eval("_blockDefaultDragDropBehavior();")
self.preview_web.set_bridge_command(self._on_bridge_cmd, self)

enable_javascript_playback(self.preview_web._page.settings())

if self._isCloze():
nums = list(self.note.cloze_numbers_in_fields())
if self.ord + 1 not in nums:
Expand Down Expand Up @@ -526,6 +530,12 @@ def _renderPreview(self) -> None:
c.ord, self.night_mode_is_enabled
)

if not self.have_autoplayed:
self.preview_web.eval("ankimedia._reset();")

if not c.autoplay():
self.preview_web.eval("ankimedia.autoplay = false;")

if self.pform.preview_front.isChecked():
q = ti(self.mw.prepare_card_text_for_display(c.question()))
q = gui_hooks.card_will_show(q, c, "clayoutQuestion")
Expand All @@ -534,6 +544,7 @@ def _renderPreview(self) -> None:
a = ti(self.mw.prepare_card_text_for_display(c.answer()), type="a")
a = gui_hooks.card_will_show(a, c, "clayoutAnswer")
text = a
self.preview_web.eval("ankimedia.skip_front = true;")

# use _showAnswer to avoid the longer delay
self.preview_web.eval(f"_showAnswer({json.dumps(text)},'{bodyclass}');")
Expand Down