mirror of
https://github.com/baserow/baserow.git
synced 2026-12-06 09:18:47 +00:00
* feat(database): format AI field text output as rich text AI fields with text output can store rich text, reusing the rich long text editor without mentions or images. New AI fields default to it in the create form; existing fields, API clients and old imports stay plain. Rich fields add a one-line formatting instruction to the prompt. Also: - forward row-modal edits of AI fields, which were never saved - keep image markdown as literal text when images are off, instead of reducing it to its alt text and losing the URL on save - escape user file image references as `![alt]\[name]` so a reference nested in another's alt text can't stay live - return 400 instead of 500 when a PATCH would make a primary field rich text Closes #6220 * fix: preserve reference image URLs in rich text Defined Markdown image tokens carry their destination and title separately from the raw reference marker. Keep that data as self-contained literal inline text when image nodes are disabled, and align the preview behavior. Add parser, AI row persistence, and browser regressions while preserving internal file references and the existing inline-image path. * fix: handle AI field changes during generation
109 lines
3.2 KiB
Vue
109 lines
3.2 KiB
Vue
<!-- eslint-disable vue/no-v-html -->
|
|
<template>
|
|
<div
|
|
class="field-rich-text--preview grid-view__cell grid-field-rich-text__cell"
|
|
>
|
|
<div
|
|
class="grid-field-rich-text__cell-content grid-field-rich-text__cell-content--preview"
|
|
v-html="renderFormattedValue()"
|
|
></div>
|
|
</div>
|
|
</template>
|
|
|
|
<script>
|
|
import { parseMarkdown } from '@baserow/modules/core/editor/markdown'
|
|
import {
|
|
IMAGE_PLACEHOLDER,
|
|
replaceImagesWithPlaceholder,
|
|
stripImageUrls,
|
|
trimUnfinishedImageRef,
|
|
} from '@baserow/modules/core/editor/richTextImageUtils'
|
|
|
|
const PREVIEW_LENGTH = 200
|
|
// Bounds the work on every scroll render, with room for long signed image URLs.
|
|
const RAW_PREVIEW_LENGTH = 5000
|
|
|
|
/**
|
|
* The start of the value as the cell shows it, each image as its placeholder.
|
|
*
|
|
* @param {string} value The cell value, with resolved image URLs.
|
|
* @return {string} Markdown of at most PREVIEW_LENGTH chars plus an ellipsis.
|
|
*/
|
|
function previewMarkdown(value) {
|
|
const rawCut = value.length > RAW_PREVIEW_LENGTH
|
|
const visible = replaceImagesWithPlaceholder(
|
|
rawCut ? trimUnfinishedImageRef(value.slice(0, RAW_PREVIEW_LENGTH)) : value
|
|
)
|
|
if (!rawCut && visible.length <= PREVIEW_LENGTH) {
|
|
return visible
|
|
}
|
|
const placeholderStart = visible.lastIndexOf(
|
|
IMAGE_PLACEHOLDER,
|
|
PREVIEW_LENGTH - 1
|
|
)
|
|
const end =
|
|
placeholderStart >= 0 &&
|
|
placeholderStart + IMAGE_PLACEHOLDER.length > PREVIEW_LENGTH
|
|
? placeholderStart
|
|
: PREVIEW_LENGTH
|
|
return `${visible.slice(0, end).trimEnd()}...`
|
|
}
|
|
|
|
/**
|
|
* The start of the value as the cell shows it, each image as its markdown.
|
|
*
|
|
* @param {string} value The cell value, with resolved image URLs.
|
|
* @return {string} Markdown of at most PREVIEW_LENGTH chars plus an ellipsis.
|
|
*/
|
|
function literalPreviewMarkdown(value) {
|
|
const rawCut = value.length > RAW_PREVIEW_LENGTH
|
|
const visible = stripImageUrls(value.slice(0, RAW_PREVIEW_LENGTH))
|
|
return rawCut || visible.length > PREVIEW_LENGTH
|
|
? `${visible.slice(0, PREVIEW_LENGTH).trimEnd()}...`
|
|
: visible
|
|
}
|
|
|
|
export default {
|
|
name: 'FunctionalGridViewFieldRichText',
|
|
props: {
|
|
value: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
workspaceId: {
|
|
type: Number,
|
|
required: true,
|
|
},
|
|
enableMentions: {
|
|
type: Boolean,
|
|
default: true,
|
|
},
|
|
enableImages: {
|
|
type: Boolean,
|
|
default: true,
|
|
},
|
|
},
|
|
methods: {
|
|
renderFormattedValue() {
|
|
const { value, workspaceId, enableMentions, enableImages } = this
|
|
const workspace = this.$store.getters['workspace/get'](workspaceId)
|
|
const loggedUserId = this.$store.getters['auth/getUserId']
|
|
// Until the rows refetch after a field is converted to rich text, the
|
|
// cell can still hold a value of the previous type.
|
|
const textValue = typeof value === 'string' ? value : ''
|
|
// Replacing an image nested in an alt can complete the image around it.
|
|
const markdown = enableImages
|
|
? replaceImagesWithPlaceholder(previewMarkdown(textValue))
|
|
: literalPreviewMarkdown(textValue)
|
|
|
|
return parseMarkdown(markdown, {
|
|
openLinkOnClick: false,
|
|
enableImages: false,
|
|
workspaceUsers: enableMentions && workspace ? workspace.users : null,
|
|
loggedUserId,
|
|
})
|
|
},
|
|
},
|
|
}
|
|
</script>
|