Skip to content

Commit

Permalink
refactor(frontend): frontend-embed/src/to-be-sharedを共通化 (misskey-dev#…
Browse files Browse the repository at this point in the history
…14536)

* refactor(frontend): shouldCollapsedを共通化

* refactor(frontend): config.js, worker-multi-dispatch.js, intl-const.jsを共通化

* fix(frontend-shared): fix type error

* refactor(frontend): is-link.jsと、同一の振る舞いをする記述を共通化

* fix

* fix lint

* lint fixes
  • Loading branch information
kakkokari-gtyih authored Sep 10, 2024
1 parent 0c2cfe3 commit 837a8e1
Show file tree
Hide file tree
Showing 118 changed files with 181 additions and 309 deletions.
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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 { url } from '@@/js/config.js';
import { parseEmbedParams } from '@@/js/embed-page.js';
import { postMessageToParentWindow, setIframeId } from '@/post-message.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmAcct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import * as Misskey from 'misskey-js';
import { toUnicode } from 'punycode/';
import { host as hostRaw } from '@/config.js';
import { host as hostRaw } from '@@/js/config.js';

defineProps<{
user: Misskey.entities.UserLite;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts">
import DrawBlurhash from '@/workers/draw-blurhash?worker';
import TestWebGL2 from '@/workers/test-webgl2?worker';
import { WorkerMultiDispatch } from '@/to-be-shared/worker-multi-dispatch.js';
import { WorkerMultiDispatch } from '@@/js/worker-multi-dispatch.js';
import { extractAvgColorFromBlurhash } from '@@/js/extract-avg-color-from-blurhash.js';

const canvasPromise = new Promise<WorkerMultiDispatch | HTMLCanvasElement>(resolve => {
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import { ref } from 'vue';
import EmA from './EmA.vue';
import { url as local } from '@/config.js';
import { url as local } from '@@/js/config.js';

const props = withDefaults(defineProps<{
url: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmMention.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SPDX-License-Identifier: AGPL-3.0-only
import { toUnicode } from 'punycode';
import { } from 'vue';
import tinycolor from 'tinycolor2';
import { host as localHost } from '@/config.js';
import { host as localHost } from '@@/js/config.js';

const props = defineProps<{
username: string;
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmMfm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import EmMention from '@/components/EmMention.vue';
import EmEmoji from '@/components/EmEmoji.vue';
import EmCustomEmoji from '@/components/EmCustomEmoji.vue';
import EmA from '@/components/EmA.vue';
import { host } from '@/config.js';
import { host } from '@@/js/config.js';

function safeParseFloat(str: unknown): number | null {
if (typeof str !== 'string' || str === '') return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend-embed/src/components/EmNote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ import EmUserName from '@/components/EmUserName.vue';
import EmTime from '@/components/EmTime.vue';
import { userPage } from '@/utils.js';
import { i18n } from '@/i18n.js';
import { shouldCollapsed } from '@/to-be-shared/collapsed.js';
import { url } from '@/config.js';
import { shouldCollapsed } from '@@/js/collapsed.js';
import { url } from '@@/js/config.js';

function getAppearNote(note: Misskey.entities.Note) {
return Misskey.note.isPureRenote(note) ? note.renote : note;
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend-embed/src/components/EmNoteDetailed.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ import EmAcct from '@/components/EmAcct.vue';
import { userPage } from '@/utils.js';
import { notePage } from '@/utils.js';
import { i18n } from '@/i18n.js';
import { shouldCollapsed } from '@/to-be-shared/collapsed.js';
import { shouldCollapsed } from '@@/js/collapsed.js';
import { serverMetadata } from '@/server-metadata.js';
import { url } from '@/config.js';
import { url } from '@@/js/config.js';
import EmMfm from '@/components/EmMfm.js';

const props = defineProps<{
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend-embed/src/components/EmSubNoteContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import * as Misskey from 'misskey-js';
import EmMediaList from '@/components/EmMediaList.vue';
import EmPoll from '@/components/EmPoll.vue';
import { i18n } from '@/i18n.js';
import { url } from '@/config.js';
import { shouldCollapsed } from '@/to-be-shared/collapsed.js';
import { url } from '@@/js/config.js';
import { shouldCollapsed } from '@@/js/collapsed.js';
import EmA from '@/components/EmA.vue';
import EmMfm from '@/components/EmMfm.js';

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmTime.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ SPDX-License-Identifier: AGPL-3.0-only
<script lang="ts" setup>
import { onMounted, onUnmounted, ref, computed } from 'vue';
import { i18n } from '@/i18n.js';
import { dateTimeFormat } from '@/to-be-shared/intl-const.js';
import { dateTimeFormat } from '@@/js/intl-const.js';

const props = withDefaults(defineProps<{
time: Date | string | number | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/components/EmUrl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ SPDX-License-Identifier: AGPL-3.0-only
import { ref } from 'vue';
import { toUnicode as decodePunycode } from 'punycode/';
import EmA from './EmA.vue';
import { url as local } from '@/config.js';
import { url as local } from '@@/js/config.js';

function safeURIDecode(str: string): string {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { markRaw } from 'vue';
import { I18n } from '@@/js/i18n.js';
import type { Locale } from '../../../locales/index.js';
import { locale } from '@/config.js';
import { locale } from '@@/js/config.js';

export const i18n = markRaw(new I18n<Locale>(locale, _DEV_));

Expand Down
2 changes: 1 addition & 1 deletion packages/frontend-embed/src/misskey-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as Misskey from 'misskey-js';
import { ref } from 'vue';
import { apiUrl } from '@/config.js';
import { apiUrl } from '@@/js/config.js';

export const pendingApiRequestsCount = ref(0);

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend-embed/src/pages/clip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import EmTimelineContainer from '@/components/EmTimelineContainer.vue';
import { misskeyApi } from '@/misskey-api.js';
import { i18n } from '@/i18n.js';
import { serverMetadata } from '@/server-metadata.js';
import { url, instanceName } from '@/config.js';
import { isLink } from '@/to-be-shared/is-link.js';
import { url, instanceName } from '@@/js/config.js';
import { isLink } from '@@/js/is-link.js';
import { defaultEmbedParams } from '@@/js/embed-page.js';
import { DI } from '@/di.js';

Expand Down
4 changes: 2 additions & 2 deletions packages/frontend-embed/src/pages/tag.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ import XNotFound from '@/pages/not-found.vue';
import EmTimelineContainer from '@/components/EmTimelineContainer.vue';
import { i18n } from '@/i18n.js';
import { serverMetadata } from '@/server-metadata.js';
import { url, instanceName } from '@/config.js';
import { isLink } from '@/to-be-shared/is-link.js';
import { url, instanceName } from '@@/js/config.js';
import { isLink } from '@@/js/is-link.js';
import { DI } from '@/di.js';
import { defaultEmbedParams } from '@@/js/embed-page.js';

Expand Down
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 @@ -59,7 +59,7 @@ import EmTimelineContainer from '@/components/EmTimelineContainer.vue';
import { misskeyApi } from '@/misskey-api.js';
import { i18n } from '@/i18n.js';
import { serverMetadata } from '@/server-metadata.js';
import { url, instanceName } from '@/config.js';
import { url, instanceName } from '@@/js/config.js';
import { defaultEmbedParams } from '@@/js/embed-page.js';
import { DI } from '@/di.js';

Expand Down
82 changes: 0 additions & 82 deletions packages/frontend-embed/src/to-be-shared/worker-multi-dispatch.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/frontend-embed/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import * as Misskey from 'misskey-js';
import { url } from '@/config.js';
import { url } from '@@/js/config.js';

export const acct = (user: Misskey.Acct) => {
return Misskey.acct.toString(user);
Expand Down
25 changes: 25 additions & 0 deletions packages/frontend-shared/@types/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type FIXME = any;

declare const _LANGS_: string[][];
declare const _VERSION_: string;
declare const _ENV_: string;
declare const _DEV_: boolean;
declare const _PERF_PREFIX_: string;
declare const _DATA_TRANSFER_DRIVE_FILE_: string;
declare const _DATA_TRANSFER_DRIVE_FOLDER_: string;
declare const _DATA_TRANSFER_DECK_COLUMN_: string;

// for dev-mode
declare const _LANGS_FULL_: string[][];

// TagCanvas
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
TagCanvas: any;
}
6 changes: 5 additions & 1 deletion packages/frontend-shared/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export default [
},
...pluginVue.configs['flat/recommended'],
{
files: ['js/**/*.{ts,vue}', '**/*.vue'],
files: [
'@types/**/*.ts',
'js/**/*.ts',
'**/*.vue',
],
languageOptions: {
globals: {
...Object.fromEntries(Object.entries(globals.node).map(([key]) => [key, 'off'])),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import * as Misskey from 'misskey-js';

export function shouldCollapsed(note: Misskey.entities.Note, urls: string[]): boolean {
const collapsed = note.cw == null && (
note.text != null && (
(note.text != null && (
(note.text.includes('$[x2')) ||
(note.text.includes('$[x3')) ||
(note.text.includes('$[x4')) ||
(note.text.includes('$[scale')) ||
(note.text.split('\n').length > 9) ||
(note.text.length > 500) ||
(urls.length >= 4)
) || note.files.length >= 5
)) || (note.files != null && note.files.length >= 5)
);

return collapsed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import type { Locale } from '../../../locales/index.js';

// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
const address = new URL(document.querySelector<HTMLMetaElement>('meta[property="instance_url"]')?.content || location.href);
const siteName = document.querySelector<HTMLMetaElement>('meta[property="og:site_name"]')?.content;

export const host = address.host;
export const hostname = address.hostname;
export const url = address.origin;
export const apiUrl = location.origin + '/api';
export const wsOrigin = location.origin;
export const lang = localStorage.getItem('lang') ?? 'en-US';
export const langs = _LANGS_;
const preParseLocale = localStorage.getItem('locale');
export const locale = preParseLocale ? JSON.parse(preParseLocale) : null;
export const instanceName = siteName === 'Misskey' || siteName == null ? host : siteName;
export let locale: Locale = preParseLocale ? JSON.parse(preParseLocale) : null;
export const version = _VERSION_;
export const instanceName = (siteName === 'Misskey' || siteName == null) ? host : siteName;
export const ui = localStorage.getItem('ui');
export const debug = localStorage.getItem('debug') === 'true';

export function updateLocale(newLocale: Locale): void {
locale = newLocale;
}
4 changes: 2 additions & 2 deletions packages/frontend-shared/js/emoji-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function char2fluentEmojiFilePath(char: string): string {
// Fluent Emojiは国旗非対応 https://github.com/microsoft/fluentui-emoji/issues/25
if (codes[0]?.startsWith('1f1')) return char2twemojiFilePath(char);
if (!codes.includes('200d')) codes = codes.filter(x => x !== 'fe0f');
codes = codes.filter(x => x && x.length);
const fileName = codes.map(x => x!.padStart(4, '0')).join('-');
codes = codes.filter(x => x != null && x.length > 0);
const fileName = (codes as string[]).map(x => x.padStart(4, '0')).join('-');
return `${fluentEmojiPngBase}/${fileName}.png`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

import { lang } from '@/config.js';
import { lang } from '@@/js/config.js';

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
export const versatileLang = (lang ?? 'ja-JP').replace('ja-KS', 'ja-JP');

let _dateTimeFormat: Intl.DateTimeFormat;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

function defaultUseWorkerNumber(prev: number, totalWorkers: number) {
function defaultUseWorkerNumber(prev: number) {
return prev + 1;
}

export class WorkerMultiDispatch<POST = any, RETURN = any> {
type WorkerNumberGetter = (prev: number, totalWorkers: number) => number;

export class WorkerMultiDispatch<POST = unknown, RETURN = unknown> {
private symbol = Symbol('WorkerMultiDispatch');
private workers: Worker[] = [];
private terminated = false;
private prevWorkerNumber = 0;
private getUseWorkerNumber = defaultUseWorkerNumber;
private getUseWorkerNumber: WorkerNumberGetter;
private finalizationRegistry: FinalizationRegistry<symbol>;

constructor(workerConstructor: () => Worker, concurrency: number, getUseWorkerNumber = defaultUseWorkerNumber) {
Expand All @@ -29,7 +31,7 @@ export class WorkerMultiDispatch<POST = any, RETURN = any> {
if (_DEV_) console.log('WorkerMultiDispatch: Created', this);
}

public postMessage(message: POST, options?: Transferable[] | StructuredSerializeOptions, useWorkerNumber: typeof defaultUseWorkerNumber = this.getUseWorkerNumber) {
public postMessage(message: POST, options?: Transferable[] | StructuredSerializeOptions, useWorkerNumber: WorkerNumberGetter = this.getUseWorkerNumber) {
let workerNumber = useWorkerNumber(this.prevWorkerNumber, this.workers.length);
workerNumber = Math.abs(Math.round(workerNumber)) % this.workers.length;
if (_DEV_) console.log('WorkerMultiDispatch: Posting message to worker', workerNumber, useWorkerNumber);
Expand All @@ -46,12 +48,14 @@ export class WorkerMultiDispatch<POST = any, RETURN = any> {
return workerNumber;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public addListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) {
this.workers.forEach(worker => {
worker.addEventListener('message', callback, options);
});
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public removeListener(callback: (this: Worker, ev: MessageEvent<RETURN>) => any, options?: boolean | AddEventListenerOptions) {
this.workers.forEach(worker => {
worker.removeEventListener('message', callback, options);
Expand Down
Loading

1 comment on commit 837a8e1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chromatic detects changes. Please review the changes on Chromatic.

Please sign in to comment.