Skip to content

Commit

Permalink
feat: support stardict and support default stardict
Browse files Browse the repository at this point in the history
  • Loading branch information
terasum committed Sep 23, 2023
1 parent 28b704b commit 0b82508
Show file tree
Hide file tree
Showing 73 changed files with 1,713 additions and 561 deletions.
4 changes: 3 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"context"
"errors"

"github.com/op/go-logging"
"github.com/skratchdot/open-golang/open"
"github.com/terasum/medict/internal/entry"
Expand Down Expand Up @@ -57,7 +58,7 @@ func NewApp() *App {
}

func (b *App) appInit() error {
conf, err := entry.DefaultConfig()
conf, err := entry.LoadApp()
if err != nil {
return err
}
Expand All @@ -71,6 +72,7 @@ func (b *App) appInit() error {
if err != nil {
return err
}
// assign backend server
b.bs = bs
// running bs, this is not blocking
bs.Start()
Expand Down
Binary file modified build/windows/icon.ico
Binary file not shown.
4 changes: 2 additions & 2 deletions frontend/src/apis/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ export const BaseDictDirectory = function():Promise<string>{

export async function requestBackend(apiName, data): Promise<model.Resp> {
if (window['go']) {
console.log(`[IPC] dispatch [${apiName}]into golang args`, data)
console.log(`[dicts-api] ipc call, dispatch [${apiName}] event, args:`, data)
return Dispatch(apiName, data);
} else {
return Promise.resolve({
data:"",
err: "browser not support",
err: "browser not support or system not initialzd yet",
code: 500,
})

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/apis/dicts-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ export const GetAllDicts = async function (): Promise<Array<IDict>> {
}

// BuildIndex
export const BuildIndex = async function (): Promise<model.Resp> {
export const BuildIndex = async function (dictid: string): Promise<model.Resp> {
try{
let resp = await requestBackend("BuildIndex", {})
let resp = await requestBackend("BuildIndexByDictId", {dictid: dictid})
console.log("[dicts-api] BuildIndex: ", resp)
return resp.data as unknown as model.Resp
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/apis/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

export interface IDict {
id: number
id: string
name: string
discrption?: string
imageURL?: string
Expand Down
39 changes: 24 additions & 15 deletions frontend/src/store/dict/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { defineStore } from 'pinia';

import { GetAllDicts, SearchWord } from '@/apis/dicts-api';
import { StaticDictServerURL } from '@/apis/apis';
import { c } from 'naive-ui';

function constructQueryURL(entry) {
let {
Expand All @@ -34,7 +35,7 @@ function constructQueryURL(entry) {
return `${baseURL}/__tcidem_query?dict_id=${dict_id}&key_word=${key_word}&record_start_offset=${record_start_offset}&record_end_offset=${record_end_offset}&key_block_idx=${key_block_idx}&entry_id=${entry_id}`;
}

const countDownJs = `
const DefaultContentTemplpate = `
<html>
<head>
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
Expand Down Expand Up @@ -76,7 +77,7 @@ export const useDictQueryStore = defineStore('dictQuery', {
state: () => ({
dictApiBaseURL: '',
queryPendingList: [],
mainContent: btoa(countDownJs),
mainContent: btoa(DefaultContentTemplpate),
mainContentURL: '',
selectDict: { id: '', name: '', path: '' },
inputSearchWord: '',
Expand All @@ -86,7 +87,7 @@ export const useDictQueryStore = defineStore('dictQuery', {
return GetAllDicts();
},
updateInputSearchWord(word: string) {
console.log(`===> updateInputSearchWord: ${word}`);
console.log(`[app-event](store-action), updateInputSearchWord: ${word}`);
if (!word || word.trim() == '') {
return;
}
Expand All @@ -98,18 +99,25 @@ export const useDictQueryStore = defineStore('dictQuery', {
},
updateMainContent(content) {
if (content === '') {
this.mainContent = btoa(countDownJs);
this.mainContent = btoa(DefaultContentTemplpate);
} else {
this.mainContent = content;
}
},
updateMainContentURL(url) {
this.mainContentURL = url;
if (url === '') {
this.mainContent = btoa(DefaultContentTemplpate);
}
},
updatePendingList(wordList) {
console.log('==== updatePendingList ====');
console.log(wordList);
console.log(`[app-event](store-action), updatePendingList`, wordList);
this.queryPendingList = wordList;
if (this.queryPendingList && this.queryPendingList.length > 0) {
this.locateWord(0);
} else {
this.resetMainContent();
}
},
updateSelectDict(dictItem) {
this.selectDict = dictItem;
Expand All @@ -127,13 +135,10 @@ export const useDictQueryStore = defineStore('dictQuery', {
}

SearchWord(this.selectDict.id, word).then((res) => {
console.log('=== SearchWord ===');
console.log(this.selectDict.id);
console.log(res);
console.log(`[app-event](store-action), SearchWord`, this.selectDict.id, word);
console.log("[app-event](store-action), SerchWord",res)
this.updatePendingList(res);
if (res.data && res.data.length > 0) {
this.locateWord(0);
}

});
},
setUpAPIBaseURL() {
Expand All @@ -150,18 +155,18 @@ export const useDictQueryStore = defineStore('dictQuery', {
urlPromise
.then((url) => {
if (url === '') {
console.log(`[init] static server url empty retry ${count}`);
console.log(`[app init] static server url is empty, retrying times: ${count}`);
return;
}
// browser
if (url === 'http://localhost:1/') {
return;
}
if (url.startsWith('http://localhost:0/')) {
console.log(`[init] static server url empty retry ${count}`);
console.log(`[app init] static server url setting failed, retrying times: ${count}`);
return;
}
console.log(`[init] static server url success ${count}`);
console.log(`[app init] static server url has setting successful, retrying times: ${count}`);
that.updateBaseURL(url);
clearInterval(inv);
})
Expand Down Expand Up @@ -210,5 +215,9 @@ export const useDictQueryStore = defineStore('dictQuery', {
})
);
},
resetMainContent() {
this.updateMainContent(btoa(DefaultContentTemplpate))

}
},
});
12 changes: 12 additions & 0 deletions frontend/src/store/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ export const useUIStore = defineStore('ui', {
},
isSearchInputActive() {
return this.currentTab === "search";
},
updateProgress(hint:string, progress:number) {
if (progress > 100) {
progress = 100;
}
if (progress < 0) {
progress = 0
}

this.progressHint = hint;
this.progressPercent = progress;

}
},
});
5 changes: 5 additions & 0 deletions frontend/src/view/main/MainContentFrame.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ function listenContentUpdate() {
break;
}
case 'updateMainContentURL': {
if (store.mainContentURL === "") {
const content = b64DecodeUnicode(store.mainContent);
updateIframeContent(content, true);
}
updateIframeContent(store.mainContentURL, false);
break;
}
Expand Down
36 changes: 13 additions & 23 deletions frontend/src/view/main/MainFooter.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,50 +18,40 @@

<template>
<AppFooter>
<span class="index-process-hint">
<em>{{ percentage_hint.value }}</em>
</span>

<div class="building-index-process">
<n-progress
type="line"
color="#c1c1c1"
:show-indicator="false"
:percentage="percnetage.value"
:percentage="percnetage"
/>
</div>
<span class="split-line"></span>

<span class="index-process-hint">
<em>{{ percentage_hint }}</em>
</span>
</AppFooter>
</template>

<script lang="ts" setup>
import { NProgress } from 'naive-ui';
import AppFooter from '@/components/layout/AppFooter.vue';
import { ref, onMounted, computed } from 'vue';
let _percentage = ref(0);
let _percentage_hint = ref('索引建立中...');
function updatePercentage() {
let intv = setInterval(() => {
let step = 2000 / 200;
_percentage.value += step;
if (_percentage.value >= 100) {
clearInterval(intv);
_percentage_hint.value = '索引建立完成';
}
}, 200);
}
import { onMounted, computed } from 'vue';
import { useUIStore } from '@/store/ui';
const uiStore = useUIStore();
onMounted(()=>{
updatePercentage();
})
const percnetage = computed(() => {
return _percentage;
return uiStore.progressPercent;
});
const percentage_hint = computed(() =>{
return _percentage_hint
return uiStore.progressHint;
})
</script>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/view/main/MainFunctions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ function changeTab(tabName) {
}
function handleChange(v) {
console.info('[event keydown enter]' + inputWord.value);
console.info('[app-event](keydown.enter), args:' + inputWord.value);
if(!uiStore.isSearchInputActive()){
console.log("input disabled, skipped")
console.log("[app-event](keydown.enter), input disabled, skipped")
return;
}
dictQueryStore.updateInputSearchWord(inputWord.value.trim());
Expand Down
51 changes: 44 additions & 7 deletions frontend/src/view/main/MainRightToolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,15 @@
</template>
<script setup>
import { useDictQueryStore } from '@/store/dict';
import { useUIStore } from '@/store/ui';
import { reactive, onMounted } from 'vue';
import { BuildIndex } from '@/apis/dicts-api';
import AppRightToolbar from '@/components/layout/AppRightToolbar.vue';
import { NPopover } from 'naive-ui';
const dictQueryStore = useDictQueryStore();
const uiStore = useUIStore();
const state = reactive({
dictList: [],
Expand All @@ -127,24 +129,59 @@ function getBackground(item) {
function loadDictionaries() {
dictQueryStore.queryDictList().then((res) => {
console.log(res);
console.log("[app-init] loading dictionaries success, result:", res)
if (res.length > 0) {
dictQueryStore.updateSelectDict(res[0]);
}
const totalNumber = res.length;
const updater = updateProgress(totalNumber);
for (let i = 0; i < res.length; i++) {
state.dictList.push(res[i]);
}
console.log(`[app-init] building dictionary, index: ${i}`, res[i])
BuildIndex(res[i].id).then((resp) => {
let progressHint = `词典 ${res[i].name} 加载完成`;
console.log(`[app-init] building success, index: ${i}`, resp);
updater(progressHint)
setTimeout(() => {
// build-index
BuildIndex().then((resp) => {
console.log('building index success:', resp);
});
}, 1000);
}
});
}
function updateProgress(totalNumber) {
let total = totalNumber;
let count = 0;
let plist = []
let progress = 0;
let intv = setInterval(() => {
if (plist.length > 0) {
let item = plist.shift();
count++;
progress = (count / total) * 100;
uiStore.updateProgress(item.hint, progress);
if (progress >= 100) {
clearInterval(intv);
setTimeout(() =>{
uiStore.updateProgress("全部加载完成", 100);
},300)
}
}
}, 200);
return function(hint) {
plist.push({
hint: hint,
})
}
}
onMounted(() => {
loadDictionaries();
});
Expand Down

0 comments on commit 0b82508

Please sign in to comment.