-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCombinedEditorComponent.vue
More file actions
203 lines (180 loc) · 6.23 KB
/
CombinedEditorComponent.vue
File metadata and controls
203 lines (180 loc) · 6.23 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<!--
Main component of the application.
Combines the code editor and the gui editor.
-->
<script lang="ts" setup>
import {computed, onMounted, onUnmounted, ref, watch} from 'vue';
import 'primeicons/primeicons.css';
import SplitterPanel from 'primevue/splitterpanel';
import Splitter from 'primevue/splitter';
import Toolbar from '@/components/toolbar/Toolbar.vue';
import Toast from 'primevue/toast';
import ConfirmDialog from 'primevue/confirmdialog';
import {useToast} from 'primevue/usetoast';
import {useConfirm} from 'primevue/useconfirm';
import {confirmationService} from '@/utility/confirmationService';
import {toastService} from '@/utility/toastService';
import {useAppRouter} from '@/router/router';
import {useDropZone, useWindowSize, watchImmediate} from '@vueuse/core';
import {readFileContentToDataLink} from '@/utility/readFileContent';
import {getDataForMode} from '@/data/useDataLink';
import {useSettings} from '@/settings/useSettings';
import {modeToRoute, SessionMode} from '@/store/sessionMode';
import {useSessionStore} from '@/store/sessionStore';
import type {SettingsInterfacePanels} from '@/settings/settingsTypes';
import {SETTINGS_DATA_DEFAULT} from '@/settings/defaultSettingsData';
import {updateSettingsWithDefaults} from '@/settings/settingsUpdater';
import {panelTypeRegistry} from '@/components/panels/panelTypeRegistry';
const props = defineProps<{
sessionMode: SessionMode;
}>();
const settings = useSettings();
let panelsDefinition: SettingsInterfacePanels = settings.value.panels;
// update panelsDefinition only when underlying data changes. Otherwise, all panels will be rebuilt every time
// any setting is changed, which is not necessary and leads to Ace Editor becoming blank if settings were modified via
// Ace Editor
watchImmediate(
() => settings,
settings => {
const panels = settings.value.panels;
if (JSON.stringify(panels) !== JSON.stringify(panelsDefinition)) {
panelsDefinition = panels;
}
// fix panels if they are not defined
for (let mode of Object.values(SessionMode)) {
if (!panels[mode]) {
panels[mode] = structuredClone(
SETTINGS_DATA_DEFAULT.panels[mode]
) as SettingsInterfacePanels[typeof mode];
}
}
}
);
const panels = computed(() => {
return panelsDefinition[props.sessionMode].map(panel => {
return {
component: panelTypeRegistry.getPanelTypeDefinition(panel.panelType).getComponent(),
sessionMode: panel.mode,
size: panel.size,
};
});
});
const panelsKey = computed(() => JSON.stringify(panels.value));
let {width} = useWindowSize();
function updateMode(newMode: SessionMode) {
const router = useAppRouter();
const route = modeToRoute(newMode);
router.push(route);
}
const toolbarRef = ref();
const mainPanel = ref();
onMounted(() => {
if (!useSessionStore().hasShownInitialDialog) {
toolbarRef.value?.showInitialSchemaDialog();
useSessionStore().hasShownInitialDialog = true;
// update user settings by adding default value for missing fields
// also performs settings migration in case of outdated settings
const userSettings = getDataForMode(SessionMode.Settings).data.value;
const defaultSettings: any = structuredClone(SETTINGS_DATA_DEFAULT);
updateSettingsWithDefaults(userSettings, defaultSettings);
}
});
const {isOverDropZone} = useDropZone(mainPanel, {
dataTypes: ['Files'],
onDrop,
});
watch(isOverDropZone, isOverDropZone => {
// note that we use manual class manipulation here instead of the vue way because we want to avoid
// that vue re-renders the whole editor when the class changes, which completely messes up the editor
if (isOverDropZone) {
mainPanel.value.classList.add('dragover');
} else {
mainPanel.value.classList.remove('dragover');
}
});
function onDrop(files: File[] | null) {
readFileContentToDataLink(files, getDataForMode(props.sessionMode));
}
confirmationService.confirm = useConfirm();
toastService.toast = useToast();
function undo() {
getDataForMode(props.sessionMode).undoManager.undo();
}
function redo() {
getDataForMode(props.sessionMode).undoManager.redo();
}
function isMacOS() {
if ('userAgentData' in navigator) {
return (navigator.userAgentData as {platform: string}).platform === 'macOS';
} else {
return /Mac/i.test(navigator.userAgent);
}
}
// Function to handle keydown events
function handleKeydown(event: KeyboardEvent) {
const isMac = isMacOS();
const undoKeys = isMac ? event.metaKey && event.key === 'z' : event.ctrlKey && event.key === 'z';
const redoKeys = isMac
? event.metaKey && event.shiftKey && event.key === 'z'
: event.ctrlKey && event.key === 'y';
if (undoKeys && !redoKeys) {
event.preventDefault();
undo();
} else if (redoKeys) {
event.preventDefault();
redo();
}
}
// Attach and remove event listeners
onMounted(() => {
window.addEventListener('keydown', handleKeydown);
});
onUnmounted(() => {
window.removeEventListener('keydown', handleKeydown);
});
</script>
<template>
<Toast position="bottom-left" />
<ConfirmDialog />
<div class="w-full h-full flex" style="max-height: 100%">
<main class="flex flex-col w-full h-full">
<!-- toolbar -->
<Toolbar ref="toolbarRef" :current-mode="props.sessionMode" @mode-selected="updateMode" />
<div class="flex-grow overflow-hidden" ref="mainPanel" id="mainpanel">
<Splitter
class="h-full"
style="min-width: 0"
:layout="width < 600 ? 'vertical' : 'horizontal'"
:key="panelsKey">
<SplitterPanel
v-for="(panel, index) in panels"
:key="`${panel.sessionMode}-${index}`"
:min-size="10"
:size="panel.size"
:resizable="true">
<component :is="panel.component" :sessionMode="panel.sessionMode" />
</SplitterPanel>
</Splitter>
</div>
</main>
</div>
</template>
<!--suppress CssUnusedSymbol -->
<style scoped>
#mainpanel.dragover::before {
content: 'Drop files anywhere to load them';
font-size: 24px;
color: #666;
display: block;
position: absolute;
top: 50%;
left: 50%;
pointer-events: none;
transform: translate(-50%, -50%);
z-index: 10;
background: rgba(255, 255, 255, 0.8);
padding: 30px;
border-radius: 4px;
border: 2px solid #111111;
}
</style>