mirror of
https://github.com/Microsoft/vscode
synced 2024-08-27 04:49:35 +00:00
Don't require a reload for Share setting (#186572)
This commit is contained in:
parent
c3fefb9c20
commit
e133f004ed
|
@ -32,6 +32,7 @@ import { IProgressService, ProgressLocation } from 'vs/platform/progress/common/
|
|||
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
|
||||
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
|
||||
import { workbenchConfigurationNodeBase } from 'vs/workbench/common/configuration';
|
||||
import { DisposableStore } from 'vs/base/common/lifecycle';
|
||||
|
||||
const targetMenus = [
|
||||
MenuId.EditorContextShare,
|
||||
|
@ -46,6 +47,8 @@ const targetMenus = [
|
|||
class ShareWorkbenchContribution {
|
||||
private static SHARE_ENABLED_SETTING = 'workbench.experimental.share.enabled';
|
||||
|
||||
private _disposables = new DisposableStore();
|
||||
|
||||
constructor(
|
||||
@IShareService private readonly shareService: IShareService,
|
||||
@IConfigurationService private readonly configurationService: IConfigurationService
|
||||
|
@ -53,75 +56,86 @@ class ShareWorkbenchContribution {
|
|||
if (this.configurationService.getValue<boolean>(ShareWorkbenchContribution.SHARE_ENABLED_SETTING)) {
|
||||
this.registerActions();
|
||||
}
|
||||
}
|
||||
|
||||
private registerActions() {
|
||||
registerAction2(class ShareAction extends Action2 {
|
||||
static readonly ID = 'workbench.action.share';
|
||||
static readonly LABEL = localize('share', 'Share...');
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
id: ShareAction.ID,
|
||||
title: { value: ShareAction.LABEL, original: 'Share...' },
|
||||
f1: true,
|
||||
icon: Codicon.linkExternal,
|
||||
precondition: ContextKeyExpr.and(ShareProviderCountContext.notEqualsTo(0), WorkspaceFolderCountContext.notEqualsTo(0)),
|
||||
keybinding: {
|
||||
weight: KeybindingWeight.WorkbenchContrib,
|
||||
primary: KeyMod.Alt | KeyMod.CtrlCmd | KeyCode.KeyS,
|
||||
},
|
||||
menu: [
|
||||
{ id: MenuId.CommandCenter, order: 1000 }
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
override async run(accessor: ServicesAccessor, ...args: any[]): Promise<void> {
|
||||
const shareService = accessor.get(IShareService);
|
||||
const activeEditor = accessor.get(IEditorService)?.activeEditor;
|
||||
const resourceUri = (activeEditor && EditorResourceAccessor.getOriginalUri(activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY }))
|
||||
?? accessor.get(IWorkspaceContextService).getWorkspace().folders[0].uri;
|
||||
const clipboardService = accessor.get(IClipboardService);
|
||||
const dialogService = accessor.get(IDialogService);
|
||||
const urlService = accessor.get(IOpenerService);
|
||||
const progressService = accessor.get(IProgressService);
|
||||
const selection = accessor.get(ICodeEditorService).getActiveCodeEditor()?.getSelection() ?? undefined;
|
||||
|
||||
const result = await progressService.withProgress({
|
||||
location: ProgressLocation.Window,
|
||||
detail: localize('generating link', 'Generating link...')
|
||||
}, async () => shareService.provideShare({ resourceUri, selection }, new CancellationTokenSource().token));
|
||||
|
||||
if (result) {
|
||||
const uriText = result.toString();
|
||||
const isResultText = typeof result === 'string';
|
||||
await clipboardService.writeText(uriText);
|
||||
|
||||
dialogService.prompt(
|
||||
{
|
||||
type: Severity.Info,
|
||||
message: isResultText ? localize('shareTextSuccess', 'Copied text to clipboard!') : localize('shareSuccess', 'Copied link to clipboard!'),
|
||||
custom: {
|
||||
icon: Codicon.check,
|
||||
markdownDetails: [{
|
||||
markdown: new MarkdownString(`<div aria-label='${uriText}'>${uriText}</div>`, { supportHtml: true }),
|
||||
classes: [isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link']
|
||||
}]
|
||||
},
|
||||
cancelButton: localize('close', 'Close'),
|
||||
buttons: isResultText ? [] : [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(result, { openExternal: true }); } }]
|
||||
}
|
||||
);
|
||||
this.configurationService.onDidChangeConfiguration(e => {
|
||||
if (e.affectsConfiguration(ShareWorkbenchContribution.SHARE_ENABLED_SETTING)) {
|
||||
if (this.configurationService.getValue<boolean>(ShareWorkbenchContribution.SHARE_ENABLED_SETTING)) {
|
||||
this.registerActions();
|
||||
} else {
|
||||
this._disposables.clear();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private registerActions() {
|
||||
this._disposables.add(
|
||||
registerAction2(class ShareAction extends Action2 {
|
||||
static readonly ID = 'workbench.action.share';
|
||||
static readonly LABEL = localize('share', 'Share...');
|
||||
|
||||
constructor() {
|
||||
super({
|
||||
id: ShareAction.ID,
|
||||
title: { value: ShareAction.LABEL, original: 'Share...' },
|
||||
f1: true,
|
||||
icon: Codicon.linkExternal,
|
||||
precondition: ContextKeyExpr.and(ShareProviderCountContext.notEqualsTo(0), WorkspaceFolderCountContext.notEqualsTo(0)),
|
||||
keybinding: {
|
||||
weight: KeybindingWeight.WorkbenchContrib,
|
||||
primary: KeyMod.Alt | KeyMod.CtrlCmd | KeyCode.KeyS,
|
||||
},
|
||||
menu: [
|
||||
{ id: MenuId.CommandCenter, order: 1000 }
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
override async run(accessor: ServicesAccessor, ...args: any[]): Promise<void> {
|
||||
const shareService = accessor.get(IShareService);
|
||||
const activeEditor = accessor.get(IEditorService)?.activeEditor;
|
||||
const resourceUri = (activeEditor && EditorResourceAccessor.getOriginalUri(activeEditor, { supportSideBySide: SideBySideEditor.PRIMARY }))
|
||||
?? accessor.get(IWorkspaceContextService).getWorkspace().folders[0].uri;
|
||||
const clipboardService = accessor.get(IClipboardService);
|
||||
const dialogService = accessor.get(IDialogService);
|
||||
const urlService = accessor.get(IOpenerService);
|
||||
const progressService = accessor.get(IProgressService);
|
||||
const selection = accessor.get(ICodeEditorService).getActiveCodeEditor()?.getSelection() ?? undefined;
|
||||
|
||||
const result = await progressService.withProgress({
|
||||
location: ProgressLocation.Window,
|
||||
detail: localize('generating link', 'Generating link...')
|
||||
}, async () => shareService.provideShare({ resourceUri, selection }, new CancellationTokenSource().token));
|
||||
|
||||
if (result) {
|
||||
const uriText = result.toString();
|
||||
const isResultText = typeof result === 'string';
|
||||
await clipboardService.writeText(uriText);
|
||||
|
||||
dialogService.prompt(
|
||||
{
|
||||
type: Severity.Info,
|
||||
message: isResultText ? localize('shareTextSuccess', 'Copied text to clipboard!') : localize('shareSuccess', 'Copied link to clipboard!'),
|
||||
custom: {
|
||||
icon: Codicon.check,
|
||||
markdownDetails: [{
|
||||
markdown: new MarkdownString(`<div aria-label='${uriText}'>${uriText}</div>`, { supportHtml: true }),
|
||||
classes: [isResultText ? 'share-dialog-input-text' : 'share-dialog-input-link']
|
||||
}]
|
||||
},
|
||||
cancelButton: localize('close', 'Close'),
|
||||
buttons: isResultText ? [] : [{ label: localize('open link', 'Open Link'), run: () => { urlService.open(result, { openExternal: true }); } }]
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
const actions = this.shareService.getShareActions();
|
||||
for (const menuId of targetMenus) {
|
||||
for (const action of actions) {
|
||||
// todo@joyceerhl avoid duplicates
|
||||
MenuRegistry.appendMenuItem(menuId, action);
|
||||
this._disposables.add(MenuRegistry.appendMenuItem(menuId, action));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue