From 5d884f63054d8519d2c941d2e8861c3dcd26bfbd Mon Sep 17 00:00:00 2001 From: thisunravisara Date: Thu, 14 Feb 2019 19:25:35 +0530 Subject: [PATCH 1/3] Fix the select method --- src/doc.js | 4 ++-- test/doc.spec.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/doc.js b/src/doc.js index f570f33..ffc9f0f 100644 --- a/src/doc.js +++ b/src/doc.js @@ -423,8 +423,8 @@ var prototype = node.derive({ return; } this.selection.start = Math.max(0, ordinal); - this.selection.end = Math.max( - typeof ordinalEnd === 'number' ? ordinalEnd : this.selection.start, + this.selection.end = Math.min( + typeof ordinalEnd === 'number' ? ordinalEnd : this.frame.length - 1, this.frame.length - 1 ); this.selectionJustChanged = true; diff --git a/test/doc.spec.js b/test/doc.spec.js index 7690269..14a0a53 100644 --- a/test/doc.spec.js +++ b/test/doc.spec.js @@ -65,6 +65,22 @@ test('select function should take end of text if end index is not specified', () script: 'normal', }, ]); + + document.select( 3, 2 ); + expect( document.selectedRange().save()).toEqual([ + { + text: 't', + size: 10, + font: 'lt_regular', + color: 'red', + bold: true, + italic: false, + underline: false, + strikeout: true, + align: 'left', + script: 'normal', + }, + ]); }); From 2b25e15e0bef710f1379af4711e04ca5511415ef Mon Sep 17 00:00:00 2001 From: thisunravisara Date: Fri, 15 Feb 2019 11:07:07 +0530 Subject: [PATCH 2/3] Select method updated --- src/doc.js | 5 ++++- test/doc.spec.js | 22 +++++----------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/src/doc.js b/src/doc.js index ffc9f0f..79ecf91 100644 --- a/src/doc.js +++ b/src/doc.js @@ -422,9 +422,12 @@ var prototype = node.derive({ // Something has gone terribly wrong - doc.transaction will rollback soon return; } + if ( ordinalEnd === 'end' ) { + ordinalEnd = this.frame.length - 1; + } this.selection.start = Math.max(0, ordinal); this.selection.end = Math.min( - typeof ordinalEnd === 'number' ? ordinalEnd : this.frame.length - 1, + typeof ordinalEnd === 'number' ? ordinalEnd : this.selection.start, this.frame.length - 1 ); this.selectionJustChanged = true; diff --git a/test/doc.spec.js b/test/doc.spec.js index 14a0a53..199c4bd 100644 --- a/test/doc.spec.js +++ b/test/doc.spec.js @@ -49,8 +49,8 @@ test('save function should return the selected content with default styles appli ]); }); -test('select function should take end of text if end index is not specified', () => { - document.select( 3 ); +test('select function should take end of text if second param is `end` ', () => { + document.select( 3, 'end' ); expect( document.selectedRange().save()).toEqual([ { text: 'here', @@ -65,22 +65,10 @@ test('select function should take end of text if end index is not specified', () script: 'normal', }, ]); + document.select( 2 ); + expect( document.selectedRange().save()).toEqual([]); - document.select( 3, 2 ); - expect( document.selectedRange().save()).toEqual([ - { - text: 't', - size: 10, - font: 'lt_regular', - color: 'red', - bold: true, - italic: false, - underline: false, - strikeout: true, - align: 'left', - script: 'normal', - }, - ]); }); + From 40b609567d8b02c402d476f728ab59c89f1b0554 Mon Sep 17 00:00:00 2001 From: thisunravisara Date: Fri, 15 Feb 2019 11:21:08 +0530 Subject: [PATCH 3/3] Added selectFrom function --- src/doc.js | 6 +++--- test/doc.spec.js | 4 +--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/doc.js b/src/doc.js index 79ecf91..5b927c7 100644 --- a/src/doc.js +++ b/src/doc.js @@ -422,9 +422,6 @@ var prototype = node.derive({ // Something has gone terribly wrong - doc.transaction will rollback soon return; } - if ( ordinalEnd === 'end' ) { - ordinalEnd = this.frame.length - 1; - } this.selection.start = Math.max(0, ordinal); this.selection.end = Math.min( typeof ordinalEnd === 'number' ? ordinalEnd : this.selection.start, @@ -441,6 +438,9 @@ var prototype = node.derive({ */ this.notifySelectionChanged(takeFocus); }, + selectFrom: function( start, takeFocus ){ + this.select( start, this.frame.length - 1, takeFocus ); + }, selectAll: function(){ this.select( 0, this.frame.length - 1, true ); }, diff --git a/test/doc.spec.js b/test/doc.spec.js index 199c4bd..2d33b71 100644 --- a/test/doc.spec.js +++ b/test/doc.spec.js @@ -50,7 +50,7 @@ test('save function should return the selected content with default styles appli }); test('select function should take end of text if second param is `end` ', () => { - document.select( 3, 'end' ); + document.selectFrom( 3 ); expect( document.selectedRange().save()).toEqual([ { text: 'here', @@ -65,8 +65,6 @@ test('select function should take end of text if second param is `end` ', () => script: 'normal', }, ]); - document.select( 2 ); - expect( document.selectedRange().save()).toEqual([]); });