summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/preview/clientside.vue
blob: 8ad5817b939be1f583b7f23cb5b2c4062b9112ea (plain)
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
128
129
130
131
<script>
import { mapState } from 'vuex';
import _ from 'underscore';
import { Manager } from 'smooshpack';
import Icon from '~/vue_shared/components/icon.vue';

export default {
  components: {
    Icon,
  },
  data() {
    return {
      iframeSrc: '',
    };
  },
  computed: {
    ...mapState(['entries']),
    normalizedEntries() {
      return Object.keys(this.entries).reduce((acc, path) => {
        const file = this.entries[path];

        return {
          ...acc,
          [`/${path}`]: {
            code: file.content || file.raw,
          },
        };
      }, {});
    },
  },
  watch: {
    entries: {
      deep: true,
      handler: 'update',
    },
  },
  mounted() {
    this.manager = new Manager('#ide-preview', {
      files: this.normalizedEntries,
      entry: '/index.js',
      dependencies: {},
    });

    this.iframeSrc = this.manager.bundlerURL;
  },
  methods: {
    update: _.debounce(function throttleUpdate() {
      this.manager.updatePreview({
        files: this.normalizedEntries,
        entry: '/index.js',
        dependencies: {},
      });
    }, 500),
  },
};
</script>

<template>
  <div class="preview h-100 w-100 d-flex flex-column">
    <header class="ide-preview-header d-flex align-items-center">
      <button
        type="button"
        class="d-transparent border-0"
      >
        <icon
          :size="18"
          name="chevron-left"
        />
      </button>
      <button
        type="button"
        class="d-transparent border-0"
      >
        <icon
          :size="18"
          name="chevron-right"
        />
      </button>
      <button
        type="button"
        class="d-transparent border-0"
      >
        <icon
          name="retry"
        />
      </button>
      <input
        v-model="iframeSrc"
        type="text"
        class="form-control bg-white w-100"
        readonly
      />
      <a
        :href="iframeSrc"
        target="_blank"
        rel="noopener noreferrer"
        class="d-transparent border-0"
      >
        <icon
          name="external-link"
        />
      </a>
    </header>
    <div id="ide-preview"></div>
  </div>
</template>

<style scoped>
.preview {
  width: 500px;
  border-left: 1px solid #eaeaea;
}

.ide-preview-header {
  padding: 8px;
  border-bottom: 1px solid #eaeaea;
  background-color: #fafafa;
}

.ide-preview-header button,
.ide-preview-header a {
  height: 16px;
  padding: 0 4px;
  color: #5c5c5c;
}

.ide-preview-header input {
  margin-left: 8px;
  margin-right: 8px;
}
</style>