Skip to content

Commit

Permalink
fix: epub parse & add useproce (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
baicie authored Jan 9, 2024
1 parent 2c12b72 commit 55d6894
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 124 deletions.
5 changes: 2 additions & 3 deletions packages/client/modules/common-reader/main.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<script lang='ts' setup>
import type { SearchOnlineResult } from '@b-reader/utils'
import { ConfigProvider, Layout, LayoutContent, LayoutSider, Tree } from 'ant-design-vue'
import type { DataNode, EventDataNode, TreeProps } from 'ant-design-vue/es/tree'
import { computed, onBeforeMount, reactive } from 'vue'
import type { SearchOnlineResult } from '@b-reader/utils'
import { useThrottleFn } from '@vueuse/core'
import { computed, onBeforeMount } from 'vue'
import { locale, theme } from '../../src/theme'
import { useCommonReader } from './use-reader'
Expand Down
2 changes: 2 additions & 0 deletions packages/client/modules/common-reader/use-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ export function useCommonReader() {
title: state.navs[0].name,
},
})
// eslint-disable-next-line no-console
console.log('navs', state.navs)
break
case 'reader:common:content:res': {
const { path, content, scroll, title } = data.data
Expand Down
48 changes: 20 additions & 28 deletions packages/epub/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,36 +114,28 @@ export class Epub {
}

private async parseConent(options?: ParserOptions) {
try {
for (const item of this.spine ?? []) {
const manifest = this.manifest?.find(manifest => manifest.id === item.idref)
if (manifest && manifest.href) {
const filePath = resolveId(this.fullPath, manifest?.href)
const content = await this.usezip.fileFileContent(filePath)
const xml = await this.usexml.parse(content, {
preserveChildrenOrder: true,
explicitChildren: true,
...options,
})
if (!xml)
continue
this.traverseImages(xml, filePath).then(() => {
const temp = expandedData(xml)
const result = {
id: manifest.href,
content: temp,
}
this.content.push(result)
}).catch((error) => {
console.error('traverseImages error', error)
})
for (const item of this.spine ?? []) {
const manifest = this.manifest?.find(manifest => manifest.id === item.idref)
if (manifest && manifest.href) {
const filePath = resolveId(this.fullPath, manifest?.href)
const content = await this.usezip.fileFileContent(filePath)
const xml = await this.usexml.parse(content, {
preserveChildrenOrder: true,
explicitChildren: true,
...options,
})

if (!xml)
continue
await this.traverseImages(xml, filePath)
const temp = expandedData(xml)
const result = {
id: manifest.href,
content: temp,
}
this.content.push(result)
}
}
catch (error) {
// eslint-disable-next-line no-console
console.log('parseConent error', error)
}
}

private async traverseImages(node: any, importer = this.fullPath) {
Expand All @@ -169,7 +161,7 @@ export class Epub {
}

// 将 img 标签中的图片转换为 base64
private async updateImageToBase64(imgNode, importer: string = this.fullPath) {
private async updateImageToBase64(imgNode: any, importer: string = this.fullPath) {
if (imgNode && imgNode[0] && imgNode[0].$ && (imgNode[0].$.src || imgNode[0].$['xlink:href']) && !imgNode[0].$.base64) {
const imageFilePath = resolveId(importer, imgNode[0].$.src || imgNode[0].$['xlink:href'])
try {
Expand Down
22 changes: 8 additions & 14 deletions packages/extension/src/book-parse/epub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ export async function parseEpub(
config: BReaderContext,
bookCache: BookCache,
) {
try {
const { config: bookConfig } = book
const epub = new Epub(bookConfig.path)
await epub.parse()
const { config: bookConfig } = book
const epub = new Epub(bookConfig.path)
await epub.parse()

// cache
// 先去json中找,如果没有再去epub 实例中找
// 意味着每次都要构建一个epub实例
bookCache[book.md5] = epub
cacheBook(book, config, epub)
}
catch (error) {
// eslint-disable-next-line no-console
console.log('parseEpub error', error)
}
// cache
// 先去json中找,如果没有再去epub 实例中找
// 意味着每次都要构建一个epub实例
bookCache[book.md5] = epub
cacheBook(book, config, epub)
}

async function cacheBook(
Expand Down
61 changes: 32 additions & 29 deletions packages/extension/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ export function useMessage() {
window.showInformationMessage(message)
}

function berror(message: string) {
function berror(err: Error | string) {
let message = ''
if (err instanceof Error)
message = `${err.message}\n${err.stack}`

else
message = err

window.showErrorMessage(message)
error(message)
error(err)
}

function bwarn(message: string) {
Expand All @@ -23,39 +30,35 @@ export function useMessage() {
}
}

export function useProgress(title?: string) {
const options: ProgressOptions = {
location: ProgressLocation.Notification,
title: title ?? 'B-Reader',
export function useProgress(options: ProgressOptions = {
location: ProgressLocation.Notification,
}) {
let isStop = false
const _options: ProgressOptions = {
...options,
title: 'Loading...',
}

function start(message: string) {
window.withProgress(
{
location: 15,
title: message,
cancellable: true,
},
(progress, token) => {
token.onCancellationRequested(() => {
// eslint-disable-next-line no-console
console.log('User canceled the long running operation')
})

progress.report({ increment: 0 })

return new Promise<void>((resolve) => {
setTimeout(() => {
resolve()
}, 1000)
})
},
)
function start(message?: string) {
isStop = false
if (message)
_options.title = message
window.withProgress(_options, async () => {
await new Promise((resolve) => {
setInterval(() => {
if (isStop)
resolve(1)
}, 100)
})
})
}

function stop() {}
function stop() {
isStop = true
}

return {
start,
stop,
}
}
Loading

0 comments on commit 55d6894

Please sign in to comment.