Skip to content

Commit

Permalink
Merge pull request #9 from misabiko/dev
Browse files Browse the repository at this point in the history
Load article sidebar menu, compact option and mute videos option
  • Loading branch information
misabiko committed Dec 28, 2023
2 parents c5c191c + dd56e1f commit 7c3e078
Show file tree
Hide file tree
Showing 24 changed files with 212 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/articles/ArticleComponent.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
loadingStates.push(loadingStore.getLoadingState(actualArticle.idPair, mediaIndex, timelineProps.shouldLoadMedia))
}
//TODO Properly test media loading
afterUpdate(() => {
//TODO Use mediaRefs?
const articleMediaEls = divRef?.querySelectorAll('.articleMedia')
Expand Down
1 change: 1 addition & 0 deletions src/articles/gallery/GalleryArticleView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
class='articleMedia'
controls
preload='auto'
muted={timelineProps.muteVideos}
on:click|preventDefault={() => onMediaClick(actualArticle.idPair, i)}
on:loadeddata={() => isLoading ? loadingStore.mediaLoaded(actualArticle.idPair, i) : undefined}
on:load={() => isLoading ? loadingStore.mediaLoaded(actualArticle.idPair, i) : undefined}
Expand Down
1 change: 1 addition & 0 deletions src/articles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export function getDerivedArticleWithRefs(a: DerivedArticleWithRefs): ArticleWit
//Props for every article in the timeline
export type TimelineArticleProps = {
animatedAsGifs: boolean;
muteVideos: boolean;
compact: boolean;
hideText: boolean;
shouldLoadMedia: boolean;
Expand Down
6 changes: 6 additions & 0 deletions src/articles/social/SocialArticleView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
let minimized = false
const isArticleRepost = articleProps.type === 'reposts'
let compact: boolean | null = null;
function onUsernameClick(clickedArticle: Article) {
if (!clickedArticle.author)
return
Expand Down Expand Up @@ -243,6 +245,7 @@
article={quoted}
{timelineProps}
onMediaClick={index => onMediaClick(actualArticle.idPair, index)}
{compact}
/>
{/if}
<SocialNav
Expand All @@ -252,6 +255,7 @@
{modal}
{onLogData}
{onLogJSON}
{compact}
/>
</div>
{/if}
Expand All @@ -262,6 +266,7 @@
repost={isArticleRepost ? rootArticle : undefined}
{onLogData}
{onLogJSON}
bind:compact
/>
</div>
</div>
Expand All @@ -274,6 +279,7 @@
bind:divRef
bind:mediaRefs
bind:loadingStates
{compact}
/>
{/if}
</div>
20 changes: 19 additions & 1 deletion src/articles/social/SocialMedia.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
export let mediaRefs: (HTMLImageElement | HTMLVideoElement)[] = [];
export let loadingStates: LoadingState[] | null = null;
export let compact: boolean | null;
afterUpdate(() => {
const articleMediaEls = divRef?.querySelectorAll('.articleMedia')
if (articleMediaEls) {
Expand All @@ -39,6 +41,11 @@
margin-top: 1rem;
}
.socialMedia.socialMediaCompact {
display: flex;
flex-wrap: wrap;
}
.socialMedia video {
width: 100%;
border-radius: 8px;
Expand All @@ -51,6 +58,15 @@
border-radius: 8px;
}
.socialMediaCompact .imagesHolder, .socialMediaCompact video.articleMedia {
width: 50%;
aspect-ratio: 1;
}
.socialMediaCompact .imagesHolder:only-child, .socialMediaCompact video.articleMedia:only-child {
width: unset;
}
.imagesHolder:not(:last-child) {
margin-bottom: 2px;
}
Expand All @@ -67,6 +83,7 @@
.moreMedia {
display: flex;
width: 100%;
}
.moreMedia > button {
margin-left: auto;
Expand All @@ -75,7 +92,7 @@
}
</style>

<div class='socialMedia' bind:this={divRef}>
<div class='socialMedia' class:socialMediaCompact={compact ?? timelineProps.compact} bind:this={divRef}>
{#each article.medias.slice(0, !showAllMedia && timelineProps.maxMediaCount !== null ? timelineProps.maxMediaCount : undefined) as media, index (index)}
{@const isLoading = loadingStates && loadingStates[index] === LoadingState.Loading}
{#if loadingStates && loadingStates[index] === LoadingState.NotLoaded}
Expand Down Expand Up @@ -109,6 +126,7 @@
class='articleMedia'
controls
preload='auto'
muted={timelineProps.muteVideos}
on:click|preventDefault={() => onMediaClick(index)}
bind:this={mediaRefs[index]}
>
Expand Down
4 changes: 3 additions & 1 deletion src/articles/social/SocialNav.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
export let onLogData: () => void
export let onLogJSON: () => void
export let compact: boolean | null;
let actions = Object.values(getServices()[article.idPair.service].articleActions)
.filter(a => a.icon !== undefined)
.sort((a, b) => a.index - b.index)
Expand Down Expand Up @@ -126,7 +128,7 @@
<button class='dropdown-item' on:click={() => toggleMarkAsRead(article.idPair)}>
Mark as read
</button>
<button class='dropdown-item' on:click={() => timelineProps.compact = !timelineProps.compact}>
<button class='dropdown-item' on:click={() => compact = !compact}>
{ timelineProps.compact ? 'Show expanded' : 'Show compact' }
</button>
{#if article.url}
Expand Down
4 changes: 3 additions & 1 deletion src/containers/MasonryContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@
}
function getRatio(article: ArticleProps): number {
return 1 + getActualArticle(article).medias.reduce((acc, curr) => acc + (curr.ratio ?? 1), 0)
return 1 + getActualArticle(article).medias
.slice(0, props.timelineArticleProps.maxMediaCount ?? undefined)
.reduce((acc, curr) => acc + (curr.ratio ?? 1), 0)
}
</script>

Expand Down
1 change: 1 addition & 0 deletions src/extension/pixiv/followIllusts/FollowIllusts.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
customMethod: null,
reversed: true,
},
compact: true,
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/extension/pixiv/userPage/UserArtworksPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import {defaultTimeline} from '../../../timelines'
import MasonryContainer from '../../../containers/MasonryContainer.svelte'
import {loadMainStorage} from '../../../storages'
import UserPageEndpoint, {getUserId, UserAPIEndpoint} from '../../../services/pixiv/endpoints/user'
import {getUserId, UserAPIEndpoint} from '../../../services/pixiv/endpoints/user'
import {everyRefreshType} from '../../../services/endpoints'
import portal from '../../../usePortal'
import {SortMethod} from '../../../sorting'
Expand All @@ -30,6 +30,7 @@
customMethod: null,
reversed: true,
},
compact: true,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/extension/pixiv/userPage/UserBookmarksPage.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
customMethod: null,
reversed: false,
},
compact: true,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/filters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type GenericFilter = {
offset: number
includeOffset: boolean
}
//TODO Number of medias (with equal, less than, greater than, etc)

export function getFilterName(filterType: GenericFilter['type'], inverted: boolean): string {
if (inverted) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/pixiv/endpoints/bookmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default class BookmarkPageEndpoint extends PageEndpoint {
url.searchParams.set('is_first_page', this.hostPage.toString());
url.searchParams.set('lang', 'en`');

const response: FollowAjaxResponse = await PixivService.fetch(url.toString()).then(r => r.json());
const response: FollowAjaxResponse = await PixivService.fetch(url.toString(), {headers: {'Accept': 'application/json'}});
if (response?.body?.works) {
for (const work of Object.values(response.body.works))
getWritable<PixivArticle>({id: parseInt(work.id), service: PixivService.name})?.update(a => {
Expand Down Expand Up @@ -108,7 +108,7 @@ export class BookmarkAPIEndpoint extends LoadableEndpoint {
url.searchParams.set('rest', this.r18 ? 'hide' : 'show');
url.searchParams.set('lang', 'en');

const response: FollowAjaxResponse = await PixivService.fetch(url.toString()).then(r => r.json());
const response: FollowAjaxResponse = await PixivService.fetch(url.toString(), {headers: {'Accept': 'application/json'}})
if (response.error) {
console.error('Failed to fetch', response);
return [];
Expand Down
2 changes: 1 addition & 1 deletion src/services/pixiv/endpoints/follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class FollowAPIEndpoint extends LoadableEndpoint {
const url = new URL('https://www.pixiv.net/ajax/follow_latest/illust');
url.searchParams.set('p', (this.currentPage + 1).toString());
url.searchParams.set('mode', this.r18 ? 'r18' : 'all');
const response: FollowAPIResponse = await PixivService.fetch(url.toString()).then(r => r.json());
const response: FollowAPIResponse = await PixivService.fetch(url.toString(), {headers: {'Accept': 'application/json'}});

const markedAsReadStorage = getMarkedAsReadStorage(PixivService);

Expand Down
4 changes: 2 additions & 2 deletions src/services/pixiv/endpoints/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class UserAPIEndpoint extends LoadableEndpoint {
let url = new URL(`https://www.pixiv.net/ajax/user/${this.userId}/profile/all`);
url.searchParams.set('p', (this.currentPage + 1).toString());
url.searchParams.set('lang', 'en`');
const listResponse: UserListAjaxResponse = await PixivService.fetch(url.toString()).then(r => r.json());
const listResponse: UserListAjaxResponse = await PixivService.fetch(url.toString(), {headers: {'Accept': 'application/json'}});
const illustIds = Object.keys(listResponse.body.illusts);
//Decreasing order sort
illustIds.sort((a, b) => parseInt(b) - parseInt(a));
Expand All @@ -91,7 +91,7 @@ export class UserAPIEndpoint extends LoadableEndpoint {
url.searchParams.set('is_first_page', (this.currentPage === 0 ? 1 : 0).toString());
url.searchParams.set('lang', 'en');

const response: UserAjaxResponse = await PixivService.fetch(url.toString()).then(r => r.json()).then(r => r.json());
const response: UserAjaxResponse = await PixivService.fetch(url.toString(), {headers: {'Accept': 'application/json'}});
if (response.error) {
console.error('Failed to fetch', response);
return [];
Expand Down
6 changes: 3 additions & 3 deletions src/services/pixiv/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const PixivService: PixivServiceType = {
...newFetchingService(),
async fetchArticle(store: Writable<PixivArticle>) {
const article = get(store);
const json: PagesResponse = await this.fetch(`https://www.pixiv.net/ajax/illust/${article.id}/pages`).then(r => r.json());
const json: PagesResponse = await this.fetch(`https://www.pixiv.net/ajax/illust/${article.id}/pages`, {headers: {'Accept': 'application/json'}});

store.update(a => {
for (let i = 0; i < a.medias.length; ++i) {
Expand Down Expand Up @@ -68,7 +68,7 @@ export const PixivService: PixivServiceType = {
'X-CSRF-TOKEN': csrfToken,
},
body: JSON.stringify({illust_id: idPair.id}),
}).then(r => r.json());
});

if (response.error)
throw new Error('Error during like: ' + response.message);
Expand Down Expand Up @@ -117,7 +117,7 @@ export const PixivService: PixivServiceType = {
comment: '',
tags: [],
}),
}).then(r => r.json());
});

if (response.error)
throw new Error('Error during bookmark: ' + response.message);
Expand Down
8 changes: 7 additions & 1 deletion src/services/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Service<A extends Article = Article> {
readonly articles: { [id: string]: Writable<A> };
readonly endpointConstructors: Record<string, EndpointConstructorInfo>
userEndpoint: ((author: ArticleAuthor) => Endpoint) | null,
loadArticle: ((id: string) => Promise<ArticleWithRefs | null>) | null,
articleActions: { [name: string]: ArticleAction<A> };
requestImageLoad?: (id: ArticleId, index: number) => void;
getCachedArticles?: () => {[id: string]: object}
Expand Down Expand Up @@ -175,6 +176,7 @@ export function newService<A extends Article = Article>(name: string): Service<A
articles: {},
endpointConstructors: {},
userEndpoint: null,
loadArticle: null,
articleActions: {},
keepArticle() { return true; },
defaultFilter(filterType: string) { return {type:filterType, service: name};},
Expand All @@ -184,7 +186,11 @@ export function newService<A extends Article = Article>(name: string): Service<A
async fetch(url, init) {
if (this.isOnDomain) {
const response = await fetch(url, init);
return await response.json();

if (init?.headers && (init.headers as Record<string, string>)['Accept'] === 'application/json')
return await response.json();
else
return await response.text();
}else if (this.fetchInfo.type === FetchType.Extension) {
const response = await fetchExtension('extensionFetch', {
soshalthing: true,
Expand Down
2 changes: 1 addition & 1 deletion src/services/twitter/pageAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ export type ResponseError = {
data: object;
};

type ResponseSingleError = {
export type ResponseSingleError = {
name: string;
source: string;
code: number;
Expand Down
Loading

0 comments on commit 7c3e078

Please sign in to comment.