Skip to content

Implement Audio Sharing for Selected Text#992

Open
TheNonPirate wants to merge 8 commits into
sillsdev:mainfrom
TheNonPirate:feature/share-audio/377
Open

Implement Audio Sharing for Selected Text#992
TheNonPirate wants to merge 8 commits into
sillsdev:mainfrom
TheNonPirate:feature/share-audio/377

Conversation

@TheNonPirate

@TheNonPirate TheNonPirate commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Resolves #377. If there is audio timing data, this causes the share button when text is selected to open a modal that gives the user a choice between sharing text and sharing audio.

Summary by CodeRabbit

  • New Features
    • Added a Share dialog to share selected verses as text.
    • Added the ability to generate and share a chapter audio extract for the selected verses.
  • Improvements
    • Integrated sharing into the app’s modal workflow, including native sharing when available and automatic fallback to download.
  • Bug Fixes
    • Updated the sharing flow to open the correct share modal when verse timing data is available.
    • Improved audio generation/trimming using timing information and support for multiple audio encodings.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR introduces a new ShareSelector modal that lets users share selected verses as text or export them as trimmed audio. The modal replaces the previous VerseOnImage modal in the type registry, integrates with TextSelectionToolbar to trigger on verse selection, and wires into the layout for dispatch and rendering. Audio sharing supports remote URL passthrough and local decode/trim/encode workflows with fallback to download.

Changes

Audio and text sharing modal

Layer / File(s) Summary
Share modal type contract
src/lib/data/stores/view.ts
ModalType registry replaces VerseOnImage with new Share entry, establishing the modal dispatch contract.
ShareSelector modal component
src/lib/components/ShareSelector.svelte
Text sharing builds a message from selected verses and logs analytics. Audio sharing detects codec support, loads chapter audio, branches on remote vs. local sources (remote uses direct URL sharing; local decodes, trims per-verse using timing data, encodes to aac/opus/wav), and attempts native file sharing with object-URL download fallback. Modal positioning derives CSS from vertOffset, showModal() is exported, and two action buttons render.
Text selection toolbar integration
src/lib/components/TextSelectionToolbar.svelte
Imports modal and ModalType, updates TODO comment, and branches shareSelectedText() to open the Share modal when audio timing is available or falls back to direct text sharing.
Layout modal dispatch and rendering
src/routes/+layout.svelte
Imports ShareSelector, declares shareSelector state, extends modal dispatch to handle ModalType.Share, and renders the bound component.

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant ShareSelector
  participant TextShare as shareText
  participant AudioSource
  participant AudioContext
  participant Mediabunny
  participant Navigator as navigator.share
  participant Fallback as Download link

  rect rgba(200, 150, 255, 0.5)
  User->>ShareSelector: Click Share Text
  ShareSelector->>ShareSelector: Build reference & message
  ShareSelector->>TextShare: shareText(filename, message)
  ShareSelector->>ShareSelector: logShareContent()
  end

  rect rgba(150, 200, 255, 0.5)
  User->>ShareSelector: Click Share Audio
  ShareSelector->>ShareSelector: pickSupportedAudioConfig()
  ShareSelector->>AudioSource: getAudioSourceInfo()
  alt Remote source (http/https)
    AudioSource->>Navigator: navigator.share({ url })
    alt Share succeeded
      Navigator->>User: Share dialog
    else Share failed
      ShareSelector->>Fallback: Click download link
    end
  else Local source
    ShareSelector->>AudioContext: Fetch & decode audio
    AudioContext->>AudioContext: Trim per verse (timing data)
    AudioContext->>Mediabunny: Encode to aac/opus/wav
    Mediabunny->>ShareSelector: File blob
    ShareSelector->>Navigator: navigator.canShare({ files: [File] })
    alt canShare true
      Navigator->>User: Share dialog
    else canShare false
      ShareSelector->>Fallback: Create download link
    end
  end
  ShareSelector->>AudioContext: Close context
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Possibly related PRs

  • sillsdev/appbuilder-pwa#963: Both PRs modify the ModalType registry; this PR replaces VerseOnImage with Share, directly countering the addition of VerseOnImage in the related PR.

