Skip to content

Commit

Permalink
feat: fix word count bug in multiple tab mode
Browse files Browse the repository at this point in the history
  • Loading branch information
1943time committed Oct 28, 2023
1 parent ccb662e commit cff7a43
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bluestone",
"version": "0.10.2",
"version": "0.11.0",
"description": "",
"main": "./out/main/index.js",
"license": "AGPL-3.0",
Expand Down
3 changes: 1 addition & 2 deletions src/renderer/src/components/Empty.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {observer} from 'mobx-react-lite'
import {CloseOutlined, FileAddOutlined, FolderOpenOutlined, FolderOutlined, HistoryOutlined} from '@ant-design/icons'
import {MainApi} from '../api/main'
import {configStore} from '../store/config'
import logo from '../../../../resources/icon.png?asset'
import {useEffect, useLayoutEffect} from 'react'
import {useLayoutEffect} from 'react'
import {useLocalState} from '../hooks/useLocalState'
import {db} from '../store/db'
import {basename, dirname} from 'path'
Expand Down
14 changes: 8 additions & 6 deletions src/renderer/src/editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const MEditor = observer(({note}: {
const schema = treeStore.schemaMap.get(nodeRef.current)
if (schema?.state) {
const path = nodeRef.current.filePath
const res = await toMarkdown(schema.state, path)
const res = await toMarkdown(schema.state)
saveRecord(path, schema.state)
await window.api.fs.writeFile(path, res, {encoding: 'utf-8'})
}
Expand All @@ -67,7 +67,7 @@ export const MEditor = observer(({note}: {
})
const count = useCallback(async (nodes: any[]) => {
if (!configStore.config.showCharactersCount || !nodeRef.current) return
const res = await toMarkdown(nodes, nodeRef.current.filePath)
const res = await toMarkdown(nodes)
const texts = Editor.nodes(editor, {
at: [],
match: n => n.text
Expand All @@ -80,7 +80,12 @@ export const MEditor = observer(({note}: {

useSubject(countThrottle$.pipe<any>(debounceTime(300)), count)


useEffect(() => {
if (treeStore.currentTab.store === store) {
const schema = treeStore.schemaMap.get(note)?.state
if (schema) count(schema)
}
}, [treeStore.currentTab, note])
const change = useCallback((v: any[]) => {
if (first.current) {
setTimeout(() => {
Expand Down Expand Up @@ -127,9 +132,6 @@ export const MEditor = observer(({note}: {
nodeRef.current = note
store.setState(state => state.pauseCodeHighlight = true)
let data = treeStore.schemaMap.get(note)
setTimeout(() => {
count(data?.state || [])
})
first.current = true
if (!data) {
data = await treeStore.getSchema(note)
Expand Down
14 changes: 8 additions & 6 deletions src/renderer/src/editor/output/md.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import Worker from './worker?worker'
import {nanoid} from 'nanoid'

const worker = new Worker()
let callbackMap = new Map<string, Function>()

worker.onmessage = e => {
if (callbackMap.get(e.data?.path)) {
callbackMap.get(e.data?.path)!(e.data.data)
callbackMap.delete(e.data?.path)
if (callbackMap.get(e.data?.id)) {
callbackMap.get(e.data?.id)!(e.data.data)
callbackMap.delete(e.data?.id)
}
}
export const toMarkdown = (state: any[], path: string):Promise<string> => {
export const toMarkdown = (state: any[]):Promise<string> => {
return new Promise(resolve => {
callbackMap.set(path, resolve)
worker.postMessage({state, path})
const id = nanoid()
callbackMap.set(id, resolve)
worker.postMessage({state, id})
})
}
2 changes: 1 addition & 1 deletion src/renderer/src/editor/output/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export const toMarkdown = (tree: any[], preString = '', parent: any[] = [{root:

onmessage = (e) => {
postMessage({
path: e.data.path,
id: e.data.id,
data: toMarkdown(e.data.state)
})
}

0 comments on commit cff7a43

Please sign in to comment.