Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom-editor: restore editor as part of layout #10787

Closed
vince-fugnitto opened this issue Feb 23, 2022 · 9 comments · Fixed by #13916
Closed

custom-editor: restore editor as part of layout #10787

vince-fugnitto opened this issue Feb 23, 2022 · 9 comments · Fixed by #13916
Labels
custom-editor issues related to custom-editor functionality
Milestone

Comments

@vince-fugnitto
Copy link
Member

Bug Description:

The framework currently does not support restoring custom-editors as part of the layout. If a given custom-editor is opened and the application is restarted (reload the window for example) the editor will not be preserved.

custom-editor-bug.mp4

Steps to Reproduce:

  1. use the custom-editor-sample extension.
  2. start the application using exampleFiles as a workspace.
  3. open example.cscratch - the custom editor should open.
  4. reload the application - confirm that the editor is not preserved.
@vince-fugnitto vince-fugnitto added the custom-editor issues related to custom-editor functionality label Feb 23, 2022
@safisa
Copy link
Contributor

safisa commented Mar 21, 2022

Hi,
Any updates on this issue?
Or, can you help on how to fix it using Theia extension?
Thanks

@vince-fugnitto
Copy link
Member Author

Or, can you help on how to fix it using Theia extension?

@safisa I haven't worked on the issue, if you are interested please don't hesitate on contributing a fix. You'll need to debug as to why the custom-editors are not being properly stored or restored in the layout.

Some interesting places to look:

override storeState(): CustomEditorWidget.State {
return {
...super.storeState(),
strResource: this.resource.toString(),
};
}
override restoreState(oldState: CustomEditorWidget.State): void {
const { strResource } = oldState;
this.resource = new URI(strResource);
super.restoreState(oldState);
}

@postConstruct()
protected init(): void {
this.widgetManager.onDidCreateWidget(({ factoryId, widget }) => {
if (factoryId === CustomEditorWidget.FACTORY_ID && widget instanceof CustomEditorWidget) {
const restoreState = widget.restoreState.bind(widget);
widget.restoreState = state => {
if (state.viewType && state.strResource) {
restoreState(state);
this.resolveWidget(widget);
} else {
widget.dispose();
}
};
}
});
}

The layout is stored in the localStorage of the browser or electron.

@safisa
Copy link
Contributor

safisa commented Aug 30, 2022

To resolve this issue, we need to registerWebviewPanelSerializer in the vscode extension code. This was missing in my implementation, once I did it (in the following example) the custom editor layout was restored on reload. BUT there was a small issue on restore, getting undefined error in Theia CustomEditorWidget class, the _modelRef is undefined (we got there from a call to the setLayoutData in the application shell):

get saveable(): Saveable {
        return this._modelRef.object;
    }

Once overriding this in my Theia extension:

@injectable()
export class MyCustomEditorWidget extends CustomEditorWidget {

    // override saveable() to fix the undefined _modelRef issue when reviving (restoring state) after reload (when using the registerWebviewPanelSerializer in the vscode extesion)
    get saveable(): Saveable {
        return this._modelRef?.object;
    }
}

An example of a registerWebviewPanelSerializer in the vscode extension code:

let command = vscode.window.registerWebviewPanelSerializer(viewType, {
	async deserializeWebviewPanel(webviewPanel: vscode.WebviewPanel, state: any) {
		// `state` is the state persisted using `setState` inside the webview
		//console.log(`Got state: ${state}`);
		// Reset the webview options so we use latest uri for `localResourceRoots`.
		webviewPanel.webview.options = getWebviewOptions(context.extensionUri);
	}
});
context.subscriptions.push(command);

I will open a PR to fix the saveable issue in the CustomEditorWidget class.

@pisv
Copy link
Contributor

pisv commented Jul 1, 2024

Just a note that I'm working on a fix.

@msujew
Copy link
Member

msujew commented Jul 1, 2024

@pisv Just a quick FYI: We had a similar issue while implementing the notebook support for Theia. We introduced the DelegatingSaveable for this, which might be useful for your fix.

@pisv
Copy link
Contributor

pisv commented Jul 1, 2024

@msujew Sure, my fix is actually based on using the DelegatingSaveable :-) Thank you!

@pisv
Copy link
Contributor

pisv commented Jul 1, 2024

If there are no objections, the fix will be submitted as a separate commit in a bigger PR for #9079, which will also fix a number of other issues; I'm currently working on saving/restoring state of side-by-side editors containing CustomEditors, and depend on this fix for implementing and testing the feature.

@msujew
Copy link
Member

msujew commented Jul 3, 2024

@pisv I think that's perfectly fine, just make sure to include testing steps for all the things you have fixed :)

pisv added a commit to pisv/theia that referenced this issue Jul 9, 2024
Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes eclipse-theia#10787
pisv added a commit to pisv/theia that referenced this issue Jul 10, 2024
Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes eclipse-theia#10787
@pisv
Copy link
Contributor

pisv commented Jul 11, 2024

I have submitted PR #13916, which contains a fix for this issue.

pisv added a commit to pisv/theia that referenced this issue Jul 11, 2024
Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes eclipse-theia#10787
pisv added a commit to pisv/theia that referenced this issue Aug 7, 2024
Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes eclipse-theia#10787
msujew pushed a commit that referenced this issue Aug 21, 2024
* Fix loading of webview resources that depend on query params

Some resource url (notably git) use the query part of the url to store
additional information. The query part of the url was incorrectly being
dropped while attempting to load these resources inside of webviews.

See also microsoft/vscode@48387df

* Fix `Error: Unknown Webview` messages in the log

* Fix design and implementation issues surrounding `CustomEditorOpener`

This commit ensures that the promise returned by `CustomEditorOpener.open`
will only resolve to a properly initialized and opened `CustomEditorWidget`.
In particular, it ensures that the widget is opened according to the specified
`WidgetOpenerOptions`, including `widgetOptions.ref` and `mode`.

Essentially, it revises the work done in #9671 and #10580
to fix #9670 and #10583.

* Restore custom editors as part of layout

Fixes an incorrect assumption that a custom editor cannot be restored
if no `WebviewPanelSerializer` is registered for its view type. (Actually,
custom editors are created and restored using a custom editor provider.)

Also, ensures that `CustomEditorWidget.modelRef` satisfies the shape for the
`CustomEditorWidget` defined in `editor.ts` and cannot return `undefined`.
(However, `CustomEditorWidget.modelRef.object` can be `undefined`
until the custom editor is resolved.)

Fixes #10787

* Fix a race condition when file system provider is activated

When file system provider is activated, wait until it is registered.

* git: add support for custom editors

* Uses `OpenerService` instead of `EditorManager` to open editors

* Contributes a `FileSystemProvider` for git-resources

* Fixes an issue with getting blob contents

* custom editor: open a diff-uri in a side-by-side editor

`CustomEditorOpener` is now able to open a diff-uri in a side-by-side editor,
which contains the corresponding `CustomEditor`s.

Fixes #9079
@jfaltermeier jfaltermeier added this to the 1.53.0 milestone Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
custom-editor issues related to custom-editor functionality
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants