Skip to content

Commit

Permalink
feat: add setting for thumbnailPlaceholder
Browse files Browse the repository at this point in the history
  • Loading branch information
aidenlx committed May 13, 2021
1 parent 51aa2e7 commit 411b1de
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class MediaExtended extends Plugin {

processInternalEmbeds = processInternalEmbeds;
processInternalLinks = processInternalLinks.bind(this);
processExternalEmbeds = processExternalEmbeds;
processExternalEmbeds = processExternalEmbeds.bind(this);

async loadSettings() {
Object.assign(this.settings, await this.loadData());
Expand Down
4 changes: 2 additions & 2 deletions src/modules/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ export class ExternalEmbedHandler {
} else return this;
}

doVideoHost(): this | null {
doVideoHost(thumbnail: boolean): this | null {
const url = this.srcUrl;
if (!url) return this;

const newEl = getPlayer(url);
const newEl = getPlayer(url, thumbnail);
if (newEl) {
this.replaceWith(newEl);
return null;
Expand Down
24 changes: 11 additions & 13 deletions src/modules/videoHostTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,24 @@ export function getVideoInfo(src: URL): videoInfo | null {
}
}

export function getPlayer(url: URL): HTMLDivElement | null {
export function getPlayer(url: URL, thumbnail = false): HTMLDivElement | null {
let info = getVideoInfo(url);
if (!info) return null;
const container = createDiv({ cls: "external-video" });
switch (info.host) {
case Host.YouTube: {
setupThumbnail(container, info);
return container;
}
case Host.Vimeo: {
setupThumbnail(container, info);
return container;
}
case Host.Bilibili: {
setupThumbnail(container, info);
return container;
}
case Host.YouTube:
case Host.Vimeo:
if (thumbnail) setupThumbnail(container, info);
else setupPlyr(container, info);
break;
case Host.Bilibili:
if (thumbnail) setupThumbnail(container, info);
else setupIFrame(container, info);
break;
default:
assertNever(info.host);
}
return container;
}

function getIFrame(info: videoInfo) {
Expand Down
7 changes: 5 additions & 2 deletions src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ export function processInternalEmbeds(el: HTMLElement) {
}
}

export function processExternalEmbeds(docEl: HTMLElement) {
export function processExternalEmbeds(this: MediaExtended, docEl: HTMLElement) {
const handler = new ExternalEmbedHandler();
for (const el of docEl.querySelectorAll("img[referrerpolicy]")) {
// <img src="" referrerpolicy="no-referrer">
const img = el as HTMLImageElement;
handler.setTarget(img).doDirectLink()?.doVideoHost();
handler
.setTarget(img)
.doDirectLink()
?.doVideoHost(this.settings.thumbnailPlaceholder);
}
}
19 changes: 19 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ export interface MxSettings {
mediaFragmentsEmbed: boolean;
timestampLink: boolean;
extendedImageEmbedSyntax: boolean;
thumbnailPlaceholder: boolean;
}

export const DEFAULT_SETTINGS: MxSettings = {
mediaFragmentsEmbed: true,
timestampLink: false,
extendedImageEmbedSyntax: false,
thumbnailPlaceholder: false,
};

export class MESettingTab extends PluginSettingTab {
Expand Down Expand Up @@ -72,6 +74,23 @@ export class MESettingTab extends PluginSettingTab {
descEl.appendText("Restart the app to take effects");
}),
},
{
k: "thumbnailPlaceholder",
name: "Placeholder in favor of full player",
desc: createFragment((descEl) => {
descEl.appendText(
"If enabled, thumbnail placeholder will be used in favor of full player when page loads",
);
descEl.createEl("br");
descEl.appendText("Works with for Youtube/Vimeo/Bilibili embeds");
descEl.createEl("br");
descEl.appendText(
"Helpful when numerous video from Youtube/Vimeo/... is embeded in one single file",
);
descEl.createEl("br");
descEl.appendText("Restart the app to take effects");
}),
},
];

for (const o of options) {
Expand Down

0 comments on commit 411b1de

Please sign in to comment.