Files
baserow/web-frontend/modules/database/components/view/form/FormViewDescription.vue
alamin-br 59da1eb2da feat: rich content in form view descriptions (#1851) (#5707)
* feat: rich text editor for form field description (creator)

* feat: render form field description as rich text (respondent)

* feat: rich text editor for form description (creator)

* feat: render form description as rich text (respondent)

Renders the form-level description read-only via RichTextEditor in
FormViewModeForm.vue (the public standard form), matching the field
description treatment from FormPageField.

FormViewModeSurvey.vue was checked and has no separate form-level
description/title render at all (it goes straight to fields), so no
change was needed there.

* docs: changelog for rich form descriptions (#1851)

* fix: only save form description after an actual edit

saveDescription ran on every editor blur and compared tiptap's
normalized markdown against the raw stored value, so clicking into
a legacy plain-text description (with chars like *, #, %) and away
without editing silently rewrote the stored value. Add a dirty flag
set only by update:modelValue (real edits) so blur without an edit
is a no-op.

Also drops the unused editor-class attribute on the description
RichTextEditor (no matching SCSS rule).

* perf: only mount field description editor when needed (v-if)

* fix: preserve in-progress description edit on external update; add e2e coverage

* fix: click-to-edit toggle for form descriptions; fix cursor reset

Replace the always-editable inline RichTextEditor on the form builder with a
shared FormViewDescription component that renders read-only markdown and mounts
the editor only while editing, behind an edit pencil (matching title/submit).

- Restores the edit affordance that regressed vs the old Editable.
- Passes model-value one-way and reads markdown back on blur, so typing after
  moving the cursor no longer resets to the end.
- No floating menu lingers on the form once editing stops.
- Removes the dirty/save-on-blur logic duplicated across FormViewField and
  FormViewModePreviewForm.

* test: cover form description toggle and save-on-blur

* fix: keep description editor menu stuck to cursor line on scroll

Pass the form preview's scroll container (.form-view__preview) to the editor so
its floating/bubble menu tracks the cursor line while the page scrolls, instead
of falling back to the editor's own non-scrolling root.

* Address PR review feedback (#5707)

- Route public form description render sites through FormViewDescription
- Pass scroll container as a resolver prop instead of a closest() lookup
- Wrap long unbroken words in form and field descriptions
- Use palette color variable for the description placeholder

* fix: clamp form description containers to the form column

The flex editor wrapper inside the inline-block description containers
made them shrink-to-fit their max-content width, letting long words
escape the form column on the public form.
2026-07-17 17:40:40 +02:00

120 lines
3.5 KiB
Vue

<template>
<div class="form-view__description-editor">
<!--
Edit mode. The editor is only mounted while editing, so unselected fields
don't pay the rich-text instantiation cost and no floating menu lingers on
the form. `model-value` is passed one-way as the initial content; the saved
value is always read back with serializeToMarkdown() on blur, which avoids
round-tripping tiptap JSON through the parent and resetting the cursor.
-->
<RichTextEditor
v-if="editing"
ref="editor"
:model-value="buffer"
:editable="true"
:enable-rich-text-formatting="true"
:placeholder="placeholder"
:scrollable-area-element="scrollAreaElement"
@update:model-value="dirty = true"
@blur="stopEditing"
></RichTextEditor>
<template v-else>
<RichTextEditor
v-if="hasValue"
class="form-view__description-rendered"
:model-value="value"
:editable="false"
:enable-rich-text-formatting="true"
></RichTextEditor>
<span
v-else-if="!readOnly"
class="form-view__description-placeholder"
@click="edit"
>{{ placeholder }}</span
>
<a v-if="!readOnly" class="form-view__edit" @click="edit">
<i class="form-view__edit-icon iconoir-edit-pencil"></i>
</a>
</template>
</div>
</template>
<script>
import RichTextEditor from '@baserow/modules/core/components/editor/RichTextEditor.vue'
export default {
name: 'FormViewDescription',
components: { RichTextEditor },
props: {
value: {
type: String,
required: false,
default: '',
},
readOnly: {
type: Boolean,
required: false,
default: false,
},
placeholder: {
type: String,
required: false,
default: '',
},
// Resolver returning the scrollable preview container, provided by the
// component that owns it (FormViewPreview) instead of a DOM lookup here.
getScrollAreaElement: {
type: Function,
required: false,
default: null,
},
},
emits: ['change'],
data() {
return {
editing: false,
dirty: false,
// Initial content handed to the editor when entering edit mode.
buffer: '',
// Scrolling container of the form preview, so the editor's floating/
// bubble menu sticks to the cursor line while the page scrolls.
scrollAreaElement: null,
}
},
computed: {
hasValue() {
return (this.value || '').trim().length > 0
},
},
methods: {
edit() {
if (this.readOnly) {
return
}
// Resolve the form preview's scroll container so the editor menus track
// it. When no resolver is provided the editor falls back to its own root.
this.scrollAreaElement = this.getScrollAreaElement?.() || null
this.buffer = this.value || ''
this.dirty = false
this.editing = true
this.$nextTick(() => {
this.$refs.editor.focus()
})
},
stopEditing() {
// Read the markdown before unmounting the editor. Only save when the
// content actually changed, so opening and closing a legacy description
// doesn't rewrite it (markdown serialization can escape stray characters).
if (this.dirty) {
const markdown = this.$refs.editor.serializeToMarkdown()
if (markdown !== (this.value || '')) {
this.$emit('change', markdown)
}
}
this.editing = false
this.dirty = false
},
},
}
</script>