-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Page.vue
324 lines (272 loc) · 10.1 KB
/
Page.vue
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<template>
<div
class="flex flex-col justify-between flex-1 h-full overflow-hidden"
:class="{
'lg:flex-row': orientation === 'left',
'lg:flex-row-reverse': orientation === 'right',
'lg:flex-col': orientation === 'top',
'lg:flex-col-reverse': orientation === 'bottom',
}"
>
<div
dusk="editors"
ref="editorContainerRef"
class="flex w-full h-full rounded-b-none"
:class="{
'flex-col': ['left', 'right'].includes(orientation),
'divide-ui-gray-700 divide-x-4 flex-row': ['top', 'bottom'].includes(orientation),
}"
>
<Editor
dusk="editor"
ref="editorRefs"
class="w-full h-full overflow-hidden"
v-for="(editor, index) in editors"
v-model="editors[index].value"
:id="editor.id"
:key="index"
:sizes="sizes"
:orientation="orientation"
:tab-size="editor.tabSize"
:language="editor.language"
:can-move-up="index !== 0"
:can-move-down="index !== editors.length - 1"
:can-remove="canRemoveEditor"
:can-toggle-layout="index === 0"
:added="editors[index].added"
:removed="editors[index].removed"
:focused="editors[index].focused"
@add="addEditor"
@remove="removeEditor"
@up="moveEditorUp"
@down="moveEditorDown"
@update:layout="toggleLayout"
@update:reverse="toggleReverse"
@update:tab-size="editors[index].tabSize = $event"
@update:language="editors[index].language = $event"
@update:added="editors[index].added = $event"
@update:removed="editors[index].removed = $event"
@update:focused="editors[index].focused = $event"
/>
</div>
<Preview
dusk="preview"
ref="previewContainerRef"
:code="code"
:languages="languages"
:name="project.tab.name"
:defaults="project.settings"
class="overflow-auto scrollbar-hide"
@update:settings="$emit('update:settings', $event)"
/>
</div>
</template>
<script>
import { v4 as uuid } from 'uuid';
import { XIcon } from 'vue-feather-icons';
import { last, range, defaults, debounce, cloneDeep } from 'lodash';
import useSplitView from '@/composables/useSplitView';
import {
ref,
toRefs,
watch,
nextTick,
computed,
reactive,
useContext,
onMounted,
} from '@nuxtjs/composition-api';
import { useWindowSize, useResizeObserver } from '@vueuse/core';
import usePreferencesStore from '@/composables/usePreferencesStore';
export default {
props: {
project: {
type: Object,
required: true,
},
},
components: { XIcon },
setup(props, { emit }) {
const { $bus } = useContext();
const preferences = usePreferencesStore();
const editorRefs = ref([]);
const editorContainerRef = ref(null);
const previewContainerRef = ref(null);
const { width } = useWindowSize();
const hasSmallScreen = computed(() => width.value <= 1024);
const data = reactive(
defaults(cloneDeep(props.project.page), {
editors: [],
sizes: [40, 60],
editorSizes: [],
previousOrientation: null,
orientation: hasSmallScreen.value ? 'top' : 'left',
})
);
const { sizes, editorSizes, editors, orientation, previousOrientation } = toRefs(data);
const canRemoveEditor = computed(() => editors.value.length > 1);
useResizeObserver(document.body, () => {
// Here we will force portrait mode when screen width is
// small, then restore the users previously saved
// orientation when screen width is increased.
if (hasSmallScreen.value) {
previousOrientation.value = previousOrientation.value ?? orientation.value;
orientation.value = 'top';
} else if (previousOrientation.value) {
const previous = previousOrientation.value;
previousOrientation.value = null;
orientation.value = previous;
}
});
const { init: initEditorSplitView } = useSplitView(
editorRefs,
computed(() => ({
gutterSize: 5,
sizes: editorSizes.value,
onDrag: (values) => (editorSizes.value = values),
direction: ['top', 'bottom'].includes(orientation.value)
? 'horizontal'
: 'vertical',
}))
);
const { init: initPageSplitView } = useSplitView(
[editorContainerRef, previewContainerRef],
computed(() => ({
gutterSize: 5,
sizes: sizes.value,
onDrag: (values) => (sizes.value = values),
direction: ['top', 'bottom'].includes(orientation.value)
? 'vertical'
: 'horizontal',
}))
);
const toggleLayout = () =>
(orientation.value = {
top: 'left',
bottom: 'right',
left: 'top',
right: 'bottom',
}[orientation.value]);
const toggleReverse = () =>
(orientation.value = {
top: 'bottom',
bottom: 'top',
left: 'right',
right: 'left',
}[orientation.value]);
const findEditorIndex = (id) => editors.value.findIndex((editor) => editor.id === id);
const moveEditor = (from, to) => {
const editor = editors.value[from];
editors.value.splice(from, 1);
editors.value.splice(to, 0, editor);
};
const moveEditorUp = (id) => {
const index = findEditorIndex(id);
moveEditor(index, index - 1);
};
const moveEditorDown = (id) => {
const index = findEditorIndex(id);
moveEditor(index, index + 1);
};
const removeEditor = (id) => {
if (!canRemoveEditor.value) {
return;
}
editors.value.splice(findEditorIndex(id), 1);
$bus.$emit('editors:refresh');
};
const makeEditor = () => {
const language = last(editors.value)?.language ?? preferences.editorLanguage;
return {
id: uuid(),
added: [],
removed: [],
focused: [],
language: language,
tabSize: preferences.editorTabSize,
value: preferences.editorInitialValue,
};
};
const addEditor = () => {
if (editors.value.length === 0) {
orientation.value = hasSmallScreen.value ? 'top' : preferences.editorOrientation;
}
editors.value.push(makeEditor());
$bus.$emit('editors:refresh');
};
const stripInitialPhpTag = (value) => value.replace('<?php', '').replace(/(\n*)/, '');
const code = computed(() => {
return editors.value.map(({ id, value, added, removed, focused }) => {
let newValue = preferences.stripIntialPhpTag ? stripInitialPhpTag(value) : value;
let lineOffset = 0;
if (preferences.stripIntialPhpTag) {
const matches = value.replace('<?php', '').match(/(\n+)/);
if (matches) {
lineOffset = matches[0].split(/\n/g).length - 1;
}
}
// prettier-ignore
return {
id,
value: newValue,
added: added?.map(line => line - lineOffset) || [],
removed: removed?.map(line => line - lineOffset) || [],
focused: focused?.map(line => line - lineOffset) || [],
};
});
});
const languages = computed(() =>
editors.value.map(({ id, language }) => ({
id,
name: language,
}))
);
watch(
data,
debounce((data) => emit('update:page', data), 5000),
{ deep: true }
);
watch(editorRefs, (refs) => {
// Here we are calculating the availble size for each
// editor after one has been added or removed, so
// that the size may be distributed equally.
editorSizes.value = range(0, 100, 100 / refs.length).map(() => 100 / refs.length);
initEditorSplitView();
});
watch(orientation, () => {
nextTick(initPageSplitView);
nextTick(initEditorSplitView);
});
watch([orientation, editorSizes], () => $bus.$emit('editors:refresh'));
// Here we are ensuring all editors that have been restored
// from localstorage have any additional properties
// that may have been added with future updates.
editors.value = editors.value.map((editor) => defaults(editor, makeEditor()));
onMounted(async () => {
if (editors.value.length === 0) {
addEditor();
}
initPageSplitView();
initEditorSplitView();
});
return {
code,
sizes,
editors,
addEditor,
languages,
editorRefs,
orientation,
moveEditorUp,
moveEditorDown,
removeEditor,
toggleLayout,
toggleReverse,
findEditorIndex,
canRemoveEditor,
editorContainerRef,
previewContainerRef,
};
},
};
</script>