1
0
forked from mirror/misskey
misskey/src/client/init.ts

318 lines
7.8 KiB
TypeScript
Raw Normal View History

/**
* App entry point
*/
import Vue from 'vue';
import Vuex from 'vuex';
import VueMeta from 'vue-meta';
import PortalVue from 'portal-vue';
import VAnimateCss from 'v-animate-css';
import VueI18n from 'vue-i18n';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import VueHotkey from './scripts/hotkey';
import App from './app.vue';
import MiOS from './mios';
import { version, langs, instanceName, getLocale } from './config';
import PostFormDialog from './components/post-form-dialog.vue';
import Dialog from './components/dialog.vue';
import Menu from './components/menu.vue';
import { router } from './router';
2020-07-10 00:20:26 +09:00
import { applyTheme, lightTheme } from './scripts/theme';
2020-03-22 20:23:35 +09:00
import { isDeviceDarkmode } from './scripts/is-device-darkmode';
2020-03-31 09:11:43 +09:00
import createStore from './store';
import { clientDb, get, count } from './db';
import { setI18nContexts } from './scripts/set-i18n-contexts';
Vue.use(Vuex);
Vue.use(VueHotkey);
Vue.use(VueMeta);
Vue.use(PortalVue);
Vue.use(VAnimateCss);
Vue.use(VueI18n);
Vue.component('fa', FontAwesomeIcon);
require('./directives');
require('./components');
require('./widgets');
require('./filters');
Vue.mixin({
methods: {
destroyDom() {
this.$destroy();
if (this.$el.parentNode) {
this.$el.parentNode.removeChild(this.$el);
}
}
}
});
console.info(`Misskey v${version}`);
if (localStorage.getItem('theme') == null) {
applyTheme(lightTheme);
}
//#region Detect the user language
2020-02-12 07:12:58 +09:00
let lang = localStorage.getItem('lang');
2020-02-12 07:12:58 +09:00
if (lang == null) {
if (langs.map(x => x[0]).includes(navigator.language)) {
lang = navigator.language;
} else {
lang = langs.map(x => x[0]).find(x => x.split('-')[0] == navigator.language);
2020-02-12 07:12:58 +09:00
if (lang == null) {
// Fallback
lang = 'en-US';
}
}
2020-02-12 07:12:58 +09:00
localStorage.setItem('lang', lang);
}
//#endregion
// Detect the user agent
const ua = navigator.userAgent.toLowerCase();
const isMobile = /mobile|iphone|ipad|android/.test(ua);
// Get the <head> element
const head = document.getElementsByTagName('head')[0];
// If mobile, insert the viewport meta tag
if (isMobile || window.innerWidth <= 1024) {
const viewport = document.getElementsByName('viewport').item(0);
viewport.setAttribute('content',
`${viewport.getAttribute('content')},minimum-scale=1,maximum-scale=1,user-scalable=no`);
head.appendChild(viewport);
}
//#region Set lang attr
const html = document.documentElement;
html.setAttribute('lang', lang);
//#endregion
// http://qiita.com/junya/items/3ff380878f26ca447f85
document.body.setAttribute('ontouchstart', '');
// アプリ基底要素マウント
document.body.innerHTML = '<div id="app"></div>';
2020-03-31 09:11:43 +09:00
const store = createStore();
const os = new MiOS(store);
os.init(async () => {
window.addEventListener('storage', e => {
if (e.key === 'vuex') {
2020-03-31 09:11:43 +09:00
store.replaceState(JSON.parse(localStorage['vuex']));
} else if (e.key === 'i') {
location.reload();
}
2020-05-10 16:31:00 +09:00
}, false);
2020-03-31 09:11:43 +09:00
store.watch(state => state.device.darkMode, darkMode => {
2020-07-10 00:20:26 +09:00
import('./scripts/theme').then(({ builtinThemes }) => {
2020-04-04 08:25:28 +09:00
const themes = builtinThemes.concat(store.state.device.themes);
applyTheme(themes.find(x => x.id === (darkMode ? store.state.device.darkTheme : store.state.device.lightTheme)));
});
2020-03-25 22:49:42 +09:00
});
2020-03-22 20:23:35 +09:00
//#region Sync dark mode
2020-03-31 09:11:43 +09:00
if (store.state.device.syncDeviceDarkMode) {
store.commit('device/set', { key: 'darkMode', value: isDeviceDarkmode() });
2020-03-22 20:23:35 +09:00
}
window.matchMedia('(prefers-color-scheme: dark)').addListener(mql => {
2020-03-31 09:11:43 +09:00
if (store.state.device.syncDeviceDarkMode) {
store.commit('device/set', { key: 'darkMode', value: mql.matches });
2020-03-22 18:39:37 +09:00
}
});
2020-03-22 20:23:35 +09:00
//#endregion
2020-03-22 18:39:37 +09:00
//#region Fetch locale data
const i18n = new VueI18n();
await count(clientDb.i18n).then(async n => {
if (n === 0) return setI18nContexts(lang, version, i18n);
if ((await get('_version_', clientDb.i18n) !== version)) return setI18nContexts(lang, version, i18n, true);
i18n.locale = lang;
i18n.setLocaleMessage(lang, await getLocale());
});
//#endregion
2020-03-31 09:11:43 +09:00
if ('Notification' in window && store.getters.isSignedIn) {
// 許可を得ていなかったらリクエスト
if (Notification.permission === 'default') {
Notification.requestPermission();
}
}
const app = new Vue({
2020-03-31 09:11:43 +09:00
store: store,
i18n,
metaInfo: {
title: null,
titleTemplate: title => title ? `${title} | ${(instanceName || 'Misskey')}` : (instanceName || 'Misskey')
},
data() {
return {
stream: os.stream,
isMobile: isMobile,
i18n // TODO: 消せないか考える SEE: https://github.com/syuilo/misskey/pull/6396#discussion_r429511030
};
},
methods: {
2020-03-31 09:11:43 +09:00
api: (endpoint: string, data: { [x: string]: any } = {}, token?) => store.dispatch('api', { endpoint, data, token }),
signout: os.signout,
new(vm, props) {
const x = new vm({
parent: this,
propsData: props
}).$mount();
document.body.appendChild(x.$el);
return x;
},
dialog(opts) {
const vm = this.new(Dialog, opts);
const p: any = new Promise((res) => {
vm.$once('ok', result => res({ canceled: false, result }));
vm.$once('cancel', () => res({ canceled: true }));
});
p.close = () => {
vm.close();
};
return p;
},
menu(opts) {
const vm = this.new(Menu, opts);
const p: any = new Promise((res) => {
vm.$once('closed', () => res());
});
return p;
},
post(opts, cb) {
if (!this.$store.getters.isSignedIn) return;
const vm = this.new(PostFormDialog, opts);
if (cb) vm.$once('closed', cb);
(vm as any).focus();
},
2020-02-20 06:08:49 +09:00
sound(type: string) {
if (this.$store.state.device.sfxVolume === 0) return;
2020-02-20 06:08:49 +09:00
const sound = this.$store.state.device['sfx' + type.substr(0, 1).toUpperCase() + type.substr(1)];
if (sound == null) return;
const audio = new Audio(`/assets/sounds/${sound}.mp3`);
audio.volume = this.$store.state.device.sfxVolume;
audio.play();
}
},
router: router,
render: createEl => createEl(App)
});
os.app = app;
// マウント
app.$mount('#app');
2020-02-20 06:08:49 +09:00
2020-04-02 22:17:17 +09:00
os.stream.on('emojiAdded', data => {
// TODO
//store.commit('instance/set', );
});
2020-03-31 09:11:43 +09:00
if (store.getters.isSignedIn) {
2020-02-20 06:08:49 +09:00
const main = os.stream.useSharedConnection('main');
// 自分の情報が更新されたとき
main.on('meUpdated', i => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', i);
2020-02-20 06:08:49 +09:00
});
main.on('readAllNotifications', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadNotification: false
});
});
main.on('unreadNotification', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadNotification: true
});
});
main.on('unreadMention', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadMentions: true
});
});
main.on('readAllUnreadMentions', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadMentions: false
});
});
main.on('unreadSpecifiedNote', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadSpecifiedNotes: true
});
});
main.on('readAllUnreadSpecifiedNotes', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadSpecifiedNotes: false
});
});
main.on('readAllMessagingMessages', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadMessagingMessage: false
});
});
main.on('unreadMessagingMessage', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadMessagingMessage: true
});
app.sound('chatBg');
});
main.on('readAllAntennas', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadAntenna: false
});
});
main.on('unreadAntenna', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadAntenna: true
});
app.sound('antenna');
});
main.on('readAllAnnouncements', () => {
2020-03-31 09:11:43 +09:00
store.dispatch('mergeMe', {
2020-02-20 06:08:49 +09:00
hasUnreadAnnouncement: false
});
});
main.on('clientSettingUpdated', x => {
2020-03-31 09:11:43 +09:00
store.commit('settings/set', {
2020-02-20 06:08:49 +09:00
key: x.key,
value: x.value
});
});
// トークンが再生成されたとき
// このままではMisskeyが利用できないので強制的にサインアウトさせる
main.on('myTokenRegenerated', () => {
os.signout();
});
}
});