<template>
  <div
    :class="['van-doc-content', `van-doc-content--${currentPage}`]"
    @click="onClick"
  >
    <slot />
  </div>
</template>

<script>
import { copyToClipboard } from '../../common';

export default {
  name: 'VanDocContent',
  data() {
    return {
      iframeDocument: null,
    };
  },

  computed: {
    currentPage() {
      const { path } = this.$route;
      if (path) {
        return path.split('/').slice(-1)[0];
      }
      return this.$route.name;
    },
  },

  watch: {
    $route(to, from) {
      if (from.path !== to.path) {
        setTimeout(() => {
          this.copyAction();
        });
      }
    },
  },

  mounted() {
    this.copyAction();
  },

  methods: {
    onClick({ target }) {
      if (['H2', 'H3', 'H4', 'H5'].includes(target.tagName)) {
        this.scrollToAnchor(target);
      }
    },

    scrollToAnchor(target) {
      if (target.id) {
        this.$router.push({
          name: this.$route.name,
          hash: '#' + target.id,
        });

        this.syncMobilePos(target.id);
      }
    },

    syncMobilePos(id) {
      // Getting the document at this point is to ensure that the target has been fully rendered.
      if (this.iframeDocument) {
        const target = this.iframeDocument.getElementById(id);
        target && target.scrollIntoView(true);
      } else {
        const iframe = document.querySelector('iframe');
        this.iframeDocument = iframe.contentWindow.document;
      }
    },

    copyAction() {
      const codeBoxes = document.querySelectorAll('.van-doc-card pre code');

      if (!codeBoxes || !codeBoxes.length) {
        return;
      }

      for (let i = 0; i < codeBoxes.length; i++) {
        const item = codeBoxes[i];
        let timer = null;

        item.addEventListener('click', () => {
          if (timer) return;
          const content = item.innerText;
          copyToClipboard(content);
          item.classList.add('code-copy-success');

          timer = setTimeout(() => {
            item.classList.remove('code-copy-success');
            timer = null;
          }, 1400);
        });
      }
    },
  },
};
</script>

