11import Helpers from '../test-helpers' ;
22import Range from 'mobiledoc-kit/utils/cursor/range' ;
33import { NO_BREAK_SPACE } from 'mobiledoc-kit/renderers/editor-dom' ;
4+ import { TAB } from 'mobiledoc-kit/utils/characters' ;
45
56const { module, test } = Helpers ;
7+ const { editor : { buildFromText } } = Helpers ;
8+ const { postAbstract : { DEFAULT_ATOM_NAME } } = Helpers ;
69
710let editor , editorElement ;
811
@@ -12,6 +15,12 @@ function renderEditor(...args) {
1215 return editor ;
1316}
1417
18+ let atom = {
19+ name : DEFAULT_ATOM_NAME ,
20+ type : 'dom' ,
21+ render ( ) { }
22+ } ;
23+
1524module ( 'Acceptance: Editor: Text Input Handlers' , {
1625 beforeEach ( ) {
1726 editorElement = $ ( '#editor' ) [ 0 ] ;
@@ -67,9 +76,7 @@ headerTests.forEach(({text, toInsert, headerTagName}) => {
6776 } ) ;
6877
6978 test ( `typing "${ text } " but not "${ toInsert } " does not convert to ${ headerTagName } ` , ( assert ) => {
70- renderEditor ( ( { post, markupSection, marker} ) => {
71- return post ( [ markupSection ( 'p' , [ marker ( text ) ] ) ] ) ;
72- } ) ;
79+ editor = buildFromText ( text , { element : editorElement } ) ;
7380 assert . hasElement ( '#editor p' , 'precond - has p' ) ;
7481 Helpers . dom . insertText ( editor , 'X' ) ;
7582
@@ -109,9 +116,7 @@ test('typing "* " at start of markup section does not remove it', (assert) => {
109116 return post ( [ markupSection ( 'p' , [ marker ( '*abc' ) ] ) ] ) ;
110117 } ) ;
111118
112- let position = editor . post . sections . head . headPosition ( ) ;
113- position . offset = 1 ;
114- editor . selectRange ( position ) ;
119+ editor . selectRange ( editor . post . sections . head . toPosition ( 1 ) ) ;
115120
116121 Helpers . dom . insertText ( editor , ' ' ) ;
117122 assert . hasElement ( '#editor p:contains(* abc)' , 'p is still there' ) ;
@@ -134,9 +139,7 @@ test('typing "* " inside of a list section does not create a new list section',
134139} ) ;
135140
136141test ( 'typing "1 " converts to ol > li' , ( assert ) => {
137- renderEditor ( ( { post, markupSection, marker} ) => {
138- return post ( [ markupSection ( 'p' , [ marker ( '1' ) ] ) ] ) ;
139- } ) ;
142+ editor = buildFromText ( [ '1|' ] , { element : editorElement } ) ;
140143 Helpers . dom . insertText ( editor , ' ' ) ;
141144 assert . hasNoElement ( '#editor p' , 'p is gone' ) ;
142145 assert . hasElement ( '#editor ol > li' , 'p -> "ol > li"' ) ;
@@ -159,9 +162,7 @@ test('typing "1 " converts to ol > li', (assert) => {
159162} ) ;
160163
161164test ( 'typing "1. " converts to ol > li' , ( assert ) => {
162- renderEditor ( ( { post, markupSection, marker} ) => {
163- return post ( [ markupSection ( 'p' , [ marker ( '1.' ) ] ) ] ) ;
164- } ) ;
165+ editor = buildFromText ( '1.|' , { element : editorElement } ) ;
165166 Helpers . dom . insertText ( editor , ' ' ) ;
166167 assert . hasNoElement ( '#editor p' , 'p is gone' ) ;
167168 assert . hasElement ( '#editor ol > li' , 'p -> "ol > li"' ) ;
@@ -171,15 +172,7 @@ test('typing "1. " converts to ol > li', (assert) => {
171172} ) ;
172173
173174test ( 'an input handler will trigger anywhere in the text' , ( assert ) => {
174- let atom = {
175- name : 'mention' ,
176- type : 'dom' ,
177- render ( ) { }
178- } ;
179-
180- renderEditor ( ( { post, markupSection, marker, atom} ) => {
181- return post ( [ markupSection ( 'p' , [ atom ( 'mention' , 'bob' ) , marker ( 'abc' ) , atom ( 'mention' , 'sue' ) ] ) ] ) ;
182- } , { atoms : [ atom ] } ) ;
175+ editor = buildFromText ( '@abc@' , { element : editorElement , atoms : [ atom ] } ) ;
183176
184177 let expandCount = 0 ;
185178 let lastMatches ;
@@ -198,7 +191,7 @@ test('an input handler will trigger anywhere in the text', (assert) => {
198191 assert . deepEqual ( lastMatches , [ '@' ] , 'correct match at start' ) ;
199192
200193 // middle
201- editor . selectRange ( Range . create ( editor . post . sections . head , '@' . length + 1 + 'ab' . length ) ) ;
194+ editor . selectRange ( editor . post . sections . head . toPosition ( '@' . length + 1 + 'ab' . length ) ) ;
202195 Helpers . dom . insertText ( editor , '@' ) ;
203196 assert . equal ( expandCount , 2 , 'expansion was run at middle' ) ;
204197 assert . deepEqual ( lastMatches , [ '@' ] , 'correct match at middle' ) ;
@@ -211,15 +204,7 @@ test('an input handler will trigger anywhere in the text', (assert) => {
211204} ) ;
212205
213206test ( 'an input handler can provide a `match` instead of `text`' , ( assert ) => {
214- let atom = {
215- name : 'mention' ,
216- type : 'dom' ,
217- render ( ) { }
218- } ;
219-
220- renderEditor ( ( { post, markupSection, marker, atom} ) => {
221- return post ( [ markupSection ( 'p' , [ atom ( 'mention' , 'bob' ) , marker ( 'abc' ) , atom ( 'mention' , 'sue' ) ] ) ] ) ;
222- } , { atoms : [ atom ] } ) ;
207+ editor = buildFromText ( '@abc@' , { element : editorElement , atoms : [ atom ] } ) ;
223208
224209 let expandCount = 0 ;
225210 let lastMatches ;
@@ -239,7 +224,7 @@ test('an input handler can provide a `match` instead of `text`', (assert) => {
239224 assert . deepEqual ( lastMatches , regex . exec ( 'abX' ) , 'correct match at start' ) ;
240225
241226 // middle
242- editor . selectRange ( Range . create ( editor . post . sections . head , 'abX' . length + 1 + 'ab' . length ) ) ;
227+ editor . selectRange ( editor . post . sections . head . toPosition ( 'abX' . length + 1 + 'ab' . length ) ) ;
243228 Helpers . dom . insertText ( editor , '..X' ) ;
244229 assert . equal ( expandCount , 2 , 'expansion was run at middle' ) ;
245230 assert . deepEqual ( lastMatches , regex . exec ( '..X' ) , 'correct match at middle' ) ;
@@ -252,15 +237,7 @@ test('an input handler can provide a `match` instead of `text`', (assert) => {
252237} ) ;
253238
254239test ( 'an input handler can provide a `match` that matches at start and end' , ( assert ) => {
255- let atom = {
256- name : 'mention' ,
257- type : 'dom' ,
258- render ( ) { }
259- } ;
260-
261- renderEditor ( ( { post, markupSection, marker, atom} ) => {
262- return post ( [ markupSection ( 'p' , [ atom ( 'mention' , 'bob' ) , marker ( 'abc' ) , atom ( 'mention' , 'sue' ) ] ) ] ) ;
263- } , { atoms : [ atom ] } ) ;
240+ editor = Helpers . editor . buildFromText ( [ '@abc@' ] , { element : editorElement , atoms : [ atom ] } ) ;
264241
265242 let expandCount = 0 ;
266243 let lastMatches ;
@@ -274,18 +251,35 @@ test('an input handler can provide a `match` that matches at start and end', (as
274251 } ) ;
275252
276253 // at start
277- editor . selectRange ( new Range ( editor . post . headPosition ( ) ) ) ;
254+ editor . selectRange ( editor . post . headPosition ( ) ) ;
278255 Helpers . dom . insertText ( editor , '123' ) ;
279256 assert . equal ( expandCount , 1 , 'expansion was run at start' ) ;
280257 assert . deepEqual ( lastMatches , regex . exec ( '123' ) , 'correct match at start' ) ;
281258
282259 // middle
283- editor . selectRange ( Range . create ( editor . post . sections . head , '123' . length + 2 ) ) ;
260+ editor . selectRange ( editor . post . sections . head . toPosition ( '123' . length + 2 ) ) ;
284261 Helpers . dom . insertText ( editor , '123' ) ;
285262 assert . equal ( expandCount , 1 , 'expansion was not run at middle' ) ;
286263
287264 // end
288- editor . selectRange ( new Range ( editor . post . tailPosition ( ) ) ) ;
265+ editor . selectRange ( editor . post . tailPosition ( ) ) ;
289266 Helpers . dom . insertText ( editor , '123' ) ;
290267 assert . equal ( expandCount , 1 , 'expansion was not run at end' ) ;
291268} ) ;
269+
270+ // See https://github.com/bustlelabs/mobiledoc-kit/issues/400
271+ test ( 'input handler can be triggered by TAB' , ( assert ) => {
272+ editor = Helpers . editor . buildFromText ( 'abc|' , { element : editorElement } ) ;
273+
274+ let didMatch ;
275+ editor . onTextInput ( {
276+ match : / a b c \t / ,
277+ run ( ) {
278+ didMatch = true ;
279+ }
280+ } ) ;
281+
282+ Helpers . dom . insertText ( editor , TAB ) ;
283+
284+ assert . ok ( didMatch ) ;
285+ } ) ;
0 commit comments