Skip to content

Commit

Permalink
ToneOZ 澳聲通字典工具整合:加入滑鼠圈選查詢功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreyxuan committed Jun 2, 2021
1 parent fbcfc84 commit b7583ce
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 62 deletions.
5 changes: 3 additions & 2 deletions ime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
## 字典

在按下 「開始」 後的編輯模式中,點選右下角 「字典」 可開啟 《國語字典簡編本》 輔助查詢字義及標準讀音。
請用滑鼠點擊或圈選任意字詞,字典工具會即時顯示查詢結果。

## 協作邀請 (詞庫)

Expand Down Expand Up @@ -66,9 +67,9 @@ poyin_db.txt 收錄了所有 phonetic/phonic_table_Z.txt 裡所有多音字的

## 協作邀請 (字典)

本工具字典檔資料來自於教育部國語辭典公眾授權網 《<a href="https://language.moe.gov.tw/001/Upload/Files/site_content/M0001/respub/dict_concised_download.html">國語辭典簡編本</a>》,採用CC BY-ND 3.0授權。
本工具字典檔資料來自於教育部國語辭典公眾授權網 《<a href="https://language.moe.gov.tw/001/Upload/Files/site_content/M0001/respub/dict_concised_download.html">國語辭典簡編本</a>》,採用 CC BY-ND 3.0 授權。

字典工具來自於開源字典架構 「<a href="https://github.com/jeffreyxuan/ToneOZDic">ToneOZDic澳聲通字典</a> ,採用 MIT 授權。 此字典工具支援使用者自行製作字典檔,請參考 <a href="https://github.com/jeffreyxuan/ToneOZDic/blob/main/README.md">字典檔生成器使用說明</a>。
字典工具來自開源的字典架構 <a href="https://github.com/jeffreyxuan/ToneOZDic">ToneOZ 澳聲通字典</a> ,採用 MIT 授權。 此字典工具支援使用者自行製作字典檔,請參考 <a href="https://github.com/jeffreyxuan/ToneOZDic/blob/main/README.md">ToneOZ Dictionary 字典檔生成器使用說明</a>。



Expand Down
10 changes: 2 additions & 8 deletions ime/ime.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,14 @@ function match(c, i, p, j, onlydic) {
textinfo[a] = {};
}
var ivsinfo = textinfo[a];
var phrasearr = ivsinfo.phrasearr;
var phraseidx = ivsinfo.phraseidx;
var phrasedata = {phrase:phrase,x:x,a:a,ivs:text[a]};;
if(phraseidx === undefined){
phraseidx = {};
}
var phrasearr = ivsinfo.phrasearr;
var phrasedata = {phrase:phrase,x:x,a:a};
if(phrasearr === undefined){
phrasearr = [phrasedata];
} else {
phrasearr.push(phrasedata);
}
phraseidx[""+x+phrase] = phrasearr.length-1;
ivsinfo.phrasearr = phrasearr;
ivsinfo.phraseidx = phraseidx;
}
}

Expand Down
17 changes: 13 additions & 4 deletions ime/tzdic/tzdicui.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ if(!window.tzdicidx){
}