<style lang="less">
.van-doc-card {
  margin-bottom: var(--van-doc-padding);
  padding: 28px 28px 32px;
  background-color: var(--van-doc-background-2);
  border-radius: var(--van-doc-border-radius);
  overflow: auto;

  > pre code {
    position: relative;
    padding-right: 30px;
    pointer-events: none;
    overflow: initial;
  }

  > pre:hover {
    code::before {
      position: absolute;
      z-index: 9;
      right: 14px;
      top: 11px;
      width: 20px;
      height: 20px;
      background-position: center;
      background-size: contain;
      background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAADGtJREFUeF7tnW2IXFcZx5+zl6wktssibErjC4J+MvhGUwWrkChUq00VNasfLPiCG1SWPXejQfBDJuAXNbv3mSwGs8W3ighdxb5pUZDuh7aCNiLV5lsRLE1pFiTGNNAxM0fuusF1szv3nDPnnnPPPf+B+TTPefs9z2+fmZvJXEF4gAAI7EhAgA0IgMDOBCAIqgMEhhCAICgPEIAgqAEQsCOADmLHDaMSIQBBEkk0jmlHAILYccOoRAhAkEQSjWPaEYAgdtwwKhECECSRROOYdgQgiB03jEqEAARJJNE4ph0BCGLHDaMSIQBBEkk0jmlHAILYccOoRAhAkEQSjWPaEYAgdtwwKhECECSRROOYdgQgiB03jEqEAATRTHSn0xm/fPnyLUqp8nmT5jCEWRDIsuxir9dbW1paWrMY7nQIBBmCM8/z1yil7iKiTxHRYafkMZkugWUiepCZH9Md4DIOgmxDU0p5KxEdJaIvEtE+l8AxlzWBc0S0zMylMN4eEGQLaimlJKKvQQxvNWi6UCnKN5n5QdOBNvEQZBO1PM9/rpT6hA1IjPFOIGdmrntVCLJBWEp5lohm6gaO+d0RGBsbO7y4uPiouxlvnAmCEJGU8vNE9P06QWPueggIId5dFMUf6pmdKHlBpJR3ENETdQHGvLUTOMfMB+paBYJI+RAR3VMXYMzrhcDRuq5uJS0I3lp5KV4fi9TWRVIXBN3DR/l6WEMpNd3tdldcL5W6IFeI6NWuoWK+IARWmHna9crJCiKlPEhEj7sGivnCEWBm5/XsfMJweMxWnpubOyKEeMBs1Hr0SSJatRiHIfoEukT0Nv3w/0b2+/29rr/gmKwgeZ5/WSn1XcMkfI6Zf2Q4BuEWBPI8f1Ip9R6ToVmW7V9YWDhvMqYqNllBpJQdIjpRBWjz63W0cJP1U4q1fAt8iJmddncIYlB1EMQA1oihEGREgKMORwcZlWC94yFIvXwrZ4cglYiCBk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kSuQmCC);
      cursor: pointer;
      content: '';
      display: block;
      pointer-events: auto;
    }
  }

  > pre .code-copy-success::after {
    content: 'Copied!';
    display: block;
    position: absolute;
    z-index: 9;
    right: -4px;
    top: 0;
    animation: ease-out code-copy-animation 0.2s;
    animation-fill-mode: forwards;
  }

  @keyframes code-copy-animation {
    0% {
      top: 0;
      opacity: 0;
    }

    100% {
      top: -20px;
      opacity: 1;
    }
  }

  > p a,
  > ul a,
  > table a,
  > blockquote a {
    margin: 0 1px;
    color: var(--van-doc-link-color);
    -webkit-font-smoothing: auto;

    &:hover {
      opacity: 0.8;
    }

    &:active {
      opacity: 0.6;
    }
  }

  > h3,
  > h4,
  > h5,
  > h6 {
    font-weight: normal;
    line-height: 1.6;

    &[id] {
      cursor: pointer;
    }
  }

  > h3 {
    margin-bottom: 16px;
    font-weight: 600;
    font-size: 20px;
  }

  > h4 {
    margin: 24px 0 12px;
    font-weight: 600;
    font-size: 18px;
  }

  > h5 {
    margin: 24px 0 12px;
    font-weight: 600;
    font-size: 16px;
  }

  > p {
    margin-top: 16px;
    color: var(--van-doc-text-color-3);
    font-size: 15px;
    line-height: 26px;

    strong {
      color: var(--van-doc-text-color-1);
    }
  }

  > table {
    width: 100%;
    margin-top: 12px;
    color: var(--van-doc-text-color-3);
    font-size: 14px;
    line-height: 1.5;
    border-collapse: collapse;

    th {
      padding: 8px 10px;
      font-weight: 600;
      text-align: left;

      &:first-child {
        padding-left: 0;
      }

      &:last-child {
        padding-right: 0;
      }
    }

    td {
      padding: 8px;
      border-top: 1px solid var(--van-doc-border-color);

      &:first-child {
        padding-left: 0;

        // version tag
        code {
          margin: 0;
          padding: 2px 6px;
          color: var(--van-doc-blue);
          font-weight: 600;
          font-size: 11px;
          background-color: rgba(25, 137, 250, 0.15);
          border-radius: 20px;
        }
      }

      &:last-child {
        padding-right: 0;
      }
    }

    em {
      display: inline-block;
      color: var(--van-doc-green);
      font-size: 14px;
      font-family: var(--van-doc-code-font-family);
      font-style: normal;
      max-width: 300px;
      -webkit-font-smoothing: auto;
    }
  }

  > ul {
    margin: 16px 0 0;
  }

  > ul li,
  > ol li {
    position: relative;
    margin: 5px 0 5px 10px;
    padding-left: 15px;
    color: var(--van-doc-text-color-3);
    font-size: 15px;
    line-height: 26px;

    &::before {
      position: absolute;
      top: 0;
      left: 0;
      box-sizing: border-box;
      width: 6px;
      height: 6px;
      margin-top: 10px;
      border: 1px solid currentColor;
      border-radius: 50%;
      content: '';
    }
  }

  > hr {
    margin: 30px 0;
    border: 0 none;
    border-top: 1px solid #eee;
  }

  > p code,
  > ul code,
  > ol code,
  > table code {
    display: inline;
    margin: 0 2px;
    padding: 3px 7px;
    font-size: 14px;
    word-break: keep-all;
    border-radius: 6px;
    -webkit-font-smoothing: auto;
    font-family: var(--van-doc-code-font-family);
  }

  > blockquote {
    margin: 16px 0 0;
    padding: 16px;
    font-size: 15px;
    line-height: 26px;
    color: var(--van-doc-blockquote-color);
    background-color: var(--van-doc-blockquote-background);
    border-radius: var(--van-doc-border-radius);
  }

  > img,
  > p img {
    width: 100%;
    margin: 16px 0 0;
    border-radius: var(--van-doc-border-radius);
  }
}

.van-doc-content {
  position: relative;
  flex: 1;
  padding: 0 0 75px;

  .van-doc-markdown-body {
    padding: var(--van-doc-padding);
    overflow: hidden;

    h1,
    h2 {
      font-weight: normal;
      line-height: 1.5;

      &[id] {
        cursor: pointer;
      }
    }

    h1 {
      margin: 0 0 30px;
      font-size: 34px;
      cursor: default;
    }

    h2 {
      margin: 52px 0 20px;
      font-size: 26px;
    }
  }

  &--changelog {
    strong {
      display: block;
      margin: 24px 0 12px;
      font-weight: 600;
      font-size: 15px;
    }

    h3 {
      + p code {
        margin: 0;
      }

      a {
        color: inherit;
        font-size: 20px;
      }
    }
  }
}
</style>
