1
1
import { observable , action , computed , toJS } from 'mobx' ;
2
+ import util from './util'
2
3
3
4
/*
4
5
@@ -269,7 +270,7 @@ export default class scripts{
269
270
const targetInnerHTML = targetElement . innerHTML || '' ;
270
271
const targetText = targetElement . textContent || '' ;
271
272
const targetHeight = targetElement . offsetHeight + this . Hdata [ targetClassName ] ;
272
- const targetOffSet = RecursionCounter ( targetElement ) [ 0 ] ;
273
+ const targetOffSet = util . RecursionCounter ( targetElement ) [ 0 ] ;
273
274
const targetLineOffSet = parseInt ( targetOffSet / this . lineCharNum [ targetClassName ] ) ;
274
275
const targetLine = parseInt ( targetText . length / this . lineCharNum [ targetClassName ] ) + 1 ;
275
276
@@ -279,8 +280,7 @@ export default class scripts{
279
280
/* ------------------------------------------------------
280
281
compute cursor line and offset in current div-editable
281
282
------------------------------------------------------ */
282
- this . paragraphs [ index ] . selectionStart . offset = targetOffSet ;
283
- this . paragraphs [ index ] . selectionStart . line = targetLineOffSet ;
283
+ this . paragraphs [ index ] . selectionStart = { line :targetLineOffSet , offset : targetOffSet } ;
284
284
285
285
/* ------------------------------------------------------
286
286
calculate and update qurrent target height and line
@@ -307,13 +307,19 @@ export default class scripts{
307
307
308
308
------------------------------------------------------ */
309
309
@action handleKey ( e ) {
310
+ // target mac, sorry windows...
311
+ // do not care if not paste or new line
312
+ if ( e . metaKey && e . keyCode != 13 && e . keyCode != 86 ) {
313
+ return ;
314
+ }
315
+
310
316
const targetElement = e . target ;
311
317
const index = parseInt ( targetElement . attributes [ 'data-unique' ] . value )
312
318
const targetClassName = targetElement . className ;
313
319
const targetInnerHTML = targetElement . innerHTML || '' ;
314
320
const targetText = targetElement . textContent || '' ;
315
321
const targetHeight = targetElement . offsetHeight + this . Hdata [ targetClassName ] ;
316
- const targetOffSet = RecursionCounter ( targetElement ) [ 0 ] ;
322
+ const targetOffSet = util . RecursionCounter ( targetElement ) [ 0 ] ;
317
323
const targetLineOffSet = parseInt ( targetOffSet / this . lineCharNum [ targetClassName ] ) ;
318
324
const targetLine = parseInt ( targetText . length / this . lineCharNum [ targetClassName ] ) + 1 ;
319
325
@@ -330,6 +336,7 @@ export default class scripts{
330
336
this . paragraphs [ index ] . line = targetLine ;
331
337
this . paragraphs [ index ] . innerHTML = targetInnerHTML ;
332
338
this . paragraphs [ index ] . text = targetText ;
339
+ this . paragraphs [ index ] . selectionStart = { line :targetLineOffSet , offset : targetOffSet } ;
333
340
}
334
341
335
342
//// =============================== DEBUG HERE ========================================== ////
@@ -493,23 +500,26 @@ export default class scripts{
493
500
}
494
501
495
502
/* ------------------------------------------------------
496
- If keydown delete at begining when prev paragraph is empty
503
+ If keydown delete at begining when prev paragraph is empty && !isSelected
497
504
------------------------------------------------------ */
498
- else if ( e . keyCode === 8 && targetOffSet === 0 && index > 1 && ! this . paragraphs [ index - 1 ] . innerHTML && targetInnerHTML ) {
505
+ else if ( e . keyCode === 8 && targetOffSet === 0 && index > 1 && ! this . paragraphs [ index - 1 ] . innerHTML && targetInnerHTML && ! util . isSelected ( ) ) {
499
506
e . preventDefault ( ) ;
500
507
this . paragraphs . splice ( index - 1 , 1 ) ;
501
508
}
502
509
503
510
// /* ------------------------------------------------------
504
511
// If keydown delete at begining when prev paragraph is not empty
505
- // !!! THERE'S SOME ISSUE WITH THIS -> (targetOffSet === 0) alone can not deside if it's select all !!!
506
512
// ------------------------------------------------------ */
507
- // else if(e.keyCode === 8 && targetOffSet === 0 && index > 1 && this.paragraphs[index - 1].innerHTML && targetInnerHTML){
508
- // e.preventDefault();
509
- // this.paragraphs[index].focus = false;
510
- // this.paragraphs[index - 1].focus = true;
511
- // this.paragraphs[index - 1].selectionStart = {line: this.paragraphs[index - 1].line - 1, offset: this.paragraphs[index - 1].text.length};
512
- // }
513
+ else if ( e . keyCode === 8 && targetOffSet === 0 && index > 1 && this . paragraphs [ index - 1 ] . innerHTML && targetInnerHTML ) {
514
+ e . preventDefault ( ) ;
515
+ if ( util . isSelected ( ) ) {
516
+ util . deleteContent ( ) ;
517
+ } else {
518
+ this . paragraphs [ index ] . focus = false ;
519
+ this . paragraphs [ index - 1 ] . focus = true ;
520
+ this . paragraphs [ index - 1 ] . selectionStart = { line : this . paragraphs [ index - 1 ] . line - 1 , offset : this . paragraphs [ index - 1 ] . text . length } ;
521
+ }
522
+ }
513
523
514
524
/* ------------------------------------------------------
515
525
If keydown backward when cursor at begining
@@ -571,30 +581,3 @@ export default class scripts{
571
581
}
572
582
573
583
} // End of mobx-script
574
-
575
- /* ------------------------------------------------------
576
- div-contentEditable cursor position calculator
577
- dealing with <b> <i> and so on...
578
- ------------------------------------------------------ */
579
- function RecursionCounter ( el ) {
580
- let textCount = 0 ;
581
- for ( let node of el . childNodes ) {
582
- if ( node . nodeType === 3 ) {
583
- let range = window . getSelection ( ) . getRangeAt ( 0 ) ;
584
- let containerNode = range . startContainer ;
585
-
586
- if ( containerNode === node ) {
587
- textCount += range . startOffset ;
588
- return ( [ textCount , true ] ) ;
589
- }
590
- textCount += node . textContent . length ;
591
- } else {
592
- let tmp = RecursionCounter ( node ) ;
593
- textCount += tmp [ 0 ] ;
594
- if ( tmp [ 1 ] ) {
595
- return ( [ textCount , true ] ) ;
596
- }
597
- }
598
- }
599
- return ( [ textCount , false ] ) ;
600
- }
0 commit comments