@@ -42,113 +42,120 @@ function makeList(text, selection, insertionBeforeEachLine) {
4242
4343export default {
4444
45- /**
46- * Makes the text bold
47- *
48- * @param {any } text
49- * @param {any } selection
50- * @returns
51- */
52- makeBold : function ( text , selection ) {
53- if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
54- // the user is pointing to a word
55- selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
56- }
57- // the user is selecting a word section
58- var { newText, insertionLength} = insertText ( text , '**' , selection [ 0 ] ) ;
59- newText = insertText ( newText , '**' , selection [ 1 ] + insertionLength ) . newText ;
60- return {
61- previousText : text ,
62- text : newText ,
63- selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
45+ makeBold : {
46+ icon : 'bold' ,
47+ tooltip : 'Add bold text' ,
48+ execute : function ( text , selection ) {
49+ if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
50+ // the user is pointing to a word
51+ selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
52+ }
53+ // the user is selecting a word section
54+ var { newText, insertionLength} = insertText ( text , '**' , selection [ 0 ] ) ;
55+ newText = insertText ( newText , '**' , selection [ 1 ] + insertionLength ) . newText ;
56+ return {
57+ previousText : text ,
58+ text : newText ,
59+ selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
60+ }
6461 }
6562 } ,
6663
67- /**
68- * Makes the text italic
69- *
70- * @param {any } text
71- * @param {any } selection
72- * @returns
73- */
74- makeItalic : function ( text , selection ) {
75- if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
76- // the user is pointing to a word
77- selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
78- }
79- // the user is selecting a word section
80- var { newText, insertionLength} = insertText ( text , '_' , selection [ 0 ] ) ;
81- newText = insertText ( newText , '_' , selection [ 1 ] + insertionLength ) . newText ;
82- return {
83- previousText : text ,
84- text : newText ,
85- selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
64+ makeItalic : {
65+ icon : 'italic' ,
66+ tooltip : 'Add italic text' ,
67+ execute : function ( text , selection ) {
68+ if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
69+ // the user is pointing to a word
70+ selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
71+ }
72+ // the user is selecting a word section
73+ var { newText, insertionLength} = insertText ( text , '_' , selection [ 0 ] ) ;
74+ newText = insertText ( newText , '_' , selection [ 1 ] + insertionLength ) . newText ;
75+ return {
76+ previousText : text ,
77+ text : newText ,
78+ selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
79+ }
8680 }
8781 } ,
8882
89- /**
90- * Makes a link
91- *
92- * @param {any } text
93- * @param {any } selection
94- * @returns
95- */
96- makeLink : function ( text , selection ) {
97- var { newText, insertionLength} = insertText ( text , '[' , selection [ 0 ] ) ;
98- newText = insertText ( newText , '](url)' , selection [ 1 ] + insertionLength ) . newText ;
99- return {
100- previousText : text ,
101- text : newText ,
102- selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
83+ makeLink : {
84+ icon : 'link' ,
85+ tooltip : 'Insert a link' ,
86+ execute : function ( text , selection ) {
87+ var { newText, insertionLength} = insertText ( text , '[' , selection [ 0 ] ) ;
88+ newText = insertText ( newText , '](url)' , selection [ 1 ] + insertionLength ) . newText ;
89+ return {
90+ previousText : text ,
91+ text : newText ,
92+ selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
93+ }
10394 }
10495 } ,
10596
106- makeQuote : function ( text , selection ) {
107- if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
108- // the user is pointing to a word
109- selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
110- }
111-
112- let insertionBefore = '> ' ;
113- if ( selection [ 0 ] > 0 ) {
114- let breaksNeeded = getBreaksNeededForEmptyLineBefore ( text , selection [ 0 ] ) ;
115- insertionBefore = Array ( breaksNeeded + 1 ) . join ( "\n" ) + insertionBefore ;
97+ makeQuote : {
98+ icon : 'quote-right' ,
99+ tooltip : 'Insert a quote' ,
100+ execute : function ( text , selection ) {
101+ if ( text && text . length && selection [ 0 ] == selection [ 1 ] ) {
102+ // the user is pointing to a word
103+ selection = getSurroundingWord ( text , selection [ 0 ] ) . position ;
104+ }
105+
106+ let insertionBefore = '> ' ;
107+ if ( selection [ 0 ] > 0 ) {
108+ let breaksNeeded = getBreaksNeededForEmptyLineBefore ( text , selection [ 0 ] ) ;
109+ insertionBefore = Array ( breaksNeeded + 1 ) . join ( "\n" ) + insertionBefore ;
110+ }
111+
112+ // the user is selecting a word section
113+ var { newText, insertionLength} = insertText ( text , insertionBefore , selection [ 0 ] ) ;
114+ newText = insertText ( newText , '\n\n' , selection [ 1 ] + insertionLength ) . newText ;
115+ return {
116+ previousText : text ,
117+ text : newText ,
118+ selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
119+ }
116120 }
121+ } ,
117122
118- // the user is selecting a word section
119- var { newText, insertionLength} = insertText ( text , insertionBefore , selection [ 0 ] ) ;
120- newText = insertText ( newText , '\n\n' , selection [ 1 ] + insertionLength ) . newText ;
121- return {
122- previousText : text ,
123- text : newText ,
124- selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
123+ makeImage : {
124+ icon : 'picture-o' ,
125+ tooltip : 'Insert a picture' ,
126+ execute : function ( text , selection ) {
127+ var { newText, insertionLength} = insertText ( text , '![' , selection [ 0 ] ) ;
128+ newText = insertText ( newText , '](image-url)' , selection [ 1 ] + insertionLength ) . newText ;
129+ return {
130+ previousText : text ,
131+ text : newText ,
132+ selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
133+ }
125134 }
126135 } ,
127136
128-
129- /**
130- * Makes an image
131- *
132- * @param {any } text
133- * @param {any } selection
134- * @returns
135- */
136- makeImage : function ( text , selection ) {
137- var { newText, insertionLength} = insertText ( text , '![' , selection [ 0 ] ) ;
138- newText = insertText ( newText , '](image-url)' , selection [ 1 ] + insertionLength ) . newText ;
139- return {
140- previousText : text ,
141- text : newText ,
142- selection : [ selection [ 0 ] + insertionLength , selection [ 1 ] + insertionLength ]
137+ makeUnorderedList : {
138+ icon : 'list-ul' ,
139+ tooltip : 'Add a bulleted list' ,
140+ execute : function ( text , selection ) {
141+ return makeList ( text , selection , '- ' ) ;
143142 }
144143 } ,
145144
146- makeUnorderedList : function ( text , selection ) {
147- return makeList ( text , selection , '- ' ) ;
145+ makeOrderedList : {
146+ icon : 'list-ol' ,
147+ tooltip : 'Add a numbered list' ,
148+ execute : function ( text , selection ) {
149+ return makeList ( text , selection , ( item , index ) => `${ index + 1 } . ` ) ;
150+ }
148151 } ,
149152
150- makeOrderedList : function ( text , selection ) {
151- return makeList ( text , selection , ( item , index ) => `${ index + 1 } . ` ) ;
153+ getDefaultCommands : function ( ) {
154+ return [
155+ [ this . makeBold , this . makeItalic ] ,
156+ [ this . makeLink , this . makeQuote , this . makeImage ] ,
157+ [ this . makeUnorderedList , this . makeOrderedList ]
158+ ]
152159 }
153160
154161}
0 commit comments