Skip to content

Commit e05fc71

Browse files
authored
Merge pull request #359 from jumpserver/pr@new_terminal@feat_console_stability
fix(chen): sync console input and theme
2 parents 5dbb4de + e897f27 commit e05fc71

2 files changed

Lines changed: 52 additions & 31 deletions

File tree

ui/chen/components/SqlEditor.client.vue

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,17 @@ const emit = defineEmits<{
3535
const colorMode = useColorMode();
3636
const container = ref<HTMLElement | null>(null);
3737
const themeSlot = new Compartment();
38+
const syntaxThemeSlot = new Compartment();
3839
const editableSlot = new Compartment();
3940
const sqlLanguageSlot = new Compartment();
4041
let editor: EditorView | null = null;
42+
let themeObserver: MutationObserver | null = null;
4143
let applyingExternalValue = false;
4244
4345
const editorExtensions: Extension[] = [
4446
basicSetup,
4547
sqlLanguageSlot.of(sql(chenSqlConfig(props.dbType, props.hints))),
46-
createCodeMirrorSyntaxTheme(),
48+
syntaxThemeSlot.of(createCodeMirrorSyntaxTheme()),
4749
EditorView.lineWrapping,
4850
EditorState.tabSize.of(2),
4951
Prec.highest(
@@ -111,6 +113,15 @@ function replaceDocument(value: string) {
111113
replaceChenSqlDocument(editor, value);
112114
}
113115
116+
function refreshTheme() {
117+
editor?.dispatch({
118+
effects: [
119+
themeSlot.reconfigure(createCodeMirrorTheme()),
120+
syntaxThemeSlot.reconfigure(createCodeMirrorSyntaxTheme())
121+
]
122+
});
123+
}
124+
114125
onMounted(() => {
115126
if (!container.value) return;
116127
editor = new EditorView({
@@ -124,6 +135,11 @@ onMounted(() => {
124135
]
125136
})
126137
});
138+
themeObserver = new MutationObserver(refreshTheme);
139+
themeObserver.observe(document.documentElement, {
140+
attributes: true,
141+
attributeFilter: ["class", "data-theme-preset", "style"]
142+
});
127143
});
128144
129145
watch(
@@ -150,15 +166,12 @@ watch(
150166
}
151167
);
152168
153-
watch(
154-
() => colorMode.value,
155-
async () => {
156-
await nextTick();
157-
editor?.dispatch({ effects: themeSlot.reconfigure(createCodeMirrorTheme()) });
158-
}
159-
);
169+
watch(() => colorMode.value, refreshTheme);
160170
161-
onBeforeUnmount(() => editor?.destroy());
171+
onBeforeUnmount(() => {
172+
themeObserver?.disconnect();
173+
editor?.destroy();
174+
});
162175
163176
defineExpose({
164177
focus,

ui/chen/composables/useChenQueryConsole.ts

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -217,32 +217,41 @@ export function useChenQueryConsole(
217217
}
218218
}
219219

220-
function runQueryTab(tab: ChenQueryConsoleTab, selectedSql = "") {
221-
if (tab.state.loading || tab.state.inQuery) return;
222-
const sql = selectedSql || tab.statement;
223-
if (!sql.trim()) return;
224-
dismissQueryMessage(tab);
225-
220+
function sendSql(tab: ChenQueryLikeWorkspaceTab, sql: string) {
226221
if (sql.length <= SQL_CHUNK_SIZE) {
227-
sendConsoleAction(tab, "query_console_action", { action: "run_sql", data: sql });
228-
return;
222+
return sendConsoleAction(tab, "query_console_action", { action: "run_sql", data: sql }) !== false;
229223
}
230224

231225
const total = Math.ceil(sql.length / SQL_CHUNK_SIZE);
232226
for (let index = 0; index < total; index += 1) {
233-
sendConsoleAction(tab, "query_console_action", {
234-
action: "run_sql_chunk",
235-
data: {
236-
chunk: sql.slice(index * SQL_CHUNK_SIZE, (index + 1) * SQL_CHUNK_SIZE),
237-
index,
238-
total
239-
}
240-
});
227+
if (
228+
sendConsoleAction(tab, "query_console_action", {
229+
action: "run_sql_chunk",
230+
data: {
231+
chunk: sql.slice(index * SQL_CHUNK_SIZE, (index + 1) * SQL_CHUNK_SIZE),
232+
index,
233+
total
234+
}
235+
}) === false
236+
) {
237+
return false;
238+
}
241239
}
242-
sendConsoleAction(tab, "query_console_action", {
243-
action: "run_sql_complete",
244-
data: { total }
245-
});
240+
return (
241+
sendConsoleAction(tab, "query_console_action", {
242+
action: "run_sql_complete",
243+
data: { total }
244+
}) !== false
245+
);
246+
}
247+
248+
function runQueryTab(tab: ChenQueryConsoleTab, selectedSql = "") {
249+
if (tab.state.loading || tab.state.inQuery) return;
250+
const sql = selectedSql || tab.statement;
251+
if (!sql.trim()) return;
252+
dismissQueryMessage(tab);
253+
254+
sendSql(tab, sql);
246255
}
247256

248257
function runQueryFile(tab: ChenQueryConsoleTab, path: string) {
@@ -296,8 +305,7 @@ export function useChenQueryConsole(
296305
tab.timelineEntries.splice(0, tab.timelineEntries.length - MAX_CONSOLE_TIMELINE_ENTRIES);
297306
}
298307
tab.activeTimelineEntryId = entry.id;
299-
const sent = sendConsoleAction(tab, "query_console_action", { action: "run_sql", data: sql });
300-
if (sent === false) {
308+
if (!sendSql(tab, sql)) {
301309
entry.status = "error";
302310
entry.completedAt = Date.now();
303311
const message = tab.connectionError || "Console websocket is not connected";

0 commit comments

Comments
 (0)