Skip to content

Reading ySyncFacet dynamically. #19

@a3626a

Description

@a3626a

Is your feature request related to a problem? Please describe.

Hi, I am working on this PR jupyterlab/jupyterlab#13093 . There is a need to dynamically update YSyncConfig after the initialization of EditorView. I think ySyncFacet is a nice place to do this. However YSyncPluginValue reads this facet during construction, and caches its value. So changing ySyncFacet after the initialization of EditorView has no effect on YSyncPluginValue.

I am changing ySyncFacet by using CodeMirror compartment.

packages/codemirror/src/editor.ts

...
    const ySyncFacetCompartment = new Compartment();
...
    this._editor = Private.createEditor(
      host,
      fullConfig,
      this._yeditorBinding,
      this._editorConfig,
      [
        this._markField,
        Prec.high(domEventHandlers),
        updateListener,
        translation,
        // might need some precedence setup here.
        ySyncFacetCompartment.of(
          ySyncFacet.of(
            new YSyncConfig(
              this._yeditorBinding?.text,
              this._yeditorBinding?.awareness
            )
          )
        ),
      ]
    );
...
    // every time the model is switched, we need to re-initialize the editor binding
    this.model.sharedModelSwitched.connect(() => {
      this._initializeEditorBinding();

      // it does change ySyncFacet. However YSyncPluginValue already cached old value.
      this._editor.dispatch({
        effects: ySyncFacetCompartment.reconfigure(
          ySyncFacet.of(
            new YSyncConfig(
              this._yeditorBinding?.text,
              this._yeditorBinding?.awareness
            )
          )
        )
      });
    }, this);
...

Describe the solution you'd like

I think just not caching the facet value can solve the problem.

Describe alternatives you've considered

No

Additional context

jupyterlab/jupyterlab#13093

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions