-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat: persist caption settings in localStorage on track mode change (code100x#1493) #1503
Merged
devsargam
merged 5 commits into
code100x:main
from
IkramBagban:feature/save-caption-settings-to-localstorage
Dec 3, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5365c3
feat: persist caption settings in localStorage on track mode change (…
IkramBagban 51422fd
Merge branch 'main' of https://github.com/IkramBagban/cms into featur…
IkramBagban 3d78a6b
Merge branch 'main' of https://github.com/code100x/cms into feature/s…
IkramBagban 7df3d38
Merge branch 'main' of https://github.com/code100x/cms into feature/s…
IkramBagban 0d3ef05
refactor: combine both the with same dependancy
IkramBagban File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,6 +92,52 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({ | |
return pipButtonContainer; | ||
}; | ||
|
||
// Fetch previously saved captions setting from localStorage | ||
useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have 2 useEffects with same dependencies array? Can you add the below login in here only? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes sure |
||
if (!player) return; | ||
|
||
const savedCaptionSetting = localStorage.getItem('captionSetting'); | ||
|
||
if (savedCaptionSetting && player) { | ||
const tracks = player.textTracks(); | ||
|
||
for (let i = 0; i < tracks.length; i++) { | ||
const track = tracks[i]; | ||
|
||
if (track) { | ||
track.mode = | ||
savedCaptionSetting === 'showing' ? 'showing' : 'disabled'; | ||
} | ||
} | ||
} | ||
}, [player]); | ||
|
||
useEffect(() => { | ||
if (!player) return; | ||
|
||
const handleTrackChange = () => { | ||
const tracks = player.textTracks(); | ||
for (let i = 0; i < tracks.length; i++) { | ||
const track = tracks[i]; | ||
if (track.kind === 'subtitles' && track.language === 'en') { | ||
// Save the mode to localStorage when it changes | ||
track.addEventListener('modechange', () => { | ||
localStorage.setItem('captionSetting', track.mode); | ||
}); | ||
} | ||
} | ||
}; | ||
|
||
handleTrackChange(); | ||
return () => { | ||
const tracks = player.textTracks(); | ||
for (let i = 0; i < tracks.length; i++) { | ||
const track = tracks[i]; | ||
track.removeEventListener('modechange', handleTrackChange); | ||
} | ||
}; | ||
}, [player]); | ||
|
||
useEffect(() => { | ||
const t = searchParams.get('timestamp'); | ||
if (contentId && player && !t) { | ||
|
@@ -255,11 +301,8 @@ export const VideoPlayer: FunctionComponent<VideoPlayerProps> = ({ | |
const track = tracks[i]; | ||
|
||
if (track.kind === 'subtitles' && track.language === 'en') { | ||
if (track.mode === 'hidden') { | ||
track.mode = 'showing'; | ||
} else { | ||
track.mode = 'hidden'; | ||
} | ||
if (track.mode === 'disabled') track.mode = 'showing'; | ||
else track.mode = 'disabled'; | ||
} | ||
} | ||
event.stopPropagation(); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you remove this comment?
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 added this comment to help other people understand these line of codes. anyways i'll remove it