clipboard.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  6. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  7. return new (P || (P = Promise))(function (resolve, reject) {
  8. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  9. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  10. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  11. step((generator = generator.apply(thisArg, _arguments || [])).next());
  12. });
  13. };
  14. import * as browser from '../../../base/browser/browser.js';
  15. import * as platform from '../../../base/common/platform.js';
  16. import { CopyOptions, InMemoryClipboardMetadataManager } from '../../browser/controller/textAreaInput.js';
  17. import { EditorAction, MultiCommand, registerEditorAction } from '../../browser/editorExtensions.js';
  18. import { ICodeEditorService } from '../../browser/services/codeEditorService.js';
  19. import { EditorContextKeys } from '../../common/editorContextKeys.js';
  20. import * as nls from '../../../nls.js';
  21. import { MenuId, MenuRegistry } from '../../../platform/actions/common/actions.js';
  22. import { IClipboardService } from '../../../platform/clipboard/common/clipboardService.js';
  23. const CLIPBOARD_CONTEXT_MENU_GROUP = '9_cutcopypaste';
  24. const supportsCut = (platform.isNative || document.queryCommandSupported('cut'));
  25. const supportsCopy = (platform.isNative || document.queryCommandSupported('copy'));
  26. // Firefox only supports navigator.clipboard.readText() in browser extensions.
  27. // See https://developer.mozilla.org/en-US/docs/Web/API/Clipboard/readText#Browser_compatibility
  28. // When loading over http, navigator.clipboard can be undefined. See https://github.com/microsoft/monaco-editor/issues/2313
  29. const supportsPaste = (typeof navigator.clipboard === 'undefined' || browser.isFirefox) ? document.queryCommandSupported('paste') : true;
  30. function registerCommand(command) {
  31. command.register();
  32. return command;
  33. }
  34. export const CutAction = supportsCut ? registerCommand(new MultiCommand({
  35. id: 'editor.action.clipboardCutAction',
  36. precondition: undefined,
  37. kbOpts: (
  38. // Do not bind cut keybindings in the browser,
  39. // since browsers do that for us and it avoids security prompts
  40. platform.isNative ? {
  41. primary: 2048 /* CtrlCmd */ | 54 /* KeyX */,
  42. win: { primary: 2048 /* CtrlCmd */ | 54 /* KeyX */, secondary: [1024 /* Shift */ | 20 /* Delete */] },
  43. weight: 100 /* EditorContrib */
  44. } : undefined),
  45. menuOpts: [{
  46. menuId: MenuId.MenubarEditMenu,
  47. group: '2_ccp',
  48. title: nls.localize({ key: 'miCut', comment: ['&& denotes a mnemonic'] }, "Cu&&t"),
  49. order: 1
  50. }, {
  51. menuId: MenuId.EditorContext,
  52. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  53. title: nls.localize('actions.clipboard.cutLabel', "Cut"),
  54. when: EditorContextKeys.writable,
  55. order: 1,
  56. }, {
  57. menuId: MenuId.CommandPalette,
  58. group: '',
  59. title: nls.localize('actions.clipboard.cutLabel', "Cut"),
  60. order: 1
  61. }, {
  62. menuId: MenuId.SimpleEditorContext,
  63. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  64. title: nls.localize('actions.clipboard.cutLabel', "Cut"),
  65. when: EditorContextKeys.writable,
  66. order: 1,
  67. }]
  68. })) : undefined;
  69. export const CopyAction = supportsCopy ? registerCommand(new MultiCommand({
  70. id: 'editor.action.clipboardCopyAction',
  71. precondition: undefined,
  72. kbOpts: (
  73. // Do not bind copy keybindings in the browser,
  74. // since browsers do that for us and it avoids security prompts
  75. platform.isNative ? {
  76. primary: 2048 /* CtrlCmd */ | 33 /* KeyC */,
  77. win: { primary: 2048 /* CtrlCmd */ | 33 /* KeyC */, secondary: [2048 /* CtrlCmd */ | 19 /* Insert */] },
  78. weight: 100 /* EditorContrib */
  79. } : undefined),
  80. menuOpts: [{
  81. menuId: MenuId.MenubarEditMenu,
  82. group: '2_ccp',
  83. title: nls.localize({ key: 'miCopy', comment: ['&& denotes a mnemonic'] }, "&&Copy"),
  84. order: 2
  85. }, {
  86. menuId: MenuId.EditorContext,
  87. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  88. title: nls.localize('actions.clipboard.copyLabel', "Copy"),
  89. order: 2,
  90. }, {
  91. menuId: MenuId.CommandPalette,
  92. group: '',
  93. title: nls.localize('actions.clipboard.copyLabel', "Copy"),
  94. order: 1
  95. }, {
  96. menuId: MenuId.SimpleEditorContext,
  97. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  98. title: nls.localize('actions.clipboard.copyLabel', "Copy"),
  99. order: 2,
  100. }]
  101. })) : undefined;
  102. MenuRegistry.appendMenuItem(MenuId.MenubarEditMenu, { submenu: MenuId.MenubarCopy, title: { value: nls.localize('copy as', "Copy As"), original: 'Copy As', }, group: '2_ccp', order: 3 });
  103. MenuRegistry.appendMenuItem(MenuId.EditorContext, { submenu: MenuId.EditorContextCopy, title: { value: nls.localize('copy as', "Copy As"), original: 'Copy As', }, group: CLIPBOARD_CONTEXT_MENU_GROUP, order: 3 });
  104. export const PasteAction = supportsPaste ? registerCommand(new MultiCommand({
  105. id: 'editor.action.clipboardPasteAction',
  106. precondition: undefined,
  107. kbOpts: (
  108. // Do not bind paste keybindings in the browser,
  109. // since browsers do that for us and it avoids security prompts
  110. platform.isNative ? {
  111. primary: 2048 /* CtrlCmd */ | 52 /* KeyV */,
  112. win: { primary: 2048 /* CtrlCmd */ | 52 /* KeyV */, secondary: [1024 /* Shift */ | 19 /* Insert */] },
  113. linux: { primary: 2048 /* CtrlCmd */ | 52 /* KeyV */, secondary: [1024 /* Shift */ | 19 /* Insert */] },
  114. weight: 100 /* EditorContrib */
  115. } : undefined),
  116. menuOpts: [{
  117. menuId: MenuId.MenubarEditMenu,
  118. group: '2_ccp',
  119. title: nls.localize({ key: 'miPaste', comment: ['&& denotes a mnemonic'] }, "&&Paste"),
  120. order: 4
  121. }, {
  122. menuId: MenuId.EditorContext,
  123. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  124. title: nls.localize('actions.clipboard.pasteLabel', "Paste"),
  125. when: EditorContextKeys.writable,
  126. order: 4,
  127. }, {
  128. menuId: MenuId.CommandPalette,
  129. group: '',
  130. title: nls.localize('actions.clipboard.pasteLabel', "Paste"),
  131. order: 1
  132. }, {
  133. menuId: MenuId.SimpleEditorContext,
  134. group: CLIPBOARD_CONTEXT_MENU_GROUP,
  135. title: nls.localize('actions.clipboard.pasteLabel', "Paste"),
  136. when: EditorContextKeys.writable,
  137. order: 4,
  138. }]
  139. })) : undefined;
  140. class ExecCommandCopyWithSyntaxHighlightingAction extends EditorAction {
  141. constructor() {
  142. super({
  143. id: 'editor.action.clipboardCopyWithSyntaxHighlightingAction',
  144. label: nls.localize('actions.clipboard.copyWithSyntaxHighlightingLabel', "Copy With Syntax Highlighting"),
  145. alias: 'Copy With Syntax Highlighting',
  146. precondition: undefined,
  147. kbOpts: {
  148. kbExpr: EditorContextKeys.textInputFocus,
  149. primary: 0,
  150. weight: 100 /* EditorContrib */
  151. }
  152. });
  153. }
  154. run(accessor, editor) {
  155. if (!editor.hasModel()) {
  156. return;
  157. }
  158. const emptySelectionClipboard = editor.getOption(32 /* emptySelectionClipboard */);
  159. if (!emptySelectionClipboard && editor.getSelection().isEmpty()) {
  160. return;
  161. }
  162. CopyOptions.forceCopyWithSyntaxHighlighting = true;
  163. editor.focus();
  164. document.execCommand('copy');
  165. CopyOptions.forceCopyWithSyntaxHighlighting = false;
  166. }
  167. }
  168. function registerExecCommandImpl(target, browserCommand) {
  169. if (!target) {
  170. return;
  171. }
  172. // 1. handle case when focus is in editor.
  173. target.addImplementation(10000, 'code-editor', (accessor, args) => {
  174. // Only if editor text focus (i.e. not if editor has widget focus).
  175. const focusedEditor = accessor.get(ICodeEditorService).getFocusedCodeEditor();
  176. if (focusedEditor && focusedEditor.hasTextFocus()) {
  177. // Do not execute if there is no selection and empty selection clipboard is off
  178. const emptySelectionClipboard = focusedEditor.getOption(32 /* emptySelectionClipboard */);
  179. const selection = focusedEditor.getSelection();
  180. if (selection && selection.isEmpty() && !emptySelectionClipboard) {
  181. return true;
  182. }
  183. document.execCommand(browserCommand);
  184. return true;
  185. }
  186. return false;
  187. });
  188. // 2. (default) handle case when focus is somewhere else.
  189. target.addImplementation(0, 'generic-dom', (accessor, args) => {
  190. document.execCommand(browserCommand);
  191. return true;
  192. });
  193. }
  194. registerExecCommandImpl(CutAction, 'cut');
  195. registerExecCommandImpl(CopyAction, 'copy');
  196. if (PasteAction) {
  197. // 1. Paste: handle case when focus is in editor.
  198. PasteAction.addImplementation(10000, 'code-editor', (accessor, args) => {
  199. const codeEditorService = accessor.get(ICodeEditorService);
  200. const clipboardService = accessor.get(IClipboardService);
  201. // Only if editor text focus (i.e. not if editor has widget focus).
  202. const focusedEditor = codeEditorService.getFocusedCodeEditor();
  203. if (focusedEditor && focusedEditor.hasTextFocus()) {
  204. const result = document.execCommand('paste');
  205. // Use the clipboard service if document.execCommand('paste') was not successful
  206. if (!result && platform.isWeb) {
  207. return (() => __awaiter(void 0, void 0, void 0, function* () {
  208. const clipboardText = yield clipboardService.readText();
  209. if (clipboardText !== '') {
  210. const metadata = InMemoryClipboardMetadataManager.INSTANCE.get(clipboardText);
  211. let pasteOnNewLine = false;
  212. let multicursorText = null;
  213. let mode = null;
  214. if (metadata) {
  215. pasteOnNewLine = (focusedEditor.getOption(32 /* emptySelectionClipboard */) && !!metadata.isFromEmptySelection);
  216. multicursorText = (typeof metadata.multicursorText !== 'undefined' ? metadata.multicursorText : null);
  217. mode = metadata.mode;
  218. }
  219. focusedEditor.trigger('keyboard', "paste" /* Paste */, {
  220. text: clipboardText,
  221. pasteOnNewLine,
  222. multicursorText,
  223. mode
  224. });
  225. }
  226. }))();
  227. }
  228. return true;
  229. }
  230. return false;
  231. });
  232. // 2. Paste: (default) handle case when focus is somewhere else.
  233. PasteAction.addImplementation(0, 'generic-dom', (accessor, args) => {
  234. document.execCommand('paste');
  235. return true;
  236. });
  237. }
  238. if (supportsCopy) {
  239. registerEditorAction(ExecCommandCopyWithSyntaxHighlightingAction);
  240. }