Skip to content

Commit

Permalink
daisy
Browse files Browse the repository at this point in the history
  • Loading branch information
YuhangGe authored and YuhangGe committed May 9, 2012
1 parent 621bb2d commit aa9ba20
Show file tree
Hide file tree
Showing 12 changed files with 202 additions and 87 deletions.
10 changes: 9 additions & 1 deletion css/style.css
Expand Up @@ -207,7 +207,15 @@ a:hover{
#col-3 #edit-1 .edit-left .switch-current:hover{
color:#1C94C4;
}

.ctrl-seperator {
display : inline-block;
position: relative;
top: -3px;
background : gray;
opacity : 0.7;
width : 1px;
height : 23px;
}
#ctrl-panel-handword a, #ctrl-panel-doodle a{
position:relative;
top: -10px;
Expand Down
51 changes: 25 additions & 26 deletions index.php
Expand Up @@ -56,7 +56,7 @@
(For Phone) 2页
</p>
</li>
-->
</ul>
</div>
Expand All @@ -83,7 +83,7 @@
2012年3月26日
</p>
</li>
-->
</ul>
</div>
Expand All @@ -98,38 +98,34 @@
<a id="ctrl-switch-doodle-edit" class="ctrl-switch" id="ctrl-doodle-edit" href="javascript:ctrlSwitch('doodle-edit');" >选择</a>
</div>
<div class="edit-right">
<span id='ctrl-edit-new'>
<a href="javascript:ctrlNewNote();" id="ctrl-newnote">新建記事</a>
</span>
<span id='ctrl-edit-save' class="hidden">
<a href="javascript:ctrlSaveNote();" id="ctrl-savenote">儲存</a>
<a href="javascript:ctrlCancelNote();" id="ctrl-cancelnote">取消</a>
</span>
<span id='ctrl-edit-new'> <a href="javascript:ctrlNewNote();" id="ctrl-newnote">新建記事</a> </span>
<span id='ctrl-edit-save' class="hidden"> <a href="javascript:ctrlSaveNote();" id="ctrl-savenote">儲存</a> <a href="javascript:ctrlCancelNote();" id="ctrl-cancelnote">取消</a> </span>
<!--
<a href="javascript:ctrlEditNote();" id="ctrl-editnote">編輯</a>
<a href="javascript:ctrlEditNote();" id="ctrl-editnote">編輯</a>
<a href="javascript:ctrlDelNote();" id="ctrl-delnote">刪除</a>
-->
</div>
-->
</div>
</div>
</div>
<div class="editor-panel">
<div class="edit-ctrl">
<div style="padding-left:10px;">
<div id="ctrl-panel-readonly" class="hidden" style="font-size:10px;color:gray;">
<p>當前處於唯讀模式</p>
<p>
當前處於唯讀模式
</p>
</div>
<div id='ctrl-panel-handword'>
<a href="javascript:alert('由於流覽器安全限制,請使用Ctrl+C复制!');" title="複製選中文本">複製</a>
<a href="javascript:alert('由於流覽器安全限制,請使用Ctrl+V粘貼!');" title="粘貼選中文本">粘貼</a>
<a title="設置文本是否加粗" id="ctrl-setbold" href="javascript:ctrlSetBold();">加粗(已關)</a>
<span id='color-picker'>
<a id='ctrl-color-black' href="javascript:ctrlSetColor('black')" class="color-block color-current" style="background: black;"></a>
<a id='ctrl-color-blue' href="javascript:ctrlSetColor('blue')" class="color-block" style="background: blue;"></a>
<a id='ctrl-color-red' href="javascript:ctrlSetColor('red')" class="color-block" style="background: red;"></a>
<a id='ctrl-color-green' href="javascript:ctrlSetColor('green')" class="color-block" style="background: green;"></a>
</span>
<span class="ctrl-seperator"></span>
<a id="ctrl-find" href="javascript:ctrlFind();">查找</a>
<input type="text" style="width:60px;position: relative; top: -10px;" id="ctrl-find-txt" value="筆記" onkeydown="ctrlFindText(event);"/>
<span class="ctrl-seperator"></span>
<span id='color-picker'> <a id='ctrl-color-black' href="javascript:ctrlSetColor('black')" class="color-block color-current" style="background: black;"></a> <a id='ctrl-color-blue' href="javascript:ctrlSetColor('blue')" class="color-block" style="background: blue;"></a> <a id='ctrl-color-red' href="javascript:ctrlSetColor('red')" class="color-block" style="background: red;"></a> <a id='ctrl-color-green' href="javascript:ctrlSetColor('green')" class="color-block" style="background: green;"></a> </span>
</div>
<div id="ctrl-panel-doodle" class="hidden">
<a href="javascript:;" title="選擇畫筆類型">畫筆:</a>
Expand All @@ -152,14 +148,17 @@
<option>13</option>
<option>18</option>
</select>
<span class="ctrl-seperator"></span>
</div>
<div id="ctrl-panel-doodle-edit">
<!--
<a href="javascript:;" title="複製選中文本">複製</a>
<!--
<a href="javascript:;" title="複製選中文本">複製</a>
<a href="javascript:;" title="粘貼選中文本">粘貼</a>
-->
</div>
-->
<p style="color:gray;">
選擇模式下請使用鼠標選擇和編輯塗鴉
</p>
</div>
</div>
</div>
Expand Down
13 changes: 13 additions & 0 deletions js/ctrl.js
Expand Up @@ -128,3 +128,16 @@ function ctrlDelNote() {
//$('#page_'+ Daisy.Global.cur_page.pageid).remove();
}

