refactor(Parser): Update view models to use RendererContext.ts

May or may not be a breaking change for some.
This commit is contained in:
Luan
2024-12-31 06:41:17 -03:00
parent 3c28c0d2c2
commit f76a8e5d75
6 changed files with 36 additions and 32 deletions

View File

@@ -11,7 +11,7 @@ export default class AvatarStackView extends YTNode {
public avatars: ObservedArray<AvatarView>;
public text?: Text;
public renderer_context?: RendererContext;
public renderer_context: RendererContext;
constructor(data: RawNode) {
super();
@@ -20,7 +20,6 @@ export default class AvatarStackView extends YTNode {
if (Reflect.has(data, 'text'))
this.text = Text.fromAttributed(data.text);
if (Reflect.has(data, 'rendererContext'))
this.renderer_context = new RendererContext(data.rendererContext);
this.renderer_context = new RendererContext(data.rendererContext);
}
}

View File

@@ -1,18 +1,18 @@
import NavigationEndpoint from './NavigationEndpoint.js';
import { YTNode } from '../helpers.js';
import type { RawNode } from '../index.js';
import RendererContext from './misc/RendererContext.js';
export default class ButtonCardView extends YTNode {
static type = 'ButtonCardView';
title: string;
icon_name: string;
on_tap_endpoint: NavigationEndpoint;
public title: string;
public icon_name: string;
public renderer_context: RendererContext;
constructor(data: RawNode) {
super();
this.title = data.title;
this.icon_name = data.image.sources[0].clientResource.imageName;
this.on_tap_endpoint = new NavigationEndpoint(data.rendererContext.commandContext.onTap);
this.renderer_context = new RendererContext(data.rendererContext);
}
}

View File

@@ -1,21 +1,19 @@
import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import NavigationEndpoint from './NavigationEndpoint.js';
import AvatarView from './AvatarView.js';
import RendererContext from './misc/RendererContext.js';
export default class DecoratedAvatarView extends YTNode {
static type = 'DecoratedAvatarView';
avatar: AvatarView | null;
a11y_label: string;
on_tap_endpoint?: NavigationEndpoint;
public avatar: AvatarView | null;
public a11y_label: string;
public renderer_context: RendererContext;
constructor(data: RawNode) {
super();
this.avatar = Parser.parseItem(data.avatar, AvatarView);
this.a11y_label = data.a11yLabel;
if (data.rendererContext?.commandContext?.onTap) {
this.on_tap_endpoint = new NavigationEndpoint(data.rendererContext.commandContext.onTap);
}
this.renderer_context = new RendererContext(data.rendererContext);
}
}

View File

@@ -2,15 +2,16 @@ import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import EngagementPanelSectionList from './EngagementPanelSectionList.js';
import Text from './misc/Text.js';
import RendererContext from './misc/RendererContext.js';
export default class DescriptionPreviewView extends YTNode {
static type = 'DescriptionPreviewView';
description?: Text;
max_lines?: number;
truncation_text?: Text;
always_show_truncation_text: boolean;
more_endpoint?: {
public description?: Text;
public max_lines?: number;
public truncation_text?: Text;
public always_show_truncation_text: boolean;
public more_endpoint?: {
show_engagement_panel_endpoint: {
engagement_panel: EngagementPanelSectionList | null,
engagement_panel_popup_type: string;
@@ -20,6 +21,7 @@ export default class DescriptionPreviewView extends YTNode {
}
}
};
public renderer_context: RendererContext;
constructor(data: RawNode) {
super();
@@ -35,6 +37,7 @@ export default class DescriptionPreviewView extends YTNode {
this.always_show_truncation_text = !!data.alwaysShowTruncationText;
// @TODO: Do something about this.
if (data.rendererContext.commandContext?.onTap?.innertubeCommand?.showEngagementPanelEndpoint) {
const endpoint = data.rendererContext.commandContext?.onTap?.innertubeCommand?.showEngagementPanelEndpoint;
@@ -49,5 +52,7 @@ export default class DescriptionPreviewView extends YTNode {
}
};
}
this.renderer_context = new RendererContext(data.rendererContext);
}
}

View File

@@ -2,24 +2,23 @@ import { YTNode } from '../helpers.js';
import { Parser, type RawNode } from '../index.js';
import CollectionThumbnailView from './CollectionThumbnailView.js';
import LockupMetadataView from './LockupMetadataView.js';
import NavigationEndpoint from './NavigationEndpoint.js';
import RendererContext from './misc/RendererContext.js';
export default class LockupView extends YTNode {
static type = 'LockupView';
content_image: CollectionThumbnailView | null;
metadata: LockupMetadataView | null;
content_id: string;
content_type: 'VIDEO' | 'MOVIE' | 'CHANNEL' | 'CLIP' | 'SOURCE' | 'PLAYLIST' | 'ALBUM' | 'PODCAST' | 'SHOPPING_COLLECTION' | 'SHORT' | 'GAME' | 'PRODUCT';
on_tap_endpoint: NavigationEndpoint;
public content_image: CollectionThumbnailView | null;
public metadata: LockupMetadataView | null;
public content_id: string;
public content_type: 'VIDEO' | 'MOVIE' | 'CHANNEL' | 'CLIP' | 'SOURCE' | 'PLAYLIST' | 'ALBUM' | 'PODCAST' | 'SHOPPING_COLLECTION' | 'SHORT' | 'GAME' | 'PRODUCT';
public renderer_context: RendererContext;
constructor(data: RawNode) {
super();
this.content_image = Parser.parseItem(data.contentImage, CollectionThumbnailView);
this.metadata = Parser.parseItem(data.metadata, LockupMetadataView);
this.content_id = data.contentId;
this.content_type = data.contentType.replace('LOCKUP_CONTENT_TYPE_', '');
this.on_tap_endpoint = new NavigationEndpoint(data.rendererContext.commandContext.onTap);
this.renderer_context = new RendererContext(data.rendererContext);
}
}

View File

@@ -12,17 +12,20 @@ export type AccessibilityContext = {
export default class RendererContext {
public command_context: CommandContext;
public accessibility_context: AccessibilityContext;
constructor(data: RawNode) {
constructor(data?: RawNode) {
this.command_context = {};
this.accessibility_context = {};
if (!data)
return;
if (Reflect.has(data, 'commandContext')) {
if (Reflect.has(data.commandContext, 'onTap')) {
this.command_context.on_tap = new NavigationEndpoint(data.commandContext.onTap);
}
}
if (Reflect.has(data, 'accessibilityContext')) {
if (Reflect.has(data.accessibilityContext, 'label')) {
this.accessibility_context.label = data.accessibilityContext.label;