function TZDicUI(){
let thisObj = this;
// ToneOZDic Parameters
let tzparam = {
objInput : null
Expand All @@ -34,6 +35,8 @@ function TZDicUI(){
// Custeom lookup table to specify the IVS fonts (Ideographic Variation Sequences) displayed in the dictionary entry iframe
, strCssIframe : tzstrCssIframe
// Custom css object to decorate the objIframe outter position
, intMaxPhrase : 16
// Max phrases per query
, strVer : ""
// Version control string
};
Expand Down Expand Up @@ -73,10 +76,13 @@ function TZDicUI(){

// events init
tzparam.objInput.on('input selectionchange propertychange keydown click focus', function(event) {
tzUpdateEvent({event:event});
thisObj.tzUpdateEvent({event:event});
});
tzparam.objInput.on('DOMSubtreeModified', function(event) {
thisObj.tzUpdateEvent({event:event});
});
$(document).on('mouseup', function(event) {
tzUpdateEvent({event:event});
thisObj.tzUpdateEvent({event:event});
});

return tzparam;
Expand All @@ -99,7 +105,7 @@ function TZDicUI(){
}

let tmrUpdateEvent = null;
window.tzUpdateEvent = function(dicParam){
this.tzUpdateEvent = function(dicParam){
if(tmrUpdateEvent){
clearTimeout(tmrUpdateEvent);
tmrUpdateEvent = null;
Expand Down Expand Up @@ -154,9 +160,12 @@ function TZDicUI(){
// get query parameters for ToneOZDic
for(let idxPhrase in phrases){
let phrase = phrases[idxPhrase];
if(qArray.length >= tzparam.intMaxPhrase){
break;
}
qArray.push(GetQuery({
phrase : phrase
}));
}));
}

let dicURLParam = JSON.stringify(qArray);
Expand Down
22 changes: 16 additions & 6 deletions ime/tzdic/tzlib/tzdicentry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// BEGIN CONFIG /////////
// Folder path of the dictionary files
const defaultDataFolder = "./tzdata/";
// Max phrases per query
const intMaxPhrase = 16;
// END CONFIG /////////
const vsbase = 0xe01e0;
let tzQuery, tzQueryArray, tzCss, tzIvsPosDic;
let tzQuery, tzQueryArray, tzCss, tzIvsPosDic, tzVer;
let winSearch = window.location.search;
if(!window.tzdic){
window.tzdic = {};
Expand All @@ -12,19 +15,23 @@ if(winSearch){
tzQuery = urlParams.get("q");
tzCss = urlParams.get("css");
tzIvsPosDic = urlParams.get("ivs");
tzVer = urlParams.get("v");
tzVer = (tzVer?"?v="+tzVer:"");
tzQueryArray = JSON.parse(tzQuery);
if(tzCss){
document.write('<link rel="stylesheet" href="'+tzCss+'" />');
document.write('<link rel="stylesheet" href="'+tzCss + tzVer +'" />');
}
if(tzIvsPosDic){
document.write('<script type="text/javascript" src="' +tzIvsPosDic+'">\x3C/script>');
document.write('<script type="text/javascript" src="' +tzIvsPosDic
+ tzVer + '">\x3C/script>');
}
if(tzQueryArray){
for(let qIdx in tzQueryArray){
if(tzQueryArray[qIdx].id){
document.write('<script type="text/javascript" src="'
+ defaultDataFolder
+ (""+tzQueryArray[qIdx].id)+'.js">\x3C/script>');
+ (""+tzQueryArray[qIdx].id)+'.js'
+ tzVer + '">\x3C/script>');
}
}
}
Expand All @@ -38,7 +45,7 @@ $(document).ready(function () {
window.ivsdic = null;
}
if(tzdic){
for(let qIdx in tzQueryArray){
for(let qIdx=0; qIdx<tzQueryArray.length; qIdx++){
let item = tzQueryArray[qIdx];
let dicSlot = null;
if(item.id){
Expand All @@ -61,7 +68,7 @@ $(document).ready(function () {
if(tzdic[dicSlot]){
// dictionary lookup
result = tzdic[dicSlot][noivs+posfix];
if(ivsdic){
if(result && ivsdic){
qarray = noivs.split("");
if(result && result.z){
yinarray = result.z.split(new RegExp(separators.join('|'), 'g'));
Expand Down Expand Up @@ -97,6 +104,9 @@ $(document).ready(function () {
}
}
}
if(qIdx >= intMaxPhrase-1){
break;
}
}
}
parent.postMessage("ready " + tzQuery, "*");
Expand Down
73 changes: 31 additions & 42 deletions ime/zhdicui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const cssUI =
const pathCssDic = "../zihaidic";
const pathJsIVSLookup = "../zhuivsdic";
const strVer="";
const intMaxPhrase = 16;
// End CONFIG======

$(document).ready(function () {
Expand All @@ -22,40 +23,38 @@ function ZiHaiDicUI(){
let domDic = {};
let isIframeReady = null;
let dicMinimized = true;

let tzDicUI;
init();

function init(){
if (typeof tzDicUI === 'undefined') {
initDom();
function init(){
initDom();

// load TonzOZDic
tzDicUI = new TZDicUI();
tzDicUI.init({
objInput : domDic["editor"]
, objOutput : domDic["dicResult"]
, fnGetPhrases : fnGetPhrases
, pathCssDic : pathCssDic
, pathJsIVSLookup : pathJsIVSLookup
, intMaxPhrase : intMaxPhrase
, strVer : strVer
});

// load TonzOZDic
window.tzDicUI = new TZDicUI();
tzDicUI.init({
objInput : domDic["editor"]
, objOutput : domDic["dicResult"]
, fnGetPhrases : fnGetPhrases
, pathCssDic : pathCssDic
, pathJsIVSLookup : pathJsIVSLookup
, strVer : strVer
});

initUIEvent();
}
initUIEvent();
}

/**
* Get words for dictionay queries
* Read "phrase" from a jQuery object's attr.
* @param {any} event : jQuery UI event
* @param {any} srcObj : a jQuery object with attr "phrase"
* @return {array} words: output words array e.g.: ["行人","行"]
* @param {any} event : jQuery UI event. Use it to find the srcObj that the user clicked
* @param {any} srcObj : a jQuery object with attr "data-i" to lookup the phrase from textinfo
* @return {array} words: output words array with ivs characters. e.g.: ["行人","行"]
*/
function fnGetPhrases(param){
let {event,srcObj} = param;
let phrases = null;
let selectedString = null;
isIframeReady = false;

if(!srcObj && event && event.target){
srcObj = $(event.target);
if (window.getSelection) {
Expand All @@ -73,46 +72,36 @@ function ZiHaiDicUI(){
let phrasearr = ivsinfo.phrasearr;
for(let ip in phrasearr){
let phrase = phrasearr[ip].phrase;
if(phrase && phrases.indexOf(phrase)<0){
if(phrase && phrases.indexOf(phrase)<0 && phrases.length < intMaxPhrase){
// update ivs info
phrase = getIVSPhrase(phrasearr[ip]);
phrases.push(phrase);
}
}
}
if(content){
if(content && phrases.length < intMaxPhrase){
phrases.push(content.charAt(0));
}
}
} else if(selectedString){
phrases = [selectedString];
}
if(phrases != null){
isIframeReady = false;
}

return {
phrases:phrases
};
}

function getIVSPhrase(param){
let {phrase, x, a, ivs} = param;
let {phrase, x, a} = param;
let phraseivs = "";
for(let ip=0; ip<phrase.length; ip++){
let preva = a-x+ip;
if(preva==a){
phraseivs += ivs;
} else {
let ivsinfo = textinfo[preva];
let previdx = ""+ip+phrase;
if(ivsinfo
&& ivsinfo.phrasearr
&& ivsinfo.phraseidx
&& ivsinfo.phraseidx[previdx] !== undefined){
phrasedata = ivsinfo.phrasearr[ivsinfo.phraseidx[previdx]];
phraseivs += phrasedata.ivs;
} else {
phraseivs += phrase[ip];
}
}
let ivsc = text[preva];
phraseivs += ivsc;
}

return phraseivs;
Expand Down Expand Up @@ -152,7 +141,7 @@ function ZiHaiDicUI(){
render();
});

$('#prev, #next, #start').click(function() {
$('#prev, #next, #start, #selector').click(function() {
ShowDicByCurr();
});

Expand All @@ -174,7 +163,7 @@ function ZiHaiDicUI(){
let dicParam = {
srcObj:currObj
};
tzUpdateEvent(dicParam);
tzDicUI.tzUpdateEvent(dicParam);
}
}
}
Expand Down

0 comments on commit b7583ce

Please sign in to comment.