feat(MilkdownEditor): add file upload support for documents and text files
Added new upload functionality to the editor supporting doc/docx/ppt/pptx/pdf/zip/txt/json files. Includes: - New upload button with file input - File type detection utilities (isTextFile, isConvertibleFile) - Initial markdown sync with trailing whitespace normalization - Warning messages for unsupported file types
This commit is contained in:
@@ -32,6 +32,21 @@
|
||||
</div>
|
||||
|
||||
<div class="action-buttons">
|
||||
<button
|
||||
type="button"
|
||||
class="action-btn"
|
||||
:aria-label="t('uploadFile')"
|
||||
:title="t('uploadFile')"
|
||||
@click="triggerFileUpload"
|
||||
>
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
|
||||
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/>
|
||||
<polyline points="14 2 14 8 20 8"/>
|
||||
</svg>
|
||||
<span class="btn-tooltip">{{ t('uploadFile') }}</span>
|
||||
</button>
|
||||
<input type="file" ref="uploadFileInputRef" @change="handleUploadFile" accept="image/*,.doc,.docx,.ppt,.pptx,.pdf,.zip,.txt,.json" style="display:none">
|
||||
|
||||
<button
|
||||
type="button"
|
||||
class="action-btn"
|
||||
@@ -133,7 +148,7 @@
|
||||
|
||||
<script setup>
|
||||
import { onMounted, onUnmounted, ref, computed, watch } from 'vue'
|
||||
import { replaceAll } from '@milkdown/kit/utils'
|
||||
import { replaceAll, replaceRange } from '@milkdown/kit/utils'
|
||||
import { Crepe } from '@milkdown/crepe'
|
||||
import { editorViewCtx, serializerCtx } from '@milkdown/kit/core'
|
||||
import { Selection } from '@milkdown/prose/state'
|
||||
@@ -143,14 +158,17 @@ import { mermaidRenderPreview, codeBlockConfig } from '../plugins/mermaidPlugin'
|
||||
import { fetchSuggestion } from '../utils/api.js'
|
||||
import { useSettingsStore } from '../stores/settings'
|
||||
import { OCR_URL } from '../utils/config.js'
|
||||
import { convertFileToMarkdown } from '../utils/convert.js'
|
||||
import { setOcrCache, clearOcrCache, clearAllOcrCache, IMAGE_SIZE_LIMIT, calculateImageHash, getOcrByHash, setOcrByHash } from '../utils/ocrCache.js'
|
||||
|
||||
const emit = defineEmits(['update:markdown'])
|
||||
const settings = useSettingsStore()
|
||||
const t = (key) => settings.t[key]
|
||||
const initialMarkdown = computed(() => settings.initialMarkdown)
|
||||
|
||||
const root = ref(null)
|
||||
const fileInputRef = ref(null)
|
||||
const uploadFileInputRef = ref(null)
|
||||
const imageInputRef = ref(null)
|
||||
const cameraInputRef = ref(null)
|
||||
const aiEnabled = ref(true)
|
||||
@@ -182,6 +200,48 @@ const objectUrls = new Set()
|
||||
const IMAGE_NODE_TYPES = new Set(['image', 'image-block', 'imageBlock'])
|
||||
const MARKDOWN_EXT_RE = /\.md$/i
|
||||
const IMAGE_EXT_RE = /\.(png|jpe?g|gif|webp|bmp|svg|heic|heif|avif)$/i
|
||||
const CONVERT_EXT_RE = /\.(docx?|pptx?|pdf|zip)$/i
|
||||
const TEXT_EXT_RE = /\.(txt|json)$/i
|
||||
const TEXT_MIME_TYPES = new Set(['text/plain', 'application/json'])
|
||||
const CONVERT_MIME_TYPES = new Set([
|
||||
'application/msword',
|
||||
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
||||
'application/vnd.ms-powerpoint',
|
||||
'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
||||
'application/pdf',
|
||||
'application/zip',
|
||||
'application/x-zip-compressed',
|
||||
])
|
||||
let lastInitialMarkdown = initialMarkdown.value
|
||||
|
||||
const normalizeTrailingWhitespace = (value) => (value || '').replace(/\s+$/, '')
|
||||
|
||||
const syncInitialMarkdown = async (nextValue) => {
|
||||
if (!crepe) {
|
||||
lastInitialMarkdown = nextValue
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const current = await crepe.getMarkdown()
|
||||
const normalizedCurrent = normalizeTrailingWhitespace(current)
|
||||
const normalizedLast = normalizeTrailingWhitespace(lastInitialMarkdown)
|
||||
if (!normalizedCurrent || normalizedCurrent === normalizedLast) {
|
||||
crepe.editor.action(replaceAll(nextValue))
|
||||
}
|
||||
} catch {
|
||||
// Ignore sync errors
|
||||
} finally {
|
||||
lastInitialMarkdown = nextValue
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => settings.initialMarkdown,
|
||||
(nextValue) => {
|
||||
void syncInitialMarkdown(nextValue)
|
||||
}
|
||||
)
|
||||
|
||||
const revokeObjectUrl = (url) => {
|
||||
if (!objectUrls.has(url)) return
|
||||
@@ -312,8 +372,36 @@ const isImageFile = (file) => {
|
||||
return type.startsWith('image/') || IMAGE_EXT_RE.test(name)
|
||||
}
|
||||
|
||||
const isTextFile = (file) => {
|
||||
if (!file) return false
|
||||
const name = (file.name || '').toLowerCase()
|
||||
const type = (file.type || '').toLowerCase()
|
||||
return TEXT_EXT_RE.test(name) || TEXT_MIME_TYPES.has(type)
|
||||
}
|
||||
|
||||
const isConvertibleFile = (file) => {
|
||||
if (!file) return false
|
||||
const name = (file.name || '').toLowerCase()
|
||||
const type = (file.type || '').toLowerCase()
|
||||
return CONVERT_EXT_RE.test(name) || CONVERT_MIME_TYPES.has(type)
|
||||
}
|
||||
|
||||
const warnUnsupportedUploadType = () => {
|
||||
alert(t('uploadFileTypeWarning') || 'Only Markdown (.md) files and image files are supported.')
|
||||
alert(t('uploadMdTypeWarning') || 'Only Markdown (.md) files and image files are supported.')
|
||||
}
|
||||
|
||||
const warnUnsupportedInsertType = () => {
|
||||
alert(t('uploadFileTypeWarning') || 'Unsupported file type. Supported: doc/docx/ppt/pptx/pdf/zip, images, txt/json.')
|
||||
}
|
||||
|
||||
const warnUploadError = (message = '') => {
|
||||
const base = t('uploadFileError') || 'File upload failed.'
|
||||
alert(message ? `${base}\n${message}` : base)
|
||||
}
|
||||
|
||||
const warnConvertError = (message = '') => {
|
||||
const base = t('uploadConvertError') || 'File conversion failed.'
|
||||
alert(message ? `${base}\n${message}` : base)
|
||||
}
|
||||
|
||||
const warnImageTooLarge = () => {
|
||||
@@ -403,7 +491,7 @@ onMounted(async () => {
|
||||
|
||||
crepe = new Crepe({
|
||||
root: root.value,
|
||||
defaultValue: '# Welcome to LLM-IN-TEXT\n\nA instant LLM system\n\nStart your creative work below...',
|
||||
defaultValue: initialMarkdown.value || '',
|
||||
features: {
|
||||
[Crepe.Feature.Latex]: true,
|
||||
[Crepe.Feature.ImageBlock]: true,
|
||||
@@ -591,6 +679,72 @@ const insertImageAtCursor = (src) => {
|
||||
})
|
||||
}
|
||||
|
||||
const insertMarkdownAtCursor = (markdown) => {
|
||||
if (!crepe || !markdown) return
|
||||
crepe.editor.action((ctx) => {
|
||||
const view = ctx.get(editorViewCtx)
|
||||
const { from, to } = view.state.selection
|
||||
replaceRange(markdown, { from, to })(ctx)
|
||||
view.focus()
|
||||
})
|
||||
}
|
||||
|
||||
const buildCodeBlock = (file, text) => {
|
||||
const name = (file?.name || '').toLowerCase()
|
||||
const lang = name.endsWith('.json') ? 'json' : 'text'
|
||||
return `\n\`\`\`${lang}\n${text}\n\`\`\`\n`
|
||||
}
|
||||
|
||||
const triggerFileUpload = () => {
|
||||
uploadFileInputRef.value?.click()
|
||||
}
|
||||
|
||||
const handleUploadFile = async (event) => {
|
||||
const input = event.target
|
||||
const file = input.files?.[0]
|
||||
if (!file) return
|
||||
|
||||
const convertible = isConvertibleFile(file)
|
||||
try {
|
||||
if (isImageFile(file)) {
|
||||
const objectUrl = await prepareImageFile(file)
|
||||
if (objectUrl) {
|
||||
clearCurrentGhost()
|
||||
insertImageAtCursor(objectUrl)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (isTextFile(file)) {
|
||||
const text = await file.text()
|
||||
clearCurrentGhost()
|
||||
insertMarkdownAtCursor(buildCodeBlock(file, text))
|
||||
return
|
||||
}
|
||||
|
||||
if (convertible) {
|
||||
const markdown = await convertFileToMarkdown(file)
|
||||
if (!markdown) {
|
||||
throw new Error('No markdown returned')
|
||||
}
|
||||
clearCurrentGhost()
|
||||
insertMarkdownAtCursor(markdown)
|
||||
return
|
||||
}
|
||||
|
||||
warnUnsupportedInsertType()
|
||||
} catch (e) {
|
||||
const message = e instanceof Error ? e.message : ''
|
||||
if (convertible) {
|
||||
warnConvertError(message)
|
||||
} else {
|
||||
warnUploadError(message)
|
||||
}
|
||||
} finally {
|
||||
input.value = ''
|
||||
}
|
||||
}
|
||||
|
||||
const handleImageUpload = async (event) => {
|
||||
const input = event.target
|
||||
const file = input.files?.[0]
|
||||
|
||||
Reference in New Issue
Block a user