Search code examples
codemirror

How to search for and highlight a substring in Codemirror 6?


I'm building a simple code editor to help children learn HTML. One feature I'm trying to add is that when users mouseover their rendered code (in an iframe), the corresponding HTML code in the editor is highlighted. So, for example, if a user mouses-over an image of kittens, the actual code, , would be highlighted in the editor.

Mousing-over the iframe to get the html source for that element is the easy part, which I've done (using document.elementFromPoint(e.clientX, e.clientY in the iframe itself, and posting that up to the parent) - so that's not the part I need help with. The part I can't figure out is how to search for and highlight that string of selected code in the code editor.

I'm using Codemirror 6 for this project, as it seems as it will give me the most flexibility to create such a feature. However, as a Codemirror 6 novice, I'm struggling with the documentation to find out where I should start. It seems like the steps I need to complete to accomplish this are:

  1. Search for a range in the editor's text that matches a string (ie.'<img src="kittens.gif"').
  2. Highlight that range in the editor.

Can anyone out there give me some advice as to where in the Codemirror 6 API I should look to start implementing this? It seems like it should be easy, but my unfamiliarity with the Codemirror API and the terse documentation is making this difficult.


Solution

  • 1. Search for a range in the editor's text that matches a string (ie.'<img src="kittens.gif"').

    You can use SearchCursor class (iterator) to get the character's range where is located the DOM element in your editor.

    // the import for SearchCursor class
    import {SearchCursor} from "@codemirror/search"
    
    // your editor's view
    let main_view = new EditorView({ /* your code */ });
    
    // will create a cursor based on the doc content and the DOM element as a string (outerHTML)
    let cursor = new SearchCursor(main_view.state.doc, element.outerHTML); 
    
    // will search the first match of the string element.outerHTML in the editor view main_view.state.doc
    cursor.next() 
    
    // display the range where is located your DOM element in your editor
    console.log(cursor.value); 
    

    2. Highlight that range in the editor.

    As described in the migration documentation here, marked text is replace by decoration. To highlight a range in the editor with codemirror 6, you need to create one decoration and apply it in a dispatch on your view. This decoration need to be provide by an extension that you add in the extensions of your editor view.

    // the import for the 3 new classes
    import {StateEffect, StateField} from "@codemirror/state"
    import {Decoration} from "@codemirror/view"
    
    // code mirror effect that you will use to define the effect you want (the decoration)
    const highlight_effect = StateEffect.define(); 
    
    // define a new field that will be attached to your view state as an extension, update will be called at each editor's change
    const highlight_extension = StateField.define({
      create() { return Decoration.none },
      update(value, transaction) {
        value = value.map(transaction.changes)
    
        for (let effect of transaction.effects) {
          if (effect.is(highlight_effect)) value = value.update({add: effect.value, sort: true})
        }
    
        return value
      },
      provide: f => EditorView.decorations.from(f)
    });
    
    // this is your decoration where you can define the change you want : a css class or directly css attributes
    const highlight_decoration = Decoration.mark({
      // attributes: {style: "background-color: red"}
      class: 'red_back'
    });
    
    // your editor's view
    let main_view = new EditorView({ 
      extensions: [highlight_extension]
    });
    
    // this is where the change takes effect by the dispatch. The of method instanciate the effect. You need to put this code where you want the change to take place
    main_view.dispatch({
      effects: highlight_effect.of([highlight_decoration.range(cursor.value.from, cursor.value.to)])
    });
    
    

    Hope it will help you to implement what you want ;)