@@ -23,8 +23,10 @@ const props = withDefaults(
2323const emit = defineEmits <{
2424 " update:modelValue" : [value : string ];
2525 validate: [];
26+ error: [message : string ];
2627}>();
2728const editorHost = ref <HTMLElement | null >(null );
29+ const fileInput = ref <HTMLInputElement | null >(null );
2830let view: EditorView | null = null ;
2931
3032const example = ` services:
@@ -93,12 +95,32 @@ function clear() {
9395function handleDrop(event : DragEvent ) {
9496 event .preventDefault ();
9597 const file = event .dataTransfer ?.files ?.[0 ];
96- if (! file || ! / \. ya? ml$ / i .test (file .name )) return ;
98+ if (! file ) return ;
99+ readFile (file );
100+ }
101+ function readFile(file : File ) {
102+ if (! / \. ya? ml$ / i .test (file .name )) {
103+ emit (" error" , " Selecione um arquivo .yml ou .yaml." );
104+ return ;
105+ }
106+ if (file .size > 1024 * 1024 ) {
107+ emit (" error" , " O arquivo Compose deve ter no máximo 1 MB." );
108+ return ;
109+ }
97110 file .text ().then ((value ) => {
98111 setValue (value );
99112 emit (" update:modelValue" , value );
100113 });
101114}
115+ function chooseFile() {
116+ fileInput .value ?.click ();
117+ }
118+ function handleFileInput(event : Event ) {
119+ const input = event .target as HTMLInputElement ;
120+ const file = input .files ?.[0 ];
121+ if (file ) readFile (file );
122+ input .value = " " ;
123+ }
102124onMounted (() => {
103125 if (editorHost .value ) {
104126 view = new EditorView ({
@@ -115,11 +137,21 @@ watch(() => props.modelValue, setValue);
115137 <div class =" compose-editor-shell" >
116138 <div class =" compose-toolbar" >
117139 <span class =" compose-file" >compose.yml</span >
140+ <button type =" button" class =" ghost" @click =" chooseFile" >
141+ Carregar arquivo
142+ </button >
118143 <button type =" button" class =" ghost" @click =" loadExample" >
119144 Inserir exemplo
120145 </button >
121146 <button type =" button" class =" ghost" @click =" clear" >Limpar</button >
122147 </div >
148+ <input
149+ ref =" fileInput"
150+ class =" visually-hidden"
151+ type =" file"
152+ accept =" .yml,.yaml,text/yaml"
153+ @change =" handleFileInput"
154+ />
123155 <div class =" compose-dropzone" @dragover.prevent @drop =" handleDrop" >
124156 <div
125157 ref =" editorHost"
0 commit comments