9654750f3e73e702738e4262b3e49f0cddaca8a7.svn-base 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <div v-bind="fullScreenParentProps">
  3. <codemirror
  4. :style="style_"
  5. ref="myEditor"
  6. :options="getOptions"
  7. :value="getValue"
  8. @input="editroChange"
  9. ></codemirror>
  10. <a-icon :type="iconType" @click="()=>fullCoder=!fullCoder" class="full-screen-icon" />
  11. </div>
  12. </template>
  13. <script>
  14. import { codemirror } from 'vue-codemirror-lite'
  15. import 'codemirror/mode/javascript/javascript.js'
  16. import 'codemirror/mode/vue/vue.js'
  17. import 'codemirror/mode/sql/sql.js'
  18. import 'codemirror/addon/hint/show-hint.js'
  19. import 'codemirror/addon/hint/show-hint.css'
  20. import 'codemirror/addon/hint/javascript-hint.js'
  21. import 'codemirror/addon/display/fullscreen.js'
  22. import 'codemirror/theme/panda-syntax.css'
  23. export default {
  24. name: 'CodeMirrorLite',
  25. components: {
  26. codemirror
  27. },
  28. props: {
  29. // 外部传入的内容,用于实现双向绑定
  30. value: {
  31. type: String,
  32. default: ''
  33. },
  34. // 外部传入的语法类型
  35. mode: {
  36. type: String,
  37. default: null
  38. },
  39. languageChange: {
  40. type: Boolean,
  41. default: false,
  42. required: false
  43. },
  44. placeholder: {
  45. type: String,
  46. default: null
  47. },
  48. // 显示行号
  49. lineNumbers: {
  50. type: Boolean,
  51. default: true
  52. },
  53. // 是否显示全屏按钮
  54. fullScreen: {
  55. type: Boolean,
  56. default: false
  57. },
  58. // 全屏以后的z-index
  59. zIndex: {
  60. type: [Number, String],
  61. default: 999
  62. },
  63. style_: { // codeMirror样式 如果需要设置样式 height是必填
  64. type: String,
  65. default: 'height:150px;'
  66. }
  67. },
  68. data() {
  69. return {
  70. // 默认配置
  71. options: {
  72. mode: 'javascript',
  73. // 缩进格式
  74. tabSize: 2,
  75. // 主题,对应主题库 JS 需要提前引入
  76. theme: 'panda-syntax',
  77. line: true,
  78. hintOptions: {
  79. tables: {
  80. users: ['name', 'score', 'birthDate'],
  81. countries: ['name', 'population', 'size']
  82. }
  83. }
  84. },
  85. iconType: 'fullscreen',
  86. // code 编辑器 是否全屏
  87. fullCoder: false
  88. }
  89. },
  90. watch: {
  91. fullCoder: {
  92. handler(value) {
  93. if (value) {
  94. this.iconType = 'fullscreen-exit'
  95. } else {
  96. this.iconType = 'fullscreen'
  97. }
  98. }
  99. }
  100. },
  101. computed: {
  102. getOptions() {
  103. if (this.mode) {
  104. this.options.mode = this.mode
  105. }
  106. this.options.lineNumbers = this.lineNumbers
  107. this.options.fullScreen = this.fullScreen
  108. return this.options
  109. },
  110. editor() {
  111. return this.$refs.myEditor.editor
  112. },
  113. getValue() {
  114. return this.value ? this.value : ''
  115. },
  116. fullScreenParentProps() {
  117. const props = {
  118. class: ['full-screen-parent', this.fullCoder ? 'full-screen' : ''],
  119. style: {}
  120. }
  121. if (this.fullCoder) {
  122. props.style['z-index'] = this.zIndex
  123. }
  124. return props
  125. }
  126. },
  127. mounted() {
  128. },
  129. methods: {
  130. editroChange() {
  131. this.$emit('input', this.$refs.myEditor.editor.getValue())
  132. }
  133. }
  134. }
  135. </script>
  136. <style lang="less">
  137. .CodeMirror {
  138. min-height: 120px;
  139. height: 100%;
  140. }
  141. .CodeMirror-fullscreen {
  142. position: fixed;
  143. top: 0; left: 0; right: 0; bottom: 0;
  144. height: auto;
  145. z-index: 9;
  146. }
  147. /* 全屏样式 */
  148. .full-screen-parent {
  149. position: relative;
  150. .full-screen-icon {
  151. opacity: 0;
  152. color: black;
  153. width: 20px;
  154. height: 20px;
  155. line-height: 24px;
  156. background-color: white;
  157. position: absolute;
  158. top: 2px;
  159. right: 2px;
  160. z-index: 9;
  161. cursor: pointer;
  162. transition: opacity 0.3s;
  163. }
  164. &:hover {
  165. .full-screen-icon {
  166. opacity: 1;
  167. &:hover {
  168. background-color: rgba(255, 255, 255, 0.88);
  169. }
  170. }
  171. }
  172. &.full-screen {
  173. position: fixed;
  174. top: 10px;
  175. left: 10px;
  176. width: calc(100% - 20px);
  177. height: calc(100% - 20px);
  178. padding: 10px;
  179. background-color: #f5f5f5;
  180. .full-screen-icon {
  181. top: 12px;
  182. right: 12px;
  183. }
  184. .full-screen-child {
  185. height: 100%;
  186. max-height: 100%;
  187. min-height: 100%;
  188. }
  189. .vue-codemirror-wrap,.cm-s-panda-syntax{
  190. height: 100% !important;
  191. }
  192. }
  193. .full-screen-child {
  194. min-height: 120px;
  195. max-height: 320px;
  196. overflow:hidden;
  197. }
  198. }
  199. </style>