diff --git a/src/h5p/build/build-page.ts b/src/h5p/build/build-page.ts index 50d2671..4edb20b 100644 --- a/src/h5p/build/build-page.ts +++ b/src/h5p/build/build-page.ts @@ -5,7 +5,7 @@ import { pack } from '@missing-elements/h5p-libraries' import { runtime } from '@missing-elements/h5p-runtime' import { baseName, compactSource, forTheChat, fragmentFromSource, kindOf, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source' import type { SourceEditor } from './editor' -import { formatSource, jsonProblem, place, type Target } from './json-scan' +import { formatSource, jsonProblem, place, type Target, wholeSource } from './json-scan' /** * The build page, `/h5p/build/`: a source an AI chat wrote with the h5p-author skill becomes a @@ -439,11 +439,11 @@ function setSource(text: string): void { } // Where the editor has not loaded, a source pasted whole is laid out here, as the editor does it, -// and a paste over the sample as the page put it replaces it whole. +// and a paste over the sample as the page put it, or of a whole source, replaces the box's text. sourceField.addEventListener('paste', (event) => { const text = event.clipboardData?.getData('text/plain') if (!text) return - const replaced = sampleUntouched() + const replaced = sampleUntouched() || wholeSource(text) if (!replaced && (sourceField.selectionStart !== 0 || sourceField.selectionEnd !== sourceField.value.length)) return const insert = formatSource(text) ?? (replaced ? text : null) if (insert === null) return diff --git a/src/h5p/build/editor.ts b/src/h5p/build/editor.ts index aad3c9c..0705416 100644 --- a/src/h5p/build/editor.ts +++ b/src/h5p/build/editor.ts @@ -6,7 +6,7 @@ import { type Diagnostic, linter, lintGutter, setDiagnostics } from '@codemirror import { EditorState } from '@codemirror/state' import { drawSelection, EditorView, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder } from '@codemirror/view' import { tags } from '@lezer/highlight' -import { formatSource, jsonProblem, place, type Target } from './json-scan' +import { formatSource, jsonProblem, place, type Target, wholeSource } from './json-scan' /** * The editor of the build page's source, `/h5p/build/`, in place of its textarea: JSON in colours, @@ -93,7 +93,8 @@ const typeOverSelection = EditorView.domEventHandlers({ * A source pasted whole, into an empty editor or over all of its text, goes in laid out, and out of * the code fence a chat put it in, so that the page needs no button for it. A paste into the text, * or of text that is not JSON, goes in as it is. Where the text is one the page put there to be - * pasted over (`over`), a paste anywhere in it replaces all of it, laid out where it is JSON. + * pasted over (`over`), or the paste is a whole source (`wholeSource`), a paste anywhere in it + * replaces all of it, laid out where it is JSON; undo brings the old text back. */ function pasteLaidOut(over: () => boolean) { return EditorView.domEventHandlers({ @@ -101,7 +102,7 @@ function pasteLaidOut(over: () => boolean) { const text = event.clipboardData?.getData('text/plain') if (!text) return false const { from, to } = view.state.selection.main - const replaced = over() + const replaced = over() || wholeSource(text) if (!replaced && (from !== 0 || to !== view.state.doc.length)) return false const insert = formatSource(text) ?? (replaced ? text : null) if (insert === null) return false diff --git a/src/h5p/build/index.njk b/src/h5p/build/index.njk index 83f50d3..6be0173 100644 --- a/src/h5p/build/index.njk +++ b/src/h5p/build/index.njk @@ -187,8 +187,9 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
- You can change the source here before you build it. A source you paste whole or open is - laid out for you. A mistake in the JSON is marked as you type, and each problem the build + You can change the source here before you build it. A whole source you paste or open + takes the place of the one in the box, laid out for you. A mistake in the JSON is marked + as you type, and each problem the build finds is marked where it is: choose its name in the list to go there. This page's address follows your changes, so you can copy it to send the source on. diff --git a/src/h5p/build/json-scan.ts b/src/h5p/build/json-scan.ts index 483cc10..2de3acc 100644 --- a/src/h5p/build/json-scan.ts +++ b/src/h5p/build/json-scan.ts @@ -326,6 +326,18 @@ export function formatSource(text: string): string | null { return JSON.stringify(JSON.parse(unfence(text).text), null, 2) } +/** + * Whether the text is a whole source, an H5P content type's `type` and a `title` at its top, which + * a paste puts in place of what the box holds: pasted into an old source, it would break both. + */ +export function wholeSource(text: string): boolean { + if (!text.trim() || jsonProblem(text)) return false + const read: unknown = JSON.parse(unfence(text).text) + if (!read || typeof read !== 'object' || Array.isArray(read)) return false + const { type, title } = read as { type?: unknown; title?: unknown } + return typeof type === 'string' && /^H5P\./.test(type) && typeof title === 'string' +} + /** Where to take the person to in a source: an offset, or what a problem's path points at. */ export type Target = { at: number } | { path: string }