Suggested reviewers

  • FyreByrd
  • chrisvire

Poem

🐰 A modal takes wing with audio and text,
Trimming verses in time, no mess, no vexed!
Remote or local, the audio flows,
ShareSelector opens—where the shared content goes! 🎵

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Implement Audio Sharing for Selected Text' directly and clearly summarizes the main change—adding audio sharing functionality. It is specific, concise, and accurately reflects the primary objective of the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@TheNonPirate TheNonPirate force-pushed the feature/share-audio/377 branch from e5d1011 to 5ab1d1e Compare June 11, 2026 21:31
@TheNonPirate TheNonPirate marked this pull request as ready for review June 11, 2026 21:34
@TheNonPirate TheNonPirate requested a review from FyreByrd June 11, 2026 21:34

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/lib/components/ShareSelector.svelte (1)

27-40: ⚡ Quick win

Extract shared text-share composition into one helper.

Lines 27-40 duplicate the same “selected verses → message → shareText + logShareContent” logic now also present in src/lib/components/TextSelectionToolbar.svelte (Lines 138-150). Centralizing this avoids drift in filename/message/analytics behavior.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/components/ShareSelector.svelte` around lines 27 - 40, Extract the
repeated "selected verses → message → shareText + logShareContent" logic into a
single helper (e.g., export a function like composeAndShareSelectedText or
shareSelectedVerses) and replace the duplicate blocks in shareSelectedText (in
ShareSelector.svelte) and the corresponding handler in
TextSelectionToolbar.svelte to call that helper; the helper should accept the
selectedVerses object (or its derived book, collection, text, and reference),
scriptureConfig.name, and internally build the filename, message body, call
shareText(scriptureName, message, filename) and then call
logShareContent('Text', bookCol, bookAbbrev, reference) so both components share
identical filename/message/analytics behavior.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/components/ShareSelector.svelte`:
- Around line 124-129: In shareAudio() in ShareSelector.svelte, modify the catch
after navigator.share(...) to detect a user-cancelled share (error.name ===
'AbortError' or error is a DOMException with name 'AbortError') and return early
so the download fallback is not triggered; only for other errors should the code
continue to createObjectURL(file) and trigger the anchor download. Ensure you
check error?.name and only fall through on non-AbortError cases.

---

Nitpick comments:
In `@src/lib/components/ShareSelector.svelte`:
- Around line 27-40: Extract the repeated "selected verses → message → shareText
+ logShareContent" logic into a single helper (e.g., export a function like
composeAndShareSelectedText or shareSelectedVerses) and replace the duplicate
blocks in shareSelectedText (in ShareSelector.svelte) and the corresponding
handler in TextSelectionToolbar.svelte to call that helper; the helper should
accept the selectedVerses object (or its derived book, collection, text, and
reference), scriptureConfig.name, and internally build the filename, message
body, call shareText(scriptureName, message, filename) and then call
logShareContent('Text', bookCol, bookAbbrev, reference) so both components share
identical filename/message/analytics behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 132775a3-fce6-4d3e-9c2d-793176254d92

📥 Commits

Reviewing files that changed from the base of the PR and between c6b8f04 and 5ab1d1e.

📒 Files selected for processing (5)
  • src/lib/components/ShareSelector.svelte
  • src/lib/components/TextSelectionToolbar.svelte
  • src/lib/components/VerseOnImage.svelte
  • src/lib/data/stores/view.ts
  • src/routes/+layout.svelte

Comment thread src/lib/components/ShareSelector.svelte Outdated
@benpaj-cps

benpaj-cps commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

