panel_ex.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. 面板扩展
  3. 功能: cc.Color 颜色显示
  4. 例子: https://microsoft.github.io/monaco-editor/playground.html#interacting-with-the-editor-listening-to-mouse-events
  5. */
  6. 'use strict';
  7. const path = require('path');
  8. const fs = require('fs');
  9. const tools = require('../../tools/tools');
  10. let id = 'editor-run-code'
  11. module.exports = {
  12. /** @type import('../../panel/vs-panel/vs-panel-base') */
  13. parent : null,
  14. styleText : `.editorRunCodeIcon{
  15. height: 0;
  16. width: 0;
  17. border-top: 8px solid transparent;
  18. border-bottom: 8px solid transparent;
  19. border-left: 8px solid #7de24a;
  20. cursor:pointer;
  21. }`,
  22. // 面板初始化
  23. ready(parent){
  24. // index.js 对象
  25. this.parent = parent;
  26. },
  27. // monaco 编辑器初始化
  28. onLoad()
  29. {
  30. // 1.读取选中区域
  31. // 2.生成图标
  32. // 3.点击图标执行运行选中的代码
  33. // this.parent.vs_editor.onDidChangeCursorSelection((e)=>{
  34. // if(this.sch == null){
  35. // this.sch = setTimeout(()=>{
  36. // this.sch = null
  37. // let selections = this.parent.vs_editor.getSelections();
  38. // this.upDecorator(selections);
  39. // },50);
  40. // }
  41. // });
  42. this.parent.vs_editor.onMouseDown((e)=>{
  43. this.is_mouse_down = true;
  44. });
  45. this.parent.vs_editor.onMouseUp((e)=>{
  46. if(!this.parent.cfg.enabledDebugBtn){
  47. return;
  48. }
  49. setTimeout(()=>{
  50. this.is_mouse_down = false;
  51. let selections = this.parent.vs_editor.getSelections();
  52. this.upDecorator(selections);
  53. },50);
  54. });
  55. this.initWidget();
  56. },
  57. initWidget()
  58. {
  59. let _this = this;
  60. // Add a content widget
  61. this.runWidget = {
  62. domNode: null,
  63. position:{
  64. lineNumber: 7,
  65. column: 8
  66. },
  67. getId: function() {
  68. return 'editor.run.code.widget';
  69. },
  70. setActive: function(isVisible){
  71. this.domNode.style.display = isVisible ? 'block' : 'none'
  72. },
  73. getDomNode: function(){
  74. if (!this.domNode)
  75. {
  76. this.domNode = document.createElement('div');
  77. this.domNode.title="点击运行选中的代码"
  78. var style = document.createElement("style");
  79. style.innerHTML = _this.styleText;
  80. this.domNode.className = 'editorRunCodeIcon'
  81. this.domNode.appendChild(style);
  82. }
  83. return this.domNode;
  84. },
  85. setPosition: function(lineNumber,column) {
  86. this.position.lineNumber = lineNumber;
  87. this.position.column = column;
  88. _this.parent.vs_editor.layoutContentWidget(this);
  89. },
  90. getPosition: function() {
  91. return {
  92. position: this.position,
  93. preference: [_this.parent.monaco.editor.ContentWidgetPositionPreference.EXACT, _this.parent.monaco.editor.ContentWidgetPositionPreference.EXACT]
  94. };
  95. }
  96. };
  97. // 图标:运行按钮
  98. let dom = this.runWidget.getDomNode();
  99. dom.addEventListener('mousedown',(e)=>
  100. {
  101. e.preventDefault();
  102. e.stopPropagation();
  103. let text = this.parent.vs_editor.getModel().getValueInRange(this.parent.vs_editor.getSelection())
  104. try {
  105. Editor.log( eval(text) )
  106. } catch (error) {
  107. Editor.log(tools.T('执行代码块出错:','Error executing code block:'),error)
  108. }
  109. this.parent.runExtendFunc('onExecCode',text);
  110. },false)
  111. this.runWidget.setActive(false);
  112. this.parent.vs_editor.addContentWidget(this.runWidget);
  113. },
  114. upDecorator(selections)
  115. {
  116. if(this.is_mouse_down || selections.length == 0 || selections.length > 1 || selections[0].isEmpty()){
  117. this.runWidget.setActive(false);
  118. return;
  119. }
  120. let selection = selections[0];
  121. let line = Math.min( selection.selectionStartLineNumber,selection.positionLineNumber);
  122. line = line == 1 ? line : line -1;
  123. let column = this.parent.vs_editor.getModel().getLineLength(line)
  124. this.runWidget.setActive(true);
  125. this.runWidget.setPosition(line,column+5);
  126. },
  127. // 广播通知需要刷新装饰物
  128. onLoadDecoratorStyleHandle(){
  129. },
  130. // 设置选项
  131. setOptions(cfg,isInit)
  132. {
  133. if(cfg.enabledDebugBtn != null){
  134. if(!cfg.enabledDebugBtn){
  135. this.runWidget.setActive(cfg.enabledDebugBtn);
  136. }
  137. }
  138. },
  139. destoryWidget(){
  140. },
  141. // 面板销毁
  142. onDestroy(){
  143. },
  144. messages:{
  145. // 'cleanFile'()
  146. // {
  147. // },
  148. },
  149. };