11/**
22 * @todo remove wrapper if selection () inside it:
3- * s([ample tex])t -> sample text
4- * s[ampl(e t)ex]t -> s[ampl]e t[ex]t @todo create splitter
5- * s(ampl[e t]ex)t -> s[ampl[e t]ex]t do not remove wrapper
6- * s([ampl]e t[ex])t -> s[[ampl]e t[ex]]t do not remove wrapper
3+ * [x] s[(ample tex)]t -> sample text
4+ * [x] s[ampl(e t)ex]t -> sample text
5+ * (?) s[ampl(e t)ex]t -> s[ampl]e t[ex]t @todo create splitter
6+ * [x] s(ampl[e t]ex)t -> s[ampl[e t]ex]t add remove wrapper
7+ * [ ] s[(ampl]e t[ex)]t -> s[[ampl]e t[ex]]t add wrapper
78 *
89 * @todo process cross-tag wrapping []:
910 * sam[ple <b>te]xt</b> -> sam[ple ]<b>[te]xt</b>
1213 * sa[mple t][ex]t -> sa[mple tex]t
1314 * sa[mpl[e t]ex]t -> sa[mple tex]t
1415 * @see https://developer.mozilla.org/en-US/docs/Web/API/Range/commonAncestorContainer
15- *
16- * @todo save selection after clicking on tool
1716 */
1817
1918class Marker {
2019 constructor ( api ) {
2120 this . api = api ;
22- console . log ( this . api ) ;
2321
2422 this . button = null ;
23+ this . TAG = 'SPAN' ;
2524 this . CSS = 'marked' ;
2625 }
2726
@@ -44,32 +43,43 @@ class Marker {
4443
4544 let state = this . checkState ( ) ;
4645
46+ /**
47+ * If start or end of selection is in the highlighted block
48+ */
4749 if ( state ) {
48- console . log ( 'range' , range ) ;
49- console . log ( 'state' , state ) ;
50-
51- /** @todo remove highlighting */
52-
53- // /**
54- // *
55- // */
56- // if (range.startContainer === range.endContainer) {
57- // /**
58- // * @todo remove whole wrapper not class
59- // */
60- // state.classList.remove(this.CSS);
61- // }
50+ /**
51+ * Expand selection
52+ */
53+ this . api . selection . expandToTag ( state ) ;
54+
55+ /**
56+ * Remove wrapper
57+ */
58+ state . outerHTML = state . innerHTML ;
59+
60+ // @todo save selection on the text
6261 } else {
63- let span = document . createElement ( 'SPAN' ) ;
62+ /**
63+ * Create a wrapper for highlighting
64+ */
65+ let span = document . createElement ( this . TAG ) ;
6466
6567 span . classList . add ( this . CSS ) ;
68+
69+ /**
70+ * Wrap text with wrapper tag
71+ */
6672 range . surroundContents ( span ) ;
67- range . selectNode ( span ) ;
73+
74+ /**
75+ * Expand (add) selection to highlighted block
76+ */
77+ this . api . selection . expandToTag ( span ) ;
6878 }
6979 }
7080
7181 checkState ( selection ) {
72- const markerTag = this . api . selection . findParentTag ( 'SPAN' , this . CSS ) ;
82+ const markerTag = this . api . selection . findParentTag ( this . TAG , this . CSS ) ;
7383
7484 this . button . classList . toggle ( 'ce-inline-tool--active' , ! ! markerTag ) ;
7585
0 commit comments