|
6 | 6 | <template #description>
|
7 | 7 | {{ t('settingsPage.editor.description') }}
|
8 | 8 | </template>
|
9 |
| - <Select |
10 |
| - v-model="selectedEditor" |
11 |
| - :options="externalEditors" |
12 |
| - item-value="name" |
13 |
| - :placeholder="t('settingsPage.editor.noEditorSelectedPlaceholder')" |
14 |
| - class="w-300px" |
15 |
| - > |
16 |
| - <template #input-prefix="{ value }"> |
17 |
| - <Icon |
18 |
| - v-if="value" |
19 |
| - :icon="value.icon" |
20 |
| - class="text-md" |
21 |
| - /> |
22 |
| - <Icon |
23 |
| - v-else |
24 |
| - :icon="IconTerminal" |
25 |
| - class="text-gray-600 text-md" |
26 |
| - /> |
27 |
| - </template> |
28 |
| - <template #item-prefix="{value}"> |
29 |
| - <Icon |
30 |
| - :icon="value.icon" |
31 |
| - class="text-md" |
32 |
| - /> |
33 |
| - </template> |
34 |
| - </Select> |
| 9 | + |
| 10 | + <div class="flex items-center"> |
| 11 | + <input |
| 12 | + v-model="editorToUse" |
| 13 | + type="radio" |
| 14 | + class="mr-5px" |
| 15 | + data-cy="use-well-known-editor" |
| 16 | + value="found" |
| 17 | + > |
| 18 | + |
| 19 | + <Select |
| 20 | + :model-value="selectedWellKnownEditor" |
| 21 | + :options="externalEditors" |
| 22 | + item-value="name" |
| 23 | + item-key="id" |
| 24 | + :placeholder="t('settingsPage.editor.noEditorSelectedPlaceholder')" |
| 25 | + class="w-400px" |
| 26 | + @update:model-value="updateEditor" |
| 27 | + > |
| 28 | + <template #input-prefix="{ value }"> |
| 29 | + <Icon |
| 30 | + v-if="value" |
| 31 | + :icon="icons[value.id]" |
| 32 | + class="text-md" |
| 33 | + /> |
| 34 | + <Icon |
| 35 | + v-else |
| 36 | + :icon="IconTerminal" |
| 37 | + class="text-gray-600 text-md" |
| 38 | + /> |
| 39 | + </template> |
| 40 | + <template #item-prefix="{ value }"> |
| 41 | + <Icon |
| 42 | + :icon="value.icon" |
| 43 | + class="text-md" |
| 44 | + /> |
| 45 | + </template> |
| 46 | + </Select> |
| 47 | + </div> |
| 48 | + |
| 49 | + <div class="flex items-center py-2"> |
| 50 | + <input |
| 51 | + v-model="editorToUse" |
| 52 | + type="radio" |
| 53 | + class="mr-5px" |
| 54 | + value="custom" |
| 55 | + data-cy="use-custom-editor" |
| 56 | + > |
| 57 | + |
| 58 | + <div class="w-400px"> |
| 59 | + <Input |
| 60 | + v-model="customBinary" |
| 61 | + input-classes="text-sm" |
| 62 | + placeholder="Custom path..." |
| 63 | + @blur="setCustomBinary" |
| 64 | + > |
| 65 | + <template #prefix> |
| 66 | + <Icon |
| 67 | + :icon="IconTerminal" |
| 68 | + class="text-gray-600 text-md" |
| 69 | + /> |
| 70 | + </template> |
| 71 | + </Input> |
| 72 | + </div> |
| 73 | + </div> |
35 | 74 | </SettingsSection>
|
36 | 75 | </template>
|
37 | 76 |
|
38 | 77 | <script lang="ts" setup>
|
39 |
| -
|
40 |
| -import { ref } from 'vue' |
| 78 | +import { ref, computed, watch, FunctionalComponent, SVGAttributes } from 'vue' |
41 | 79 | import Icon from '@cy/components/Icon.vue'
|
42 | 80 | import SettingsSection from '../SettingsSection.vue'
|
43 | 81 | import { useI18n } from '@cy/i18n'
|
44 | 82 | import Select from '@cy/components/Select.vue'
|
| 83 | +import Input from '@cy/components/Input.vue' |
45 | 84 | import VSCode from '~icons/logos/visual-studio-code'
|
46 | 85 | import Atom from '~icons/logos/atom-icon'
|
47 | 86 | import Webstorm from '~icons/logos/webstorm'
|
48 | 87 | import Vim from '~icons/logos/vim'
|
49 | 88 | import Sublime from '~icons/logos/sublimetext-icon'
|
50 | 89 | import Emacs from '~icons/logos/emacs'
|
51 | 90 | import IconTerminal from '~icons/mdi/terminal'
|
| 91 | +import { gql } from '@urql/core' |
| 92 | +import { SetPreferredEditorBinaryDocument, ExternalEditorSettingsFragment } from '../../generated/graphql' |
| 93 | +import { useMutation } from '@urql/vue' |
| 94 | +
|
| 95 | +// @ts-ignore (lachlan): add all icons for all editors such as RubyMine, etc |
| 96 | +const icons: Record<string, FunctionalComponent<SVGAttributes, {}>> = { |
| 97 | + 'code': VSCode, |
| 98 | + 'webstorm': Webstorm, |
| 99 | + 'atom': Atom, |
| 100 | + 'sublimetext': Sublime, |
| 101 | + 'sublimetext2': Sublime, |
| 102 | + 'sublimetextdev': Sublime, |
| 103 | + 'vim': Vim, |
| 104 | + 'emacs': Emacs, |
| 105 | +} |
| 106 | +
|
| 107 | +const externalEditors = computed(() => { |
| 108 | + return props.gql.localSettings.availableEditors?.map((x) => ({ ...x, icon: icons[x.id] })) || [] |
| 109 | +}) |
| 110 | +
|
| 111 | +gql` |
| 112 | +mutation SetPreferredEditorBinary ($value: String!) { |
| 113 | + setPreferredEditorBinary (value: $value) |
| 114 | +}` |
| 115 | +
|
| 116 | +gql` |
| 117 | +fragment ExternalEditorSettings on Query { |
| 118 | + localSettings { |
| 119 | + availableEditors { |
| 120 | + id |
| 121 | + name |
| 122 | + binary |
| 123 | + } |
| 124 | +
|
| 125 | + preferences { |
| 126 | + preferredEditorBinary |
| 127 | + } |
| 128 | + } |
| 129 | +}` |
| 130 | +
|
| 131 | +const setPreferredEditor = useMutation(SetPreferredEditorBinaryDocument) |
52 | 132 |
|
53 |
| -// TODO, grab these from gql or the user's machine. |
54 |
| -const externalEditors = [ |
55 |
| - { |
56 |
| - name: 'Visual Studio Code', |
57 |
| - key: 'vscode', |
58 |
| - icon: VSCode, |
59 |
| - }, |
60 |
| - { |
61 |
| - name: 'Webstorm', |
62 |
| - key: 'webstorm', |
63 |
| - icon: Webstorm, |
64 |
| - }, |
65 |
| - { |
66 |
| - name: 'Atom', |
67 |
| - key: 'atom', |
68 |
| - icon: Atom, |
69 |
| - }, |
70 |
| - { |
71 |
| - name: 'Sublime Text', |
72 |
| - key: 'sublime', |
73 |
| - icon: Sublime, |
74 |
| - }, |
75 |
| - { |
76 |
| - name: 'Vim', |
77 |
| - key: 'vim', |
78 |
| - icon: Vim, |
79 |
| - }, |
80 |
| - { |
81 |
| - name: 'Emacs', |
82 |
| - key: 'emacs', |
83 |
| - icon: Emacs, |
84 |
| - }, |
85 |
| -] |
| 133 | +const props = defineProps<{ |
| 134 | + gql: ExternalEditorSettingsFragment |
| 135 | +}>() |
86 | 136 |
|
87 | 137 | const { t } = useI18n()
|
88 |
| -const selectedEditor = ref<Record<string, any>>() |
| 138 | +
|
| 139 | +type Editor = ExternalEditorSettingsFragment['localSettings']['availableEditors'][number] |
| 140 | +
|
| 141 | +const customBinary = ref<string>('') |
| 142 | +const selectedWellKnownEditor = ref<Editor>() |
| 143 | +const editorToUse = ref<'found' | 'custom'>('found') |
| 144 | +
|
| 145 | +watch( |
| 146 | + () => props.gql.localSettings.preferences.preferredEditorBinary, |
| 147 | + (perferredEditorBinary) => { |
| 148 | + const isWellKnownEditor = props.gql.localSettings.availableEditors.find((x) => { |
| 149 | + return x.binary === perferredEditorBinary |
| 150 | + }) |
| 151 | +
|
| 152 | + editorToUse.value = isWellKnownEditor ? 'found' : 'custom' |
| 153 | +
|
| 154 | + if (isWellKnownEditor) { |
| 155 | + selectedWellKnownEditor.value = isWellKnownEditor |
| 156 | + } |
| 157 | +
|
| 158 | + if (editorToUse.value === 'custom' && perferredEditorBinary) { |
| 159 | + customBinary.value = perferredEditorBinary |
| 160 | + } |
| 161 | + }, { immediate: true }, |
| 162 | +) |
| 163 | +
|
| 164 | +watch(editorToUse, (val) => { |
| 165 | + if (val === 'custom') { |
| 166 | + setPreferredEditor.executeMutation({ value: customBinary.value }) |
| 167 | + } |
| 168 | +}) |
| 169 | +
|
| 170 | +const setCustomBinary = () => { |
| 171 | + if (editorToUse.value === 'custom') { |
| 172 | + setPreferredEditor.executeMutation({ value: customBinary.value }) |
| 173 | + } |
| 174 | +} |
| 175 | +
|
| 176 | +const updateEditor = (editor: Editor) => { |
| 177 | + if (editorToUse.value !== 'found') { |
| 178 | + editorToUse.value = 'found' |
| 179 | + } |
| 180 | +
|
| 181 | + setPreferredEditor.executeMutation({ value: editor.binary }) |
| 182 | +} |
89 | 183 | </script>
|
0 commit comments