diff --git a/src/components/CodeEditor.vue b/src/components/CodeEditor.vue index ebc52a7..af74980 100644 --- a/src/components/CodeEditor.vue +++ b/src/components/CodeEditor.vue @@ -80,7 +80,7 @@ function onReady(payload: { :tabSize="4" :placeholder="props.placeholder" :style="{ - height: props.label ? 'calc(100% - 60px)' : '100%', + height: !!props.label ? 'calc(100% - 60px)' : '100%', fontSize: props.fontSize + 'px', }" @change="onChange" diff --git a/src/composables/code.ts b/src/composables/code.ts index 886c716..0737b17 100644 --- a/src/composables/code.ts +++ b/src/composables/code.ts @@ -24,7 +24,7 @@ export const code = reactive({ export const input = ref(cache.input.value) export const output = ref("") export const status = ref(Status.NotStarted) -export const loading = ref(false) +export const loading = ref(!code.value) export const size = ref(cache.fontsize) watch(size, (value: number) => { @@ -45,7 +45,7 @@ watch( () => code.value, (value: string) => { cache.code[code.language].value = value - if (!code.value) loading.value = true + loading.value = !value }, ) diff --git a/src/composables/helper.ts b/src/composables/helper.ts index 3404263..c5efcaf 100644 --- a/src/composables/helper.ts +++ b/src/composables/helper.ts @@ -1,3 +1,33 @@ import { ref } from "vue" export const insertText = ref("") + +export const cTexts = [ + ",", + ";", + "printf();", + "{\n}", + "int", + "%d", + "if () ", + " else ", +] + +export const pythonTexts = [ + ":", + '""', + " = ", + " == ", + " > ", + " < ", + " != ", + "print()", + "input()", + "if ", + "else:", + "elif ", + "for ", + " in ", + "range():", + "while", +] diff --git a/src/mobile/Content.vue b/src/mobile/Content.vue index cbdbee2..b580e90 100644 --- a/src/mobile/Content.vue +++ b/src/mobile/Content.vue @@ -13,6 +13,7 @@ import { EditorView } from "@codemirror/view" import { insertText } from "../composables/helper" import { whenever } from "@vueuse/core" import { onUnmounted } from "vue" +import { watch } from "vue" let codeEditor: EditorView | null = null @@ -38,8 +39,26 @@ function onReady(view: EditorView) { whenever(insertText, (text: string) => { if (!codeEditor) return codeEditor.dispatch(codeEditor.state.replaceSelection(text)) + codeEditor.focus() // 保持光标选中状态 + // 处理换行或者移动光标 + let delta = 0 + if (text === '""' || text === "''") delta = 1 + if (text[text.length - 1] === ")") delta = 1 + if (text[text.length - 1] === ":" && text[text.length - 2] === ")") { + delta = 2 + } + if (delta > 0) { + const newPos = codeEditor.state.selection.ranges[0].from - delta + codeEditor.dispatch({ + selection: { + anchor: newPos, + head: newPos, + }, + }) + } insertText.value = "" }) + onUnmounted(() => { codeEditor = null }) @@ -53,7 +72,7 @@ onUnmounted(() => { @update:value="onChangeTab" > -
+ { :font-size="size" @ready="onReady" /> -
+
@@ -106,4 +125,7 @@ onUnmounted(() => { .label { font-size: 16px; } +.wrapper { + height: 100%; +} diff --git a/src/mobile/Helper.vue b/src/mobile/Helper.vue index a1c9449..380206f 100644 --- a/src/mobile/Helper.vue +++ b/src/mobile/Helper.vue @@ -1,5 +1,5 @@