ノーマライズ処理を共通化

This commit is contained in:
kakkokari-gtyih 2024-06-26 11:38:18 +09:00
parent be15242bf6
commit 57906fd563
2 changed files with 17 additions and 6 deletions

View file

@ -124,12 +124,12 @@ function ok() {
// URLparams // URLparams
const paramsForUrl = computed<EmbedParams>(() => ({ const paramsForUrl = computed<EmbedParams>(() => ({
header: header.value === true ? undefined : header.value, header: header.value,
autoload: autoload.value === false ? undefined : autoload.value, autoload: autoload.value,
maxHeight: typeof maxHeight.value === 'number' ? Math.max(0, maxHeight.value) : undefined, maxHeight: typeof maxHeight.value === 'number' ? Math.max(0, maxHeight.value) : undefined,
colorMode: colorMode.value === 'auto' ? undefined : colorMode.value, colorMode: colorMode.value === 'auto' ? undefined : colorMode.value,
rounded: rounded.value === true ? undefined : rounded.value, rounded: rounded.value,
border: border.value === true ? undefined : border.value, border: border.value,
})); }));
// paramsref // paramsref
@ -142,7 +142,7 @@ const embedPreviewUrl = computed(() => {
const maxHeight = parseInt(paramClass.get('maxHeight')!); const maxHeight = parseInt(paramClass.get('maxHeight')!);
paramClass.set('maxHeight', maxHeight === 0 ? '500' : Math.min(maxHeight, 700).toString()); // 700px paramClass.set('maxHeight', maxHeight === 0 ? '500' : Math.min(maxHeight, 700).toString()); // 700px
} }
return `${url}/embed/${props.entity}/${_idOrUsername}?${paramClass.toString()}`; return `${url}/embed/${props.entity}/${_idOrUsername}${paramClass.toString() ? '?' + paramClass.toString() : ''}`;
}); });
const isEmbedWithScrollbar = computed(() => embedRouteWithScrollbar.includes(props.entity)); const isEmbedWithScrollbar = computed(() => embedRouteWithScrollbar.includes(props.entity));

View file

@ -35,11 +35,22 @@ export type EmbedParams = {
header?: boolean; header?: boolean;
}; };
// パラメータのデフォルトの値
export const defaultEmbedParams: EmbedParams = {
maxHeight: undefined,
colorMode: undefined,
rounded: true,
border: true,
autoload: false,
header: true,
};
export function normalizeEmbedParams(params: EmbedParams): Record<string, string> { export function normalizeEmbedParams(params: EmbedParams): Record<string, string> {
// paramsのvalueをすべてstringに変換。undefinedやnullはプロパティごと消す // paramsのvalueをすべてstringに変換。undefinedやnullはプロパティごと消す
const normalizedParams: Record<string, string> = {}; const normalizedParams: Record<string, string> = {};
for (const key in params) { for (const key in params) {
if (params[key] == null) { // デフォルトの値と同じならparamsに含めない
if (params[key] == null || params[key] === defaultEmbedParams[key]) {
continue; continue;
} }
switch (typeof params[key]) { switch (typeof params[key]) {