Monaco Editor教程(十八):使用api来完成某些键盘操作,格式化,查找,显示右侧菜单等。
创始人
2024-02-10 19:42:10
0

背景

在一般的Web IDE中,我们需要将经常用到的一些操作放到顶部操作栏里,类似语雀的文档编辑。 代码编辑器,一般也会放一些查找,格式化,撤销,恢复。有些人喜欢用快捷键来进行这些操作,但由于monaco中内置的键盘快捷操作非常地多,所以有些人喜欢用按钮来实现某种操作。本篇文章就来带大家完成使用代码来触发某些action,完成点击一个按钮进行格式化,查找,显示右键菜单的操作。

核心方法

调用api来触发某个action或command,有二种方式,分别是

第一种:

直接使用editor.trigger(source: string, handlerId: string, payload: any): void 来触发某个内置或已经自定义的操作。只要知道handerId就可以完成。

第二种:

先使用editor.getAction(id: string): IEditorAction方法获取action实例,然后调用实例的run(): Promise方法。返回一个Promise。

先说第一种,直接使用trigger方法触发某个操作。传入一个handlerId,也可以添加一些额外的数据。常用的handlerId有,

  • editor.action.showContextMenu 显示右键菜单
  • actions.find 查找操作
  • editor.action.formatDocument 格式化文档

具体使用方法

// 执行格式化操作
editor.trigger('你自定义一个字符串', `editor.action.formatDocument`)// 执行查找操作,会显示查找框
editor.trigger('你自定义一个字符串', `action.find`)

具体效果如下图:
在这里插入图片描述

第二种方案
传入一个 handlerId,就可以运行,

function getActionToTrigger(id){editor.getAction(id).run().then(() => {console.log('运行成功')})
}// 触发查找操作
getActionToTrigger('action.find')

默认的actions

上面提到了要触发某个操作,必须要知道该操作的handlerId,那么具体有哪些HandlerId供使用哪?
对于这个问题我也查了很多资料。也搜索了官方的文档和代码。都没找到。最后我在控制台打印出editor的原型对象。最后发现 editor下有一个_actions属性。这里存放了所有可用的handlerId。
具体请看。
在这里插入图片描述
根据资料可以得知,目前有154个action

