Skip to content

Commit

Permalink
feat: readables
Browse files Browse the repository at this point in the history
  • Loading branch information
diauweb committed Dec 24, 2022
1 parent 101cc3b commit c7309c6
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 48 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
"license": "AGPL-3.0-or-later",
"type": "module",
"dependencies": {
"@svelteuidev/composables": "^0.7.7",
"@svelteuidev/core": "^0.7.7",
"fast-deep-equal": "^3.1.3",
"fs-extra": "^10.1.0",
"mongodb": "^4.13.0",
Expand Down
35 changes: 1 addition & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<meta charset="utf-8" />
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
<meta name="viewport" content="width=device-width" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.15.19/js/uikit.min.js" integrity="sha512-XnuVMFWQq1fYOxz8Q5ropIfdt3BrXEfABFjHmEBxhG7c6/K75aaOferYt2yU1YnxZiKGTIH8jdhs/DzchnGy/Q==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/uikit/3.15.19/css/uikit-core.min.css" integrity="sha512-0jWB8FzcCPpyKnIxS5Xqr3c2klF2X7dwoHrEQYwgzN5p1sttK8WoXEHTATBiFm5PQJ9PsUenL2u+7dxogt0SZQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
%sveltekit.head%
</head>
<body>
Expand Down
7 changes: 6 additions & 1 deletion src/lib/server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Reminder from './schema/Reminder';
import Talk from './schema/Talk';
import TextMap from './schema/TextMap';
import Material from './schema/Material';
import Document from './schema/Document';
import Localization from './schema/Localization';

const uri = 'mongodb://127.0.0.1:27017';

Expand Down Expand Up @@ -51,6 +53,7 @@ export async function initDatabase() {
console.timeEnd('regenerate');
}

// get db version list
db.collection('Version')
.find({})
.forEach((e) => {
Expand Down Expand Up @@ -88,7 +91,9 @@ const collections: [string, Schema, OneOrMany<KV>?][] = [
['Dialog', Dialog, [{ Id: 1 }, { TalkRole__Id: 1 }, { NextDialogs: 1 }, { TalkContentTextMapHash: 1, TalkTitleTextMapHash: 1, TalkRoleNameTextMapHash: 1}]],
['Talk', Talk, { Id: 1 }],
['Reminder', Reminder, { Id: 1 }],
['Material', Material, { Id: 1 }]
['Material', Material, { Id: 1 }],
['Document', Document, { Id: 1 }],
['Localization', Localization, { Id: 1}],
];

async function addCollections() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/server/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export async function getSchemaObject (schema: Schema | ((args: any) => Schema),
function byString (o: any, s: any) {
s = s.replace(/\[(\w+)\]/g, '.$1');
s = s.replace(/^\./, '');
let a = s.split('.');
const a = s.split('.');
for (let i = 0, n = a.length; i < n; ++i) {
let k = a[i];
const k = a[i];
if (k in o) {
o = o[k];
} else {
Expand Down
21 changes: 21 additions & 0 deletions src/lib/server/schema/Document.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default [
{
match: "**",
type: "remapColl",
filename: "ExcelBinOutput/DocumentExcelConfigData.json",
},
{
match: ">=3.3.0",
remap: {
Id: 'id',
TitleTextMapHash: 'titleTextMapHash',
ContentLocalizedId: 'contentLocalizedId',
PreviewPath: 'previewPath',
VideoPath: 'videoPath',
}
},
{
match: "*",
remap: {}
}
]
19 changes: 19 additions & 0 deletions src/lib/server/schema/Localization.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default [
{
match: "**",
type: "remapColl",
filename: "ExcelBinOutput/LocalizationExcelConfigData.json",
},
{
match: ">=3.3.0",
remap: {
Id: 'id',
AssetType: 'assetType',
DefaultPath: 'defaultPath',
},
},
{
match: "*",
remap: {}
}
]
1 change: 0 additions & 1 deletion src/lib/server/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export async function inlineLanguage(resultItem: any) {
_ver: resultItem._ver,
hash: { $in: Object.keys(translate).map(i => parseInt(i)) }
}))

q.forEach(e => resultItem[translate[e.hash]] = e)
Object.values(translate).forEach(e => {
if (typeof resultItem[e] !== 'object') {
Expand Down
22 changes: 16 additions & 6 deletions src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import 'uikit/dist/js/uikit';
import 'uikit/dist/css/uikit.css';
import Lang from './Lang.svelte';
// import 'uikit/dist/js/uikit';
// import 'uikit/dist/css/uikit.css';
import { lang } from '$lib/client-settings';
let ver = __version__;
export let data: { dataver: { ver: string }};
Expand All @@ -22,13 +22,23 @@
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li><a href="/npc">NPC</a></li>
<li><a href="/document">Documents</a></li>
</ul>
</div>
</li>
</ul>
<ul class="uk-navbar-nav">
<li>
<a href={"#"}>{$lang.toUpperCase()}</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li><a href={"#"} on:click="{() => lang.set('cn')}">CN</a></li>
<li><a href={"#"} on:click="{() => lang.set('en')}">EN</a></li>
<li><a href={"#"} on:click="{() => lang.set('jp')}">JP</a></li>
</ul>
</div>
</li>
</ul>
</div>
<div class="uk-navbar-right">
<Lang />
</div>
</div>
</header>
Expand Down
13 changes: 13 additions & 0 deletions src/routes/(app)/document/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { toPlainObject } from '$lib/util';
import { inlineLanguage } from "$lib/server/util";
import { tillCurrentOid, currentOid, find } from '$lib/server/db';

export const load: PageLoad = async () => {
const documents = await find('Document', {
_ver: currentOid(),
});
const d = await Promise.all(documents.map(e => inlineLanguage({...e, _ver: tillCurrentOid()})));
return {
documents: toPlainObject(d),
};
};
23 changes: 23 additions & 0 deletions src/routes/(app)/document/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import Translated from '$lib/components/Translated.svelte';
import { lang } from '$lib/client-settings';
export let data;
let showAll = true;
let filter = '';
</script>

<h1>Documents</h1>
<input type="search" class="uk-input uk-margin-top" bind:value={filter} placeholder="Filter"/>

<table class="uk-table uk-table-small uk-table-striped">
<tbody>
{#each data.documents as n}
{#if filter.trim() === '' || RegExp(filter).test(n.TitleTextMapHash[$lang]) }
<tr>
<th>{n.Id}</th>
<td><a href={`/document/${n.Id}`}><Translated id={n.TitleTextMapHash} /></a></td>
</tr>
{/if}
{/each}
</tbody>
</table>
42 changes: 42 additions & 0 deletions src/routes/(app)/document/[id]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { toPlainObject } from '$lib/util';
import { error } from '@sveltejs/kit';
import { currentOid, findOne } from "$lib/server/db";
import { getVersion } from "$lib/server/version";
import { getFile } from "$lib/server/git";
import log from 'npmlog';

// import { inlineLanguage } from "$lib/server/util";

export const load: PageServerLoad = async ({ params }) => {
const id = params.id;
if (!id) throw error(404);

const document = await findOne('Document', {
_ver: currentOid(),
Id: parseInt(id),
});

const content = await findOne('Localization', {
_ver: currentOid(),
Id: document.ContentLocalizedId,
});

const path = content.DefaultPath.split('/').slice(-1)[0];
const text = {};
try {
const readablePath = (lang, name) => `Readable/${lang}/${name}${lang === 'CHS' ? '' : '_' + lang}.txt`;
const sha = getVersion();
text['cn'] = (await getFile(readablePath('CHS', path), sha)).toString();
text['en'] = (await getFile(readablePath('EN', path), sha)).toString();
text['jp'] = (await getFile(readablePath('JP', path), sha)).toString();
} catch (e) {
log.error(e);
}

// const d = await inlineLanguage(document);
return {
document: toPlainObject(document),
content: toPlainObject(content),
text,
};
};
43 changes: 43 additions & 0 deletions src/routes/(app)/document/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script lang="ts">
import { lang } from '$lib/client-settings';
import Translated from '$lib/components/Translated.svelte';
export let data;
</script>

<h1>Document {data.document.Id}</h1>
<table class="uk-table uk-table-small uk-table-divider">
<tr>
<th>ContentLocalizedId</th>
<td>{data.document.ContentLocalizedId}</td>
</tr>
<tr>
<th>PreviewPath</th>
<td>{data.document.PreviewPath}</td>
</tr>
<tr>
<th>AssetType</th>
<td>{data.content.AssetType}</td>
</tr>
<tr>
<th>DefaultPath</th>
<td>{data.content.DefaultPath}</td>
</tr>
</table>
<article>
<h5><Translated id={data.document.TitleTextMapHash}/></h5>
<p class="content">
{#if $lang === 'en'}
{data.text.en};
{:else if $lang === 'jp'}
{data.text.jp}
{:else}<!-- is chinese -->
{data.text.cn}
{/if}
</p>
</article>

<style>
.content {
white-space: pre-wrap;
}
</style>
4 changes: 2 additions & 2 deletions src/routes/(prepare)/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import 'uikit/dist/js/uikit';
import 'uikit/dist/css/uikit.css';
// import 'uikit/dist/js/uikit';
// import 'uikit/dist/css/uikit.css';
import { onMount } from 'svelte';
onMount(() => {
setInterval(() => {
Expand Down

0 comments on commit c7309c6

Please sign in to comment.