74 lines
1.8 KiB
Vue
74 lines
1.8 KiB
Vue
<template>
|
|
<t-button v-popup="'代码块'" :variant="active ? 'base' : 'text'" :theme="active ? 'light' : 'default'" @click="openDialog">
|
|
<t-icon name="code" />
|
|
</t-button>
|
|
<t-popup
|
|
v-model="visible"
|
|
placement="bottom"
|
|
height="30vh"
|
|
destroy-on-close
|
|
>
|
|
<t-picker
|
|
title="插入代码块"
|
|
:model-value="pickerValue"
|
|
:columns="pickerColumns"
|
|
@confirm="onConfirm"
|
|
@cancel="visible = false"
|
|
@pick="handlePickerPick"
|
|
/>
|
|
</t-popup>
|
|
</template>
|
|
|
|
<script lang="ts" setup>
|
|
import type { PickerValue } from "tdesign-mobile-vue";
|
|
import { supportedCodeLanguages } from "timi-web/editor";
|
|
import type { TiptapEditorInstance } from "timi-web/editor";
|
|
|
|
defineOptions({
|
|
name: "TiptapCodeBlockButton"
|
|
});
|
|
|
|
const props = defineProps<{
|
|
editor?: TiptapEditorInstance;
|
|
active: boolean;
|
|
}>();
|
|
|
|
// markup-templating 仅作为内部依赖语法,不作为独立选项。
|
|
const OTHER_LANGUAGE_VALUE = "other";
|
|
const codeLanguageOptions = [
|
|
...supportedCodeLanguages,
|
|
{ label: "其他", value: OTHER_LANGUAGE_VALUE }
|
|
];
|
|
|
|
const visible = ref(false);
|
|
const codeLanguage = ref("");
|
|
const pickerValue = computed<PickerValue[]>(() => [codeLanguage.value || OTHER_LANGUAGE_VALUE]);
|
|
const pickerColumns = [codeLanguageOptions];
|
|
|
|
function openDialog() {
|
|
if (!props.editor) {
|
|
return;
|
|
}
|
|
codeLanguage.value = "";
|
|
visible.value = true;
|
|
}
|
|
|
|
function handlePickerPick(values: PickerValue[]) {
|
|
const language = String(values[0] ?? OTHER_LANGUAGE_VALUE);
|
|
codeLanguage.value = language === OTHER_LANGUAGE_VALUE ? "" : language;
|
|
}
|
|
|
|
function onConfirm() {
|
|
if (!props.editor) {
|
|
return;
|
|
}
|
|
const language = codeLanguage.value.trim();
|
|
if (language) {
|
|
props.editor.chain().focus().setCodeBlock({ language }).run();
|
|
} else {
|
|
props.editor.chain().focus().setCodeBlock().run();
|
|
}
|
|
visible.value = false;
|
|
}
|
|
</script>
|