(index)idlabelalias
0‘editor.action.setSelectionAnchor’‘Set Selection Anchor’‘Set Selection Anchor’
1‘editor.action.goToSelectionAnchor’‘Go to Selection Anchor’‘Go to Selection Anchor’
2‘editor.action.selectFromAnchorToCursor’‘Select from Anchor to Cursor’‘Select from Anchor to Cursor’
3‘editor.action.cancelSelectionAnchor’‘Cancel Selection Anchor’‘Cancel Selection Anchor’
4‘editor.action.moveCarretLeftAction’‘Move Selected Text Left’‘Move Selected Text Left’
5‘editor.action.moveCarretRightAction’‘Move Selected Text Right’‘Move Selected Text Right’
6‘editor.action.transposeLetters’‘Transpose Letters’‘Transpose Letters’
7‘editor.action.clipboardCopyWithSyntaxHighlightingAction’‘Copy With Syntax Highlighting’‘Copy With Syntax Highlighting’
8‘editor.action.showContextMenu’‘Show Editor Context Menu’‘Show Editor Context Menu’
9‘cursorUndo’‘Cursor Undo’‘Cursor Undo’
10‘cursorRedo’‘Cursor Redo’‘Cursor Redo’
11‘editor.action.fontZoomIn’‘Editor Font Zoom In’‘Editor Font Zoom In’
12‘editor.action.fontZoomOut’‘Editor Font Zoom Out’‘Editor Font Zoom Out’
13‘editor.action.fontZoomReset’‘Editor Font Zoom Reset’‘Editor Font Zoom Reset’
14‘editor.action.formatDocument’‘Format Document’‘Format Document’
15‘editor.action.formatSelection’‘Format Selection’‘Format Selection’
16‘expandLineSelection’‘Expand Line Selection’‘Expand Line Selection’
17‘editor.action.smartSelect.expand’‘Expand Selection’‘Expand Selection’
18‘editor.action.smartSelect.shrink’‘Shrink Selection’‘Shrink Selection’
19‘editor.action.toggleTabFocusMode’‘Toggle Tab Key Moves Focus’‘Toggle Tab Key Moves Focus’
20‘editor.action.forceRetokenize’‘Developer: Force Retokenize’‘Developer: Force Retokenize’
21‘editor.action.commentLine’‘Toggle Line Comment’‘Toggle Line Comment’
22‘editor.action.addCommentLine’‘Add Line Comment’‘Add Line Comment’
23‘editor.action.removeCommentLine’‘Remove Line Comment’‘Remove Line Comment’
24‘editor.action.blockComment’‘Toggle Block Comment’‘Toggle Block Comment’
25‘editor.action.indentationToSpaces’‘Convert Indentation to Spaces’‘Convert Indentation to Spaces’
26‘editor.action.indentationToTabs’‘Convert Indentation to Tabs’‘Convert Indentation to Tabs’
27‘editor.action.indentUsingTabs’‘Indent Using Tabs’‘Indent Using Tabs’
28‘editor.action.indentUsingSpaces’‘Indent Using Spaces’‘Indent Using Spaces’
29‘editor.action.detectIndentation’‘Detect Indentation from Content’‘Detect Indentation from Content’
30‘editor.action.reindentlines’‘Reindent Lines’‘Reindent Lines’
31‘editor.action.reindentselectedlines’‘Reindent Selected Lines’‘Reindent Selected Lines’
32‘editor.action.copyLinesUpAction’‘Copy Line Up’‘Copy Line Up’
33‘editor.action.copyLinesDownAction’‘Copy Line Down’‘Copy Line Down’
34‘editor.action.duplicateSelection’‘Duplicate Selection’‘Duplicate Selection’
35‘editor.action.moveLinesUpAction’‘Move Line Up’‘Move Line Up’
36‘editor.action.moveLinesDownAction’‘Move Line Down’‘Move Line Down’
37‘editor.action.sortLinesAscending’‘Sort Lines Ascending’‘Sort Lines Ascending’
38‘editor.action.sortLinesDescending’‘Sort Lines Descending’‘Sort Lines Descending’
39‘editor.action.removeDuplicateLines’‘Delete Duplicate Lines’‘Delete Duplicate Lines’
40‘editor.action.trimTrailingWhitespace’‘Trim Trailing Whitespace’‘Trim Trailing Whitespace’
41‘editor.action.deleteLines’‘Delete Line’‘Delete Line’
42‘editor.action.indentLines’‘Indent Line’‘Indent Line’
43‘editor.action.outdentLines’‘Outdent Line’‘Outdent Line’
44‘editor.action.insertLineBefore’‘Insert Line Above’‘Insert Line Above’
45‘editor.action.insertLineAfter’‘Insert Line Below’‘Insert Line Below’
46‘deleteAllLeft’‘Delete All Left’‘Delete All Left’
47‘deleteAllRight’‘Delete All Right’‘Delete All Right’
48‘editor.action.joinLines’‘Join Lines’‘Join Lines’
49‘editor.action.transpose’‘Transpose characters around the cursor’‘Transpose characters around the cursor’
50‘editor.action.transformToUppercase’‘Transform to Uppercase’‘Transform to Uppercase’
51‘editor.action.transformToLowercase’‘Transform to Lowercase’‘Transform to Lowercase’
52‘editor.action.transformToSnakecase’‘Transform to Snake Case’‘Transform to Snake Case’
53‘editor.action.transformToTitlecase’‘Transform to Title Case’‘Transform to Title Case’
54‘editor.action.transformToKebabcase’‘Transform to Kebab Case’‘Transform to Kebab Case’
55‘deleteInsideWord’‘Delete Word’‘Delete Word’
56‘editor.action.quickCommand’‘Command Palette’‘Command Palette’
57‘codelens.showLensesInCurrentLine’‘Show CodeLens Commands For Current Line’‘Show CodeLens Commands For Current Line’
58‘editor.action.gotoLine’‘Go to Line/Column…’‘Go to Line/Column…’
59‘editor.action.inPlaceReplace.up’‘Replace with Previous Value’‘Replace with Previous Value’
60‘editor.action.inPlaceReplace.down’‘Replace with Next Value’‘Replace with Next Value’
61‘editor.action.quickFix’‘Quick Fix…’‘Quick Fix…’
62‘editor.action.refactor’‘Refactor…’‘Refactor…’
63‘editor.action.refactor.preview’‘Refactor with Preview…’‘Refactor Preview…’
64‘editor.action.sourceAction’‘Source Action…’‘Source Action…’
65‘editor.action.organizeImports’‘Organize Imports’‘Organize Imports’
66‘editor.action.autoFix’‘Auto Fix…’‘Auto Fix…’
67‘editor.action.fixAll’‘Fix All’‘Fix All’
68‘editor.action.rename’‘Rename Symbol’‘Rename Symbol’
69‘editor.action.quickOutline’‘Go to Symbol…’‘Go to Symbol…’
70‘editor.action.showAccessibilityHelp’‘Show Accessibility Help’‘Show Accessibility Help’
71‘editor.action.inspectTokens’‘Developer: Inspect Tokens’‘Developer: Inspect Tokens’
72‘editor.action.selectToBracket’‘Select to Bracket’‘Select to Bracket’
73‘editor.action.jumpToBracket’‘Go to Bracket’‘Go to Bracket’
74‘editor.action.linkedEditing’‘Start Linked Editing’‘Start Linked Editing’
75‘editor.action.openLink’‘Open Link’‘Open Link’
76‘editor.action.wordHighlight.next’‘Go to Next Symbol Highlight’‘Go to Next Symbol Highlight’
77‘editor.action.wordHighlight.prev’‘Go to Previous Symbol Highlight’‘Go to Previous Symbol Highlight’
78‘editor.action.wordHighlight.trigger’‘Trigger Symbol Highlight’‘Trigger Symbol Highlight’
79‘editor.action.revealDefinition’‘Go to Definition’‘Go to Definition’
80‘editor.action.revealDefinitionAside’‘Open Definition to the Side’‘Open Definition to the Side’
81‘editor.action.peekDefinition’‘Peek Definition’‘Peek Definition’
82‘editor.action.revealDeclaration’‘Go to Declaration’‘Go to Declaration’
83‘editor.action.peekDeclaration’‘Peek Declaration’‘Peek Declaration’
84‘editor.action.goToTypeDefinition’‘Go to Type Definition’‘Go to Type Definition’
85‘editor.action.peekTypeDefinition’‘Peek Type Definition’‘Peek Type Definition’
86‘editor.action.goToImplementation’‘Go to Implementations’‘Go to Implementations’
87‘editor.action.peekImplementation’‘Peek Implementations’‘Peek Implementations’
88‘editor.action.goToReferences’‘Go to References’‘Go to References’
89‘editor.action.referenceSearch.trigger’‘Peek References’‘Peek References’
90‘editor.action.diffReview.next’‘Go to Next Difference’‘Go to Next Difference’
91‘editor.action.diffReview.prev’‘Go to Previous Difference’‘Go to Previous Difference’
92‘editor.action.triggerParameterHints’‘Trigger Parameter Hints’‘Trigger Parameter Hints’
93‘editor.action.toggleHighContrast’‘Toggle High Contrast Theme’‘Toggle High Contrast Theme’
94‘actions.find’‘Find’‘Find’
95‘editor.action.startFindReplaceAction’‘Replace’‘Replace’
96‘editor.actions.findWithArgs’‘Find With Arguments’‘Find With Arguments’
97‘actions.findWithSelection’‘Find With Selection’‘Find With Selection’
98‘editor.action.nextMatchFindAction’‘Find Next’‘Find Next’
99‘editor.action.previousMatchFindAction’‘Find Previous’‘Find Previous’
100‘editor.action.nextSelectionMatchFindAction’‘Find Next Selection’‘Find Next Selection’
101‘editor.action.previousSelectionMatchFindAction’‘Find Previous Selection’‘Find Previous Selection’
102‘editor.action.insertCursorAbove’‘Add Cursor Above’‘Add Cursor Above’
103‘editor.action.insertCursorBelow’‘Add Cursor Below’‘Add Cursor Below’
104‘editor.action.insertCursorAtEndOfEachLineSelected’‘Add Cursors to Line Ends’‘Add Cursors to Line Ends’
105‘editor.action.addSelectionToNextFindMatch’‘Add Selection To Next Find Match’‘Add Selection To Next Find Match’
106‘editor.action.addSelectionToPreviousFindMatch’‘Add Selection To Previous Find Match’‘Add Selection To Previous Find Match’
107‘editor.action.moveSelectionToNextFindMatch’‘Move Last Selection To Next Find Match’‘Move Last Selection To Next Find Match’
108‘editor.action.moveSelectionToPreviousFindMatch’‘Move Last Selection To Previous Find Match’‘Move Last Selection To Previous Find Match’
109‘editor.action.selectHighlights’‘Select All Occurrences of Find Match’‘Select All Occurrences of Find Match’
110‘editor.action.changeAll’‘Change All Occurrences’‘Change All Occurrences’
111‘editor.action.addCursorsToBottom’‘Add Cursors To Bottom’‘Add Cursors To Bottom’
112‘editor.action.addCursorsToTop’‘Add Cursors To Top’‘Add Cursors To Top’
113‘editor.action.focusNextCursor’‘Focus Next Cursor’‘Focus Next Cursor’
114‘editor.action.focusPreviousCursor’‘Focus Previous Cursor’‘Focus Previous Cursor’
115‘editor.unfold’‘Unfold’‘Unfold’
116‘editor.unfoldRecursively’‘Unfold Recursively’‘Unfold Recursively’
117‘editor.fold’‘Fold’‘Fold’
118‘editor.foldRecursively’‘Fold Recursively’‘Fold Recursively’
119‘editor.foldAll’‘Fold All’‘Fold All’
120‘editor.unfoldAll’‘Unfold All’‘Unfold All’
121‘editor.foldAllBlockComments’‘Fold All Block Comments’‘Fold All Block Comments’
122‘editor.foldAllMarkerRegions’‘Fold All Regions’‘Fold All Regions’
123‘editor.unfoldAllMarkerRegions’‘Unfold All Regions’‘Unfold All Regions’
124‘editor.foldAllExcept’‘Fold All Regions Except Selected’‘Fold All Regions Except Selected’
125‘editor.unfoldAllExcept’‘Unfold All Regions Except Selected’‘Unfold All Regions Except Selected’
126‘editor.toggleFold’‘Toggle Fold’‘Toggle Fold’
127‘editor.gotoParentFold’‘Go to Parent Fold’‘Go to Parent Fold’
128‘editor.gotoPreviousFold’‘Go to Previous Folding Range’‘Go to Previous Folding Range’
129‘editor.gotoNextFold’‘Go to Next Folding Range’‘Go to Next Folding Range’
130‘editor.createFoldingRangeFromSelection’‘Create Manual Folding Range from Selection’‘Create Folding Range from Selection’
131‘editor.removeManualFoldingRanges’‘Remove Manual Folding Ranges’‘Remove Manual Folding Ranges’
132‘editor.foldLevel1’‘Fold Level 1’‘Fold Level 1’
133‘editor.foldLevel2’‘Fold Level 2’‘Fold Level 2’
134‘editor.foldLevel3’‘Fold Level 3’‘Fold Level 3’
135‘editor.foldLevel4’‘Fold Level 4’‘Fold Level 4’
136‘editor.foldLevel5’‘Fold Level 5’‘Fold Level 5’
137‘editor.foldLevel6’‘Fold Level 6’‘Fold Level 6’
138‘editor.foldLevel7’‘Fold Level 7’‘Fold Level 7’
139‘editor.action.marker.next’‘Go to Next Problem (Error, Warning, Info)’‘Go to Next Problem (Error, Warning, Info)’
140‘editor.action.marker.prev’‘Go to Previous Problem (Error, Warning, Info)’‘Go to Previous Problem (Error, Warning, Info)’
141‘editor.action.marker.nextInFiles’‘Go to Next Problem in Files (Error, Warning, Info)’‘Go to Next Problem in Files (Error, Warning, Info)’
142‘editor.action.marker.prevInFiles’‘Go to Previous Problem in Files (Error, Warning, Info)’‘Go to Previous Problem in Files (Error, Warning, Info)’
143‘editor.action.showHover’‘Show Hover’‘Show Hover’
144‘editor.action.showDefinitionPreviewHover’‘Show Definition Preview Hover’‘Show Definition Preview Hover’
145‘editor.action.unicodeHighlight.disableHighlightingOfAmbiguousCharacters’‘Disable highlighting of ambiguous characters’‘Disable highlighting of ambiguous characters’
146‘editor.action.unicodeHighlight.disableHighlightingOfInvisibleCharacters’‘Disable highlighting of invisible characters’‘Disable highlighting of invisible characters’
147‘editor.action.unicodeHighlight.disableHighlightingOfNonBasicAsciiCharacters’‘Disable highlighting of non basic ASCII characters’‘Disable highlighting of non basic ASCII characters’
148‘editor.action.unicodeHighlight.showExcludeOptions’‘Show Exclude Options’‘Show Exclude Options’
149‘editor.action.triggerSuggest’‘Trigger Suggest’‘Trigger Suggest’
150‘editor.action.resetSuggestSize’‘Reset Suggest Widget Size’‘Reset Suggest Widget Size’
151‘editor.action.inlineSuggest.trigger’‘Trigger Inline Suggestion’‘Trigger Inline Suggestion’
152‘editor.action.inlineSuggest.showNext’‘Show Next Inline Suggestion’‘Show Next Inline Suggestion’
153‘editor.action.inlineSuggest.showPrevious’‘Show Previous Inline Suggestion’‘Show Previous Inline Suggestion’

