21fb4538e1fe6b3242fa53198ba53401c34c512e10897010654ded0eb620c5914fcc34c258d3bf89683e8759fba36ef586ddd8a6a2c2ae0b1134c30dbae58c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  6. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  7. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  8. 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;
  9. return c > 3 && r && Object.defineProperty(target, key, r), r;
  10. };
  11. var __param = (this && this.__param) || function (paramIndex, decorator) {
  12. return function (target, key) { decorator(target, key, paramIndex); }
  13. };
  14. import * as aria from '../../../base/browser/ui/aria/aria.js';
  15. import { Disposable, toDisposable, DisposableStore } from '../../../base/common/lifecycle.js';
  16. import { ICodeEditorService } from '../../browser/services/codeEditorService.js';
  17. import { CodeEditorWidget } from '../../browser/widget/codeEditorWidget.js';
  18. import { DiffEditorWidget } from '../../browser/widget/diffEditorWidget.js';
  19. import { InternalEditorAction } from '../../common/editorAction.js';
  20. import { IEditorWorkerService } from '../../common/services/editorWorker.js';
  21. import { StandaloneKeybindingService, updateConfigurationService } from './standaloneServices.js';
  22. import { IStandaloneThemeService } from '../common/standaloneTheme.js';
  23. import { MenuId, MenuRegistry } from '../../../platform/actions/common/actions.js';
  24. import { CommandsRegistry, ICommandService } from '../../../platform/commands/common/commands.js';
  25. import { IConfigurationService } from '../../../platform/configuration/common/configuration.js';
  26. import { ContextKeyExpr, IContextKeyService } from '../../../platform/contextkey/common/contextkey.js';
  27. import { IContextMenuService } from '../../../platform/contextview/browser/contextView.js';
  28. import { IInstantiationService } from '../../../platform/instantiation/common/instantiation.js';
  29. import { IKeybindingService } from '../../../platform/keybinding/common/keybinding.js';
  30. import { INotificationService } from '../../../platform/notification/common/notification.js';
  31. import { IThemeService } from '../../../platform/theme/common/themeService.js';
  32. import { IAccessibilityService } from '../../../platform/accessibility/common/accessibility.js';
  33. import { StandaloneCodeEditorNLS } from '../../common/standaloneStrings.js';
  34. import { IClipboardService } from '../../../platform/clipboard/common/clipboardService.js';
  35. import { IEditorProgressService } from '../../../platform/progress/common/progress.js';
  36. import { IModelService } from '../../common/services/model.js';
  37. import { ILanguageService } from '../../common/languages/language.js';
  38. import { StandaloneCodeEditorService } from './standaloneCodeEditorService.js';
  39. import { PLAINTEXT_LANGUAGE_ID } from '../../common/languages/modesRegistry.js';
  40. import { ILanguageConfigurationService } from '../../common/languages/languageConfigurationRegistry.js';
  41. import { ILanguageFeaturesService } from '../../common/services/languageFeatures.js';
  42. let LAST_GENERATED_COMMAND_ID = 0;
  43. let ariaDomNodeCreated = false;
  44. /**
  45. * Create ARIA dom node inside parent,
  46. * or only for the first editor instantiation inside document.body.
  47. * @param parent container element for ARIA dom node
  48. */
  49. function createAriaDomNode(parent) {
  50. if (!parent) {
  51. if (ariaDomNodeCreated) {
  52. return;
  53. }
  54. ariaDomNodeCreated = true;
  55. }
  56. aria.setARIAContainer(parent || document.body);
  57. }
  58. /**
  59. * A code editor to be used both by the standalone editor and the standalone diff editor.
  60. */
  61. let StandaloneCodeEditor = class StandaloneCodeEditor extends CodeEditorWidget {
  62. constructor(domElement, _options, instantiationService, codeEditorService, commandService, contextKeyService, keybindingService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService) {
  63. const options = Object.assign({}, _options);
  64. options.ariaLabel = options.ariaLabel || StandaloneCodeEditorNLS.editorViewAccessibleLabel;
  65. options.ariaLabel = options.ariaLabel + ';' + (StandaloneCodeEditorNLS.accessibilityHelpMessage);
  66. super(domElement, options, {}, instantiationService, codeEditorService, commandService, contextKeyService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
  67. if (keybindingService instanceof StandaloneKeybindingService) {
  68. this._standaloneKeybindingService = keybindingService;
  69. }
  70. else {
  71. this._standaloneKeybindingService = null;
  72. }
  73. createAriaDomNode(options.ariaContainerElement);
  74. }
  75. addCommand(keybinding, handler, context) {
  76. if (!this._standaloneKeybindingService) {
  77. console.warn('Cannot add command because the editor is configured with an unrecognized KeybindingService');
  78. return null;
  79. }
  80. const commandId = 'DYNAMIC_' + (++LAST_GENERATED_COMMAND_ID);
  81. const whenExpression = ContextKeyExpr.deserialize(context);
  82. this._standaloneKeybindingService.addDynamicKeybinding(commandId, keybinding, handler, whenExpression);
  83. return commandId;
  84. }
  85. createContextKey(key, defaultValue) {
  86. return this._contextKeyService.createKey(key, defaultValue);
  87. }
  88. addAction(_descriptor) {
  89. if ((typeof _descriptor.id !== 'string') || (typeof _descriptor.label !== 'string') || (typeof _descriptor.run !== 'function')) {
  90. throw new Error('Invalid action descriptor, `id`, `label` and `run` are required properties!');
  91. }
  92. if (!this._standaloneKeybindingService) {
  93. console.warn('Cannot add keybinding because the editor is configured with an unrecognized KeybindingService');
  94. return Disposable.None;
  95. }
  96. // Read descriptor options
  97. const id = _descriptor.id;
  98. const label = _descriptor.label;
  99. const precondition = ContextKeyExpr.and(ContextKeyExpr.equals('editorId', this.getId()), ContextKeyExpr.deserialize(_descriptor.precondition));
  100. const keybindings = _descriptor.keybindings;
  101. const keybindingsWhen = ContextKeyExpr.and(precondition, ContextKeyExpr.deserialize(_descriptor.keybindingContext));
  102. const contextMenuGroupId = _descriptor.contextMenuGroupId || null;
  103. const contextMenuOrder = _descriptor.contextMenuOrder || 0;
  104. const run = (accessor, ...args) => {
  105. return Promise.resolve(_descriptor.run(this, ...args));
  106. };
  107. const toDispose = new DisposableStore();
  108. // Generate a unique id to allow the same descriptor.id across multiple editor instances
  109. const uniqueId = this.getId() + ':' + id;
  110. // Register the command
  111. toDispose.add(CommandsRegistry.registerCommand(uniqueId, run));
  112. // Register the context menu item
  113. if (contextMenuGroupId) {
  114. const menuItem = {
  115. command: {
  116. id: uniqueId,
  117. title: label
  118. },
  119. when: precondition,
  120. group: contextMenuGroupId,
  121. order: contextMenuOrder
  122. };
  123. toDispose.add(MenuRegistry.appendMenuItem(MenuId.EditorContext, menuItem));
  124. }
  125. // Register the keybindings
  126. if (Array.isArray(keybindings)) {
  127. for (const kb of keybindings) {
  128. toDispose.add(this._standaloneKeybindingService.addDynamicKeybinding(uniqueId, kb, run, keybindingsWhen));
  129. }
  130. }
  131. // Finally, register an internal editor action
  132. const internalAction = new InternalEditorAction(uniqueId, label, label, precondition, run, this._contextKeyService);
  133. // Store it under the original id, such that trigger with the original id will work
  134. this._actions[id] = internalAction;
  135. toDispose.add(toDisposable(() => {
  136. delete this._actions[id];
  137. }));
  138. return toDispose;
  139. }
  140. _triggerCommand(handlerId, payload) {
  141. if (this._codeEditorService instanceof StandaloneCodeEditorService) {
  142. // Help commands find this editor as the active editor
  143. try {
  144. this._codeEditorService.setActiveCodeEditor(this);
  145. super._triggerCommand(handlerId, payload);
  146. }
  147. finally {
  148. this._codeEditorService.setActiveCodeEditor(null);
  149. }
  150. }
  151. else {
  152. super._triggerCommand(handlerId, payload);
  153. }
  154. }
  155. };
  156. StandaloneCodeEditor = __decorate([
  157. __param(2, IInstantiationService),
  158. __param(3, ICodeEditorService),
  159. __param(4, ICommandService),
  160. __param(5, IContextKeyService),
  161. __param(6, IKeybindingService),
  162. __param(7, IThemeService),
  163. __param(8, INotificationService),
  164. __param(9, IAccessibilityService),
  165. __param(10, ILanguageConfigurationService),
  166. __param(11, ILanguageFeaturesService)
  167. ], StandaloneCodeEditor);
  168. export { StandaloneCodeEditor };
  169. let StandaloneEditor = class StandaloneEditor extends StandaloneCodeEditor {
  170. constructor(domElement, _options, instantiationService, codeEditorService, commandService, contextKeyService, keybindingService, themeService, notificationService, configurationService, accessibilityService, modelService, languageService, languageConfigurationService, languageFeaturesService) {
  171. const options = Object.assign({}, _options);
  172. updateConfigurationService(configurationService, options, false);
  173. const themeDomRegistration = themeService.registerEditorContainer(domElement);
  174. if (typeof options.theme === 'string') {
  175. themeService.setTheme(options.theme);
  176. }
  177. if (typeof options.autoDetectHighContrast !== 'undefined') {
  178. themeService.setAutoDetectHighContrast(Boolean(options.autoDetectHighContrast));
  179. }
  180. const _model = options.model;
  181. delete options.model;
  182. super(domElement, options, instantiationService, codeEditorService, commandService, contextKeyService, keybindingService, themeService, notificationService, accessibilityService, languageConfigurationService, languageFeaturesService);
  183. this._configurationService = configurationService;
  184. this._standaloneThemeService = themeService;
  185. this._register(themeDomRegistration);
  186. let model;
  187. if (typeof _model === 'undefined') {
  188. const languageId = languageService.getLanguageIdByMimeType(options.language) || options.language || PLAINTEXT_LANGUAGE_ID;
  189. model = createTextModel(modelService, languageService, options.value || '', languageId, undefined);
  190. this._ownsModel = true;
  191. }
  192. else {
  193. model = _model;
  194. this._ownsModel = false;
  195. }
  196. this._attachModel(model);
  197. if (model) {
  198. const e = {
  199. oldModelUrl: null,
  200. newModelUrl: model.uri
  201. };
  202. this._onDidChangeModel.fire(e);
  203. }
  204. }
  205. dispose() {
  206. super.dispose();
  207. }
  208. updateOptions(newOptions) {
  209. updateConfigurationService(this._configurationService, newOptions, false);
  210. if (typeof newOptions.theme === 'string') {
  211. this._standaloneThemeService.setTheme(newOptions.theme);
  212. }
  213. if (typeof newOptions.autoDetectHighContrast !== 'undefined') {
  214. this._standaloneThemeService.setAutoDetectHighContrast(Boolean(newOptions.autoDetectHighContrast));
  215. }
  216. super.updateOptions(newOptions);
  217. }
  218. _postDetachModelCleanup(detachedModel) {
  219. super._postDetachModelCleanup(detachedModel);
  220. if (detachedModel && this._ownsModel) {
  221. detachedModel.dispose();
  222. this._ownsModel = false;
  223. }
  224. }
  225. };
  226. StandaloneEditor = __decorate([
  227. __param(2, IInstantiationService),
  228. __param(3, ICodeEditorService),
  229. __param(4, ICommandService),
  230. __param(5, IContextKeyService),
  231. __param(6, IKeybindingService),
  232. __param(7, IStandaloneThemeService),
  233. __param(8, INotificationService),
  234. __param(9, IConfigurationService),
  235. __param(10, IAccessibilityService),
  236. __param(11, IModelService),
  237. __param(12, ILanguageService),
  238. __param(13, ILanguageConfigurationService),
  239. __param(14, ILanguageFeaturesService)
  240. ], StandaloneEditor);
  241. export { StandaloneEditor };
  242. let StandaloneDiffEditor = class StandaloneDiffEditor extends DiffEditorWidget {
  243. constructor(domElement, _options, instantiationService, contextKeyService, editorWorkerService, codeEditorService, themeService, notificationService, configurationService, contextMenuService, editorProgressService, clipboardService) {
  244. const options = Object.assign({}, _options);
  245. updateConfigurationService(configurationService, options, true);
  246. const themeDomRegistration = themeService.registerEditorContainer(domElement);
  247. if (typeof options.theme === 'string') {
  248. themeService.setTheme(options.theme);
  249. }
  250. if (typeof options.autoDetectHighContrast !== 'undefined') {
  251. themeService.setAutoDetectHighContrast(Boolean(options.autoDetectHighContrast));
  252. }
  253. super(domElement, options, {}, clipboardService, editorWorkerService, contextKeyService, instantiationService, codeEditorService, themeService, notificationService, contextMenuService, editorProgressService);
  254. this._configurationService = configurationService;
  255. this._standaloneThemeService = themeService;
  256. this._register(themeDomRegistration);
  257. }
  258. dispose() {
  259. super.dispose();
  260. }
  261. updateOptions(newOptions) {
  262. updateConfigurationService(this._configurationService, newOptions, true);
  263. if (typeof newOptions.theme === 'string') {
  264. this._standaloneThemeService.setTheme(newOptions.theme);
  265. }
  266. if (typeof newOptions.autoDetectHighContrast !== 'undefined') {
  267. this._standaloneThemeService.setAutoDetectHighContrast(Boolean(newOptions.autoDetectHighContrast));
  268. }
  269. super.updateOptions(newOptions);
  270. }
  271. _createInnerEditor(instantiationService, container, options) {
  272. return instantiationService.createInstance(StandaloneCodeEditor, container, options);
  273. }
  274. getOriginalEditor() {
  275. return super.getOriginalEditor();
  276. }
  277. getModifiedEditor() {
  278. return super.getModifiedEditor();
  279. }
  280. addCommand(keybinding, handler, context) {
  281. return this.getModifiedEditor().addCommand(keybinding, handler, context);
  282. }
  283. createContextKey(key, defaultValue) {
  284. return this.getModifiedEditor().createContextKey(key, defaultValue);
  285. }
  286. addAction(descriptor) {
  287. return this.getModifiedEditor().addAction(descriptor);
  288. }
  289. };
  290. StandaloneDiffEditor = __decorate([
  291. __param(2, IInstantiationService),
  292. __param(3, IContextKeyService),
  293. __param(4, IEditorWorkerService),
  294. __param(5, ICodeEditorService),
  295. __param(6, IStandaloneThemeService),
  296. __param(7, INotificationService),
  297. __param(8, IConfigurationService),
  298. __param(9, IContextMenuService),
  299. __param(10, IEditorProgressService),
  300. __param(11, IClipboardService)
  301. ], StandaloneDiffEditor);
  302. export { StandaloneDiffEditor };
  303. /**
  304. * @internal
  305. */
  306. export function createTextModel(modelService, languageService, value, languageId, uri) {
  307. value = value || '';
  308. if (!languageId) {
  309. const firstLF = value.indexOf('\n');
  310. let firstLine = value;
  311. if (firstLF !== -1) {
  312. firstLine = value.substring(0, firstLF);
  313. }
  314. return doCreateModel(modelService, value, languageService.createByFilepathOrFirstLine(uri || null, firstLine), uri);
  315. }
  316. return doCreateModel(modelService, value, languageService.createById(languageId), uri);
  317. }
  318. /**
  319. * @internal
  320. */
  321. function doCreateModel(modelService, value, languageSelection, uri) {
  322. return modelService.createModel(value, languageSelection, uri);
  323. }