demo-block.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div
  3. class="demo-block"
  4. :class="[blockClass, { 'hover': hovering }]"
  5. @mouseenter="hovering = true"
  6. @mouseleave="hovering = false">
  7. <slot name="source"></slot>
  8. <div class="meta">
  9. <div class="description">
  10. <slot></slot>
  11. <el-tooltip effect="dark" :content="langConfig['tooltip-text']" placement="right">
  12. <el-button size="small" type="primary" @click="goJsfiddle">{{ langConfig['button-text'] }}</el-button>
  13. </el-tooltip>
  14. </div>
  15. <slot name="highlight"></slot>
  16. </div>
  17. <div class="demo-block-control" @click="isExpanded = !isExpanded">
  18. <transition name="arrow-slide">
  19. <i :class="[iconClass, { 'hovering': hovering }]"></i>
  20. </transition>
  21. <transition name="text-slide">
  22. <span v-show="hovering">{{ controlText }}</span>
  23. </transition>
  24. </div>
  25. </div>
  26. </template>
  27. <style>
  28. .demo-block {
  29. border: solid 1px #eaeefb;
  30. border-radius: 4px;
  31. transition: .2s;
  32. &.hover {
  33. box-shadow: 0 0 8px 0 rgba(232, 237, 250, .6), 0 2px 4px 0 rgba(232, 237, 250, .5);
  34. }
  35. code {
  36. font-family: Menlo, Monaco, Consolas, Courier, monospace;
  37. }
  38. .demo-button {
  39. float: right;
  40. }
  41. .source {
  42. padding: 24px;
  43. }
  44. .meta {
  45. background-color: #f9fafc;
  46. border-top: solid 1px #eaeefb;
  47. clear: both;
  48. overflow: hidden;
  49. height: 0;
  50. transition: height .2s;
  51. }
  52. .description {
  53. padding: 18px 24px;
  54. width: 40%;
  55. box-sizing: border-box;
  56. border-left: solid 1px #eaeefb;
  57. float: right;
  58. font-size: 14px;
  59. line-height: 1.8;
  60. color: #5e6d82;
  61. word-break: break-word;
  62. p {
  63. margin: 0 0 12px;
  64. }
  65. code {
  66. color: #5e6d82;
  67. background-color: #e6effb;
  68. margin: 0 4px;
  69. transform: translateY(-2px);
  70. display: inline-block;
  71. padding: 1px 5px;
  72. font-size: 12px;
  73. border-radius: 3px;
  74. }
  75. }
  76. .highlight {
  77. width: 60%;
  78. border-right: solid 1px #eaeefb;
  79. pre {
  80. margin: 0;
  81. }
  82. code.hljs {
  83. margin: 0;
  84. border: none;
  85. max-height: none;
  86. border-radius: 0;
  87. &::before {
  88. content: none;
  89. }
  90. }
  91. }
  92. .demo-block-control {
  93. border-top: solid 1px #eaeefb;
  94. height: 36px;
  95. box-sizing: border-box;
  96. background-color: #fff;
  97. border-bottom-left-radius: 4px;
  98. border-bottom-right-radius: 4px;
  99. text-align: center;
  100. margin-top: -1px;
  101. color: #d3dce6;
  102. cursor: pointer;
  103. transition: .2s;
  104. position: relative;
  105. i {
  106. font-size: 12px;
  107. line-height: 36px;
  108. transition: .3s;
  109. &.hovering {
  110. transform: translateX(-40px);
  111. }
  112. }
  113. span {
  114. position: absolute;
  115. transform: translateX(-30px);
  116. font-size: 14px;
  117. line-height: 36px;
  118. transition: .3s;
  119. display: inline-block;
  120. }
  121. &:hover {
  122. color: #20a0ff;
  123. background-color: #f9fafc;
  124. }
  125. & .text-slide-enter,
  126. & .text-slide-leave-active {
  127. opacity: 0;
  128. transform: translateX(10px);
  129. }
  130. }
  131. }
  132. </style>
  133. <script type="text/babel">
  134. import compoLang from '../i18n/component.json';
  135. import { version } from 'main/index.js';
  136. export default {
  137. data() {
  138. return {
  139. hovering: false,
  140. isExpanded: false
  141. };
  142. },
  143. props: {
  144. jsfiddle: Object,
  145. default() {
  146. return {};
  147. }
  148. },
  149. methods: {
  150. goJsfiddle() {
  151. const { script, html, style } = this.jsfiddle;
  152. const resourcesTpl = '<scr' + 'ipt src="//unpkg.com/vue/dist/vue.js"></scr' + 'ipt>' +
  153. '\n<scr' + `ipt src="//unpkg.com/element-ui@${ version }/lib/index.js"></scr` + 'ipt>';
  154. let jsTpl = (script || '').replace(/export default/, 'var Main =').trim();
  155. let htmlTpl = `${resourcesTpl}\n<div id="app">\n${html.trim()}\n</div>`;
  156. let cssTpl = `@import url("//unpkg.com/element-ui@${ version }/lib/theme-default/index.css");\n${(style || '').trim()}\n`;
  157. jsTpl = jsTpl
  158. ? jsTpl + '\nvar Ctor = Vue.extend(Main)\nnew Ctor().$mount(\'#app\')'
  159. : 'new Vue().$mount(\'#app\')';
  160. const data = {
  161. js: jsTpl,
  162. css: cssTpl,
  163. html: htmlTpl,
  164. panel_js: 3,
  165. panel_css: 1
  166. };
  167. const form = document.getElementById('fiddle-form') || document.createElement('form');
  168. form.innerHTML = '';
  169. const node = document.createElement('textarea');
  170. form.method = 'post';
  171. form.action = 'https://jsfiddle.net/api/post/library/pure/';
  172. form.target = '_blank';
  173. for (let name in data) {
  174. node.name = name;
  175. node.value = data[name].toString();
  176. form.appendChild(node.cloneNode());
  177. }
  178. form.setAttribute('id', 'fiddle-form');
  179. form.style.display = 'none';
  180. document.body.appendChild(form);
  181. form.submit();
  182. }
  183. },
  184. computed: {
  185. lang() {
  186. return this.$route.path.split('/')[1];
  187. },
  188. langConfig() {
  189. return compoLang.filter(config => config.lang === this.lang)[0]['demo-block'];
  190. },
  191. blockClass() {
  192. return `demo-${ this.lang } demo-${ this.$router.currentRoute.path.split('/').pop() }`;
  193. },
  194. iconClass() {
  195. return this.isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom';
  196. },
  197. controlText() {
  198. return this.isExpanded ? this.langConfig['hide-text'] : this.langConfig['show-text'];
  199. },
  200. codeArea() {
  201. return this.$el.getElementsByClassName('meta')[0];
  202. },
  203. codeAreaHeight() {
  204. if (this.$el.getElementsByClassName('description').length > 0) {
  205. return Math.max(this.$el.getElementsByClassName('description')[0].clientHeight, this.$el.getElementsByClassName('highlight')[0].clientHeight);
  206. }
  207. return this.$el.getElementsByClassName('highlight')[0].clientHeight;
  208. }
  209. },
  210. watch: {
  211. isExpanded(val) {
  212. this.codeArea.style.height = val ? `${ this.codeAreaHeight + 1 }px` : '0';
  213. }
  214. },
  215. mounted() {
  216. this.$nextTick(() => {
  217. let highlight = this.$el.getElementsByClassName('highlight')[0];
  218. if (this.$el.getElementsByClassName('description').length === 0) {
  219. highlight.style.width = '100%';
  220. highlight.borderRight = 'none';
  221. }
  222. });
  223. }
  224. };
  225. </script>