完整代码


Hello World Monaco Editor

Hello World CSDN@拿我格子衫来 Monaco Editor

在这里插入图片描述

总结

使用api来触发某个action,能够实现很多自动化操作。总之就是很牛X啊。

相关内容

热门资讯

自己内心矛盾的句子精选181... 自己内心矛盾的句子 精选104句1. 如果,最后在身边的真的不是你。如果你经历了那么多的起起落落,最...
赞美校园保洁的句子精选115... 赞美校园保洁的句子 精选102句1. 你们用汗水与辛劳挥舞着手中笨拙的扫帚,给校园一个整洁的容貌,给...
环境描写死气沉沉句子精选98... 环境描写死气沉沉句子 精选69句1. 教室中死气沉沉,同学们个个都泪流满面,惟有几位同学装作一脸苦笑...
一生能遇到的句子精选420句 一生能遇到的句子 精选63句1. 选择你所爱的,然后爱你所选择的。2. 你的温柔,我懂,你的疼爱,我...
诚信的句子 有关诚信的句子大全  诚信是一种美德,会让你更加完美。下面是小编整理的有关诚信的句子大全,欢迎阅读!...
时间过得快的搞笑句子精选26... 时间过得快的搞笑句子 精选132句1. 我们不可能都成为英雄。2. 要找出时间来考虑一下,一天中做了...
你好六月的优美句子 你好六月的优美句子(精选100句)  在学习、工作或生活中,大家都听说过或者使用过一些比较经典的句子...
怀念好句子大全要短的精选38... 怀念好句子大全要短的 精选35句1. 小学同学聚会能聚这么多人真的不容易,好怀念以前小的时候现在大家...
有哲理的唯美句子精选76句 有哲理的唯美句子 精选50句1. 池塘边的榕树上,还有知了在声声叫着;家门口的小路旁,还有小狗在快乐...
自我独特的个性签名 自我独特的个性签名(精选70句)  不管现实多么惨不忍睹,都要持之以恒地相信,这只是黎明前短暂的黑暗...
人类破坏环境污染句子精选30... 人类破坏环境污染句子 精选64句1. 排放的气息,是乌云盖天的狂欢;森林的骤减,是沙漠扩展的心愿;灾...
繁体字情侣个性签名   繁体字情侣个性签名  1、討厭自己想刺猬一樣小心防備。討厭自己想小丑一樣假冒開心。  2、如果決...
抖音名字 抖音名字▼※目录※▼抖音名字(1-100个)抖音名字(101-200个)抖音名字(201-300个)...
爱情的经典个性签名 关于爱情的经典个性签名集锦  1、其实只要两个人幸福就好了,何必在乎别人的眼光和议论。  2、距离让...
女生爱情个性签名 女生爱情个性签名  永远都不好停止微笑,即使是在你难过的时候,说不定有人会正因你的笑容而爱上你。以下...
微信名字最好听的昵称(精选5... 微信名字最好听的昵称(精选500个)  一、什么是网名  网名指在网上使用的名字。由于网络是一个虚拟...
塘尾村 塘尾村广东省东莞市石排镇塘尾村塘尾村(广东省东莞市石排镇塘尾村)塘尾村位于东莞市石排内,古村以古围墙...
父亲节个性签名 父亲节个性签名  引导语:我不害怕前途再多挫折挣扎只是愧疚没能在家给你们报答。下面由yjbys小编与...
感情的个性签名 关于感情的个性签名80条  1、不是所有的牛奶都叫特仑苏,不是所有的喜欢都是爱。  2、如果你明明知...
此省人口排名中国第一,GDP... 此省人口排名中国第一,GDP排名中国第一,还是一座历史名城中国历代皇帝中,秦始皇绝对称得上千古一帝。...