demo-block.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. export default {
  136. data() {
  137. return {
  138. hovering: false,
  139. isExpanded: false
  140. };
  141. },
  142. props: {
  143. jsfiddle: Object,
  144. default() {
  145. return {};
  146. }
  147. },
  148. methods: {
  149. goJsfiddle() {
  150. const { script, html, style } = this.jsfiddle;
  151. const resourcesTpl = '<scr' + 'ipt src="//unpkg.com/vue/dist/vue.js"></scr' + 'ipt>' +
  152. '\n<scr' + 'ipt src="//unpkg.com/element-ui/lib/index.js"></scr' + 'ipt>';
  153. let jsTpl = (script || '').replace(/export default/, 'var Main =').trim();
  154. let htmlTpl = `${resourcesTpl}\n<div id="app">\n${html.trim()}\n</div>`;
  155. let cssTpl = `@import url("//unpkg.com/element-ui/lib/theme-default/index.css");\n${(style || '').trim()}\n`;
  156. jsTpl = jsTpl
  157. ? jsTpl + '\nvar Ctor = Vue.extend(Main)\nnew Ctor().$mount(\'#app\')'
  158. : 'new Vue().$mount(\'#app\')';
  159. const data = {
  160. js: jsTpl,
  161. css: cssTpl,
  162. html: htmlTpl,
  163. panel_js: 3,
  164. panel_css: 1
  165. };
  166. const form = document.createElement('form');
  167. const node = document.createElement('textarea');
  168. form.method = 'post';
  169. form.action = 'http://jsfiddle.net/api/post/library/pure/';
  170. form.target = '_blank';
  171. for (let name in data) {
  172. node.name = name;
  173. node.value = data[name].toString();
  174. form.appendChild(node.cloneNode());
  175. }
  176. form.submit();
  177. }
  178. },
  179. computed: {
  180. lang() {
  181. return this.$route.path.split('/')[1];
  182. },
  183. langConfig() {
  184. return compoLang.filter(config => config.lang === this.lang)[0]['demo-block'];
  185. },
  186. blockClass() {
  187. return `demo-${ this.lang } demo-${ this.$router.currentRoute.path.split('/').pop() }`;
  188. },
  189. iconClass() {
  190. return this.isExpanded ? 'el-icon-caret-top' : 'el-icon-caret-bottom';
  191. },
  192. controlText() {
  193. return this.isExpanded ? this.langConfig['hide-text'] : this.langConfig['show-text'];
  194. },
  195. codeArea() {
  196. return this.$el.getElementsByClassName('meta')[0];
  197. },
  198. codeAreaHeight() {
  199. if (this.$el.getElementsByClassName('description').length > 0) {
  200. return Math.max(this.$el.getElementsByClassName('description')[0].clientHeight, this.$el.getElementsByClassName('highlight')[0].clientHeight);
  201. }
  202. return this.$el.getElementsByClassName('highlight')[0].clientHeight;
  203. }
  204. },
  205. watch: {
  206. isExpanded(val) {
  207. this.codeArea.style.height = val ? `${ this.codeAreaHeight + 1 }px` : '0';
  208. }
  209. },
  210. mounted() {
  211. this.$nextTick(() => {
  212. let highlight = this.$el.getElementsByClassName('highlight')[0];
  213. if (this.$el.getElementsByClassName('description').length === 0) {
  214. highlight.style.width = '100%';
  215. highlight.borderRight = 'none';
  216. }
  217. });
  218. }
  219. };
  220. </script>