clientside.vue 4.42 KB
Newer Older
Phil Hughes's avatar
Phil Hughes committed
1 2 3 4
<script>
import { mapActions, mapGetters, mapState } from 'vuex';
import _ from 'underscore';
import { Manager } from 'smooshpack';
5
import { listen } from 'codesandbox-api';
6
import { GlLoadingIcon } from '@gitlab/ui';
Phil Hughes's avatar
Phil Hughes committed
7 8 9 10 11 12 13
import Navigator from './navigator.vue';
import { packageJsonPath } from '../../constants';
import { createPathWithExt } from '../../utils';

export default {
  components: {
    Navigator,
14
    GlLoadingIcon,
Phil Hughes's avatar
Phil Hughes committed
15 16 17 18 19
  },
  data() {
    return {
      manager: {},
      loading: false,
20
      sandpackReady: false,
Phil Hughes's avatar
Phil Hughes committed
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
    };
  },
  computed: {
    ...mapState(['entries', 'promotionSvgPath', 'links']),
    ...mapGetters(['packageJson', 'currentProject']),
    normalizedEntries() {
      return Object.keys(this.entries).reduce((acc, path) => {
        const file = this.entries[path];

        if (file.type === 'tree' || !(file.raw || file.content)) return acc;

        return {
          ...acc,
          [`/${path}`]: {
            code: file.content || file.raw,
          },
        };
      }, {});
    },
    mainEntry() {
      if (!this.packageJson.raw) return false;

      const parsedPackage = JSON.parse(this.packageJson.raw);

      return parsedPackage.main;
    },
    showPreview() {
      return this.mainEntry && !this.loading;
    },
    showEmptyState() {
      return !this.mainEntry && !this.loading;
    },
    showOpenInCodeSandbox() {
      return this.currentProject && this.currentProject.visibility === 'public';
    },
    sandboxOpts() {
      return {
        files: { ...this.normalizedEntries },
        entry: `/${this.mainEntry}`,
        showOpenInCodeSandbox: this.showOpenInCodeSandbox,
      };
    },
  },
  watch: {
    entries: {
      deep: true,
      handler: 'update',
    },
  },
  mounted() {
    this.loading = true;

    return this.loadFileContent(packageJsonPath)
      .then(() => {
        this.loading = false;
      })
      .then(() => this.$nextTick())
      .then(() => this.initPreview());
  },
  beforeDestroy() {
    if (!_.isEmpty(this.manager)) {
      this.manager.listener();
    }
    this.manager = {};

86 87 88 89
    if (this.listener) {
      this.listener();
    }

Phil Hughes's avatar
Phil Hughes committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    clearTimeout(this.timeout);
    this.timeout = null;
  },
  methods: {
    ...mapActions(['getFileData', 'getRawFileData']),
    loadFileContent(path) {
      return this.getFileData({ path, makeFileActive: false }).then(() =>
        this.getRawFileData({ path }),
      );
    },
    initPreview() {
      if (!this.mainEntry) return null;

      return this.loadFileContent(this.mainEntry)
        .then(() => this.$nextTick())
105
        .then(() => {
Phil Hughes's avatar
Phil Hughes committed
106 107 108 109 110
          this.initManager('#ide-preview', this.sandboxOpts, {
            fileResolver: {
              isFile: p => Promise.resolve(!!this.entries[createPathWithExt(p)]),
              readFile: p => this.loadFileContent(createPathWithExt(p)).then(content => content),
            },
111 112 113 114 115 116 117 118 119 120 121 122
          });

          this.listener = listen(e => {
            switch (e.type) {
              case 'done':
                this.sandpackReady = true;
                break;
              default:
                break;
            }
          });
        });
Phil Hughes's avatar
Phil Hughes committed
123 124
    },
    update() {
125 126 127
      if (!this.sandpackReady) return;

      clearTimeout(this.timeout);
Phil Hughes's avatar
Phil Hughes committed
128 129 130 131 132 133 134 135 136

      this.timeout = setTimeout(() => {
        if (_.isEmpty(this.manager)) {
          this.initPreview();

          return;
        }

        this.manager.updatePreview(this.sandboxOpts);
137
      }, 250);
Phil Hughes's avatar
Phil Hughes committed
138 139 140 141 142 143 144 145 146 147 148
    },
    initManager(el, opts, resolver) {
      this.manager = new Manager(el, opts, resolver);
    },
  },
};
</script>

<template>
  <div class="preview h-100 w-100 d-flex flex-column">
    <template v-if="showPreview">
Mike Greiling's avatar
Mike Greiling committed
149
      <navigator :manager="manager" />
Phil Hughes's avatar
Phil Hughes committed
150 151 152 153 154 155 156
      <div id="ide-preview"></div>
    </template>
    <div
      v-else-if="showEmptyState"
      v-once
      class="d-flex h-100 flex-column align-items-center justify-content-center svg-content"
    >
Mike Greiling's avatar
Mike Greiling committed
157 158
      <img :src="promotionSvgPath" :alt="s__('IDE|Live Preview')" width="130" height="100" />
      <h3>{{ s__('IDE|Live Preview') }}</h3>
Phil Hughes's avatar
Phil Hughes committed
159 160 161 162 163 164 165 166 167 168 169 170
      <p class="text-center">
        {{ s__('IDE|Preview your web application using Web IDE client-side evaluation.') }}
      </p>
      <a
        :href="links.webIDEHelpPagePath"
        class="btn btn-primary"
        target="_blank"
        rel="noopener noreferrer"
      >
        {{ s__('IDE|Get started with Live Preview') }}
      </a>
    </div>
Mike Greiling's avatar
Mike Greiling committed
171
    <gl-loading-icon v-else :size="2" class="align-self-center mt-auto mb-auto" />
Phil Hughes's avatar
Phil Hughes committed
172 173
  </div>
</template>