feat(exercises): update inline media functionality and version bump to 0.8.63
Deploy Development / deploy (push) Successful in 34s
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 30s
Deploy Development / deploy (push) Successful in 34s
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 30s
- Incremented application version to 0.8.63 and updated changelog with new features. - Enhanced inline media handling in the Rich Text Editor, including support for captions. - Introduced new CSS styles for improved media display and layout in the editor. - Replaced `ExerciseMediaEmbed` with `ExerciseAttachmentMediaStrip` for better media management in exercise content.
This commit is contained in:
1 parent
311a106d93
commit
337f29401b
13 files changed
+887
-429
No files matched your search
@@ -1,6 +1,11 @@
|
||||
import React, { useRef, useEffect, useState, useCallback } from 'react'
|
||||
import ExerciseInlineFileMediaModal from './ExerciseInlineFileMediaModal'
|
||||
import ExerciseInlineEmbedModal from './ExerciseInlineEmbedModal'
|
||||
import { sanitizeInlineMediaCaption } from '../utils/inlineMediaCaption'
|
||||
import {
|
||||
SHINKAN_EXERCISE_MEDIA_DRAG_MIME,
|
||||
parseExerciseMediaDragPayload,
|
||||
} from '../utils/exerciseInlineMediaRefs'
|
||||
|
||||
function exec(cmd, value = null) {
|
||||
try {
|
||||
@@ -47,16 +52,48 @@ function normalText() {
|
||||
formatBlock('p')
|
||||
}
|
||||
|
||||
function buildInlineExerciseMediaHtml(mediaId, displaySize = 'medium') {
|
||||
function escapeHtmlAttr(s) {
|
||||
return String(s)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/</g, '<')
|
||||
}
|
||||
|
||||
function buildInlineExerciseMediaHtml(mediaId, displaySize = 'medium', caption = '') {
|
||||
const sid = parseInt(String(mediaId), 10)
|
||||
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>`
|
||||
const cap = sanitizeInlineMediaCaption(caption)
|
||||
let attrs = `data-shinkan-exercise-media="${sid}" data-shinkan-exercise-media-size="${sz}" class="shinkan-inline-media"`
|
||||
if (cap) attrs += ` data-shinkan-exercise-media-caption="${escapeHtmlAttr(cap)}"`
|
||||
return `<span ${attrs}>\u2060</span>`
|
||||
}
|
||||
|
||||
function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium') {
|
||||
function patchExitGlyphAfterPlaceholder(editorEl, mediaId, sel) {
|
||||
const sid = String(parseInt(String(mediaId), 10))
|
||||
const hits = editorEl.querySelectorAll(`span.shinkan-inline-media[data-shinkan-exercise-media="${sid}"]`)
|
||||
const span = hits[hits.length - 1]
|
||||
if (!span?.parentNode || !sel) return
|
||||
let n = span.nextSibling
|
||||
if (!n || n.nodeType !== Node.TEXT_NODE || !n.textContent.includes('\u200B')) {
|
||||
const zn = document.createTextNode('\u200B')
|
||||
span.parentNode.insertBefore(zn, span.nextSibling)
|
||||
n = zn
|
||||
}
|
||||
try {
|
||||
const r = document.createRange()
|
||||
r.setStart(n, n.textContent.length)
|
||||
r.collapse(true)
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(r)
|
||||
} catch {
|
||||
/* noop */
|
||||
}
|
||||
}
|
||||
|
||||
function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium', caption = '') {
|
||||
if (!editorEl || mediaId == null) return false
|
||||
const html = buildInlineExerciseMediaHtml(mediaId, displaySize)
|
||||
const html = buildInlineExerciseMediaHtml(mediaId, displaySize, caption)
|
||||
if (!html) return false
|
||||
|
||||
editorEl.focus()
|
||||
@@ -90,7 +127,10 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium
|
||||
} catch {
|
||||
inserted = false
|
||||
}
|
||||
if (inserted) return true
|
||||
if (inserted) {
|
||||
patchExitGlyphAfterPlaceholder(editorEl, mediaId, sel)
|
||||
return true
|
||||
}
|
||||
|
||||
let range = null
|
||||
try {
|
||||
@@ -112,7 +152,9 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium
|
||||
if (!span) return false
|
||||
range.deleteContents()
|
||||
range.insertNode(span)
|
||||
range.setStartAfter(span)
|
||||
const zn = document.createTextNode('\u200B')
|
||||
span.parentNode.insertBefore(zn, span.nextSibling)
|
||||
range.setStart(zn, zn.textContent.length)
|
||||
range.collapse(true)
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(range)
|
||||
@@ -124,7 +166,9 @@ function insertExerciseMediaPlaceholder(editorEl, mediaId, displaySize = 'medium
|
||||
|
||||
/**
|
||||
* Leichter WYSIWYG (contenteditable).
|
||||
* @param {{ inlineExerciseId?: number|null, onExerciseMediaListChanged?: () => Promise<void> }} [extra]
|
||||
* @param {{
|
||||
* linkedExerciseMedia?: object[],
|
||||
* }} [extra]
|
||||
*/
|
||||
export default function RichTextEditor({
|
||||
value,
|
||||
@@ -132,6 +176,7 @@ export default function RichTextEditor({
|
||||
placeholder,
|
||||
minHeight = '140px',
|
||||
inlineExerciseId = null,
|
||||
linkedExerciseMedia = [],
|
||||
onExerciseMediaListChanged,
|
||||
}) {
|
||||
const ref = useRef(null)
|
||||
@@ -140,6 +185,8 @@ export default function RichTextEditor({
|
||||
const [fileModalOpen, setFileModalOpen] = useState(false)
|
||||
const [embedModalOpen, setEmbedModalOpen] = useState(false)
|
||||
|
||||
const showInlineToolbar = inlineExerciseId != null && Number(inlineExerciseId) > 0
|
||||
|
||||
useEffect(() => {
|
||||
const el = ref.current
|
||||
if (!el || focused) return
|
||||
@@ -169,13 +216,13 @@ export default function RichTextEditor({
|
||||
}, [])
|
||||
|
||||
const finalizeInsertFromModal = useCallback(
|
||||
(mediaId, displaySize) => {
|
||||
(mediaId, displaySize, caption) => {
|
||||
queueMicrotask(() => {
|
||||
const shell = ref.current
|
||||
if (!shell) return
|
||||
shell.focus()
|
||||
restoreSelection(pendingRangeRef.current)
|
||||
const ok = insertExerciseMediaPlaceholder(shell, mediaId, displaySize)
|
||||
const ok = insertExerciseMediaPlaceholder(shell, mediaId, displaySize, caption)
|
||||
if (!ok) {
|
||||
alert(
|
||||
'Einfügen ist fehlgeschlagen — bitte Cursor ins Textfeld setzen und den Schalter erneut verwenden.',
|
||||
@@ -189,6 +236,93 @@ export default function RichTextEditor({
|
||||
[sync],
|
||||
)
|
||||
|
||||
const onEditorKeyDown = useCallback(
|
||||
(e) => {
|
||||
const el = ref.current
|
||||
if (!el || e.key !== 'Enter') return
|
||||
const sel = window.getSelection()
|
||||
if (!sel || sel.rangeCount === 0 || !el.contains(sel.focusNode)) return
|
||||
let node = sel.focusNode
|
||||
let elNode = node.nodeType === Node.ELEMENT_NODE ? node : node.parentElement
|
||||
const host = elNode?.closest?.('.shinkan-inline-media')
|
||||
if (!host || !el.contains(host)) return
|
||||
e.preventDefault()
|
||||
patchExitGlyphAfterPlaceholder(el, host.getAttribute('data-shinkan-exercise-media'), sel)
|
||||
try {
|
||||
exec('insertParagraph')
|
||||
} catch {
|
||||
exec('insertLineBreak')
|
||||
}
|
||||
sync()
|
||||
},
|
||||
[sync],
|
||||
)
|
||||
|
||||
const onEditorDragOver = useCallback(
|
||||
(e) => {
|
||||
if (!showInlineToolbar) return
|
||||
const types = e.dataTransfer?.types ? Array.from(e.dataTransfer.types) : []
|
||||
if (types.includes(SHINKAN_EXERCISE_MEDIA_DRAG_MIME)) {
|
||||
e.preventDefault()
|
||||
e.dataTransfer.dropEffect = 'copy'
|
||||
}
|
||||
},
|
||||
[showInlineToolbar],
|
||||
)
|
||||
|
||||
const onEditorDrop = useCallback(
|
||||
(e) => {
|
||||
const el = ref.current
|
||||
if (!el || !showInlineToolbar) return
|
||||
const raw = e.dataTransfer?.getData(SHINKAN_EXERCISE_MEDIA_DRAG_MIME)
|
||||
const parsed = parseExerciseMediaDragPayload(raw)
|
||||
if (!parsed) return
|
||||
e.preventDefault()
|
||||
el.focus()
|
||||
const sel = window.getSelection()
|
||||
if (!sel) return
|
||||
let r = null
|
||||
try {
|
||||
if (document.caretRangeFromPoint) {
|
||||
r = document.caretRangeFromPoint(e.clientX, e.clientY)
|
||||
}
|
||||
} catch {
|
||||
r = null
|
||||
}
|
||||
if (!r) {
|
||||
try {
|
||||
const p = document.caretPositionFromPoint?.(e.clientX, e.clientY)
|
||||
if (p?.offsetNode) {
|
||||
const nr = document.createRange()
|
||||
nr.setStart(p.offsetNode, p.offset)
|
||||
nr.collapse(true)
|
||||
r = nr
|
||||
}
|
||||
} catch {
|
||||
r = null
|
||||
}
|
||||
}
|
||||
if (r && el.contains(r.commonAncestorContainer)) {
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(r)
|
||||
} else {
|
||||
const anchor = document.createRange()
|
||||
try {
|
||||
anchor.selectNodeContents(el)
|
||||
anchor.collapse(false)
|
||||
} catch {
|
||||
return
|
||||
}
|
||||
sel.removeAllRanges()
|
||||
sel.addRange(anchor)
|
||||
}
|
||||
insertExerciseMediaPlaceholder(el, parsed.exerciseMediaId, 'medium', parsed.caption)
|
||||
sync()
|
||||
el.focus()
|
||||
},
|
||||
[sync, showInlineToolbar],
|
||||
)
|
||||
|
||||
const run = (fn) => (e) => {
|
||||
e.preventDefault()
|
||||
e.stopPropagation()
|
||||
@@ -221,8 +355,6 @@ export default function RichTextEditor({
|
||||
}
|
||||
}
|
||||
|
||||
const showInlineToolbar = inlineExerciseId != null && Number(inlineExerciseId) > 0
|
||||
|
||||
return (
|
||||
<div className="rich-text-editor-wrap">
|
||||
<div className="rich-text-toolbar" role="toolbar" aria-label="Formatierung">
|
||||
@@ -303,6 +435,10 @@ export default function RichTextEditor({
|
||||
sync()
|
||||
}}
|
||||
onInput={sync}
|
||||
onKeyDown={onEditorKeyDown}
|
||||
onDragEnter={onEditorDragOver}
|
||||
onDragOver={onEditorDragOver}
|
||||
onDrop={onEditorDrop}
|
||||
/>
|
||||
|
||||
{showInlineToolbar ? (
|
||||
@@ -310,8 +446,9 @@ export default function RichTextEditor({
|
||||
open={fileModalOpen}
|
||||
onClose={() => setFileModalOpen(false)}
|
||||
exerciseId={Number(inlineExerciseId)}
|
||||
linkedExerciseMedia={linkedExerciseMedia}
|
||||
onMediaListChanged={refreshExerciseMedia}
|
||||
onInserted={(mid, sz) => finalizeInsertFromModal(mid, sz)}
|
||||
onInserted={(mid, sz, cap) => finalizeInsertFromModal(mid, sz, cap)}
|
||||
/>
|
||||
) : null}
|
||||
{showInlineToolbar ? (
|
||||
@@ -320,7 +457,7 @@ export default function RichTextEditor({
|
||||
onClose={() => setEmbedModalOpen(false)}
|
||||
exerciseId={Number(inlineExerciseId)}
|
||||
onMediaListChanged={refreshExerciseMedia}
|
||||
onInserted={(mid, sz) => finalizeInsertFromModal(mid, sz)}
|
||||
onInserted={(mid, sz, cap) => finalizeInsertFromModal(mid, sz, cap)}
|
||||
/>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user