Skip to content

Commit

Permalink
fix(frontend/frontend-embed): インポートパス・テーマまわりなどの修正 (#14535)
Browse files Browse the repository at this point in the history
* fix(frontend/frontend-embed): wrong imports

* enhance(frontend-embed): サーバーデフォルトのテーマがある場合はそちらを利用するように

* 🎨

* 🎨

* 🎨
  • Loading branch information
kakkokari-gtyih committed Sep 10, 2024
1 parent 672779a commit f393b6b
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 20 deletions.
37 changes: 28 additions & 9 deletions packages/frontend-embed/src/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,42 @@ import '@tabler/icons-webfont/dist/tabler-icons.scss';

import '@/style.scss';
import { createApp, defineAsyncComponent } from 'vue';
import lightTheme from '@@/themes/l-light.json5';
import darkTheme from '@@/themes/d-dark.json5';
import defaultLightTheme from '@@/themes/l-light.json5';
import defaultDarkTheme from '@@/themes/d-dark.json5';
import { MediaProxy } from '@@/js/media-proxy.js';
import { applyTheme } from './theme.js';
import { fetchCustomEmojis } from './custom-emojis.js';
import { DI } from './di.js';
import { serverMetadata } from './server-metadata.js';
import { url } from './config.js';
import { applyTheme, assertIsTheme } from '@/theme.js';
import { fetchCustomEmojis } from '@/custom-emojis.js';
import { DI } from '@/di.js';
import { serverMetadata } from '@/server-metadata.js';
import { url } from '@/config.js';
import { parseEmbedParams } from '@@/js/embed-page.js';
import { postMessageToParentWindow, setIframeId } from '@/post-message.js';

console.info('Misskey Embed');
import type { Theme } from '@/theme.js';

console.log('Misskey Embed');

const params = new URLSearchParams(location.search);
const embedParams = parseEmbedParams(params);

console.info(embedParams);
if (_DEV_) console.log(embedParams);

function parseThemeOrNull(theme: string | null): Theme | null {
if (theme == null) return null;
try {
const parsed = JSON.parse(theme);
if (assertIsTheme(parsed)) {
return parsed;
} else {
return null;
}
} catch (err) {
return null;
}
}

const lightTheme = parseThemeOrNull(serverMetadata.defaultLightTheme) ?? defaultLightTheme;
const darkTheme = parseThemeOrNull(serverMetadata.defaultDarkTheme) ?? defaultDarkTheme;

if (embedParams.colorMode === 'dark') {
applyTheme(darkTheme);
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/pages/clip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ misskeyApi('clips/show', {
.instanceIcon {
height: 24px;
border-radius: 4px;
border-radius: 3px;
}
}
</style>
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/pages/tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function top(ev: MouseEvent) {
.instanceIcon {
height: 24px;
border-radius: 4px;
border-radius: 3px;
}
}
</style>
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/pages/user-timeline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ misskeyApi('users/show', {
.instanceIcon {
height: 24px;
border-radius: 4px;
border-radius: 3px;
}
}
</style>
5 changes: 3 additions & 2 deletions packages/frontend-embed/src/server-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import * as Misskey from 'misskey-js';
import { misskeyApi } from '@/misskey-api.js';

const providedMetaEl = document.getElementById('misskey_meta');

const _serverMetadata = (providedMetaEl && providedMetaEl.textContent) ? JSON.parse(providedMetaEl.textContent) : null;
const _serverMetadata: Misskey.entities.MetaDetailed | null = (providedMetaEl && providedMetaEl.textContent) ? JSON.parse(providedMetaEl.textContent) : null;

// NOTE: devモードのときしか _serverMetadata が null になることは無い
export const serverMetadata = _serverMetadata ?? await misskeyApi('meta', {
export const serverMetadata: Misskey.entities.MetaDetailed = _serverMetadata ?? await misskeyApi('meta', {
detail: true,
});
8 changes: 5 additions & 3 deletions packages/frontend-embed/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export type Theme = {

let timeout: number | null = null;

export function assertIsTheme(theme: Record<string, unknown>): theme is Theme {
return typeof theme === 'object' && theme !== null && 'id' in theme && 'name' in theme && 'author' in theme && 'props' in theme;
}

export function applyTheme(theme: Theme, persist = true) {
if (timeout) window.clearTimeout(timeout);

Expand All @@ -35,8 +39,6 @@ export function applyTheme(theme: Theme, persist = true) {
document.documentElement.classList.remove('_themeChanging_');
}, 1000);

const colorScheme = theme.base === 'dark' ? 'dark' : 'light';

// Deep copy
const _theme = JSON.parse(JSON.stringify(theme));

Expand All @@ -58,7 +60,7 @@ export function applyTheme(theme: Theme, persist = true) {
document.documentElement.style.setProperty(`--${k}`, v.toString());
}

document.documentElement.style.setProperty('color-scheme', colorScheme);
// iframeを正常に透過させるために、cssのcolor-schemeは `light dark;` 固定にしてある。style.scss参照
}

function compile(theme: Theme): Record<string, string> {
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/ui.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import XNotFound from '@/pages/not-found.vue';
const page = location.pathname.split('/')[2];
const contentId = location.pathname.split('/')[3];
console.log(page, contentId);
if (_DEV_) console.log(page, contentId);
const embedParams = inject(DI.embedParams, defaultEmbedParams);
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/components/MkEmbedCodeGenDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SPDX-License-Identifier: AGPL-3.0-only
@close="cancel()"
@closed="$emit('closed')"
>
<template #header>{{ i18n.ts._embedCodeGen.title }}</template>
<template #header><i class="ti ti-code"></i> {{ i18n.ts._embedCodeGen.title }}</template>

<div :class="$style.embedCodeGenRoot">
<Transition
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend/src/scripts/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const getBuiltinThemes = () => Promise.all(
'd-cherry',
'd-ice',
'd-u0',
].map(name => import(`@/themes/${name}.json5`).then(({ default: _default }): Theme => _default)),
].map(name => import(`@@/themes/${name}.json5`).then(({ default: _default }): Theme => _default)),
);

export const getBuiltinThemesRef = () => {
Expand Down

0 comments on commit f393b6b

Please sign in to comment.