feat: 添加vscode编辑器
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { $, addDisposableListener, EventType, isHTMLElement } from '../../../base/browser/dom.js';
|
||||
import { StandardMouseEvent } from '../../../base/browser/mouseEvent.js';
|
||||
import { Menu } from '../../../base/browser/ui/menu/menu.js';
|
||||
import { ActionRunner } from '../../../base/common/actions.js';
|
||||
import { isCancellationError } from '../../../base/common/errors.js';
|
||||
import { combinedDisposable, DisposableStore } from '../../../base/common/lifecycle.js';
|
||||
import { attachMenuStyler } from '../../theme/common/styler.js';
|
||||
export class ContextMenuHandler {
|
||||
constructor(contextViewService, telemetryService, notificationService, keybindingService, themeService) {
|
||||
this.contextViewService = contextViewService;
|
||||
this.telemetryService = telemetryService;
|
||||
this.notificationService = notificationService;
|
||||
this.keybindingService = keybindingService;
|
||||
this.themeService = themeService;
|
||||
this.focusToReturn = null;
|
||||
this.block = null;
|
||||
this.options = { blockMouse: true };
|
||||
}
|
||||
configure(options) {
|
||||
this.options = options;
|
||||
}
|
||||
showContextMenu(delegate) {
|
||||
const actions = delegate.getActions();
|
||||
if (!actions.length) {
|
||||
return; // Don't render an empty context menu
|
||||
}
|
||||
this.focusToReturn = document.activeElement;
|
||||
let menu;
|
||||
const shadowRootElement = isHTMLElement(delegate.domForShadowRoot) ? delegate.domForShadowRoot : undefined;
|
||||
this.contextViewService.showContextView({
|
||||
getAnchor: () => delegate.getAnchor(),
|
||||
canRelayout: false,
|
||||
anchorAlignment: delegate.anchorAlignment,
|
||||
anchorAxisAlignment: delegate.anchorAxisAlignment,
|
||||
render: (container) => {
|
||||
const className = delegate.getMenuClassName ? delegate.getMenuClassName() : '';
|
||||
if (className) {
|
||||
container.className += ' ' + className;
|
||||
}
|
||||
// Render invisible div to block mouse interaction in the rest of the UI
|
||||
if (this.options.blockMouse) {
|
||||
this.block = container.appendChild($('.context-view-block'));
|
||||
this.block.style.position = 'fixed';
|
||||
this.block.style.cursor = 'initial';
|
||||
this.block.style.left = '0';
|
||||
this.block.style.top = '0';
|
||||
this.block.style.width = '100%';
|
||||
this.block.style.height = '100%';
|
||||
this.block.style.zIndex = '-1';
|
||||
// TODO@Steven: this is never getting disposed
|
||||
addDisposableListener(this.block, EventType.MOUSE_DOWN, e => e.stopPropagation());
|
||||
}
|
||||
const menuDisposables = new DisposableStore();
|
||||
const actionRunner = delegate.actionRunner || new ActionRunner();
|
||||
actionRunner.onBeforeRun(this.onActionRun, this, menuDisposables);
|
||||
actionRunner.onDidRun(this.onDidActionRun, this, menuDisposables);
|
||||
menu = new Menu(container, actions, {
|
||||
actionViewItemProvider: delegate.getActionViewItem,
|
||||
context: delegate.getActionsContext ? delegate.getActionsContext() : null,
|
||||
actionRunner,
|
||||
getKeyBinding: delegate.getKeyBinding ? delegate.getKeyBinding : action => this.keybindingService.lookupKeybinding(action.id)
|
||||
});
|
||||
menuDisposables.add(attachMenuStyler(menu, this.themeService));
|
||||
menu.onDidCancel(() => this.contextViewService.hideContextView(true), null, menuDisposables);
|
||||
menu.onDidBlur(() => this.contextViewService.hideContextView(true), null, menuDisposables);
|
||||
menuDisposables.add(addDisposableListener(window, EventType.BLUR, () => this.contextViewService.hideContextView(true)));
|
||||
menuDisposables.add(addDisposableListener(window, EventType.MOUSE_DOWN, (e) => {
|
||||
if (e.defaultPrevented) {
|
||||
return;
|
||||
}
|
||||
const event = new StandardMouseEvent(e);
|
||||
let element = event.target;
|
||||
// Don't do anything as we are likely creating a context menu
|
||||
if (event.rightButton) {
|
||||
return;
|
||||
}
|
||||
while (element) {
|
||||
if (element === container) {
|
||||
return;
|
||||
}
|
||||
element = element.parentElement;
|
||||
}
|
||||
this.contextViewService.hideContextView(true);
|
||||
}));
|
||||
return combinedDisposable(menuDisposables, menu);
|
||||
},
|
||||
focus: () => {
|
||||
menu === null || menu === void 0 ? void 0 : menu.focus(!!delegate.autoSelectFirstItem);
|
||||
},
|
||||
onHide: (didCancel) => {
|
||||
var _a;
|
||||
(_a = delegate.onHide) === null || _a === void 0 ? void 0 : _a.call(delegate, !!didCancel);
|
||||
if (this.block) {
|
||||
this.block.remove();
|
||||
this.block = null;
|
||||
}
|
||||
if (this.focusToReturn) {
|
||||
this.focusToReturn.focus();
|
||||
}
|
||||
}
|
||||
}, shadowRootElement, !!shadowRootElement);
|
||||
}
|
||||
onActionRun(e) {
|
||||
this.telemetryService.publicLog2('workbenchActionExecuted', { id: e.action.id, from: 'contextMenu' });
|
||||
this.contextViewService.hideContextView(false);
|
||||
// Restore focus here
|
||||
if (this.focusToReturn) {
|
||||
this.focusToReturn.focus();
|
||||
}
|
||||
}
|
||||
onDidActionRun(e) {
|
||||
if (e.error && !isCancellationError(e.error)) {
|
||||
this.notificationService.error(e.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
import { ModifierKeyEmitter } from '../../../base/browser/dom.js';
|
||||
import { Emitter } from '../../../base/common/event.js';
|
||||
import { Disposable } from '../../../base/common/lifecycle.js';
|
||||
import { IKeybindingService } from '../../keybinding/common/keybinding.js';
|
||||
import { INotificationService } from '../../notification/common/notification.js';
|
||||
import { ITelemetryService } from '../../telemetry/common/telemetry.js';
|
||||
import { IThemeService } from '../../theme/common/themeService.js';
|
||||
import { ContextMenuHandler } from './contextMenuHandler.js';
|
||||
import { IContextViewService } from './contextView.js';
|
||||
let ContextMenuService = class ContextMenuService extends Disposable {
|
||||
constructor(telemetryService, notificationService, contextViewService, keybindingService, themeService) {
|
||||
super();
|
||||
this._onDidShowContextMenu = new Emitter();
|
||||
this._onDidHideContextMenu = new Emitter();
|
||||
this.contextMenuHandler = new ContextMenuHandler(contextViewService, telemetryService, notificationService, keybindingService, themeService);
|
||||
}
|
||||
configure(options) {
|
||||
this.contextMenuHandler.configure(options);
|
||||
}
|
||||
// ContextMenu
|
||||
showContextMenu(delegate) {
|
||||
this.contextMenuHandler.showContextMenu(Object.assign(Object.assign({}, delegate), { onHide: (didCancel) => {
|
||||
var _a;
|
||||
(_a = delegate.onHide) === null || _a === void 0 ? void 0 : _a.call(delegate, didCancel);
|
||||
this._onDidHideContextMenu.fire();
|
||||
} }));
|
||||
ModifierKeyEmitter.getInstance().resetKeyStatus();
|
||||
this._onDidShowContextMenu.fire();
|
||||
}
|
||||
};
|
||||
ContextMenuService = __decorate([
|
||||
__param(0, ITelemetryService),
|
||||
__param(1, INotificationService),
|
||||
__param(2, IContextViewService),
|
||||
__param(3, IKeybindingService),
|
||||
__param(4, IThemeService)
|
||||
], ContextMenuService);
|
||||
export { ContextMenuService };
|
||||
@@ -0,0 +1,7 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
import { createDecorator } from '../../instantiation/common/instantiation.js';
|
||||
export const IContextViewService = createDecorator('contextViewService');
|
||||
export const IContextMenuService = createDecorator('contextMenuService');
|
||||
@@ -0,0 +1,67 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
||||
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
||||
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
||||
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
||||
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
||||
};
|
||||
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
import { ContextView } from '../../../base/browser/ui/contextview/contextview.js';
|
||||
import { Disposable, toDisposable } from '../../../base/common/lifecycle.js';
|
||||
import { ILayoutService } from '../../layout/browser/layoutService.js';
|
||||
let ContextViewService = class ContextViewService extends Disposable {
|
||||
constructor(layoutService) {
|
||||
super();
|
||||
this.layoutService = layoutService;
|
||||
this.currentViewDisposable = Disposable.None;
|
||||
this.container = layoutService.hasContainer ? layoutService.container : null;
|
||||
this.contextView = this._register(new ContextView(this.container, 1 /* ContextViewDOMPosition.ABSOLUTE */));
|
||||
this.layout();
|
||||
this._register(layoutService.onDidLayout(() => this.layout()));
|
||||
}
|
||||
// ContextView
|
||||
setContainer(container, domPosition) {
|
||||
this.contextView.setContainer(container, domPosition || 1 /* ContextViewDOMPosition.ABSOLUTE */);
|
||||
}
|
||||
showContextView(delegate, container, shadowRoot) {
|
||||
if (container) {
|
||||
if (container !== this.container || this.shadowRoot !== shadowRoot) {
|
||||
this.container = container;
|
||||
this.setContainer(container, shadowRoot ? 3 /* ContextViewDOMPosition.FIXED_SHADOW */ : 2 /* ContextViewDOMPosition.FIXED */);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (this.layoutService.hasContainer && this.container !== this.layoutService.container) {
|
||||
this.container = this.layoutService.container;
|
||||
this.setContainer(this.container, 1 /* ContextViewDOMPosition.ABSOLUTE */);
|
||||
}
|
||||
}
|
||||
this.shadowRoot = shadowRoot;
|
||||
this.contextView.show(delegate);
|
||||
const disposable = toDisposable(() => {
|
||||
if (this.currentViewDisposable === disposable) {
|
||||
this.hideContextView();
|
||||
}
|
||||
});
|
||||
this.currentViewDisposable = disposable;
|
||||
return disposable;
|
||||
}
|
||||
getContextViewElement() {
|
||||
return this.contextView.getViewElement();
|
||||
}
|
||||
layout() {
|
||||
this.contextView.layout();
|
||||
}
|
||||
hideContextView(data) {
|
||||
this.contextView.hide(data);
|
||||
}
|
||||
};
|
||||
ContextViewService = __decorate([
|
||||
__param(0, ILayoutService)
|
||||
], ContextViewService);
|
||||
export { ContextViewService };
|
||||
Reference in New Issue
Block a user