demo-block.vue 7.9 KB

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