This commit is contained in:
syuilo
2024-08-23 08:12:09 +09:00
parent 2a915374dd
commit ffba07a6e7
5 changed files with 91 additions and 88 deletions

View File

@@ -5,16 +5,6 @@
//#region Embed関連の定義
let _isEmbedPage: boolean | null = null;
/** 埋め込みページかどうか */
export function isEmbedPage() {
if (_isEmbedPage === null) {
_isEmbedPage = location.pathname.startsWith('/embed/');
}
return _isEmbedPage;
}
/** 埋め込みの対象となるエンティティ(/embed/xxx の xxx の部分と対応させる) */
const embeddableEntities = [
'notes',
@@ -30,7 +20,7 @@ export type EmbeddableEntity = typeof embeddableEntities[number];
export const embedRouteWithScrollbar: EmbeddableEntity[] = [
'clips',
'tags',
'user-timeline'
'user-timeline',
];
/** 埋め込みコードのパラメータ */
@@ -57,42 +47,3 @@ export const defaultEmbedParams = {
} as const satisfies EmbedParams;
//#endregion
/**
* パラメータを正規化する(埋め込みページ初期化用)
* @param searchParams URLSearchParamsもしくはクエリ文字列
* @returns 正規化されたパラメータ
*/
export function parseEmbedParams(searchParams: URLSearchParams | string): ParsedEmbedParams {
let _searchParams: URLSearchParams;
if (typeof searchParams === 'string') {
_searchParams = new URLSearchParams(searchParams);
} else if (searchParams instanceof URLSearchParams) {
_searchParams = searchParams;
} else {
throw new Error('searchParams must be URLSearchParams or string');
}
const params: EmbedParams = {};
for (const key in defaultEmbedParams) {
const value = _searchParams.get(key);
if (value != null) {
if (value === 'true') {
params[key] = true;
} else if (value === 'false') {
params[key] = false;
} else if (!isNaN(Number(value))) {
params[key] = Number(value);
} else if (key === 'colorMode' && ['light', 'dark'].includes(value)) {
params[key] = value as 'light' | 'dark';
} else {
params[key] = value;
}
}
}
return {
...defaultEmbedParams,
...params,
};
}