On my phone (Pixel, Android 16, Chrome) the audio share fails in the PWA because of a reported lack of AAC, while the native Android app works fine sharing audio. If this isn't just a bug and there's no simple way to use a different method, it might be good to give a message to the user to the effect of "This feature requires the AAC audio codec to be installed on your phone", rather than silently failing.

image

@TheNonPirate TheNonPirate force-pushed the feature/share-audio/377 branch from 9ee24f1 to 4cd56d4 Compare June 15, 2026 17:21
@TheNonPirate

Copy link
Copy Markdown
Contributor Author

I tried adding another type of audio that it could try to fall back to. Does it work now?

@benpaj-cps

Copy link
Copy Markdown
Contributor

It gets the audio, but uses the download fallback. Also, the filename seems to only be set once per page load (i.e. all further downloads have the same filename, no matter the verse)?

@benpaj-cps

Copy link
Copy Markdown
Contributor

The ironic thing is that the Android app exports to .m4a, which is almost certainly using the AAC codec since I don't expect my Google phone to be using the "Apple Lossless Audio Codec". So either AAC is actually installed on my phone after all or the Android app is supplying it... I'll see if there's a simple way to check that.

@TheNonPirate

Copy link
Copy Markdown
Contributor Author

I fixed the filename only being set once per page load. I have found that the sharing option never seems to work, but I'm not sure if that's something I can fix or not.

@benpaj-cps

benpaj-cps commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

I think the AAC problem was due to my accessing it from my phone on LAN over HTTP, whereas AudioEncoder is only supported over HTTPS (see https://developer.mozilla.org/en-US/docs/Web/API/AudioEncoder). Running it on localhost and port forwarding 5173 from my laptop did work. It still gets the "download" behavior, but at least it's functional.

@benpaj-cps

Copy link
Copy Markdown
Contributor

The Android app adds a bit more information when sharing text.

PWA:

SAB Test PWA

The beginning of the Good News of Jesus Christ, the Son of God.
Mark 1:1

Android:

The beginning of the Good News of Jesus Christ, the Son of God. 
Mark 1:1
The World English Bible (WEB) is a Public Domain translation of the Holy Bible.
https://dwr8g.app.link?ref=ENGWEB/MRK.1.1

@benpaj-cps

Copy link
Copy Markdown
Contributor

However it looks like the URL link from the Android app doesn't work properly. Perhaps that will eventually be another config field, but that's likely a different issue.

@TheNonPirate

Copy link
Copy Markdown
Contributor Author

As far as I can tell, the copyright information (The World English Bible (WEB) is a Public Domain translation of the Holy Bible.) isn't even exported anywhere (It's not in config, for example). I think what the shared text says is a separate issue from sharing the audio.

@benpaj-cps

Copy link
Copy Markdown
Contributor

Right, okay. After a little digging I found that it comes from the .appDef but isn't converted by any of the current PWA scripts.

image

Since the share text functionality was in the new modal I didn't realize it predated this PR - that is probably a separate issue. I'll open one for completeness.

@FyreByrd

FyreByrd commented Jun 16, 2026

Copy link
Copy Markdown
Collaborator

I get the following error when trying this on Mark 13, which has audio defined but no local files (i.e. the audio is downloaded from elsewhere). The audio itself plays fine in the PWA, it's only an issue with sharing.

Access to fetch at 'https://dem-sab-assets-test.s3.amazonaws.com/web/B02___13_Mark________ENGWEBN2DA.webm' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

@TheNonPirate

Copy link
Copy Markdown
Contributor Author

I was able to figure out how to detect if it has a remote file and share it if it does (Although it seems to just share it as a link, not a file), but the closest I can find to a download option just opens a link to the file. Should I have that as the fallback in case sharing fails, or do something else? It looks like the usual method of fetching the file won't always work for remote files (If their server has the wrong permissions set for cross-site fetches).

