How to restore text content in codeEditorWrapper object upon refresh

I have a codeEditorWrapper object in a MainAreaWidget (see below) and when I refresh, all the text content in it disappears. Is there any way I can retain it similar to a regular file editor?

    app.commands.addCommand(command, {
      label: 'A Text Editor',
      execute: () => {

        const editorFactory = (options: CodeEditor.IOptions) => {
          options.config = {
            lineNumbers: true
          return new CodeMirrorEditor(options);

        let model = new CodeEditor.Model();
        let content = new CodeEditorWrapper({model, factory: editorFactory});
        let widget = new MainAreaWidget({content}); = 'a-text-editor';
        widget.title.label = 'A Text Editor';
        widget.title.closable = true;

        if (!tracker.has(widget)) {
        if (!widget.isAttached) {
, 'main');

        // function that deals with my keyboard events
        keyboardEvents(widget, content);
    palette.addItem({ command, category: 'editor' });

    let tracker = new WidgetTracker<MainAreaWidget>({
      namespace: 'text-editor'

    restorer.restore(tracker, {
      name: () => 'text-editor'

Responding to this and

At a basic level you need the context to have information about the underlying file on the disk where the contents will be saved. One way to do this is to register your factory with the document registry so that the document manager can make use of it (and pass the relevant context with the document model to your factory).

Given the example factory of:

you want to register it:

and then to get restoration of content upon refresh:

This equals to delegating all tasks like opening and saving files to docmanager; you can still invoke those actions manually, e.g.:

I have not touched this extension in a while so there might be something I forgotten about, but this is the gist.

Of course, unless you want to save the file in a cloud or elsewhere - then you would need to take care of providing the appropriate context yourself.

So from my understanding from your answer, I would need to create a new factory with context and register the factory with the document registry (app.docRegistry.addWidgetFactory(factory);). Then I would need to replace my editorFactory with the new factory object. But if I create the new factory, the factory object would be of type WidgetFactory and not type Factory which is needed to pass into the CodeEditorWrapper constructor ( let content = new CodeEditorWrapper({model, factory: editorFactory});). How would I handle that? I’m new that at this so let me know if I’m misunderstanding anything please. Thanks.