function ctrlFindText(e){
if(e.keyCode===13){
ctrlFind(true);
}
}
function ctrlFind(not_focus){
var str = $('#ctrl-find-txt').val();
if(str==="")
return;
SNEditor.findText(str);
if(!not_focus)
SNEditor.focus();
}
39 changes: 25 additions & 14 deletions js/editor/editor.js
Expand Up @@ -93,7 +93,7 @@

this.caret.style.font = this.font;
this.caret.style.color = this.color;

//当前是否在正在手写
this.hand_mode = false;
this.hand_bihua = [];
Expand Down Expand Up @@ -136,7 +136,7 @@
}
Daisy.WebNote.prototype = {
setMode : function(mode) {
this.select_doodle = null;
this.select_doodle = null;
if(mode === 'doodle') {
this.canvas.style.cursor = "crosshair";
this.caret.style.opacity = "0";
Expand All @@ -147,33 +147,33 @@
this.edit_doodle.attachDoodle(this.select_doodle);
}
this.caret.style.opacity = "0";
} else if(mode === 'handword' || mode === 'readonly'){
} else if(mode === 'handword' || mode === 'readonly') {
this.caret.style.opacity = "1";
this.canvas.style.cursor = "text";
this.canvas.style.cursor = "text";
} else {
throw 'unknown mode';
}
this.cur_mode = mode;
throw 'unknown mode';
}
this.cur_mode = mode;
this.focus();
this.render.paint();
},
setColor : function(color) {
this.color = color;

if(this.cur_mode!=='readonly' && this.cur_page.select_mode) {
if(this.cur_mode !== 'readonly' && this.cur_page.select_mode) {
this.cur_page.setSelectColor(color);
this.render.paint();
}

},
setReadOnly : function(read_only) {
if(read_only === true || read_only === 'readonly')
this.cur_mode = 'readonly';
this.cur_mode = 'readonly';
},
setBold : function(is_bold) {
this.font_bold = is_bold;
this.font = (this.font_bold ? 'bold ' : '') + this.font_size + "px " + this.font_name;
if(this.cur_mode!=='readonly' && this.cur_page.select_mode) {
if(this.cur_mode !== 'readonly' && this.cur_page.select_mode) {
this.cur_page.setSelectBold(is_bold);
this.render.paint();
}
Expand Down Expand Up @@ -232,11 +232,11 @@
},
_getEventPoint : function(e, is_chrome, not_scale) {
var x = 0, y = 0;
if(is_chrome){
if(is_chrome) {
var off = $.getOffset(this.container);
x = e.x - off.left + this.container.scrollLeft + document.body.scrollLeft;
y = e.y - off.top + this.container.scrollTop + document.body.scrollTop;
}else if( typeof e.offsetX !== 'undefined') {
} else if( typeof e.offsetX !== 'undefined') {
x = e.offsetX;
y = e.offsetY;
} else if( typeof e.x !== 'undefined') {
Expand All @@ -247,7 +247,7 @@
} else {
throw "no x in event(_getEventPoint)";
}
if(e.target === this.caret && !is_chrome){
if(e.target === this.caret && !is_chrome) {
x += this.caret.offsetLeft;
y += this.caret.offsetTop;
}
Expand Down Expand Up @@ -499,7 +499,7 @@
break;
}
this.cur_page.select(null);
this.render.paint();
this.render.paint();
this._setCaret(new_cp);
},
getThumb : function() {
Expand All @@ -522,6 +522,17 @@
clearUndoRedo : function() {
this.text_history.clear();
this.doodle_history.clear();
},
findText : function(txt) {
var idx = this.cur_page.findText(txt, this.caret_pos.index + 1);
if(idx<0)
return;
else{
var fc = this.cur_page.getCaretByIndex(idx-1), tc = this.cur_page.getCaretByIndex(idx-1+txt.length);
this.cur_page.select(fc,tc);
this._setCaret(tc);
this.render.paint();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion js/editor/element.js
Expand Up @@ -131,7 +131,7 @@
return new Daisy._HandElement(this._copyBihua(),$.copyJson(this.style),this.width,this.height);
},
draw : function(ctx){
ctx.lineWidth = this.style.weight;
ctx.lineWidth = this.style.bold?this.style.weight * 1.5 : this.style.weight;
ctx.strokeStyle = this.style.color;
ctx.save();
ctx.translate(this.left, this.bottom - this.height);
Expand Down
3 changes: 2 additions & 1 deletion js/editor/handword.js
Expand Up @@ -6,7 +6,8 @@
_createHandWord : function(bihuas) {
var hw = new Daisy._HandElement([], {
color : this.color,
weight : Daisy.Global.hand_weight
weight : Daisy.Global.hand_weight,
bold : this.font_bold
},0,0);

var p0 = bihuas[0][0], x1 = p0.x, y1 = p0.y, x2 = p0.x, y2 = p0.y;
Expand Down
11 changes: 4 additions & 7 deletions js/editor/load_content.js
Expand Up @@ -81,8 +81,8 @@
//$.log("bold s:%d,e:%d",s,e);
for(var j = this.read(); j < this.read(); j++) {
var ele = this.editor.cur_page.ele_array[j];
ele.style.bold = true;
if(ele.type === Daisy._Element.Type.CHAR) {
ele.style.bold = true;
ele.style.font = "bold " + ele.style.font;
}
}
Expand All @@ -92,11 +92,7 @@
var s = this.read(), e = this.read(), c = this._getColorStr(this.read(), this.read(), this.read());
//$.log("color s:%d,e:%d c:%s",s,e,c);
for(var j = s; j < e; j++) {

var ele = this.editor.cur_page.ele_array[j];
if(ele.type === Daisy._Element.Type.CHAR) {
ele.style.color = c;
}
this.editor.cur_page.ele_array[j].style.color = c;
}
}
},
Expand Down Expand Up @@ -130,7 +126,8 @@
height = this.read(),
style = {
weight : this.read_float(),
color : this._getColorStr(this.read(), this.read(), this.read())
color : this._getColorStr(this.read(), this.read(), this.read()),
bold : false
},
value = [];
var hw = new Daisy._HandElement(value, style, width, height);
Expand Down
51 changes: 44 additions & 7 deletions js/editor/page.js
Expand Up @@ -23,15 +23,21 @@
from : null,
to : null
}

/**
* 保存上一次查找的字符串
*/
this.last_find = {
text : "",
next : null
}
this.doodle_list.length = 0;
this.ele_array.length = 0;
},
reset : function() {
this._init();
},
selectAll : function(){
return this.selectByIndex(-1,this.ele_array.length-1);
selectAll : function() {
return this.selectByIndex(-1, this.ele_array.length - 1);
},
select : function(from, to) {
if(from == null || to == null) {
Expand Down Expand Up @@ -208,9 +214,9 @@
ele.line_at += para.line_cross - pre_lc;
}
/**
* 如果当前段落最后有换行\n也要同时移动。
* 如果当前段落最后有换行\n也要同时移动。
*/
if((ele = this.ele_array[j])!=null){
if(( ele = this.ele_array[j]) != null) {
ele.bottom += (para.line_cross - pre_lc) * this.editor.render.line_height;
ele.line_at += para.line_cross - pre_lc;
}
Expand All @@ -220,7 +226,7 @@
},
insertLine : function(caret) {
var n_e = new Daisy._NewLineElement(), p_e = null, a_e = null;

this.ele_array.splice(caret.index + 1, 0, n_e);
var para = this.para_info[caret.para], l_len = caret.para_at + 1, r_len = para.length - l_len;
para.length = l_len;
Expand Down Expand Up @@ -371,8 +377,8 @@
var f = this.select_range.from.index + 1, t = this.select_range.to.index;
for(var i = f; i <= t; i++) {
var ele = this.ele_array[i];
ele.style.bold = is_bold;
if(ele.type === Daisy._Element.Type.CHAR) {
ele.style.bold = is_bold;
ele.style.font = this.editor.font;
}
}
Expand All @@ -387,6 +393,37 @@
},
removeDoodle : function(doodle) {
this.doodle_list.splice(this.doodle_list.indexOf(doodle), 1);
},
findText : function(txt, start) {
var arr = this.ele_array, lf = this.last_find, t_next = lf.text === txt ? lf.next : (lf.next = $.getKmpNext(lf.text = txt));
var i = (start < 0 || start >= arr.length) ? 0 : start, idx = 0;
/**
* 在整个文本中从start位置循环查找字符串。
* 两个while循环的实际复杂度是线性的,这个是KMP字符串匹配算法的特色。
*/
while(true) {
while(true) {
if(arr[i].value === txt[idx]) {
idx++;
if(idx === txt.length) {
return i - idx + 1;
}
break;
} else if(idx === 0) {
break;
} else {
idx = t_next[idx];
}
}
i++;
if(i === start) {
break;
} else if(i === arr.length) {
i = 0;
}
}

return -1;
}
}

Expand Down

0 comments on commit aa9ba20

Please sign in to comment.