-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.vue
157 lines (145 loc) · 3.97 KB
/
App.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
<template>
<div class="builder">
<div class="builder__toolbar">
<Toolbar @run="onActionRun" :actions="toolbarActions" />
<Selector
@select="setWidth($event)"
:options="viewportSizes"
:value="viewportSize"
/>
<Selector
@select="setLayout($event)"
:options="layoutOptions"
:value="layout"
/>
</div>
<div class="builder__canvas">
<div
class="builder__preview"
v-if="
snippet &&
(layout === SetLayoutAction.layouts.both ||
layout === SetLayoutAction.layouts.preview)
"
>
<div
class="builder__preview-frame"
v-html="snippet"
v-bind:style="{ maxWidth: viewportSize }"
></div>
</div>
<div v-if="!snippet" class="builder__greeting">
<h1 class="builder__greeting-title">Let's get started!</h1>
<p class="builder__greeting-text">
Add instructions below to create or update the current markup.<br />
Use Tailwind 2.x for styling.
</p>
</div>
<textarea
class="builder__code"
v-model="snippet"
v-if="
layout === SetLayoutAction.layouts.both ||
layout === SetLayoutAction.layouts.code
"
@change="onSnippetUpdate"
></textarea>
</div>
<form class="builder__instruction" @submit.prevent="onApply">
<textarea
:rows="height"
class="builder__input"
v-model="input"
@keyup="onKeyUp"
></textarea>
<button
class="builder__btn builder__btn-primary"
type="submit"
:disabled="!input || running"
>
<span v-if="running" class="material-icons animate-spin">refresh</span>
<span v-else class="material-icons">send</span>
</button>
<button
v-if="history.length"
class="builder__btn builder__btn-secondary"
@click="undo"
type="button"
>
<span class="material-icons">undo</span>
</button>
</form>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted } from "vue";
import Selector from "./Selector.vue";
import Toolbar from "./Toolbar.vue";
import { select, dispatch, set } from "./state.js";
import {
HistoryLoadAction,
HistorySaveAction,
SetLayoutAction,
UndoAction,
UpdateAction,
SetViewportSizeAction,
PublishAction,
} from "./actions";
import "./effects.js";
const viewportSize = select((s) => s.viewportSize);
const viewportSizes = select((s) => s.viewportSizes);
const layoutOptions = select((s) => s.layoutOptions);
const layout = select((s) => s.layout);
const history = select((s) => s.history);
const input = select((s) => s.input);
const snippet = select((s) => s.snippet);
const running = select((s) => s.running);
const publishing = select((s) => s.publishing);
const saving = select((s) => s.saving);
const height = computed(() => input.value.split("\n").length);
const toolbarActions = computed(() => [
{
id: "save",
label: "Save",
icon: "save",
action: HistorySaveAction,
busy: saving.value,
disabled: !(snippet.value.length || history.value.length),
},
{
id: "publish",
label: "Publish",
icon: "publish",
action: PublishAction,
busy: publishing.value,
disabled: !(snippet.value.length || history.value.length),
},
{
id: 'undo',
label: "",
icon: "undo",
action: UndoAction,
},
]);
function onActionRun(action) {
dispatch(new action.action());
}
function setWidth(value: string) {
dispatch(new SetViewportSizeAction(value));
}
function setLayout(value: string) {
dispatch(new SetLayoutAction(value));
}
async function onSnippetUpdate() {
set("snippet", snippet.value);
}
async function onApply() {
dispatch(new UpdateAction());
}
function onKeyUp(event: KeyboardEvent) {
if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
dispatch(new UpdateAction());
}
}
onMounted(() => dispatch(new HistoryLoadAction()));
</script>