forked from akai/readest
This commit is contained in:
@@ -11,13 +11,14 @@ import { DBBookConfig, DBBook, DBBookNote } from '@/types/records';
|
||||
import { sanitizeString } from './sanitize';
|
||||
|
||||
export const transformBookConfigToDB = (bookConfig: unknown, userId: string): DBBookConfig => {
|
||||
const { bookHash, progress, location, searchConfig, viewSettings, updatedAt } =
|
||||
const { bookHash, progress, location, xpointer, searchConfig, viewSettings, updatedAt } =
|
||||
bookConfig as BookConfig;
|
||||
|
||||
return {
|
||||
user_id: userId,
|
||||
book_hash: bookHash!,
|
||||
location: location,
|
||||
xpointer: xpointer,
|
||||
progress: progress && JSON.stringify(progress),
|
||||
search_config: searchConfig && JSON.stringify(searchConfig),
|
||||
view_settings: viewSettings && JSON.stringify(viewSettings),
|
||||
@@ -26,10 +27,12 @@ export const transformBookConfigToDB = (bookConfig: unknown, userId: string): DB
|
||||
};
|
||||
|
||||
export const transformBookConfigFromDB = (dbBookConfig: DBBookConfig): BookConfig => {
|
||||
const { book_hash, progress, location, search_config, view_settings, updated_at } = dbBookConfig;
|
||||
const { book_hash, progress, location, xpointer, search_config, view_settings, updated_at } =
|
||||
dbBookConfig;
|
||||
return {
|
||||
bookHash: book_hash,
|
||||
location,
|
||||
xpointer,
|
||||
progress: progress && JSON.parse(progress),
|
||||
searchConfig: search_config && JSON.parse(search_config),
|
||||
viewSettings: view_settings && JSON.parse(view_settings),
|
||||
|
||||
@@ -502,13 +502,13 @@ export class XCFI {
|
||||
|
||||
export const getCFIFromXPointer = async (
|
||||
xpointer: string,
|
||||
doc: Document,
|
||||
index: number,
|
||||
doc?: Document,
|
||||
index?: number,
|
||||
bookDoc?: BookDoc,
|
||||
) => {
|
||||
const xSpineIndex = XCFI.extractSpineIndex(xpointer);
|
||||
let converter: XCFI;
|
||||
if (index === xSpineIndex) {
|
||||
if (index === xSpineIndex && doc) {
|
||||
converter = new XCFI(doc, index || 0);
|
||||
} else {
|
||||
const doc = await bookDoc?.sections?.[xSpineIndex]?.createDocument();
|
||||
@@ -519,3 +519,23 @@ export const getCFIFromXPointer = async (
|
||||
const cfi = converter.xPointerToCFI(xpointer);
|
||||
return cfi;
|
||||
};
|
||||
|
||||
export const getXPointerFromCFI = async (
|
||||
cfi: string,
|
||||
doc?: Document,
|
||||
index?: number,
|
||||
bookDoc?: BookDoc,
|
||||
): Promise<XPointer> => {
|
||||
const xSpineIndex = XCFI.extractSpineIndex(cfi);
|
||||
let converter: XCFI;
|
||||
if (index === xSpineIndex && doc) {
|
||||
converter = new XCFI(doc, index || 0);
|
||||
} else {
|
||||
const doc = await bookDoc?.sections?.[xSpineIndex]?.createDocument();
|
||||
if (!doc) throw new Error('Failed to load document for CFI conversion.');
|
||||
converter = new XCFI(doc, xSpineIndex || 0);
|
||||
}
|
||||
|
||||
const xpointer = converter.cfiToXPointer(cfi);
|
||||
return xpointer;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user