@@ -104,7 +104,15 @@ class Playground extends React.Component {
104104 this . setContent = ( content ) => this . setState ( { content } ) ;
105105 this . clearContent = this . setContent . bind ( this , "" ) ;
106106 this . resetOptions = ( ) => this . setState ( { options : defaultOptions } ) ;
107- this . setSelection = ( selection ) => this . setState ( { selection } ) ;
107+ this . setSelection = ( selection ) => {
108+ this . setState ( { selection } ) ;
109+ if ( this . state . trackCursorOffset ) {
110+ this . handleOptionValueChange (
111+ this . cursorOffsetOption ,
112+ util . convertSelectionToRange ( selection , this . state . content ) [ 0 ] ,
113+ ) ;
114+ }
115+ } ;
108116 this . setSelectionAsRange = ( ) => {
109117 const { selection, content, options } = this . state ;
110118 const [ rangeStart , rangeEnd ] = util . convertSelectionToRange (
@@ -126,6 +134,9 @@ class Playground extends React.Component {
126134 this . rangeEndOption = props . availableOptions . find (
127135 ( opt ) => opt . name === "rangeEnd" ,
128136 ) ;
137+ this . cursorOffsetOption = props . availableOptions . find (
138+ ( opt ) => opt . name === "cursorOffset" ,
139+ ) ;
129140
130141 this . formatInput = this . formatInput . bind ( this ) ;
131142 this . insertDummyId = this . insertDummyId . bind ( this ) ;
@@ -172,6 +183,7 @@ class Playground extends React.Component {
172183 ...ENABLED_OPTIONS ,
173184 "rangeStart" ,
174185 "rangeEnd" ,
186+ "cursorOffset" ,
175187 ] ) ;
176188 const cliOptions = util . buildCliArgs ( orderedOptions , options ) ;
177189
@@ -247,7 +259,7 @@ class Playground extends React.Component {
247259 reformat = { editorState . showSecondFormat }
248260 rethrowEmbedErrors = { editorState . rethrowEmbedErrors }
249261 >
250- { ( { formatted, debug } ) => {
262+ { ( { formatted, debug, cursorOffset } ) => {
251263 const fullReport = this . getMarkdown ( {
252264 formatted,
253265 reformatted : debug . reformatted ,
@@ -294,6 +306,49 @@ class Playground extends React.Component {
294306 Set selected text as range
295307 </ Button >
296308 </ SidebarCategory >
309+ < SidebarCategory title = "Cursor" >
310+ < Option
311+ option = { this . cursorOffsetOption }
312+ value = {
313+ options . cursorOffset >= 0
314+ ? options . cursorOffset
315+ : ""
316+ }
317+ onChange = { this . handleOptionValueChange }
318+ />
319+ < div
320+ style = { {
321+ display : "flex" ,
322+ alignItems : "baseline" ,
323+ gap : "10px" ,
324+ } }
325+ >
326+ < Checkbox
327+ label = "track"
328+ checked = { Boolean ( this . state . trackCursorOffset ) }
329+ onChange = { ( ) =>
330+ this . setState ( ( state ) => ( {
331+ trackCursorOffset : ! state . trackCursorOffset ,
332+ } ) )
333+ }
334+ />
335+ { options . cursorOffset >= 0 && (
336+ < >
337+ < Button
338+ onClick = { ( ) => {
339+ this . handleOptionValueChange (
340+ this . cursorOffsetOption ,
341+ - 1 ,
342+ ) ;
343+ } }
344+ >
345+ Reset
346+ </ Button >
347+ < label > Result: { cursorOffset } </ label >
348+ </ >
349+ ) }
350+ </ div >
351+ </ SidebarCategory >
297352 < SidebarCategory title = "Debug" >
298353 < Checkbox
299354 label = "show input"
@@ -419,6 +474,12 @@ class Playground extends React.Component {
419474 mode = { util . getCodemirrorMode ( options . parser ) }
420475 value = { formatted }
421476 ruler = { options . printWidth }
477+ overlayStart = {
478+ cursorOffset === - 1 ? undefined : cursorOffset
479+ }
480+ overlayEnd = {
481+ cursorOffset === - 1 ? undefined : cursorOffset + 1
482+ }
422483 />
423484 )
424485 ) : null }
0 commit comments