feat(exercises): implement inline media support in exercise content
Deploy Development / deploy (push) Successful in 37s
Test Suite / pytest-backend (push) Successful in 25s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 24s
Deploy Development / deploy (push) Successful in 37s
Test Suite / pytest-backend (push) Successful in 25s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 24s
- Added functionality for inline media references in exercise text using `{{exerciseMedia:id}}` syntax, which normalizes to a canonical `<span>` element.
- Updated the frontend to utilize `ExerciseRichTextBlock` for rendering exercise content, allowing for embedded media display.
- Enhanced the Rich Text Editor to support inserting inline media placeholders.
- Version bump to 0.8.60 to reflect these changes in media handling and exercise content management.
This commit is contained in:
1 parent
f745e5d082
commit
cc51b0f08f
17 files changed
+655
-149
No files matched your search
@@ -3,52 +3,8 @@
|
||||
*/
|
||||
import React from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { sanitizeTrainerHtml } from '../utils/htmlUtils'
|
||||
import { resolveExerciseMediaFileUrl } from '../utils/exerciseMediaUrl'
|
||||
|
||||
function HtmlBlock({ html, className = '' }) {
|
||||
if (!html || !String(html).trim()) return null
|
||||
const safe = sanitizeTrainerHtml(html)
|
||||
return (
|
||||
<div className={`rich-text-content ${className}`} dangerouslySetInnerHTML={{ __html: safe }} />
|
||||
)
|
||||
}
|
||||
|
||||
function MediaBlock({ media, exerciseId }) {
|
||||
if (media.embed_url) {
|
||||
return (
|
||||
<div style={{ marginTop: '0.5rem' }}>
|
||||
<a href={media.embed_url} target="_blank" rel="noreferrer">
|
||||
{media.embed_url}
|
||||
</a>
|
||||
{media.embed_platform && (
|
||||
<span style={{ color: 'var(--text2)', marginLeft: '0.5rem', fontSize: '0.8rem' }}>
|
||||
({media.embed_platform})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const src = resolveExerciseMediaFileUrl(exerciseId, media)
|
||||
if (!src) return null
|
||||
if (media.media_type === 'image' || (media.mime_type && media.mime_type.startsWith('image/'))) {
|
||||
return (
|
||||
<img
|
||||
src={src}
|
||||
alt={media.title || media.original_filename || ''}
|
||||
style={{ maxWidth: '100%', borderRadius: '8px', marginTop: '0.5rem' }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (media.media_type === 'video' || (media.mime_type && media.mime_type.startsWith('video/'))) {
|
||||
return <video src={src} controls style={{ width: '100%', marginTop: '0.5rem', borderRadius: '8px' }} />
|
||||
}
|
||||
return (
|
||||
<a href={src} target="_blank" rel="noreferrer" style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
||||
{media.title || media.original_filename || 'Datei öffnen'}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
import ExerciseRichTextBlock from './ExerciseRichTextBlock'
|
||||
import ExerciseMediaEmbed from './ExerciseMediaEmbed'
|
||||
|
||||
function TagRow({ exercise }) {
|
||||
const tags = []
|
||||
@@ -112,6 +68,7 @@ export default function ExerciseFullContent({ exercise, loading, error, exercise
|
||||
}
|
||||
if (!exercise) return null
|
||||
|
||||
const resolvedId = exercise.id ?? exerciseId
|
||||
const meta = metaParts(exercise)
|
||||
const visibleMedia = (exercise.media || []).filter((m) => {
|
||||
const lc = String(m.asset_lifecycle_state || 'active').toLowerCase()
|
||||
@@ -132,13 +89,13 @@ export default function ExerciseFullContent({ exercise, loading, error, exercise
|
||||
<h3 style={{ fontSize: '0.78rem', textTransform: 'uppercase', color: 'var(--text3)', marginBottom: '8px', letterSpacing: '0.04em' }}>
|
||||
Kurzbeschreibung
|
||||
</h3>
|
||||
<HtmlBlock html={exercise.summary} />
|
||||
<ExerciseRichTextBlock html={exercise.summary} exerciseId={resolvedId} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
{exercise.goal && (
|
||||
<section className="card" style={{ marginTop: '10px', padding: '12px 14px' }}>
|
||||
<h3 style={{ fontSize: '0.78rem', textTransform: 'uppercase', color: 'var(--text3)', marginBottom: '8px' }}>Ziel</h3>
|
||||
<HtmlBlock html={exercise.goal} />
|
||||
<ExerciseRichTextBlock html={exercise.goal} exerciseId={resolvedId} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
{(exercise.equipment || []).length > 0 && (
|
||||
@@ -156,13 +113,13 @@ export default function ExerciseFullContent({ exercise, loading, error, exercise
|
||||
{exercise.preparation && (
|
||||
<section className="card" style={{ marginTop: '10px', padding: '12px 14px' }}>
|
||||
<h3 style={{ fontSize: '0.78rem', textTransform: 'uppercase', color: 'var(--text3)', marginBottom: '8px' }}>Vorbereitung</h3>
|
||||
<HtmlBlock html={exercise.preparation} />
|
||||
<ExerciseRichTextBlock html={exercise.preparation} exerciseId={resolvedId} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
{exercise.execution && (
|
||||
<section className="card" style={{ marginTop: '10px', padding: '12px 14px' }}>
|
||||
<h3 style={{ fontSize: '0.78rem', textTransform: 'uppercase', color: 'var(--text3)', marginBottom: '8px' }}>Ablauf</h3>
|
||||
<HtmlBlock html={exercise.execution} />
|
||||
<ExerciseRichTextBlock html={exercise.execution} exerciseId={resolvedId} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
{visibleMedia.length > 0 && (
|
||||
@@ -179,7 +136,7 @@ export default function ExerciseFullContent({ exercise, loading, error, exercise
|
||||
</p>
|
||||
)}
|
||||
{m.description && <p style={{ color: 'var(--text2)', fontSize: '0.82rem', marginTop: '4px' }}>{m.description}</p>}
|
||||
<MediaBlock media={m} exerciseId={exercise.id ?? exerciseId} />
|
||||
<ExerciseMediaEmbed media={m} exerciseId={resolvedId} />
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
@@ -189,7 +146,7 @@ export default function ExerciseFullContent({ exercise, loading, error, exercise
|
||||
<h3 style={{ fontSize: '0.78rem', textTransform: 'uppercase', color: 'var(--text3)', marginBottom: '8px' }}>
|
||||
Hinweise Trainer (Katalog)
|
||||
</h3>
|
||||
<HtmlBlock html={exercise.trainer_notes} />
|
||||
<ExerciseRichTextBlock html={exercise.trainer_notes} exerciseId={resolvedId} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
{exerciseId != null && (
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import React from 'react'
|
||||
import { resolveExerciseMediaFileUrl } from '../utils/exerciseMediaUrl'
|
||||
|
||||
/**
|
||||
* Ein ausgeliefertes exercise_media für Übungslisten (Liste + Inline gleiche Darstellung).
|
||||
* @param {{ media: object, exerciseId: number }} props
|
||||
*/
|
||||
export default function ExerciseMediaEmbed({ exerciseId, media }) {
|
||||
if (!media || exerciseId == null) return null
|
||||
if (media.embed_url) {
|
||||
return (
|
||||
<div style={{ marginTop: '0.5rem' }}>
|
||||
<a href={media.embed_url} target="_blank" rel="noreferrer">
|
||||
{media.embed_url}
|
||||
</a>
|
||||
{media.embed_platform && (
|
||||
<span style={{ color: 'var(--text2)', marginLeft: '0.5rem', fontSize: '0.8rem' }}>
|
||||
({media.embed_platform})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const src = resolveExerciseMediaFileUrl(exerciseId, media)
|
||||
if (!src) return null
|
||||
if (media.media_type === 'image' || (media.mime_type && media.mime_type.startsWith('image/'))) {
|
||||
return (
|
||||
<img
|
||||
src={src}
|
||||
alt={media.title || media.original_filename || ''}
|
||||
style={{ maxWidth: '100%', borderRadius: '8px', marginTop: '0.5rem' }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (media.media_type === 'video' || (media.mime_type && media.mime_type.startsWith('video/'))) {
|
||||
return <video src={src} controls style={{ width: '100%', marginTop: '0.5rem', borderRadius: '8px' }} />
|
||||
}
|
||||
return (
|
||||
<a href={src} target="_blank" rel="noreferrer" style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
||||
{media.title || media.original_filename || 'Datei öffnen'}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
@@ -4,15 +4,7 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Link } from 'react-router-dom'
|
||||
import api from '../utils/api'
|
||||
import { sanitizeTrainerHtml } from '../utils/htmlUtils'
|
||||
|
||||
function HtmlBlock({ html, className = '' }) {
|
||||
if (!html || !String(html).trim()) return null
|
||||
const safe = sanitizeTrainerHtml(html)
|
||||
return (
|
||||
<div className={`rich-text-content ${className}`} dangerouslySetInnerHTML={{ __html: safe }} />
|
||||
)
|
||||
}
|
||||
import ExerciseRichTextBlock from './ExerciseRichTextBlock'
|
||||
|
||||
function TagMini({ exercise }) {
|
||||
const parts = []
|
||||
@@ -129,7 +121,7 @@ export default function ExercisePeekModal({
|
||||
</div>
|
||||
{variant.description ? (
|
||||
<div style={{ marginTop: 8, fontSize: '0.9rem', color: 'var(--text2)' }}>
|
||||
<HtmlBlock html={variant.description} />
|
||||
<ExerciseRichTextBlock html={variant.description} exerciseId={exercise.id} media={exercise.media} />
|
||||
</div>
|
||||
) : null}
|
||||
{variant.execution_changes ? (
|
||||
@@ -137,14 +129,14 @@ export default function ExercisePeekModal({
|
||||
<h4 style={{ fontSize: '0.85rem', color: 'var(--text3)', marginBottom: 6 }}>
|
||||
Durchführung (Variante)
|
||||
</h4>
|
||||
<HtmlBlock html={variant.execution_changes} />
|
||||
<ExerciseRichTextBlock html={variant.execution_changes} exerciseId={exercise.id} media={exercise.media} />
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{exercise.summary && (
|
||||
<div style={{ fontSize: '0.95rem', color: 'var(--text2)' }}>
|
||||
<HtmlBlock html={exercise.summary} />
|
||||
<ExerciseRichTextBlock html={exercise.summary} exerciseId={exercise.id} media={exercise.media} />
|
||||
</div>
|
||||
)}
|
||||
<TagMini exercise={exercise} />
|
||||
@@ -154,25 +146,25 @@ export default function ExercisePeekModal({
|
||||
{exercise.goal && (
|
||||
<>
|
||||
<h4 style={{ fontSize: '0.85rem', color: 'var(--text3)', marginBottom: 6 }}>Ziel</h4>
|
||||
<HtmlBlock html={exercise.goal} />
|
||||
<ExerciseRichTextBlock html={exercise.goal} exerciseId={exercise.id} media={exercise.media} />
|
||||
</>
|
||||
)}
|
||||
{exercise.preparation && (
|
||||
<>
|
||||
<h4 style={{ fontSize: '0.85rem', color: 'var(--text3)', margin: '14px 0 6px' }}>Vorbereitung</h4>
|
||||
<HtmlBlock html={exercise.preparation} />
|
||||
<ExerciseRichTextBlock html={exercise.preparation} exerciseId={exercise.id} media={exercise.media} />
|
||||
</>
|
||||
)}
|
||||
{exercise.execution && (
|
||||
<>
|
||||
<h4 style={{ fontSize: '0.85rem', color: 'var(--text3)', margin: '14px 0 6px' }}>Ablauf</h4>
|
||||
<HtmlBlock html={exercise.execution} />
|
||||
<ExerciseRichTextBlock html={exercise.execution} exerciseId={exercise.id} media={exercise.media} />
|
||||
</>
|
||||
)}
|
||||
{exercise.trainer_notes && (
|
||||
<>
|
||||
<h4 style={{ fontSize: '0.85rem', color: 'var(--text3)', margin: '14px 0 6px' }}>Trainer-Hinweise</h4>
|
||||
<HtmlBlock html={exercise.trainer_notes} />
|
||||
<ExerciseRichTextBlock html={exercise.trainer_notes} exerciseId={exercise.id} media={exercise.media} />
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
import React, { useMemo } from 'react'
|
||||
import { sanitizeExerciseRichDisplayHtml } from '../utils/exerciseRichTextSanitize'
|
||||
import ExerciseMediaEmbed from './ExerciseMediaEmbed'
|
||||
|
||||
function isTrashHidden(m) {
|
||||
return String(m?.asset_lifecycle_state || 'active').toLowerCase() === 'trash_hidden'
|
||||
}
|
||||
|
||||
function buildVisibleMediaMap(mediaList) {
|
||||
const map = new Map()
|
||||
for (const m of mediaList || []) {
|
||||
if (!m || m.id == null || isTrashHidden(m)) continue
|
||||
map.set(Number(m.id), m)
|
||||
}
|
||||
return map
|
||||
}
|
||||
|
||||
function domToReactNodes(node, exerciseId, mediaById, path) {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
const t = node.textContent
|
||||
return t ? t : null
|
||||
}
|
||||
if (node.nodeType !== Node.ELEMENT_NODE) return null
|
||||
|
||||
const el = node
|
||||
const tag = el.tagName.toLowerCase()
|
||||
const key = path.join('.')
|
||||
|
||||
if (tag === 'span' && el.getAttribute('data-shinkan-exercise-media')) {
|
||||
const raw = el.getAttribute('data-shinkan-exercise-media')
|
||||
const mid = parseInt(raw, 10)
|
||||
if (!Number.isFinite(mid) || mid < 1) {
|
||||
return (
|
||||
<span key={key} className="shinkan-inline-media-missing" style={{ color: 'var(--text3)', fontSize: '0.9em' }}>
|
||||
[Ungültiger Medienverweis]
|
||||
</span>
|
||||
)
|
||||
}
|
||||
const media = mediaById.get(mid)
|
||||
if (!media) {
|
||||
return (
|
||||
<span key={key} className="shinkan-inline-media-missing" style={{ color: 'var(--text3)', fontSize: '0.9em' }}>
|
||||
[Medium nicht verfügbar]
|
||||
</span>
|
||||
)
|
||||
}
|
||||
const lc = String(media.asset_lifecycle_state || 'active').toLowerCase()
|
||||
return (
|
||||
<span key={key} className="shinkan-inline-media-wrap" style={{ display: 'inline-block', verticalAlign: 'top', maxWidth: '100%' }}>
|
||||
{lc === 'trash_soft' && (
|
||||
<span style={{ fontSize: '0.75rem', color: 'var(--danger)', display: 'block', marginBottom: '4px' }}>
|
||||
Dieses Medium ist im Papierkorb.
|
||||
</span>
|
||||
)}
|
||||
<ExerciseMediaEmbed exerciseId={exerciseId} media={media} />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
const children = []
|
||||
const childNodes = Array.from(el.childNodes)
|
||||
childNodes.forEach((ch, i) => {
|
||||
const sub = domToReactNodes(ch, exerciseId, mediaById, [...path, String(i)])
|
||||
if (sub != null && sub !== false) children.push(sub)
|
||||
})
|
||||
|
||||
const props = { key }
|
||||
if (tag === 'a' && el.getAttribute('href')) {
|
||||
props.href = el.getAttribute('href')
|
||||
props.target = '_blank'
|
||||
props.rel = 'noreferrer'
|
||||
}
|
||||
return React.createElement(tag, props, children.length ? children : null)
|
||||
}
|
||||
|
||||
/**
|
||||
* Zentraler Anzeige-Pfad für Übungs-Rich-Text inkl. §11 Inline-Medien.
|
||||
* @param {{ html?: string|null, exerciseId?: number|null, media?: object[]|null, className?: string }} props
|
||||
*/
|
||||
export default function ExerciseRichTextBlock({ html, exerciseId, media, className = '' }) {
|
||||
const safe = useMemo(() => sanitizeExerciseRichDisplayHtml(html), [html])
|
||||
const mediaById = useMemo(() => buildVisibleMediaMap(media), [media])
|
||||
|
||||
const body = useMemo(() => {
|
||||
if (!safe.trim()) return null
|
||||
const tpl = document.createElement('template')
|
||||
tpl.innerHTML = safe
|
||||
const nodes = []
|
||||
Array.from(tpl.content.childNodes).forEach((ch, i) => {
|
||||
const r = domToReactNodes(ch, exerciseId, mediaById, [String(i)])
|
||||
if (r != null) nodes.push(r)
|
||||
})
|
||||
return nodes
|
||||
}, [safe, exerciseId, mediaById])
|
||||
|
||||
if (!body || body.length === 0) return null
|
||||
|
||||
return <div className={`rich-text-content ${className}`.trim()}>{body}</div>
|
||||
}
|
||||
@@ -45,10 +45,50 @@ function normalText() {
|
||||
formatBlock('p')
|
||||
}
|
||||
|
||||
function insertExerciseMediaPlaceholder(editorEl, mediaId) {
|
||||
if (!editorEl || mediaId == null) return
|
||||
const sid = parseInt(String(mediaId), 10)
|
||||
if (!Number.isFinite(sid) || sid < 1) return
|
||||
editorEl.focus()
|
||||
const sel = window.getSelection()
|
||||
if (!sel) return
|
||||
let range = null
|
||||
if (sel?.rangeCount) {
|
||||
try {
|
||||
const r0 = sel.getRangeAt(0)
|
||||
if (editorEl.contains(r0.commonAncestorContainer)) range = r0
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
if (!range) {
|
||||
range = document.createRange()
|
||||
range.selectNodeContents(editorEl)
|
||||
range.collapse(false)
|
||||
}
|
||||
const span = document.createElement('span')
|
||||
span.setAttribute('data-shinkan-exercise-media', String(sid))
|
||||
span.className = 'shinkan-inline-media'
|
||||
span.appendChild(document.createTextNode('\u2060'))
|
||||
range.deleteContents()
|
||||
range.insertNode(span)
|
||||
range.setStartAfter(span)
|
||||
range.collapse(true)
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(range)
|
||||
}
|
||||
|
||||
/**
|
||||
* Leichter WYSIWYG (contenteditable). Wert kommt von außen zuverlässig ins DOM (Edit-Modus).
|
||||
* @param {{ id: number, label: string }[]} [insertExerciseMediaSlots] — §11 Verweise auf exercise_media.id
|
||||
*/
|
||||
export default function RichTextEditor({ value, onChange, placeholder, minHeight = '140px' }) {
|
||||
export default function RichTextEditor({
|
||||
value,
|
||||
onChange,
|
||||
placeholder,
|
||||
minHeight = '140px',
|
||||
insertExerciseMediaSlots,
|
||||
}) {
|
||||
const ref = useRef(null)
|
||||
const [focused, setFocused] = useState(false)
|
||||
|
||||
@@ -98,6 +138,38 @@ export default function RichTextEditor({ value, onChange, placeholder, minHeight
|
||||
}
|
||||
}
|
||||
|
||||
const showMediaPick = Array.isArray(insertExerciseMediaSlots) && insertExerciseMediaSlots.length > 0
|
||||
|
||||
const onInsertExerciseMediaClick = (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const el = ref.current
|
||||
if (!el || !insertExerciseMediaSlots?.length) return
|
||||
let choice = ''
|
||||
if (insertExerciseMediaSlots.length === 1) {
|
||||
choice = String(insertExerciseMediaSlots[0].id)
|
||||
} else {
|
||||
choice = window.prompt(
|
||||
`Medium-ID eingeben oder aus Liste:\n${insertExerciseMediaSlots
|
||||
.slice(0, 30)
|
||||
.map((s) => `${s.id}: ${s.label}`)
|
||||
.join('\n')}`,
|
||||
'',
|
||||
)
|
||||
}
|
||||
const idParsed = parseInt(String(choice).trim(), 10)
|
||||
if (!Number.isFinite(idParsed)) return
|
||||
if (!insertExerciseMediaSlots.some((s) => Number(s.id) === idParsed)) {
|
||||
alert('Diese Übungs-ID ist nicht in der Medienliste.')
|
||||
return
|
||||
}
|
||||
const saved = saveSelectionInside(el)
|
||||
el.focus()
|
||||
restoreSelection(saved)
|
||||
insertExerciseMediaPlaceholder(el, idParsed)
|
||||
sync()
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="rich-text-editor-wrap">
|
||||
<div className="rich-text-toolbar" role="toolbar" aria-label="Formatierung">
|
||||
@@ -133,6 +205,16 @@ export default function RichTextEditor({ value, onChange, placeholder, minHeight
|
||||
<button type="button" className="rte-btn" title="Link einfügen" onMouseDown={onLink}>
|
||||
Link
|
||||
</button>
|
||||
{showMediaPick ? (
|
||||
<button
|
||||
type="button"
|
||||
className="rte-btn"
|
||||
title="Übungs-Medium inline einfügen (§11)"
|
||||
onMouseDown={onInsertExerciseMediaClick}
|
||||
>
|
||||
Bild/Video im Text
|
||||
</button>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
className="rte-btn"
|
||||
|
||||
@@ -1,57 +1,10 @@
|
||||
import React, { useEffect, useState } from 'react'
|
||||
import { Link, useNavigate, useParams } from 'react-router-dom'
|
||||
import api from '../utils/api'
|
||||
import { resolveExerciseMediaFileUrl } from '../utils/exerciseMediaUrl'
|
||||
import { sanitizeTrainerHtml } from '../utils/htmlUtils'
|
||||
import ExerciseRichTextBlock from '../components/ExerciseRichTextBlock'
|
||||
import ExerciseMediaEmbed from '../components/ExerciseMediaEmbed'
|
||||
import { formatSkillLevelSlug } from '../constants/skillLevels'
|
||||
|
||||
function HtmlBlock({ html, className = '' }) {
|
||||
if (!html || !String(html).trim()) return null
|
||||
const safe = sanitizeTrainerHtml(html)
|
||||
return (
|
||||
<div
|
||||
className={`rich-text-content ${className}`}
|
||||
dangerouslySetInnerHTML={{ __html: safe }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
function MediaBlock({ media, exerciseId }) {
|
||||
if (media.embed_url) {
|
||||
return (
|
||||
<div style={{ marginTop: '0.5rem' }}>
|
||||
<a href={media.embed_url} target="_blank" rel="noreferrer">
|
||||
{media.embed_url}
|
||||
</a>
|
||||
{media.embed_platform && (
|
||||
<span style={{ color: 'var(--text2)', marginLeft: '0.5rem', fontSize: '0.8rem' }}>
|
||||
({media.embed_platform})
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
const src = resolveExerciseMediaFileUrl(exerciseId, media)
|
||||
if (!src) return null
|
||||
if (media.media_type === 'image' || (media.mime_type && media.mime_type.startsWith('image/'))) {
|
||||
return (
|
||||
<img
|
||||
src={src}
|
||||
alt={media.title || media.original_filename || ''}
|
||||
style={{ maxWidth: '100%', borderRadius: '8px', marginTop: '0.5rem' }}
|
||||
/>
|
||||
)
|
||||
}
|
||||
if (media.media_type === 'video' || (media.mime_type && media.mime_type.startsWith('video/'))) {
|
||||
return <video src={src} controls style={{ width: '100%', marginTop: '0.5rem', borderRadius: '8px' }} />
|
||||
}
|
||||
return (
|
||||
<a href={src} target="_blank" rel="noreferrer" style={{ display: 'inline-block', marginTop: '0.5rem' }}>
|
||||
{media.title || media.original_filename || 'Datei öffnen'}
|
||||
</a>
|
||||
)
|
||||
}
|
||||
|
||||
function TagRow({ exercise }) {
|
||||
const tags = []
|
||||
;(exercise.focus_areas || []).forEach((f) => {
|
||||
@@ -171,7 +124,7 @@ function ExerciseDetailPage() {
|
||||
<h1 style={{ margin: 0, fontSize: '1.35rem', lineHeight: 1.25 }}>{exercise.title}</h1>
|
||||
{exercise.summary && (
|
||||
<div style={{ marginTop: '10px', color: 'var(--text2)', fontSize: '15px' }}>
|
||||
<HtmlBlock html={exercise.summary} />
|
||||
<ExerciseRichTextBlock html={exercise.summary} exerciseId={exercise.id} media={exercise.media} />
|
||||
</div>
|
||||
)}
|
||||
<TagRow exercise={exercise} />
|
||||
@@ -186,7 +139,7 @@ function ExerciseDetailPage() {
|
||||
{exercise.goal && (
|
||||
<section className="card exercise-detail-section">
|
||||
<h2>Ziel</h2>
|
||||
<HtmlBlock html={exercise.goal} />
|
||||
<ExerciseRichTextBlock html={exercise.goal} exerciseId={exercise.id} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
|
||||
@@ -204,14 +157,14 @@ function ExerciseDetailPage() {
|
||||
{exercise.preparation && (
|
||||
<section className="card exercise-detail-section">
|
||||
<h2>Vorbereitung</h2>
|
||||
<HtmlBlock html={exercise.preparation} />
|
||||
<ExerciseRichTextBlock html={exercise.preparation} exerciseId={exercise.id} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
|
||||
{exercise.execution && (
|
||||
<section className="card exercise-detail-section">
|
||||
<h2>Ablauf</h2>
|
||||
<HtmlBlock html={exercise.execution} />
|
||||
<ExerciseRichTextBlock html={exercise.execution} exerciseId={exercise.id} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
|
||||
@@ -227,7 +180,7 @@ function ExerciseDetailPage() {
|
||||
</p>
|
||||
)}
|
||||
{m.description && <p style={{ color: 'var(--text2)', fontSize: '0.9rem' }}>{m.description}</p>}
|
||||
<MediaBlock media={m} exerciseId={exercise.id} />
|
||||
<ExerciseMediaEmbed media={m} exerciseId={exercise.id} />
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
@@ -236,7 +189,7 @@ function ExerciseDetailPage() {
|
||||
{exercise.trainer_notes && (
|
||||
<section className="card exercise-detail-section">
|
||||
<h2>Hinweise für Trainer</h2>
|
||||
<HtmlBlock html={exercise.trainer_notes} />
|
||||
<ExerciseRichTextBlock html={exercise.trainer_notes} exerciseId={exercise.id} media={exercise.media} />
|
||||
</section>
|
||||
)}
|
||||
|
||||
@@ -307,7 +260,11 @@ function ExerciseDetailPage() {
|
||||
{v.description && <p style={{ color: 'var(--text2)', marginTop: '4px' }}>{v.description}</p>}
|
||||
{v.execution_changes && (
|
||||
<div style={{ marginTop: '8px' }}>
|
||||
<HtmlBlock html={v.execution_changes} />
|
||||
<ExerciseRichTextBlock
|
||||
html={v.execution_changes}
|
||||
exerciseId={exercise.id}
|
||||
media={exercise.media}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -169,7 +169,7 @@ function buildVariantPayloadFromRow(row) {
|
||||
}
|
||||
|
||||
/** Gemeinsame Felder für „Variante bearbeiten“ und „Neue Variante“. */
|
||||
function ExerciseVariantFields({ row, onPatch, prerequisiteOthers, rteMinHeight = '110px' }) {
|
||||
function ExerciseVariantFields({ row, onPatch, prerequisiteOthers, rteMinHeight = '110px', exerciseMediaInsertSlots }) {
|
||||
return (
|
||||
<>
|
||||
<div className="form-row">
|
||||
@@ -198,6 +198,7 @@ function ExerciseVariantFields({ row, onPatch, prerequisiteOthers, rteMinHeight
|
||||
onChange={(html) => onPatch({ execution_changes: html })}
|
||||
placeholder="Was unterscheidet diese Variante? (Listen über Symbolleiste)"
|
||||
minHeight={rteMinHeight}
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: '10px' }}>
|
||||
@@ -459,6 +460,16 @@ function ExerciseFormPage() {
|
||||
const [archiveError, setArchiveError] = useState(null)
|
||||
const [mediaPreview, setMediaPreview] = useState(null)
|
||||
|
||||
const exerciseMediaInsertSlots = useMemo(() => {
|
||||
if (!isEdit) return []
|
||||
return (mediaList || [])
|
||||
.filter((m) => m?.id != null)
|
||||
.map((m) => ({
|
||||
id: m.id,
|
||||
label: (m.title && String(m.title).trim()) || m.original_filename || `Medium #${m.id}`,
|
||||
}))
|
||||
}, [isEdit, mediaList])
|
||||
|
||||
useEffect(() => {
|
||||
const next = {}
|
||||
for (const m of mediaList) {
|
||||
@@ -1040,6 +1051,7 @@ function ExerciseFormPage() {
|
||||
onChange={(html) => updateFormField('summary', html)}
|
||||
placeholder="Kurzbeschreibung (optional)"
|
||||
minHeight="80px"
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1050,6 +1062,7 @@ function ExerciseFormPage() {
|
||||
onChange={(html) => updateFormField('goal', html)}
|
||||
placeholder="Trainingsziel"
|
||||
minHeight="120px"
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1060,6 +1073,7 @@ function ExerciseFormPage() {
|
||||
onChange={(html) => updateFormField('execution', html)}
|
||||
placeholder="Ablauf Schritt für Schritt"
|
||||
minHeight="180px"
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1070,6 +1084,7 @@ function ExerciseFormPage() {
|
||||
onChange={(html) => updateFormField('preparation', html)}
|
||||
placeholder="Matten, Raum, …"
|
||||
minHeight="100px"
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1080,6 +1095,7 @@ function ExerciseFormPage() {
|
||||
onChange={(html) => updateFormField('trainer_notes', html)}
|
||||
placeholder="Sicherheit, Varianten-Hinweise, …"
|
||||
minHeight="100px"
|
||||
insertExerciseMediaSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -1374,6 +1390,7 @@ function ExerciseFormPage() {
|
||||
onPatch={(patch) => setVariantDraft((d) => ({ ...d, ...patch }))}
|
||||
prerequisiteOthers={variants}
|
||||
rteMinHeight="110px"
|
||||
exerciseMediaInsertSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
<button type="submit" className="btn btn-primary" style={{ marginTop: '10px' }} disabled={variantBusy}>
|
||||
{variantBusy ? 'Anlegen…' : 'Variante anlegen'}
|
||||
@@ -1440,6 +1457,7 @@ function ExerciseFormPage() {
|
||||
onPatch={(patch) => updateVariantField(selectedVariantForEdit.id, patch)}
|
||||
prerequisiteOthers={variants.filter((o) => o.id !== selectedVariantForEdit.id)}
|
||||
rteMinHeight="110px"
|
||||
exerciseMediaInsertSlots={exerciseMediaInsertSlots}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
@@ -19,6 +19,7 @@ import MultiSelectCombo from '../components/MultiSelectCombo'
|
||||
import ExerciseFocusRulePicker from '../components/ExerciseFocusRulePicker'
|
||||
import CatalogRulePicker from '../components/CatalogRulePicker'
|
||||
import ExerciseProgressionGraphPanel from '../components/ExerciseProgressionGraphPanel'
|
||||
import ExerciseRichTextBlock from '../components/ExerciseRichTextBlock'
|
||||
import PageSectionNav from '../components/PageSectionNav'
|
||||
import {
|
||||
INITIAL_EXERCISE_LIST_FILTERS,
|
||||
@@ -27,7 +28,7 @@ import {
|
||||
splitMnCatalogRules,
|
||||
splitScalarCatalogRules,
|
||||
} from '../constants/exerciseListFilters'
|
||||
import { sanitizeExerciseRichText, coerceApiNameList } from '../utils/sanitizeHtml'
|
||||
import { coerceApiNameList } from '../utils/sanitizeHtml'
|
||||
import { canUserRequestExerciseDelete } from '../utils/exercisePermissions'
|
||||
|
||||
const PAGE_SIZE = 100
|
||||
@@ -1380,9 +1381,6 @@ function ExercisesListPage() {
|
||||
const focusNames = exerciseFocusNames(exercise)
|
||||
const styleNames = coerceApiNameList(exercise.style_direction_names)
|
||||
const typeNames = coerceApiNameList(exercise.training_type_names)
|
||||
const summaryHtml = exercise.summary
|
||||
? sanitizeExerciseRichText(exercise.summary)
|
||||
: ''
|
||||
return (
|
||||
<div key={exercise.id} className={exerciseCardClassName(exercise, user?.id)}>
|
||||
<div className="exercise-card-layout exercise-card-layout--grow">
|
||||
@@ -1410,11 +1408,14 @@ function ExercisesListPage() {
|
||||
<span key={`tt:${name}`} className="exercise-tag exercise-tag--training">{name}</span>
|
||||
))}
|
||||
</div>
|
||||
{summaryHtml ? (
|
||||
<div
|
||||
className="exercise-card-summary exercise-card-summary--rich"
|
||||
dangerouslySetInnerHTML={{ __html: summaryHtml }}
|
||||
/>
|
||||
{exercise.summary && String(exercise.summary).trim() ? (
|
||||
<div className="exercise-card-summary exercise-card-summary--rich">
|
||||
<ExerciseRichTextBlock
|
||||
html={exercise.summary}
|
||||
exerciseId={exercise.id}
|
||||
media={exercise.media || []}
|
||||
/>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* Sanitizer für Übungs-Rich-HTML inkl. §11 Platzhalter (span data-shinkan-exercise-media).
|
||||
* Restriktiver als sanitizeTrainerHtml: Allowlist für XSS-Minimierung.
|
||||
*/
|
||||
|
||||
const ALLOWED_BLOCK = new Set(['p', 'div', 'br', 'ul', 'ol', 'li', 'h3'])
|
||||
const ALLOWED_INLINE = new Set(['b', 'strong', 'i', 'em', 'u', 'span', 'a'])
|
||||
|
||||
function isHttpsUrl(val) {
|
||||
if (!val || typeof val !== 'string') return false
|
||||
const s = val.trim()
|
||||
return s.startsWith('http://') || s.startsWith('https://')
|
||||
}
|
||||
|
||||
/** Nur für unsere Embed-Markierung: erlaubt data-attribut und optionale Marker-Klasse. */
|
||||
function isInlineExerciseMediaPlaceholderSpan(el) {
|
||||
if (!el?.getAttribute || el.tagName.toLowerCase() !== 'span') return false
|
||||
const raw = el.getAttribute('data-shinkan-exercise-media')
|
||||
if (!raw || !String(raw).trim().match(/^\d+$/)) return false
|
||||
return true
|
||||
}
|
||||
|
||||
function sanitizeAttributes(el, tagLower) {
|
||||
if (tagLower === 'a') {
|
||||
const href = el.getAttribute('href')
|
||||
if (href && isHttpsUrl(href)) {
|
||||
const out = document.createElement('a')
|
||||
out.setAttribute('href', href.trim())
|
||||
return out.attributes
|
||||
}
|
||||
return []
|
||||
}
|
||||
if (tagLower === 'span' && isInlineExerciseMediaPlaceholderSpan(el)) {
|
||||
const out = document.createElement('span')
|
||||
out.setAttribute('data-shinkan-exercise-media', el.getAttribute('data-shinkan-exercise-media').trim())
|
||||
const cls = (el.getAttribute('class') || '').trim().split(/\s+/).filter(Boolean)
|
||||
const keep = cls.filter((c) => c === 'shinkan-inline-media')
|
||||
if (keep.length) out.setAttribute('class', keep.join(' '))
|
||||
return out.attributes
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
function cloneAsAllowed(tagLower, el) {
|
||||
const fresh = document.createElement(tagLower)
|
||||
const attrs = sanitizeAttributes(el, tagLower)
|
||||
for (const a of attrs) {
|
||||
fresh.setAttribute(a.name, a.value)
|
||||
}
|
||||
return fresh
|
||||
}
|
||||
|
||||
function cleanTree(parent) {
|
||||
const nodes = Array.from(parent.childNodes)
|
||||
for (const node of nodes) {
|
||||
if (node.nodeType === Node.TEXT_NODE) continue
|
||||
if (node.nodeType === Node.COMMENT_NODE) {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
if (node.nodeType !== Node.ELEMENT_NODE) {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
const tag = node.tagName.toLowerCase()
|
||||
if (tag === 'script' || tag === 'iframe' || tag === 'object' || tag === 'embed') {
|
||||
parent.removeChild(node)
|
||||
continue
|
||||
}
|
||||
if (ALLOWED_BLOCK.has(tag) || ALLOWED_INLINE.has(tag)) {
|
||||
const repl = cloneAsAllowed(tag, node)
|
||||
while (node.firstChild) {
|
||||
repl.appendChild(node.firstChild)
|
||||
}
|
||||
cleanTree(repl)
|
||||
parent.replaceChild(repl, node)
|
||||
continue
|
||||
}
|
||||
while (node.firstChild) {
|
||||
parent.insertBefore(node.firstChild, node)
|
||||
}
|
||||
parent.removeChild(node)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|null|undefined} html
|
||||
* @returns {string}
|
||||
*/
|
||||
export function sanitizeExerciseRichDisplayHtml(html) {
|
||||
if (html == null || typeof html !== 'string') return ''
|
||||
const trimmed = html.trim()
|
||||
if (!trimmed) return ''
|
||||
|
||||
const tpl = document.createElement('template')
|
||||
tpl.innerHTML = trimmed
|
||||
cleanTree(tpl.content)
|
||||
return tpl.innerHTML
|
||||
}
|
||||
Reference in new issue
Block a user