Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/components/StartPanel/StartPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
import { useGlobalDrop } from "../useGlobalDrop";
import { CharacterCard } from "@lenml/char-card-reader";
import { useGlobalPaste } from "../useGlobalPaste";
import { createBlackImage } from "../../tools/images";

// 对于某些特别的网站链接,转换为源图片非压缩地址
function getRawImageUrl(url: string) {
Expand Down Expand Up @@ -125,7 +126,28 @@ export const StartPanel = ({
setOriginalFileName(file.name);
async function readCardFile() {
if (file.type === "application/json") {
return CharacterCard.from_json(JSON.parse(await file.text()));
const json = JSON.parse(await file.text());
if (json.entries) {
if (!Array.isArray(json.entries) && typeof json.entries === "object") {
json.entries = Object.values(json.entries);
}
const tempAvatar = createBlackImage(file.name);
const avatar_url = tempAvatar.toDataURL();
return CharacterCard.from_json(
{
spec: "chara_card_v2",
spec_version: "3.0",
data: {
name: file.name.replace(/\.json$/i, ""),
description: "Auto-generated card for lorebook",
first_mes: "",
character_book: json,
},
},
avatar_url
);
}
return CharacterCard.from_json(json);
}
const arrayBuffer = await file.arrayBuffer();
setOriginalFile(arrayBuffer);
Expand Down
20 changes: 18 additions & 2 deletions src/components/tabs/CharacterBookTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ export const CharacterBookTab: FC<{
);
}, [bookData]);

const handleExportJson = () => {
const jsonStr = JSON.stringify(bookData, null, 2);
const blob = new Blob([jsonStr], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = (bookData.name || "lorebook") + ".json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
};

return (
<div>
<Field
Expand Down Expand Up @@ -106,9 +119,12 @@ export const CharacterBookTab: FC<{
onDeleteEntry={handleDeleteEntry}
/>
))}
<div className={styles.bookActions}>
<div className={styles.bookActions} style={{ display: "flex", gap: "8px" }}>
<Button icon={<Add24Regular />} onClick={handleAddEntry}>
Add Entry
{t("Add Entry")}
</Button>
<Button onClick={handleExportJson}>
{t("Export JSON")}
</Button>
</div>
</div>
Expand Down