@theia/editor-preview 1.53.0-next.4 → 1.53.0-next.55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,52 +1,52 @@
1
- <div align='center'>
2
-
3
- <br />
4
-
5
- <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
-
7
- <h2>ECLIPSE THEIA - EDITOR-PREVIEW EXTENSION</h2>
8
-
9
- <hr />
10
-
11
- </div>
12
-
13
- ## Description
14
-
15
- The `@theia/editor-preview` extension contributes the preview editor which supports the same functionality as a regular editor widget with the exception:
16
- - if a preview editor has not "transitioned to a permanent editor" at the time an additional request to
17
- preview a file is received, instead of opening a new editor, it will display the contents of the
18
- newly requested file.
19
-
20
- Events that will transition the preview to a permanent editor are as follows:
21
- * Modifying file contents being previewed
22
- * Double clicking the preview tab
23
- * Performing a drag/drop operation of the editor preview tab resulting in the tab being moved.
24
- * Issuing a request to open the file being previewed (e.g. double clicking the file in the
25
- navigator)
26
-
27
- The preview editor is enabled by default when the extension is included in a Theia application, but
28
- may be disabled by modifying the preference:
29
- ```json
30
- editor.enablePreview
31
- ```
32
-
33
- In addition to this value, the preference:
34
- ```json
35
- list.openMode
36
- ```
37
- must be set to "singleClick" to enable opening files in preview mode.
38
-
39
- ## Additional Information
40
-
41
- - [API documentation for `@theia/editor-preview`](https://eclipse-theia.github.io/theia/docs/next/modules/editor_preview.html)
42
- - [Theia - GitHub](https://github.com/eclipse-theia/theia)
43
- - [Theia - Website](https://theia-ide.org/)
44
-
45
- ## License
46
-
47
- - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
48
- - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
49
-
50
- ## Trademark
51
- "Theia" is a trademark of the Eclipse Foundation
52
- https://www.eclipse.org/theia
1
+ <div align='center'>
2
+
3
+ <br />
4
+
5
+ <img src='https://raw.githubusercontent.com/eclipse-theia/theia/master/logo/theia.svg?sanitize=true' alt='theia-ext-logo' width='100px' />
6
+
7
+ <h2>ECLIPSE THEIA - EDITOR-PREVIEW EXTENSION</h2>
8
+
9
+ <hr />
10
+
11
+ </div>
12
+
13
+ ## Description
14
+
15
+ The `@theia/editor-preview` extension contributes the preview editor which supports the same functionality as a regular editor widget with the exception:
16
+ - if a preview editor has not "transitioned to a permanent editor" at the time an additional request to
17
+ preview a file is received, instead of opening a new editor, it will display the contents of the
18
+ newly requested file.
19
+
20
+ Events that will transition the preview to a permanent editor are as follows:
21
+ * Modifying file contents being previewed
22
+ * Double clicking the preview tab
23
+ * Performing a drag/drop operation of the editor preview tab resulting in the tab being moved.
24
+ * Issuing a request to open the file being previewed (e.g. double clicking the file in the
25
+ navigator)
26
+
27
+ The preview editor is enabled by default when the extension is included in a Theia application, but
28
+ may be disabled by modifying the preference:
29
+ ```json
30
+ editor.enablePreview
31
+ ```
32
+
33
+ In addition to this value, the preference:
34
+ ```json
35
+ list.openMode
36
+ ```
37
+ must be set to "singleClick" to enable opening files in preview mode.
38
+
39
+ ## Additional Information
40
+
41
+ - [API documentation for `@theia/editor-preview`](https://eclipse-theia.github.io/theia/docs/next/modules/editor_preview.html)
42
+ - [Theia - GitHub](https://github.com/eclipse-theia/theia)
43
+ - [Theia - Website](https://theia-ide.org/)
44
+
45
+ ## License
46
+
47
+ - [Eclipse Public License 2.0](http://www.eclipse.org/legal/epl-2.0/)
48
+ - [一 (Secondary) GNU General Public License, version 2 with the GNU Classpath Exception](https://projects.eclipse.org/license/secondary-gpl-2.0-cp)
49
+
50
+ ## Trademark
51
+ "Theia" is a trademark of the Eclipse Foundation
52
+ https://www.eclipse.org/theia
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@theia/editor-preview",
3
- "version": "1.53.0-next.4+9e9124118",
3
+ "version": "1.53.0-next.55+d1a989a68c",
4
4
  "description": "Theia - Editor Preview Extension",
5
5
  "dependencies": {
6
- "@theia/core": "1.53.0-next.4+9e9124118",
7
- "@theia/editor": "1.53.0-next.4+9e9124118",
8
- "@theia/navigator": "1.53.0-next.4+9e9124118",
6
+ "@theia/core": "1.53.0-next.55+d1a989a68c",
7
+ "@theia/editor": "1.53.0-next.55+d1a989a68c",
8
+ "@theia/navigator": "1.53.0-next.55+d1a989a68c",
9
9
  "tslib": "^2.6.2"
10
10
  },
11
11
  "publishConfig": {
@@ -41,10 +41,10 @@
41
41
  "watch": "theiaext watch"
42
42
  },
43
43
  "devDependencies": {
44
- "@theia/ext-scripts": "1.52.0"
44
+ "@theia/ext-scripts": "1.53.0"
45
45
  },
46
46
  "nyc": {
47
47
  "extends": "../../configs/nyc.json"
48
48
  },
49
- "gitHead": "9e912411843f32b2a549ef6580b07872edd95e8b"
49
+ "gitHead": "d1a989a68c1b5ec1f9098e9126653c6346844769"
50
50
  }
@@ -1,67 +1,67 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2021 Ericsson and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { ApplicationShell, CommonCommands, KeybindingContribution, KeybindingRegistry, SHELL_TABBAR_CONTEXT_PIN, Widget } from '@theia/core/lib/browser';
18
- import { nls } from '@theia/core/lib/common/nls';
19
- import { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core/lib/common';
20
- import { inject, injectable } from '@theia/core/shared/inversify';
21
- import { EditorPreviewWidget } from './editor-preview-widget';
22
- import { CurrentWidgetCommandAdapter } from '@theia/core/lib/browser/shell/current-widget-command-adapter';
23
-
24
- export namespace EditorPreviewCommands {
25
- export const PIN_PREVIEW_COMMAND = Command.toDefaultLocalizedCommand({
26
- id: 'workbench.action.keepEditor',
27
- category: CommonCommands.VIEW_CATEGORY,
28
- label: 'Keep Editor',
29
- });
30
- }
31
-
32
- @injectable()
33
- export class EditorPreviewContribution implements CommandContribution, MenuContribution, KeybindingContribution {
34
- @inject(ApplicationShell) protected readonly shell: ApplicationShell;
35
-
36
- registerCommands(registry: CommandRegistry): void {
37
- registry.registerCommand(EditorPreviewCommands.PIN_PREVIEW_COMMAND, new CurrentWidgetCommandAdapter(this.shell, {
38
- execute: async title => {
39
- if (title?.owner instanceof EditorPreviewWidget) {
40
- title.owner.convertToNonPreview();
41
- await this.shell.activateWidget(title.owner.id);
42
- }
43
- },
44
- isEnabled: title => title?.owner instanceof EditorPreviewWidget && title.owner.isPreview,
45
- isVisible: title => title?.owner instanceof EditorPreviewWidget,
46
- }));
47
- }
48
-
49
- registerKeybindings(registry: KeybindingRegistry): void {
50
- registry.registerKeybinding({
51
- command: EditorPreviewCommands.PIN_PREVIEW_COMMAND.id,
52
- keybinding: 'ctrlcmd+k enter'
53
- });
54
- }
55
-
56
- registerMenus(registry: MenuModelRegistry): void {
57
- registry.registerMenuAction(SHELL_TABBAR_CONTEXT_PIN, {
58
- commandId: EditorPreviewCommands.PIN_PREVIEW_COMMAND.id,
59
- label: nls.localizeByDefault('Keep Open'),
60
- order: '6',
61
- });
62
- }
63
-
64
- protected getTargetWidget(event?: Event): Widget | undefined {
65
- return event ? this.shell.findTargetedWidget(event) : this.shell.activeWidget;
66
- }
67
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2021 Ericsson and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { ApplicationShell, CommonCommands, KeybindingContribution, KeybindingRegistry, SHELL_TABBAR_CONTEXT_PIN, Widget } from '@theia/core/lib/browser';
18
+ import { nls } from '@theia/core/lib/common/nls';
19
+ import { Command, CommandContribution, CommandRegistry, MenuContribution, MenuModelRegistry } from '@theia/core/lib/common';
20
+ import { inject, injectable } from '@theia/core/shared/inversify';
21
+ import { EditorPreviewWidget } from './editor-preview-widget';
22
+ import { CurrentWidgetCommandAdapter } from '@theia/core/lib/browser/shell/current-widget-command-adapter';
23
+
24
+ export namespace EditorPreviewCommands {
25
+ export const PIN_PREVIEW_COMMAND = Command.toDefaultLocalizedCommand({
26
+ id: 'workbench.action.keepEditor',
27
+ category: CommonCommands.VIEW_CATEGORY,
28
+ label: 'Keep Editor',
29
+ });
30
+ }
31
+
32
+ @injectable()
33
+ export class EditorPreviewContribution implements CommandContribution, MenuContribution, KeybindingContribution {
34
+ @inject(ApplicationShell) protected readonly shell: ApplicationShell;
35
+
36
+ registerCommands(registry: CommandRegistry): void {
37
+ registry.registerCommand(EditorPreviewCommands.PIN_PREVIEW_COMMAND, new CurrentWidgetCommandAdapter(this.shell, {
38
+ execute: async title => {
39
+ if (title?.owner instanceof EditorPreviewWidget) {
40
+ title.owner.convertToNonPreview();
41
+ await this.shell.activateWidget(title.owner.id);
42
+ }
43
+ },
44
+ isEnabled: title => title?.owner instanceof EditorPreviewWidget && title.owner.isPreview,
45
+ isVisible: title => title?.owner instanceof EditorPreviewWidget,
46
+ }));
47
+ }
48
+
49
+ registerKeybindings(registry: KeybindingRegistry): void {
50
+ registry.registerKeybinding({
51
+ command: EditorPreviewCommands.PIN_PREVIEW_COMMAND.id,
52
+ keybinding: 'ctrlcmd+k enter'
53
+ });
54
+ }
55
+
56
+ registerMenus(registry: MenuModelRegistry): void {
57
+ registry.registerMenuAction(SHELL_TABBAR_CONTEXT_PIN, {
58
+ commandId: EditorPreviewCommands.PIN_PREVIEW_COMMAND.id,
59
+ label: nls.localizeByDefault('Keep Open'),
60
+ order: '6',
61
+ });
62
+ }
63
+
64
+ protected getTargetWidget(event?: Event): Widget | undefined {
65
+ return event ? this.shell.findTargetedWidget(event) : this.shell.activeWidget;
66
+ }
67
+ }
@@ -1,46 +1,46 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018-2021 Google and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import '../../src/browser/style/index.css';
18
- import { FrontendApplicationContribution, KeybindingContribution, WidgetFactory } from '@theia/core/lib/browser';
19
- import { ContainerModule } from '@theia/core/shared/inversify';
20
- import { bindEditorPreviewPreferences } from './editor-preview-preferences';
21
- import { EditorPreviewManager } from './editor-preview-manager';
22
- import { EditorManager } from '@theia/editor/lib/browser';
23
- import { EditorPreviewWidgetFactory } from './editor-preview-widget-factory';
24
- import { EditorPreviewContribution } from './editor-preview-contribution';
25
- import { CommandContribution, MenuContribution } from '@theia/core/lib/common';
26
- import { OpenEditorsTreeDecorator } from '@theia/navigator/lib/browser/open-editors-widget/navigator-open-editors-decorator-service';
27
- import { EditorPreviewTreeDecorator } from './editor-preview-tree-decorator';
28
-
29
- export default new ContainerModule((bind, unbind, isBound, rebind) => {
30
-
31
- bind(EditorPreviewWidgetFactory).toSelf().inSingletonScope();
32
- bind(WidgetFactory).toService(EditorPreviewWidgetFactory);
33
-
34
- bind(EditorPreviewManager).toSelf().inSingletonScope();
35
- rebind(EditorManager).toService(EditorPreviewManager);
36
-
37
- bind(EditorPreviewContribution).toSelf().inSingletonScope();
38
- bind(CommandContribution).toService(EditorPreviewContribution);
39
- bind(KeybindingContribution).toService(EditorPreviewContribution);
40
- bind(MenuContribution).toService(EditorPreviewContribution);
41
-
42
- bind(EditorPreviewTreeDecorator).toSelf().inSingletonScope();
43
- bind(OpenEditorsTreeDecorator).toService(EditorPreviewTreeDecorator);
44
- bind(FrontendApplicationContribution).toService(EditorPreviewTreeDecorator);
45
- bindEditorPreviewPreferences(bind);
46
- });
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018-2021 Google and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import '../../src/browser/style/index.css';
18
+ import { FrontendApplicationContribution, KeybindingContribution, WidgetFactory } from '@theia/core/lib/browser';
19
+ import { ContainerModule } from '@theia/core/shared/inversify';
20
+ import { bindEditorPreviewPreferences } from './editor-preview-preferences';
21
+ import { EditorPreviewManager } from './editor-preview-manager';
22
+ import { EditorManager } from '@theia/editor/lib/browser';
23
+ import { EditorPreviewWidgetFactory } from './editor-preview-widget-factory';
24
+ import { EditorPreviewContribution } from './editor-preview-contribution';
25
+ import { CommandContribution, MenuContribution } from '@theia/core/lib/common';
26
+ import { OpenEditorsTreeDecorator } from '@theia/navigator/lib/browser/open-editors-widget/navigator-open-editors-decorator-service';
27
+ import { EditorPreviewTreeDecorator } from './editor-preview-tree-decorator';
28
+
29
+ export default new ContainerModule((bind, unbind, isBound, rebind) => {
30
+
31
+ bind(EditorPreviewWidgetFactory).toSelf().inSingletonScope();
32
+ bind(WidgetFactory).toService(EditorPreviewWidgetFactory);
33
+
34
+ bind(EditorPreviewManager).toSelf().inSingletonScope();
35
+ rebind(EditorManager).toService(EditorPreviewManager);
36
+
37
+ bind(EditorPreviewContribution).toSelf().inSingletonScope();
38
+ bind(CommandContribution).toService(EditorPreviewContribution);
39
+ bind(KeybindingContribution).toService(EditorPreviewContribution);
40
+ bind(MenuContribution).toService(EditorPreviewContribution);
41
+
42
+ bind(EditorPreviewTreeDecorator).toSelf().inSingletonScope();
43
+ bind(OpenEditorsTreeDecorator).toService(EditorPreviewTreeDecorator);
44
+ bind(FrontendApplicationContribution).toService(EditorPreviewTreeDecorator);
45
+ bindEditorPreviewPreferences(bind);
46
+ });
@@ -1,125 +1,125 @@
1
- // *****************************************************************************
2
- // Copyright (C) 2018-2021 Google and others.
3
- //
4
- // This program and the accompanying materials are made available under the
5
- // terms of the Eclipse Public License v. 2.0 which is available at
6
- // http://www.eclipse.org/legal/epl-2.0.
7
- //
8
- // This Source Code may also be made available under the following Secondary
9
- // Licenses when the conditions for such availability set forth in the Eclipse
10
- // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
- // with the GNU Classpath Exception which is available at
12
- // https://www.gnu.org/software/classpath/license.html.
13
- //
14
- // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
- // *****************************************************************************
16
-
17
- import { EditorManager, EditorOpenerOptions, EditorWidget } from '@theia/editor/lib/browser';
18
- import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
19
- import { EditorPreviewPreferences } from './editor-preview-preferences';
20
- import { MaybePromise } from '@theia/core/lib/common';
21
- import URI from '@theia/core/lib/common/uri';
22
- import { EditorPreviewWidgetFactory, EditorPreviewOptions } from './editor-preview-widget-factory';
23
- import { EditorPreviewWidget } from './editor-preview-widget';
24
- import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
25
-
26
- @injectable()
27
- export class EditorPreviewManager extends EditorManager {
28
- override readonly id = EditorPreviewWidgetFactory.ID;
29
-
30
- @inject(EditorPreviewPreferences) protected readonly preferences: EditorPreviewPreferences;
31
- @inject(FrontendApplicationStateService) protected readonly stateService: FrontendApplicationStateService;
32
-
33
- /**
34
- * Until the layout has been restored, widget state is not reliable, so we ignore creation events.
35
- */
36
- protected layoutIsSet = false;
37
-
38
- @postConstruct()
39
- protected override init(): void {
40
- super.init();
41
- // All editors are created, but not all are opened. This sets up the logic to swap previews when the editor is attached.
42
- this.onCreated((widget: EditorPreviewWidget) => {
43
- if (this.layoutIsSet && widget.isPreview) {
44
- const oneTimeDisposable = widget.onDidChangeVisibility(() => {
45
- this.handleNewPreview(widget);
46
- oneTimeDisposable.dispose();
47
- });
48
- }
49
- });
50
-
51
- this.preferences.onPreferenceChanged(change => {
52
- if (change.preferenceName === 'editor.enablePreview' && !change.newValue) {
53
- this.all.forEach((editor: EditorPreviewWidget) => {
54
- if (editor.isPreview) {
55
- editor.convertToNonPreview();
56
- }
57
- });
58
- };
59
- });
60
-
61
- this.stateService.reachedState('initialized_layout').then(() => {
62
- const editors = this.all as EditorPreviewWidget[];
63
- const currentPreview = editors.find(editor => editor.isPreview);
64
- if (currentPreview) {
65
- this.handleNewPreview(currentPreview);
66
- }
67
- this.layoutIsSet = true;
68
- });
69
-
70
- document.addEventListener('dblclick', this.convertEditorOnDoubleClick.bind(this));
71
- }
72
-
73
- protected override async doOpen(widget: EditorPreviewWidget, options?: EditorOpenerOptions): Promise<void> {
74
- const { preview, widgetOptions = { area: 'main' }, mode = 'activate' } = options ?? {};
75
- if (!widget.isAttached) {
76
- this.shell.addWidget(widget, widgetOptions);
77
- } else if (!preview && widget.isPreview) {
78
- widget.convertToNonPreview();
79
- }
80
-
81
- if (mode === 'activate') {
82
- await this.shell.activateWidget(widget.id);
83
- } else if (mode === 'reveal') {
84
- await this.shell.revealWidget(widget.id);
85
- }
86
- }
87
-
88
- protected handleNewPreview(newPreviewWidget: EditorPreviewWidget): void {
89
- if (newPreviewWidget.isPreview) {
90
- const tabbar = this.shell.getTabBarFor(newPreviewWidget);
91
- if (tabbar) {
92
- for (const title of tabbar.titles) {
93
- if (title.owner !== newPreviewWidget && title.owner instanceof EditorPreviewWidget && title.owner.isPreview) {
94
- title.owner.dispose();
95
- }
96
- }
97
- }
98
- }
99
- }
100
-
101
- protected override tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
102
- return super.tryGetPendingWidget(uri, { ...options, preview: true }) ?? super.tryGetPendingWidget(uri, { ...options, preview: false });
103
- }
104
-
105
- protected override async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
106
- return (await super.getWidget(uri, { ...options, preview: true })) ?? super.getWidget(uri, { ...options, preview: false });
107
- }
108
-
109
- protected override async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
110
- return this.tryGetPendingWidget(uri, options) ?? super.getOrCreateWidget(uri, options);
111
- }
112
-
113
- protected override createWidgetOptions(uri: URI, options?: EditorOpenerOptions): EditorPreviewOptions {
114
- const navigatableOptions = super.createWidgetOptions(uri, options) as EditorPreviewOptions;
115
- navigatableOptions.preview = !!(options?.preview && this.preferences['editor.enablePreview']);
116
- return navigatableOptions;
117
- }
118
-
119
- protected convertEditorOnDoubleClick(event: Event): void {
120
- const widget = this.shell.findTargetedWidget(event);
121
- if (widget instanceof EditorPreviewWidget && widget.isPreview) {
122
- widget.convertToNonPreview();
123
- }
124
- }
125
- }
1
+ // *****************************************************************************
2
+ // Copyright (C) 2018-2021 Google and others.
3
+ //
4
+ // This program and the accompanying materials are made available under the
5
+ // terms of the Eclipse Public License v. 2.0 which is available at
6
+ // http://www.eclipse.org/legal/epl-2.0.
7
+ //
8
+ // This Source Code may also be made available under the following Secondary
9
+ // Licenses when the conditions for such availability set forth in the Eclipse
10
+ // Public License v. 2.0 are satisfied: GNU General Public License, version 2
11
+ // with the GNU Classpath Exception which is available at
12
+ // https://www.gnu.org/software/classpath/license.html.
13
+ //
14
+ // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
+ // *****************************************************************************
16
+
17
+ import { EditorManager, EditorOpenerOptions, EditorWidget } from '@theia/editor/lib/browser';
18
+ import { inject, injectable, postConstruct } from '@theia/core/shared/inversify';
19
+ import { EditorPreviewPreferences } from './editor-preview-preferences';
20
+ import { MaybePromise } from '@theia/core/lib/common';
21
+ import URI from '@theia/core/lib/common/uri';
22
+ import { EditorPreviewWidgetFactory, EditorPreviewOptions } from './editor-preview-widget-factory';
23
+ import { EditorPreviewWidget } from './editor-preview-widget';
24
+ import { FrontendApplicationStateService } from '@theia/core/lib/browser/frontend-application-state';
25
+
26
+ @injectable()
27
+ export class EditorPreviewManager extends EditorManager {
28
+ override readonly id = EditorPreviewWidgetFactory.ID;
29
+
30
+ @inject(EditorPreviewPreferences) protected readonly preferences: EditorPreviewPreferences;
31
+ @inject(FrontendApplicationStateService) protected readonly stateService: FrontendApplicationStateService;
32
+
33
+ /**
34
+ * Until the layout has been restored, widget state is not reliable, so we ignore creation events.
35
+ */
36
+ protected layoutIsSet = false;
37
+
38
+ @postConstruct()
39
+ protected override init(): void {
40
+ super.init();
41
+ // All editors are created, but not all are opened. This sets up the logic to swap previews when the editor is attached.
42
+ this.onCreated((widget: EditorPreviewWidget) => {
43
+ if (this.layoutIsSet && widget.isPreview) {
44
+ const oneTimeDisposable = widget.onDidChangeVisibility(() => {
45
+ this.handleNewPreview(widget);
46
+ oneTimeDisposable.dispose();
47
+ });
48
+ }
49
+ });
50
+
51
+ this.preferences.onPreferenceChanged(change => {
52
+ if (change.preferenceName === 'editor.enablePreview' && !change.newValue) {
53
+ this.all.forEach((editor: EditorPreviewWidget) => {
54
+ if (editor.isPreview) {
55
+ editor.convertToNonPreview();
56
+ }
57
+ });
58
+ };
59
+ });
60
+
61
+ this.stateService.reachedState('initialized_layout').then(() => {
62
+ const editors = this.all as EditorPreviewWidget[];
63
+ const currentPreview = editors.find(editor => editor.isPreview);
64
+ if (currentPreview) {
65
+ this.handleNewPreview(currentPreview);
66
+ }
67
+ this.layoutIsSet = true;
68
+ });
69
+
70
+ document.addEventListener('dblclick', this.convertEditorOnDoubleClick.bind(this));
71
+ }
72
+
73
+ protected override async doOpen(widget: EditorPreviewWidget, options?: EditorOpenerOptions): Promise<void> {
74
+ const { preview, widgetOptions = { area: 'main' }, mode = 'activate' } = options ?? {};
75
+ if (!widget.isAttached) {
76
+ this.shell.addWidget(widget, widgetOptions);
77
+ } else if (!preview && widget.isPreview) {
78
+ widget.convertToNonPreview();
79
+ }
80
+
81
+ if (mode === 'activate') {
82
+ await this.shell.activateWidget(widget.id);
83
+ } else if (mode === 'reveal') {
84
+ await this.shell.revealWidget(widget.id);
85
+ }
86
+ }
87
+
88
+ protected handleNewPreview(newPreviewWidget: EditorPreviewWidget): void {
89
+ if (newPreviewWidget.isPreview) {
90
+ const tabbar = this.shell.getTabBarFor(newPreviewWidget);
91
+ if (tabbar) {
92
+ for (const title of tabbar.titles) {
93
+ if (title.owner !== newPreviewWidget && title.owner instanceof EditorPreviewWidget && title.owner.isPreview) {
94
+ title.owner.dispose();
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+
101
+ protected override tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
102
+ return super.tryGetPendingWidget(uri, { ...options, preview: true }) ?? super.tryGetPendingWidget(uri, { ...options, preview: false });
103
+ }
104
+
105
+ protected override async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
106
+ return (await super.getWidget(uri, { ...options, preview: true })) ?? super.getWidget(uri, { ...options, preview: false });
107
+ }
108
+
109
+ protected override async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
110
+ return this.tryGetPendingWidget(uri, options) ?? super.getOrCreateWidget(uri, options);
111
+ }
112
+
113
+ protected override createWidgetOptions(uri: URI, options?: EditorOpenerOptions): EditorPreviewOptions {
114
+ const navigatableOptions = super.createWidgetOptions(uri, options) as EditorPreviewOptions;
115
+ navigatableOptions.preview = !!(options?.preview && this.preferences['editor.enablePreview']);
116
+ return navigatableOptions;
117
+ }
118
+
119
+ protected convertEditorOnDoubleClick(event: Event): void {
120
+ const widget = this.shell.findTargetedWidget(event);
121
+ if (widget instanceof EditorPreviewWidget && widget.isPreview) {
122
+ widget.convertToNonPreview();
123
+ }
124
+ }
125
+ }