-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathFiledisplayWidget.vue
More file actions
167 lines (159 loc) · 4.79 KB
/
FiledisplayWidget.vue
File metadata and controls
167 lines (159 loc) · 4.79 KB
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
<!--
# Copyright 2025 OpenC3, Inc.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
-->
<template>
<v-card :style="computedStyle">
<v-card-title class="d-flex align-center pa-2">
<span class="text-caption text-medium-emphasis font-weight-regular">
{{ filePath }}
</span>
<v-spacer />
<v-btn
size="x-small"
icon="mdi-refresh"
variant="text"
:loading="loading"
@click="fetchFile"
/>
</v-card-title>
<v-divider />
<v-card-text class="pa-0">
<div ref="editor" :style="contentStyle"></div>
</v-card-text>
</v-card>
</template>
<script>
import * as ace from 'ace-builds'
import 'ace-builds/src-min-noconflict/mode-json'
import 'ace-builds/src-min-noconflict/mode-ruby'
import 'ace-builds/src-min-noconflict/mode-python'
import 'ace-builds/src-min-noconflict/mode-text'
import 'ace-builds/src-min-noconflict/theme-twilight'
import { Api } from '@openc3/js-common/services'
import Widget from './Widget'
export default {
mixins: [Widget],
data() {
return {
filePath: '',
fileContent: 'Loading...',
loading: false,
width: 600,
height: 300,
editor: null,
}
},
computed: {
contentStyle() {
return {
width: this.width + 'px',
height: this.height + 'px',
}
},
aceMode() {
const ext = this.filePath.split('.').pop()?.toLowerCase()
const modeMap = {
json: 'ace/mode/json',
rb: 'ace/mode/ruby',
py: 'ace/mode/python',
txt: 'ace/mode/text',
}
return modeMap[ext] || 'ace/mode/text'
},
},
created() {
// Parameter 0: File path (required) e.g. "INST/procedures/test.txt"
// Parameter 1: Width (optional, default 600)
// Parameter 2: Height (optional, default 300)
if (this.parameters[0]) {
this.filePath = this.parameters[0]
}
if (this.parameters[1]) {
this.width = parseInt(this.parameters[1])
}
if (this.parameters[2]) {
this.height = parseInt(this.parameters[2])
}
},
mounted() {
this.editor = ace.edit(this.$refs.editor)
this.editor.setTheme('ace/theme/twilight')
this.editor.setReadOnly(true)
this.editor.setShowPrintMargin(false)
this.editor.setHighlightActiveLine(false)
this.editor.renderer.setShowGutter(false)
this.editor.renderer.setScrollMargin(8, 8, 0, 0)
this.editor.renderer.setPadding(8)
this.editor.setValue(this.fileContent, -1)
if (this.filePath) {
this.fetchFile()
}
},
beforeUnmount() {
if (this.editor) {
this.editor.destroy()
this.editor.container.remove()
}
},
methods: {
updateEditor() {
if (this.editor) {
this.editor.session.setMode(this.aceMode)
this.editor.setValue(this.fileContent, -1)
}
},
async fetchFile() {
if (!this.filePath) {
this.fileContent =
'Error: No file path specified. Usage: FILEDISPLAY "TARGET/path/to/file.txt"'
this.updateEditor()
return
}
this.loading = true
try {
const scope = window.openc3Scope || 'DEFAULT'
// Try targets_modified first, then fall back to targets
// Use Ignore-Errors header to suppress toast for expected 404/500
let objectPath = `${scope}/targets_modified/${this.filePath}`
let response = await Api.get(
`/openc3-api/storage/download_file/${encodeURIComponent(objectPath)}`,
{
params: { bucket: 'OPENC3_CONFIG_BUCKET' },
headers: { 'Ignore-Errors': '404,500' },
},
).catch(() => null)
if (!response || response.status === 404) {
objectPath = `${scope}/targets/${this.filePath}`
response = await Api.get(
`/openc3-api/storage/download_file/${encodeURIComponent(objectPath)}`,
{ params: { bucket: 'OPENC3_CONFIG_BUCKET' } },
)
}
if (response?.data?.contents) {
this.fileContent = atob(response.data.contents)
} else {
this.fileContent = 'Error: Could not load file'
}
} catch (error) {
this.fileContent = `Error: ${error.message || error}`
} finally {
this.loading = false
this.updateEditor()
}
},
},
}
</script>