b3ca8b68b5008078716b8412d5bf50e2b1e22d1d.svn-base 12 KB

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