Search code examples
javascriptgtk3gjs

How to share contents between widgets in gjs


I'm trying to create a simple web editor demo. I want a web view widget to render the contents of a gtk source view widget... I have the widgets drawn, but I can't get the content out of the sourceview to give it to the webview. I thought I should use a buffer, but I can't get them to share a buffer. How is that supposed to work? Any pointers would be greatly appreciated.

something like this is what I was thinking:

const htmlBuffer = new GtkSource.Buffer();
const messagehtml = new GtkSource.View(htmlBuffer);
const webView = new Webkit.WebView({ vexpand: true });

htmlBuffer.connect('changed', upDateWebView);

Then in the upDateWebView() method, call

webView.load_html(htmlBuffer);

Solution

  • So... I finally got it to work like this:

    const htmlBuffer = new GtkSource.Buffer();
    const messagehtml = new GtkSource.View({ buffer: htmlBuffer });
    const webView = new Webkit.WebView({ vexpand: true });
    
    
      htmlBuffer.connect('changed', function () {
        webView.load_html(htmlBuffer.text, null);
      });