|
| 1 | +<script setup lang="ts"> |
| 2 | + import { __ } from "@/utils/i18n"; |
| 3 | + import { Button } from '@/components/ui/button'; |
| 4 | + import { Editor, getMarkRange, getMarkType } from "@tiptap/vue-3"; |
| 5 | + import { ref } from "vue"; |
| 6 | + import { Input } from "@/components/ui/input"; |
| 7 | + import { Label } from "@/components/ui/label"; |
| 8 | + import { Toggle } from "@/components/ui/toggle"; |
| 9 | + import { FileCode, ChevronDown } from "lucide-vue-next"; |
| 10 | + import { Popover, PopoverAnchor, PopoverContent, PopoverTrigger } from "@/components/ui/popover"; |
| 11 | + import { useId } from "@/composables/useId"; |
| 12 | + import { FormFieldProps } from "@/form/types"; |
| 13 | + import { FormEditorFieldData } from "@/types"; |
| 14 | +
|
| 15 | + const props = defineProps<FormFieldProps<FormEditorFieldData> & { |
| 16 | + editor: Editor, |
| 17 | + }>(); |
| 18 | +
|
| 19 | + const open = ref(false); |
| 20 | + const language = ref<string | null>(null); |
| 21 | +
|
| 22 | + props.editor.on('transaction', () => { |
| 23 | + language.value = props.editor.getAttributes('codeBlock')?.language; |
| 24 | + }); |
| 25 | +
|
| 26 | + function onOpen() { |
| 27 | + } |
| 28 | +
|
| 29 | + function onHide() { |
| 30 | + props.editor.chain() |
| 31 | + .focus() |
| 32 | + .run(); |
| 33 | + } |
| 34 | +
|
| 35 | + function onSubmit() { |
| 36 | + props.editor.chain().focus().updateAttributes('codeBlock', { language: language.value?.toLowerCase() }).run(); |
| 37 | + } |
| 38 | +
|
| 39 | + function onToggleClick() { |
| 40 | + if(props.editor.isActive('codeBlock')) { |
| 41 | + open.value = true; |
| 42 | + } else { |
| 43 | + props.editor.chain().focus().toggleCodeBlock().run(); |
| 44 | + } |
| 45 | + } |
| 46 | +
|
| 47 | + function onUntoggleCodeBlockClick() { |
| 48 | + if(props.editor.isActive('codeBlock')) { |
| 49 | + props.editor.chain().focus().toggleCodeBlock().run(); |
| 50 | + } else { |
| 51 | + props.editor.chain().focus().run(); |
| 52 | + } |
| 53 | + } |
| 54 | +
|
| 55 | + defineExpose({ |
| 56 | + open, |
| 57 | + }); |
| 58 | +</script> |
| 59 | + |
| 60 | +<template> |
| 61 | + <Popover |
| 62 | + v-model:open="open" |
| 63 | + @update:open="$event ? onOpen() : onHide()" |
| 64 | + :modal="false" |
| 65 | + > |
| 66 | + <PopoverAnchor as-child> |
| 67 | + <Toggle |
| 68 | + :model-value="open || props.editor.isActive('codeBlock')" |
| 69 | + size="sm" |
| 70 | + :disabled="props.field.readOnly" |
| 71 | + :title="__('sharp::form.editor.toolbar.code_block.title')" |
| 72 | + @click="onToggleClick" |
| 73 | + > |
| 74 | + <FileCode class="size-4" /> |
| 75 | + </Toggle> |
| 76 | + </PopoverAnchor> |
| 77 | + |
| 78 | + <PopoverContent class="w-max"> |
| 79 | + <form @submit.prevent="onSubmit()"> |
| 80 | + <div class="flex gap-3 "> |
| 81 | + <Input class="flex-1 min-w-0 w-24" v-model="language" :placeholder="__('sharp::form.editor.dialogs.code_block.language_label')" /> |
| 82 | + <Button type="submit">{{ __('sharp::modals.ok_button') }}</Button> |
| 83 | + </div> |
| 84 | + </form> |
| 85 | + <Button class="mt-3 w-full" variant="outline" size="sm" @click="onUntoggleCodeBlockClick"> |
| 86 | + {{ __('sharp::form.editor.dialogs.code_block.toggle_off') }} |
| 87 | + </Button> |
| 88 | + </PopoverContent> |
| 89 | + </Popover> |
| 90 | +</template> |
0 commit comments