all files / src/components/FileContent/Title/ index.vue

62.5% Statements 5/8
50% Branches 5/10
100% Functions 0/0
62.5% Lines 5/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127                                                                                                                                                                                                                                                   
<template>
  <div class="title" v-if="activeFile">
    <div class="title-main">
      <div class="left">
        <i class="iconfont icon-history" :class="{ active: openHistory }" @click="toggleHistory">
          <Tooltip position="bottom">Toggle versions panel</Tooltip>
        </i>
        <i class="iconfont icon-diff" :class="{ active: openDiff }" @click="toggleDiff">
          <Tooltip position="bottom">Toggle diff display</Tooltip>
        </i>
      </div>
      <h3>
        {{ activeFile.path.directory + '/' }}<b>{{ activeFile.name }}</b>
      </h3>
      <div class="right">
        <i class="iconfont icon-copy" @click="handleCopy">
          <Tooltip position="bottom">Copy to clipboard</Tooltip>
        </i>
        <a :href="contentUri" :download="activeFile.name">
          <i class="iconfont icon-download">
            <Tooltip position="left">Download the file</Tooltip>
          </i>
        </a>
      </div>
    </div>
  </div>
</template>
 
<script>
import { mapGetters } from 'vuex'
import Tooltip from '../../Tooltip'
 
import copyToClipboard from 'copy-to-clipboard'
 
export default {
  name: 'Title',
  components: {
    Tooltip
  },
  props: {
    openHistory: {
      type: Boolean,
      default: true
    },
    openDiff: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    ...mapGetters(['server']),
    ...mapGetters('files', ['activeFile', 'activeIndex']),
    ...mapGetters('project', ['active']),
    contentUri () {
      if (this.activeFile && this.activeFile.versions && this.activeFile.versions.length) {
        return `data:text/plain;charset=utf-8,${encodeURIComponent(this.activeFile.versions[this.activeIndex].content)}`
      } else {
        return `${this.server.domain}/${this.active.slug}/files/${this.activeFile.path.full}`
      }
    }
  },
  methods: {
    // toggle version history panel
    toggleHistory () {
      this.$emit('toggle-history')
    },
 
    // toggle diff display
    toggleDiff () {
      this.$emit('toggle-diff')
    },
 
    // copy content to clipboard
    handleCopy () {
      if (this.activeFile && this.activeFile.versions && this.activeFile.versions.length) {
        copyToClipboard(this.activeFile.versions[this.activeIndex].content)
      } else {
        copyToClipboard(`${this.server.domain}/${this.active.slug}/files/${this.activeFile.path.full}`)
      }
    }
  }
}
</script>
 
<style lang="scss" scoped>
  .title {
    height: 50px;
    .title-main {
      display: flex;
      justify-content: space-between;
      height: 50px;
      h3 {
        margin: 0;
        text-align: center;
        line-height: 50px;
        font-size: 14px;
        font-weight: 300;
        color: rgba(255, 255, 255, 0.3);
        b {
          color: white
        }
      }
      .left, .right {
        flex: 0 0 100px;
        font-size: 0;
        i {
          position: relative;
          display: inline-block;
          width: 50px;
          height: 50px;
          cursor: pointer;
          line-height: 50px;
          font-size: 20px;
          text-align: center;
          color: rgba(255, 255, 255, .4);
          &:hover {
            color: #4bd1c5;
          }
          &.active {
            background: rgb(34, 32, 58);
          }
        }
      }
    }
  }
</style>