Skip to content

Commit

Permalink
feat(web): 使术语表导出导入格式更加人类可读
Browse files Browse the repository at this point in the history
  • Loading branch information
FishHawk committed Dec 17, 2024
1 parent 9549fba commit 14f375f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 18 deletions.
43 changes: 37 additions & 6 deletions web/src/model/Glossary.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
export type Glossary = { [key: string]: string };

export namespace Glossary {
export const encodeToText = (glossary: Glossary) => {
const toJson = (glossary: Glossary) => {
return JSON.stringify(glossary, null, 2);
};

export const decodeFromText = (text: string): Glossary | undefined => {
const fromJson = (text: string): Glossary | undefined => {
try {
const obj = JSON.parse(text);
if (typeof obj !== 'object') return;
const inputGlossary: { [key: string]: string } = {};
const glossary: Glossary = {};
for (const jp in obj) {
const zh = obj[jp];
if (typeof zh !== 'string') return;
inputGlossary[jp] = zh;
glossary[jp] = zh;
}
return inputGlossary;
return glossary;
} catch {
return;
}
};

const delimiter = '=>';
const toHumanReadableFormat = (glossary: Glossary) => {
const lines = [];
for (const jp in glossary) {
const zh = glossary[jp];
lines.push(`${jp} ${delimiter} ${zh}`);
}
return lines.join('\n');
};
const fromHumanReadableFormat = (text: string): Glossary | undefined => {
const glossary: Glossary = {};
for (let line of text.split('\n')) {
line = line.trim();
if (line === '') continue;

const parts = line.split(delimiter);
console.log(parts);
if (parts.length !== 2) return;

const [jp, zh] = parts;
glossary[jp.trim()] = zh.trim();
}
return glossary;
};

export const toText = (glossary: Glossary) => {
return toHumanReadableFormat(glossary);
};
export const fromText = (text: string): Glossary | undefined => {
return fromJson(text) ?? fromHumanReadableFormat(text);
};
}
24 changes: 12 additions & 12 deletions web/src/pages/components/GlossaryButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ const addTerm = () => {
};
const exportGlossary = () => {
navigator.clipboard.writeText(Glossary.encodeToText(glossary.value));
navigator.clipboard.writeText(Glossary.toText(glossary.value));
message.info('导出成功,已经将术语表复制到剪切板');
};
const importGlossary = () => {
const importedGlossary = Glossary.decodeFromText(importGlossaryRaw.value);
const importedGlossary = Glossary.fromText(importGlossaryRaw.value);
if (importedGlossary === undefined) {
message.error('导入失败:术语表格式不正确');
} else {
Expand Down Expand Up @@ -174,13 +174,16 @@ const importGlossary = () => {
/>
</n-input-group>

<n-input-group>
<n-input
v-model:value="importGlossaryRaw"
size="small"
placeholder="批量导入术语表"
:input-props="{ spellcheck: false }"
/>
<n-input
v-model:value="importGlossaryRaw"
type="textarea"
size="small"
placeholder="批量导入术语表"
:input-props="{ spellcheck: false }"
:rows="1"
/>

<n-flex align="center" :wrap="false">
<c-button
label="导出"
:round="false"
Expand All @@ -193,9 +196,6 @@ const importGlossary = () => {
size="small"
@action="importGlossary"
/>
</n-input-group>

<n-flex align="center" :wrap="false">
<c-button
v-if="whoami.isMaintainer"
secondary
Expand Down

0 comments on commit 14f375f

Please sign in to comment.