Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/h5p/build/build-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/h5p/build/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -93,15 +93,16 @@ 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({
paste(event, view) {
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
Expand Down
5 changes: 3 additions & 2 deletions src/h5p/build/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,9 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
</div>
<p class="hint" id="source-state" role="status" aria-live="polite"></p>
<p class="hint">
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.
<span class="when-linked">This page's address follows your changes, so you can copy it to
send the source on.</span>
Expand Down
12 changes: 12 additions & 0 deletions src/h5p/build/json-scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down