JCodeEditor.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <template>
  2. <div v-bind="fullScreenParentProps">
  3. <a-icon v-if="fullScreen" class="full-screen-icon" :type="iconType" @click="()=>fullCoder=!fullCoder"/>
  4. <div class="code-editor-cust full-screen-child">
  5. <textarea ref="textarea"></textarea>
  6. <span @click="nullTipClick" class="null-tip" :class="{'null-tip-hidden':hasCode}" :style="nullTipStyle">{{ placeholderShow }}</span>
  7. <template v-if="languageChange">
  8. <a-select v-model="mode" size="small" class="code-mode-select" @change="changeMode" placeholder="请选择主题">
  9. <a-select-option
  10. v-for="mode in modes"
  11. :key="mode.value"
  12. :value="mode.value">
  13. {{ mode.label }}
  14. </a-select-option>
  15. </a-select>
  16. </template>
  17. </div>
  18. </div>
  19. </template>
  20. <script type="text/ecmascript-6">
  21. // 引入全局实例
  22. import _CodeMirror from 'codemirror'
  23. // 核心样式
  24. import 'codemirror/lib/codemirror.css'
  25. // 引入主题后还需要在 options 中指定主题才会生效 darcula gruvbox-dark hopscotch monokai
  26. import 'codemirror/theme/panda-syntax.css'
  27. //提示css
  28. import "codemirror/addon/hint/show-hint.css";
  29. // 需要引入具体的语法高亮库才会有对应的语法高亮效果
  30. // codemirror 官方其实支持通过 /addon/mode/loadmode.js 和 /mode/meta.js 来实现动态加载对应语法高亮库
  31. // 但 vue 貌似没有无法在实例初始化后再动态加载对应 JS ,所以此处才把对应的 JS 提前引入
  32. import 'codemirror/mode/javascript/javascript.js'
  33. import 'codemirror/mode/css/css.js'
  34. import 'codemirror/mode/xml/xml.js'
  35. import 'codemirror/mode/clike/clike.js'
  36. import 'codemirror/mode/markdown/markdown.js'
  37. import 'codemirror/mode/python/python.js'
  38. import 'codemirror/mode/r/r.js'
  39. import 'codemirror/mode/shell/shell.js'
  40. import 'codemirror/mode/sql/sql.js'
  41. import 'codemirror/mode/swift/swift.js'
  42. import 'codemirror/mode/vue/vue.js'
  43. // 尝试获取全局实例
  44. const CodeMirror = window.CodeMirror || _CodeMirror
  45. export default {
  46. name: 'JCodeEditor',
  47. props: {
  48. // 外部传入的内容,用于实现双向绑定
  49. value: {
  50. type: String,
  51. default: ''
  52. },
  53. // 外部传入的语法类型
  54. language: {
  55. type: String,
  56. default: null
  57. },
  58. languageChange:{
  59. type: Boolean,
  60. default:false,
  61. required:false
  62. },
  63. placeholder: {
  64. type: String,
  65. default: null
  66. },
  67. // 显示行号
  68. lineNumbers: {
  69. type: Boolean,
  70. default: true
  71. },
  72. // 是否显示全屏按钮
  73. fullScreen: {
  74. type: Boolean,
  75. default: false
  76. },
  77. // 全屏以后的z-index
  78. zIndex: {
  79. type: [Number, String],
  80. default: 999
  81. }
  82. },
  83. data () {
  84. return {
  85. // 内部真实的内容
  86. code: '',
  87. iconType: 'fullscreen',
  88. hasCode:false,
  89. // 默认的语法类型
  90. mode: 'javascript',
  91. // 编辑器实例
  92. coder: null,
  93. // 默认配置
  94. options: {
  95. // 缩进格式
  96. tabSize: 2,
  97. // 主题,对应主题库 JS 需要提前引入
  98. theme: 'panda-syntax',
  99. line: true,
  100. // extraKeys: {'Ctrl': 'autocomplete'},//自定义快捷键
  101. hintOptions: {
  102. tables: {
  103. users: ['name', 'score', 'birthDate'],
  104. countries: ['name', 'population', 'size']
  105. }
  106. },
  107. },
  108. // 支持切换的语法高亮类型,对应 JS 已经提前引入
  109. // 使用的是 MIME-TYPE ,不过作为前缀的 text/ 在后面指定时写死了
  110. modes: [{
  111. value: 'css',
  112. label: 'CSS'
  113. }, {
  114. value: 'javascript',
  115. label: 'Javascript'
  116. }, {
  117. value: 'html',
  118. label: 'XML/HTML'
  119. }, {
  120. value: 'x-java',
  121. label: 'Java'
  122. }, {
  123. value: 'x-objectivec',
  124. label: 'Objective-C'
  125. }, {
  126. value: 'x-python',
  127. label: 'Python'
  128. }, {
  129. value: 'x-rsrc',
  130. label: 'R'
  131. }, {
  132. value: 'x-sh',
  133. label: 'Shell'
  134. }, {
  135. value: 'x-sql',
  136. label: 'SQL'
  137. }, {
  138. value: 'x-swift',
  139. label: 'Swift'
  140. }, {
  141. value: 'x-vue',
  142. label: 'Vue'
  143. }, {
  144. value: 'markdown',
  145. label: 'Markdown'
  146. }],
  147. // code 编辑器 是否全屏
  148. fullCoder: false
  149. }
  150. },
  151. watch: {
  152. fullCoder:{
  153. handler(value) {
  154. if(value){
  155. this.iconType="fullscreen-exit"
  156. }else{
  157. this.iconType="fullscreen"
  158. }
  159. }
  160. },
  161. // value: {
  162. // immediate: false,
  163. // handler(value) {
  164. // this._getCoder().then(() => {
  165. // this.coder.setValue(value)
  166. // })
  167. // }
  168. // },
  169. language: {
  170. immediate: true,
  171. handler(language) {
  172. this._getCoder().then(() => {
  173. // 尝试从父容器获取语法类型
  174. if (language) {
  175. // 获取具体的语法类型对象
  176. let modeObj = this._getLanguage(language)
  177. // 判断父容器传入的语法是否被支持
  178. if (modeObj) {
  179. this.mode = modeObj.label
  180. this.coder.setOption('mode', `text/${modeObj.value}`)
  181. }
  182. }
  183. })
  184. }
  185. }
  186. },
  187. computed: {
  188. placeholderShow() {
  189. if (this.placeholder == null) {
  190. return `请在此输入${this.language}代码`
  191. } else {
  192. return this.placeholder
  193. }
  194. },
  195. nullTipStyle(){
  196. if (this.lineNumbers) {
  197. return { left: '36px' }
  198. } else {
  199. return { left: '12px' }
  200. }
  201. },
  202. // coder 配置
  203. coderOptions() {
  204. return {
  205. tabSize: this.options.tabSize,
  206. theme: this.options.theme,
  207. lineNumbers: this.lineNumbers,
  208. line: true,
  209. hintOptions: this.options.hintOptions
  210. }
  211. },
  212. fullScreenParentProps(){
  213. let props = {
  214. class: ['full-screen-parent', this.fullCoder ? 'full-screen' : ''],
  215. style: {}
  216. }
  217. if (this.fullCoder) {
  218. props.style['z-index'] = this.zIndex
  219. }
  220. return props
  221. }
  222. },
  223. mounted () {
  224. // 初始化
  225. this._initialize()
  226. },
  227. methods: {
  228. // 初始化
  229. _initialize () {
  230. // 初始化编辑器实例,传入需要被实例化的文本域对象和默认配置
  231. this.coder = CodeMirror.fromTextArea(this.$refs.textarea, this.coderOptions)
  232. // 编辑器赋值
  233. if(this.value||this.code){
  234. this.hasCode=true
  235. this.coder.setValue(this.value || this.code)
  236. }else{
  237. this.coder.setValue('')
  238. this.hasCode=false
  239. }
  240. // 支持双向绑定
  241. this.coder.on('change', (coder) => {
  242. this.code = coder.getValue()
  243. if(this.code){
  244. this.hasCode=true
  245. }else{
  246. this.hasCode=false
  247. }
  248. if (this.$emit) {
  249. this.$emit('input', this.code)
  250. }
  251. })
  252. this.coder.on('focus', () => {
  253. this.hasCode=true
  254. })
  255. this.coder.on('blur', () => {
  256. if(this.code){
  257. this.hasCode=true
  258. }else{
  259. this.hasCode=false
  260. }
  261. })
  262. /* this.coder.on('cursorActivity',()=>{
  263. this.coder.showHint()
  264. })*/
  265. },
  266. getCodeContent(){
  267. return this.code
  268. },
  269. setCodeContent(val){
  270. setTimeout(()=>{
  271. if(!val){
  272. this.coder.setValue('')
  273. }else{
  274. this.coder.setValue(val)
  275. }
  276. },300)
  277. },
  278. // 获取当前语法类型
  279. _getLanguage (language) {
  280. // 在支持的语法类型列表中寻找传入的语法类型
  281. return this.modes.find((mode) => {
  282. // 所有的值都忽略大小写,方便比较
  283. let currentLanguage = language.toLowerCase()
  284. let currentLabel = mode.label.toLowerCase()
  285. let currentValue = mode.value.toLowerCase()
  286. // 由于真实值可能不规范,例如 java 的真实值是 x-java ,所以讲 value 和 label 同时和传入语法进行比较
  287. return currentLabel === currentLanguage || currentValue === currentLanguage
  288. })
  289. },
  290. _getCoder() {
  291. let _this = this
  292. return new Promise((resolve) => {
  293. (function get() {
  294. if (_this.coder) {
  295. resolve(_this.coder)
  296. } else {
  297. setTimeout(get, 10)
  298. }
  299. })()
  300. })
  301. },
  302. // 更改模式
  303. changeMode (val) {
  304. // 修改编辑器的语法配置
  305. this.coder.setOption('mode', `text/${val}`)
  306. // 获取修改后的语法
  307. let label = this._getLanguage(val).label.toLowerCase()
  308. // 允许父容器通过以下函数监听当前的语法值
  309. this.$emit('language-change', label)
  310. },
  311. nullTipClick(){
  312. this.coder.focus()
  313. }
  314. }
  315. }
  316. </script>
  317. <style lang="less">
  318. .code-editor-cust{
  319. flex-grow:1;
  320. display:flex;
  321. position:relative;
  322. height:100%;
  323. .CodeMirror{
  324. flex-grow:1;
  325. z-index:1;
  326. .CodeMirror-code{
  327. line-height:19px;
  328. }
  329. }
  330. .code-mode-select{
  331. position:absolute;
  332. z-index:2;
  333. right:10px;
  334. top:10px;
  335. max-width:130px;
  336. }
  337. .CodeMirror{
  338. height: auto;
  339. min-height:100%;
  340. }
  341. .null-tip{
  342. position: absolute;
  343. top: 4px;
  344. left: 36px;
  345. z-index: 10;
  346. color: #ffffffc9;
  347. line-height: initial;
  348. }
  349. .null-tip-hidden{
  350. display: none;
  351. }
  352. }
  353. /* 全屏样式 */
  354. .full-screen-parent {
  355. position: relative;
  356. .full-screen-icon {
  357. opacity: 0;
  358. color: black;
  359. width: 20px;
  360. height: 20px;
  361. line-height: 24px;
  362. background-color: white;
  363. position: absolute;
  364. top: 2px;
  365. right: 2px;
  366. z-index: 9;
  367. cursor: pointer;
  368. transition: opacity 0.3s;
  369. }
  370. &:hover {
  371. .full-screen-icon {
  372. opacity: 1;
  373. &:hover {
  374. background-color: rgba(255, 255, 255, 0.88);
  375. }
  376. }
  377. }
  378. &.full-screen {
  379. position: fixed;
  380. top: 10px;
  381. left: 10px;
  382. width: calc(100% - 20px);
  383. height: calc(100% - 20px);
  384. padding: 10px;
  385. background-color: #f5f5f5;
  386. .full-screen-icon {
  387. top: 12px;
  388. right: 12px;
  389. }
  390. .full-screen-child {
  391. height: 100%;
  392. max-height: 100%;
  393. min-height: 100%;
  394. }
  395. }
  396. .full-screen-child {
  397. min-height: 120px;
  398. max-height: 320px;
  399. overflow:hidden;
  400. }
  401. }
  402. .CodeMirror-cursor{
  403. height:18.4px !important;
  404. }
  405. </style>