-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConverterWorkArea.vue
170 lines (161 loc) · 5.05 KB
/
ConverterWorkArea.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
<script setup lang="ts">
import { TheTool } from '~~/code/conversion/thing'
const templates: Record<string, string> = {
typedef: `/**
* @typedef ReportObject Contains information on a report that may be requested.
* @property {string[]} fields
* @property {string} reportName
* @property {string} reportType - Indicates the type of report you are requesting.
* @property {string} dateRangeType The date range your report should encompass.
* @property {Date} [startDate]
* @property {Date} [endDate]
* @property {ReportDownloadFormat} format
* @property {ReportObjectAdditionalHeaders} [additionalHeaders]
* @property {ReportFilter[]} [filters]
*/`,
typedefEnum: '/** @typedef {\'Strawberry\'|\'Grape\'|\'Peach\'} GoodFruit Some of the best fruits. */',
typedefImport: `/** @typedef {import("bull").Job} Job */
/** @typedef {import("handlebars").TemplateDelegate} HandlebarsTemplateDelegate */
/** @typedef {import("./tasks/task-queue")} TaskQueue */`,
constructor: `class AdwordsAccount {
/**
* Creates a new instance of \`AdwordsAccount\`.
*
* @param {string} cid - The Google Ads account ID.
* @param {object} [clientInfo] - Custom credentials to use when authenticating.
*/
constructor(cid, clientInfo = null) {}
}`,
func: `class Blah {
/**
* Gets the \`GoogleAdsCustomer\` info for the provided Google Ads account.
*
* @param {string} [clientCustomerId] - The Google Ads account ID of the account to get info on.
* @returns {Promise<GoogleAdsCustomer[]>}
*/
getCustomers(clientCustomerId) {
return null;
}
}`,
}
const templateOptions = ref([
{
label: '@typedef',
value: 'typedef',
},
{
label: '@typedef Enum',
value: 'typedefEnum',
},
{
label: '@typedef Import',
value: 'typedefImport',
},
{
label: 'constructor',
value: 'constructor',
},
{
label: 'function',
value: 'func',
},
])
const sourceInputText = useStorage<string | undefined>('sourceInput', '')
const selectedTemplate = ref<string | null>(null)
const outputText = computed(() => {
if (!sourceInputText.value)
return ''
const result = TheTool.parseInput(sourceInputText.value)
let output = '';
[
...result.importStatements,
...result.interfaces,
...result.typeStatements,
...result.functions,
].forEach((v, idx, arr) => {
output += `${v}`
if (idx !== arr.length - 1)
output += '\n\n'
})
return output
})
onMounted(() => {
const sourceInput = sourceInputText.value
if (!sourceInput) {
if (selectedTemplate.value)
selectedTemplate.value = 'typedef'
}
else {
// determine if the source code is from a template and select it if it is
const matchingTemplate = Object.keys(templates).find(k => sourceInput === templates[k])
if (matchingTemplate)
selectedTemplate.value = matchingTemplate
}
})
watch(sourceInputText, (newVal) => {
// if there's no template selected, don't do anything
if (!selectedTemplate.value)
return
const templateText = templates[selectedTemplate.value]
const wasTemplateModified = sourceInputText.value && templateText !== sourceInputText.value
if (wasTemplateModified)
selectedTemplate.value = null
})
watch(selectedTemplate, (newVal, oldVal) => {
const oldTemplateText = oldVal ? templates[oldVal] : undefined
const wasTemplateModified = sourceInputText.value && oldTemplateText !== sourceInputText.value
// do nothing if nothing changed (happens upon startup)
if (!newVal && !oldVal)
return
// If the template was modified, we don't want to overwrite the user's changes.
if (oldVal && wasTemplateModified)
return
// if no template was selected
if (!newVal)
sourceInputText.value = ''
else
sourceInputText.value = templates[newVal] || undefined
}, { immediate: true })
</script>
<template>
<div class="converter-work-area" flex h-full overflow-hidden px-1>
<div class="area left-area default-border">
<div flex class="default-border" b-b-1>
<label for="template-select" font-bold p-2>Template</label>
<Dropdown id="template-select" v-model="selectedTemplate" :options="templateOptions" name="template-select" />
</div>
<ClientOnly>
<EditorInput id="inputTx" v-model="sourceInputText" name="inputTx" h-full w-full />
</ClientOnly>
</div>
<div class="middle-area" px-2 grow-0 shrink flex flex-col items-center justify-center gap-5>
<span flex flex-col items-center>
<i i-mdi:arrow-left />
<p>Input</p>
</span>
<span flex flex-col items-center>
<i i-mdi:arrow-right />
<p>Output</p>
</span>
</div>
<div class="area right-area default-border">
<ClientOnly>
<CodeHightlight class="output-tx" h-full w-full :model-value="outputText" />
</ClientOnly>
</div>
</div>
</template>
<style lang="scss">
.converter-work-area {
--at-apply: text-dark dark:text-gray:80;
.default-border {
--at-apply: dark:border-dark-100 border-gray-300;
}
.area {
--at-apply: h-full overflow-hidden flex-1 border-1;
}
.left-area {
--at-apply: flex flex-col;
}
}
</style>