-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathtexteditor.html
327 lines (301 loc) · 11.1 KB
/
texteditor.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html>
<head>
<!-- 必须强制指定页面编码为 UTF-8 -->
<meta charset="utf-8">
<!-- Demo 的简要说明,必须要填写 -->
<meta name="description" content="Kity 实现模拟光标输入">
<!-- Demo 的作者,建议填写 -->
<meta name="author" content="[email protected]">
<!-- Demo 的标题,必须填写 -->
<title>模拟光标</title>
<!-- Demo 开发过程中使用 CMD 引入 Kity 源码,方便调试 -->
<!-- dev start -->
<!-- 目录型的 Demo 注意修正源码引用路径 -->
<script src="../dev-lib/sea.js"></script>
<script>
// 设置好 kity 源代码目录
seajs.config( { base: '../src'} );
// 定义 Demo 模块
define('demo', function(require) { require('kity'); });
</script>
<script>
// 启动 Demo 模块
seajs.use('demo');
</script>
<!-- dev 版本 示例所需脚本文件 end -->
<script src="public/jquery.js"></script>
<style>
.editor-container{
}
.receiver{
position:absolute;
padding:0;
margin:0;
word-wrap:break-word;
clip:rect(1em 1em 1em 1em);
}
</style>
</head>
<body>
<script>
var Cursor = kity.createClass('Cursor',{
base: kity.Line,
constructor: function(height, color, width) {
this.callBase();
this.height = height || 20;
this.stroke(color || 'blue', width || 1);
this.setHide();
this.timer = null;
},
setPosition: function(offset) {
try{
this.setPoint1(offset.x,offset.y);
this.setPoint2(offset.x,offset.y + this.height);
}catch(e){
debugger
}
return this;
},
setHeight:function(height){
this.height = height;
},
setHide:function(){
clearInterval(this.timer);
this.setStyle('display','none');
return this;
},
setShowHold : function(){
clearInterval(this.timer);
this.setStyle('display','');
return this;
},
setShow:function(){
clearInterval(this.timer);
var me = this,
state = '';
this.timer = setInterval(function(){
me.setStyle('display',state);
state = state ? '' : 'none';
},300);
return this;
},
setTextShape:function(text){
if(!text){
this.text = new kity.Text();
}else{
this.text = text;
}
return this
},
getTextShape:function(){
return this.text
},
setTxtContent : function(text){
this.text.setContent(text)
},
updatePosition:function(index){
}
});
var Receiver = kity.createClass('Receiver',{
clear : function(){
this.$container.html('');
this.index = 0;
return this;
},
constructor : function(){
this.$container = $('<div contenteditable="true" class="receiver"></div>');
this.$container.appendTo(document.body);
this.$container.on('keydown keypress keyup', $.proxy(this.keyboardEvents,this));
this.timer = null;
this.index = 0;
},
setPosition : function(textShapeOffset){
this.$container.css({
top:textShapeOffset.x,
left:textShapeOffset.y
});
this.textShape.setPosition(textShapeOffset.x, textShapeOffset.y);
return this;
},
setRange : function(range,index){
this.index = index || this.index;
var text = this.$container[0].firstChild;
console.log(text)
console.log(this.index)
range.setStart(text || this.$container[0], this.index).collapse(true);
setTimeout(function(){
range.select()
});
return this;
},
setTextShape:function(textShape){
if(!textShape){
textShape = new kity.Text();
}
this.textShape = textShape;
return this;
},
setTextShapeSize:function(size){
this.textShape.setSize(size);
return this;
},
getTextShapeHeight:function(){
return this.textShape.getRenderBox().height;
},
appendTextShapeToPaper:function(paper){
paper.addShape(this.textShape);
return this;
},
keyboardEvents : function(e){
clearTimeout(this.timer);
var me = this;
switch(e.type){
case 'keyup':
this.textShape.setContent(this.$container.text().replace(/\u200b/g,''));
this.updateTextData();
this.updateCursor();
this.timer = setTimeout(function(){
me.cursor.setShow()
},500);
break;
case 'keypress':
case 'keyup':
}
},
updateTextData : function(){
this.textShape.textData = this.getTextOffsetData();
this.index = this.index + 1;
},
setCursor : function(cursor){
this.cursor = cursor;
return this;
},
updateCursor : function(){
this.cursor.setShowHold();
if(this.index == this.textData.length){
this.cursor.setPosition({
x : this.textData[this.index-1].x + this.textData[this.index-1].width,
y : this.textData[this.index-1].y
})
}else if(this.index == 0){
this.cursor.setPosition({
x : this.textShape.getX(),
y : this.textData.getY()
})
}else{
if(this.index + 1 == this.textData.length){
var lastChar = this.textData[this.index];
this.cursor.setPosition({
x : lastChar.x + lastChar.width,
y : lastChar.y
})
}else{
this.cursor.setPosition(this.textData[this.index])
}
}
return this;
},
getTextOffsetData:function(){
var text = this.textShape.getContent();
this.textData = [];
// this.textShape.clearContent();
for(var i= 0,l = text.length;i<l;i++){
var box = this.textShape.getExtentOfChar(i);
this.textData.push({
x:box.x,
y:box.y,
width:box.width,
height:box.height
})
}
return this;
},
setCurrentIndex:function(offset){
var me = this;
this.getTextOffsetData();
var hadChanged = false;
$.each(this.textData,function(i,v){
if(offset.x >= v.x && offset.x <= v.x + v.width){
if(offset.x - v.x > v.width/2){
me.index = i + 1;
}else {
me.index = i
}
hadChanged = true;
return false;
}
})
return this;
},
setCursorHeight:function(){
this.cursor.setHeight(this.getTextShapeHeight())
return this;
}
});
var Range = kity.createClass('Range',{
constructor : function(){
this.nativeRange = document.createRange();
this.nativeSel = window.getSelection();
},
select:function(){
var start = this.nativeRange.startContainer;
if(start.nodeType == 1 && start.childNodes.length == 0){
var char = document.createTextNode('\u200b');
start.appendChild(char);
this.nativeRange.setStart(char,1);
this.nativeRange.collapse(true);
}
this.nativeSel.removeAllRanges();
this.nativeSel.addRange(this.nativeRange);
return this;
},
setStart:function(node,index){
this.nativeRange.setStart(node,index);
return this;
},
setEnd:function(node,index){
this.nativeRange.setEnd(node,index);
return this;
},
collapse:function(toStart){
this.nativeRange.collapse(toStart === true);
return this;
},
insertNode:function(node){
this.nativeRange.insertNode(node);
return this;
}
});
var cursor = new Cursor();
var receiver = new Receiver();
var range = new Range();
var paper = new kity.Paper(document.body).setHeight(500);
paper.addShape(cursor)
.setStyle('border','1px solid #ccc')
.setStyle('cursor','text')
.on('click',function(e){
var originEvent = e.originEvent;
var position = e.getPosition('top');
if(e.targetShape.getType() != 'Text'){
var offset = e.getPosition('top');
cursor.setShow().setPosition(offset);
receiver.clear()
.setTextShape()
.setTextShapeSize(cursor.height)
.appendTextShapeToPaper(paper)
.setPosition(position)
.setRange(range,0)
.setCursor(cursor)
}else{
receiver.setCursor(cursor)
.setTextShape(e.targetShape)
.setCursorHeight()
.setCurrentIndex(position)
.updateCursor()
.setRange(range)
}
})
</script>
</body>
</html>