gaelj / BlazorCodeMirror6

Blazor wrapper for CodeMirror 6
MIT License
20 stars 2 forks source link

Question: Accessing BlazorCodeMirror javascript object in javascript #159

Closed domingoladron closed 8 months ago

domingoladron commented 8 months ago

Hey there,

I know that the BlazorCodeMirror is a Blazor wrapper over the underlying CodeMirror javascript objects and code. So I want to be able to access the CodeMirror javascript object from my js code. Is there a convention or simple means of getting a handle on the CodeMirror Editor js object in my javascript? I want to be able to hook into the underlying CodeMirror js code to manipulate it accordingly, then use Blazor JSInterop to trigger these behaviours from user interaction.

Cheers

pm64 commented 8 months ago

Hey @domingoladron, you can use the getCmInstance() export for this purpose.

First, import the method:

import { getCmInstance } from "./_content/GaelJ.BlazorCodeMirror6/index.js"

Then pass the instance's ID to getCmInstance():

var view = getCmInstance(id).view;

You'll then have access to the object. Hope that helps!

domingoladron commented 8 months ago

Aw, that's mint! Cheers for the great answer and rapid turnaround. Super helpful!

domingoladron commented 8 months ago

One last thing. HOW do I get the ID of the current CodeMirror from my Blazor code so I can pass it along to the javascript? I note the "ID" property of the CodeMirror6Wrapper has been removed in more recent versions of the library.

pm64 commented 8 months ago

CodeMirror6Wrapper has a Setup property of type CodeMirrorSetup, which exposes a public string Id.

domingoladron commented 8 months ago

Ah, there it is! That's great. Thanks again.