feat(exercises): enhance inline media functionality and update styles
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 24s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 27s
Deploy Development / deploy (push) Successful in 33s
Test Suite / pytest-backend (push) Successful in 24s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 7s
Test Suite / playwright-tests (push) Successful in 27s
- Updated inline media markup to include a new data attribute for media size. - Enhanced the Rich Text Editor to support media size selection when inserting inline media. - Improved CSS styles for inline media display, accommodating different sizes (small, medium, full). - Bumped version to 0.8.62 and updated changelog to reflect these changes.
This commit is contained in:
1 parent
979e328cef
commit
311a106d93
14 files changed
+703
-113
No files matched your search
@@ -1,4 +1,6 @@
|
||||
import React, { useRef, useEffect, useState, useCallback } from 'react'
|
||||
import ExerciseInlineFileMediaModal from './ExerciseInlineFileMediaModal'
|
||||
import ExerciseInlineEmbedModal from './ExerciseInlineEmbedModal'
|
||||
|
||||
function exec(cmd, value = null) {
|
||||
try {
|
||||
@@ -45,10 +47,17 @@ function normalText() {
|
||||
formatBlock('p')
|
||||
}
|
||||
|
||||
function insertExerciseMediaPlaceholder(editorEl, mediaId) {
|
||||
if (!editorEl || mediaId == null) return false
|
||||
function buildInlineExerciseMediaHtml(mediaId, displaySize = 'medium') {
|
||||
const sid = parseInt(String(mediaId), 10)
|
||||
if (!Number.isFinite(sid) || sid < 1) return false
|
||||
if (!Number.isFinite(sid) || sid < 1) return null
|
||||
const sz = ['small', 'medium', 'full'].includes(displaySize) ? displaySize : 'medium'
|
||||
return `<span data-shinkan-exercise-media="${sid}" data-shinkan-exercise-media-size="${sz}" class="shinkan-inline-media">\u2060</span>`
|
||||
}
|
||||
|
||||
function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium') {
|
||||
if (!editorEl || mediaId == null) return false
|
||||
const html = buildInlineExerciseMediaHtml(mediaId, displaySize)
|
||||
if (!html) return false
|
||||
|
||||
editorEl.focus()
|
||||
const sel = window.getSelection()
|
||||
@@ -75,7 +84,6 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId) {
|
||||
sel.addRange(anchor)
|
||||
}
|
||||
|
||||
const html = `<span data-shinkan-exercise-media="${sid}" class="shinkan-inline-media">\u2060</span>`
|
||||
let inserted = false
|
||||
try {
|
||||
inserted = document.execCommand('insertHTML', false, html)
|
||||
@@ -98,10 +106,10 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId) {
|
||||
sel.addRange(range)
|
||||
}
|
||||
try {
|
||||
const span = document.createElement('span')
|
||||
span.setAttribute('data-shinkan-exercise-media', String(sid))
|
||||
span.className = 'shinkan-inline-media'
|
||||
span.appendChild(document.createTextNode('\u2060'))
|
||||
const tpl = document.createElement('template')
|
||||
tpl.innerHTML = html
|
||||
const span = tpl.content.firstChild
|
||||
if (!span) return false
|
||||
range.deleteContents()
|
||||
range.insertNode(span)
|
||||
range.setStartAfter(span)
|
||||
@@ -115,18 +123,22 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Leichter WYSIWYG (contenteditable).
|
||||
* @param {{ inlineExerciseId?: number|null, onExerciseMediaListChanged?: () => Promise<void> }} [extra]
|
||||
*/
|
||||
export default function RichTextEditor({
|
||||
value,
|
||||
onChange,
|
||||
placeholder,
|
||||
minHeight = '140px',
|
||||
insertExerciseMediaSlots,
|
||||
inlineExerciseId = null,
|
||||
onExerciseMediaListChanged,
|
||||
}) {
|
||||
const ref = useRef(null)
|
||||
const pendingRangeRef = useRef(null)
|
||||
const [focused, setFocused] = useState(false)
|
||||
const [fileModalOpen, setFileModalOpen] = useState(false)
|
||||
const [embedModalOpen, setEmbedModalOpen] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current
|
||||
@@ -142,6 +154,41 @@ export default function RichTextEditor({
|
||||
onChange(ref.current.innerHTML)
|
||||
}, [onChange])
|
||||
|
||||
const refreshExerciseMedia = useCallback(async () => {
|
||||
if (onExerciseMediaListChanged) {
|
||||
await onExerciseMediaListChanged()
|
||||
}
|
||||
}, [onExerciseMediaListChanged])
|
||||
|
||||
const stashRangeAndOpen = useCallback((openFn) => (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const el = ref.current
|
||||
pendingRangeRef.current = el ? saveSelectionInside(el) : null
|
||||
openFn()
|
||||
}, [])
|
||||
|
||||
const finalizeInsertFromModal = useCallback(
|
||||
(mediaId, displaySize) => {
|
||||
queueMicrotask(() => {
|
||||
const shell = ref.current
|
||||
if (!shell) return
|
||||
shell.focus()
|
||||
restoreSelection(pendingRangeRef.current)
|
||||
const ok = insertExerciseMediaPlaceholder(shell, mediaId, displaySize)
|
||||
if (!ok) {
|
||||
alert(
|
||||
'Einfügen ist fehlgeschlagen — bitte Cursor ins Textfeld setzen und den Schalter erneut verwenden.',
|
||||
)
|
||||
return
|
||||
}
|
||||
sync()
|
||||
shell.focus()
|
||||
})
|
||||
},
|
||||
[sync],
|
||||
)
|
||||
|
||||
const run = (fn) => (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
@@ -174,57 +221,7 @@ export default function RichTextEditor({
|
||||
}
|
||||
}
|
||||
|
||||
const showMediaPick = Array.isArray(insertExerciseMediaSlots) && insertExerciseMediaSlots.length > 0
|
||||
|
||||
const onInsertExerciseMediaClick = (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
const el = ref.current
|
||||
const slots = insertExerciseMediaSlots
|
||||
if (!el || !slots?.length) return
|
||||
let choice = ''
|
||||
if (slots.length === 1) {
|
||||
choice = String(slots[0].id)
|
||||
} else {
|
||||
choice =
|
||||
window.prompt(
|
||||
`Medium-ID eingeben oder aus Liste:\n${slots
|
||||
.slice(0, 30)
|
||||
.map((s) => `${s.id}: ${s.label}`)
|
||||
.join('\n')}`,
|
||||
String(slots[0].id),
|
||||
) ?? ''
|
||||
}
|
||||
const idParsed = parseInt(String(choice).trim(), 10)
|
||||
if (!Number.isFinite(idParsed)) {
|
||||
if (slots.length > 1) {
|
||||
alert('Keine gültige Medium-ID angegeben.')
|
||||
}
|
||||
return
|
||||
}
|
||||
if (!slots.some((s) => Number(s.id) === idParsed)) {
|
||||
alert('Diese ID ist nicht in der Medienliste dieser Übung.')
|
||||
return
|
||||
}
|
||||
|
||||
const savedRange = saveSelectionInside(el)
|
||||
|
||||
queueMicrotask(() => {
|
||||
const shell = ref.current
|
||||
if (!shell) return
|
||||
shell.focus()
|
||||
restoreSelection(savedRange)
|
||||
const ok = insertExerciseMediaPlaceholder(shell, idParsed)
|
||||
if (!ok) {
|
||||
alert(
|
||||
'Einfügen ist fehlgeschlagen — bitte einmal ins Textfeld klicken (Cursor setzen), dann „Bild/Video im Text“ erneut.',
|
||||
)
|
||||
return
|
||||
}
|
||||
sync()
|
||||
shell.focus()
|
||||
})
|
||||
}
|
||||
const showInlineToolbar = inlineExerciseId != null && Number(inlineExerciseId) > 0
|
||||
|
||||
return (
|
||||
<div className="rich-text-editor-wrap">
|
||||
@@ -261,15 +258,25 @@ export default function RichTextEditor({
|
||||
<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>
|
||||
{showInlineToolbar ? (
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
className="rte-btn"
|
||||
title="Datei aus Mediathek oder neu hochladen, in den Text einfügen"
|
||||
onMouseDown={stashRangeAndOpen(() => setFileModalOpen(true))}
|
||||
>
|
||||
Medien im Text
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
className="rte-btn"
|
||||
title="Embed-URL hinzufügen und im Text einfügen"
|
||||
onMouseDown={stashRangeAndOpen(() => setEmbedModalOpen(true))}
|
||||
>
|
||||
Embed im Text
|
||||
</button>
|
||||
</>
|
||||
) : null}
|
||||
<button
|
||||
type="button"
|
||||
@@ -297,6 +304,25 @@ export default function RichTextEditor({
|
||||
}}
|
||||
onInput={sync}
|
||||
/>
|
||||
|
||||
{showInlineToolbar ? (
|
||||
<ExerciseInlineFileMediaModal
|
||||
open={fileModalOpen}
|
||||
onClose={() => setFileModalOpen(false)}
|
||||
exerciseId={Number(inlineExerciseId)}
|
||||
onMediaListChanged={refreshExerciseMedia}
|
||||
onInserted={(mid, sz) => finalizeInsertFromModal(mid, sz)}
|
||||
/>
|
||||
) : null}
|
||||
{showInlineToolbar ? (
|
||||
<ExerciseInlineEmbedModal
|
||||
open={embedModalOpen}
|
||||
onClose={() => setEmbedModalOpen(false)}
|
||||
exerciseId={Number(inlineExerciseId)}
|
||||
onMediaListChanged={refreshExerciseMedia}
|
||||
onInserted={(mid, sz) => finalizeInsertFromModal(mid, sz)}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user