@TheNonPirate TheNonPirate force-pushed the feature/share-audio/377 branch from 30e8782 to c230408 Compare June 16, 2026 21:16

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/lib/components/ShareSelector.svelte (2)

107-107: ⚡ Quick win

Use let instead of var for loop variable.

Using var in a for loop creates function-scoped binding which can lead to subtle bugs in closures. Modern JavaScript best practice is to use let for block-scoped loop variables.

♻️ Proposed fix
-                for (var j = 0; j < (audioSourceInfo?.timing?.length || 0); j++) {
+                for (let j = 0; j < (audioSourceInfo?.timing?.length || 0); j++) {
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/components/ShareSelector.svelte` at line 107, Replace the `var`
keyword with `let` for the loop variable j in the for loop that iterates over
audioSourceInfo?.timing?.length. The var keyword creates function-scoped
bindings which can cause subtle closure bugs, while let provides proper
block-scoping that is the modern JavaScript best practice for loop variables.

188-209: ⚖️ Poor tradeoff

Consider extracting pickSupportedAudioConfig to a shared utility.

The comment on line 209 correctly identifies that this function is duplicated in VerseOnImage.svelte. Extracting it to a shared module (e.g., $lib/data/audio.ts) would reduce duplication and ensure consistent codec fallback behavior across the codebase.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/lib/components/ShareSelector.svelte` around lines 188 - 209, Extract the
`pickSupportedAudioConfig` function to a shared utility module (such as a
dedicated audio configuration module under the lib directory) to eliminate
duplication. Move the function definition from this component to the shared
module, then import and use it in both locations where it currently exists. This
ensures consistent codec fallback behavior across the codebase and reduces
maintenance burden.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/lib/components/ShareSelector.svelte`:
- Line 69: Remove the debug console.log statement that logs
audioSourceInfo?.source from the ShareSelector.svelte component. This appears to
be leftover development code and should be deleted entirely to keep the codebase
clean.
- Around line 78-97: The navigator.share() call in the isRemote block is not
awaited, causing the AbortError catch block to never execute and the download
fallback to always run regardless of share success. Add await before the
navigator.share() call so that promise rejections are properly caught by the
catch block. Additionally, add a return statement after the try-catch block
completes successfully to prevent the download fallback anchor creation and
click from executing when the share succeeds, matching the pattern used
elsewhere in the component.

---

Nitpick comments:
In `@src/lib/components/ShareSelector.svelte`:
- Line 107: Replace the `var` keyword with `let` for the loop variable j in the
for loop that iterates over audioSourceInfo?.timing?.length. The var keyword
creates function-scoped bindings which can cause subtle closure bugs, while let
provides proper block-scoping that is the modern JavaScript best practice for
loop variables.
- Around line 188-209: Extract the `pickSupportedAudioConfig` function to a
shared utility module (such as a dedicated audio configuration module under the
lib directory) to eliminate duplication. Move the function definition from this
component to the shared module, then import and use it in both locations where
it currently exists. This ensures consistent codec fallback behavior across the
codebase and reduces maintenance burden.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f2d04711-2996-4692-903d-ad3e6e21b4e3

📥 Commits

Reviewing files that changed from the base of the PR and between 30e8782 and c230408.

📒 Files selected for processing (5)
  • src/lib/components/ShareSelector.svelte
  • src/lib/components/TextSelectionToolbar.svelte
  • src/lib/components/VerseOnImage.svelte
  • src/lib/data/stores/view.ts
  • src/routes/+layout.svelte
✅ Files skipped from review due to trivial changes (2)
  • src/lib/data/stores/view.ts
  • src/lib/components/VerseOnImage.svelte
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/routes/+layout.svelte
  • src/lib/components/TextSelectionToolbar.svelte

Comment thread src/lib/components/ShareSelector.svelte Outdated
Comment thread src/lib/components/ShareSelector.svelte
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TextSelectionToolbar: Share Audio

3 participants