Skip to content

Commit 1cb636c

Browse files
committed
fix(build): a whole source pasted into the box replaces the one there (#158)
1 parent 31fb997 commit 1cb636c

4 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎src/h5p/build/build-page.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { pack } from '@missing-elements/h5p-libraries'
55
import { runtime } from '@missing-elements/h5p-runtime'
66
import { baseName, compactSource, forTheChat, fragmentFromSource, kindOf, linksSource, matchFiles, namedFiles, packageName, parseSource, sized, sourceFromFragment, typesNamed, type Problem, type TypeFile } from './build-source'
77
import type { SourceEditor } from './editor'
8-
import { formatSource, jsonProblem, place, type Target } from './json-scan'
8+
import { formatSource, jsonProblem, place, type Target, wholeSource } from './json-scan'
99

1010
/**
1111
* 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 {
439439
}
440440

441441
// Where the editor has not loaded, a source pasted whole is laid out here, as the editor does it,
442-
// and a paste over the sample as the page put it replaces it whole.
442+
// and a paste over the sample as the page put it, or of a whole source, replaces the box's text.
443443
sourceField.addEventListener('paste', (event) => {
444444
const text = event.clipboardData?.getData('text/plain')
445445
if (!text) return
446-
const replaced = sampleUntouched()
446+
const replaced = sampleUntouched() || wholeSource(text)
447447
if (!replaced && (sourceField.selectionStart !== 0 || sourceField.selectionEnd !== sourceField.value.length)) return
448448
const insert = formatSource(text) ?? (replaced ? text : null)
449449
if (insert === null) return

‎src/h5p/build/editor.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { type Diagnostic, linter, lintGutter, setDiagnostics } from '@codemirror
66
import { EditorState } from '@codemirror/state'
77
import { drawSelection, EditorView, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder } from '@codemirror/view'
88
import { tags } from '@lezer/highlight'
9-
import { formatSource, jsonProblem, place, type Target } from './json-scan'
9+
import { formatSource, jsonProblem, place, type Target, wholeSource } from './json-scan'
1010

1111
/**
1212
* The editor of the build page's source, `/h5p/build/`, in place of its textarea: JSON in colours,
@@ -93,15 +93,16 @@ const typeOverSelection = EditorView.domEventHandlers({
9393
* A source pasted whole, into an empty editor or over all of its text, goes in laid out, and out of
9494
* the code fence a chat put it in, so that the page needs no button for it. A paste into the text,
9595
* or of text that is not JSON, goes in as it is. Where the text is one the page put there to be
96-
* pasted over (`over`), a paste anywhere in it replaces all of it, laid out where it is JSON.
96+
* pasted over (`over`), or the paste is a whole source (`wholeSource`), a paste anywhere in it
97+
* replaces all of it, laid out where it is JSON; undo brings the old text back.
9798
*/
9899
function pasteLaidOut(over: () => boolean) {
99100
return EditorView.domEventHandlers({
100101
paste(event, view) {
101102
const text = event.clipboardData?.getData('text/plain')
102103
if (!text) return false
103104
const { from, to } = view.state.selection.main
104-
const replaced = over()
105+
const replaced = over() || wholeSource(text)
105106
if (!replaced && (from !== 0 || to !== view.state.doc.length)) return false
106107
const insert = formatSource(text) ?? (replaced ? text : null)
107108
if (insert === null) return false

‎src/h5p/build/index.njk‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,9 @@ csp: "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' da
187187
</div>
188188
<p class="hint" id="source-state" role="status" aria-live="polite"></p>
189189
<p class="hint">
190-
You can change the source here before you build it. A source you paste whole or open is
191-
laid out for you. A mistake in the JSON is marked as you type, and each problem the build
190+
You can change the source here before you build it. A whole source you paste or open
191+
takes the place of the one in the box, laid out for you. A mistake in the JSON is marked
192+
as you type, and each problem the build
192193
finds is marked where it is: choose its name in the list to go there.
193194
<span class="when-linked">This page's address follows your changes, so you can copy it to
194195
send the source on.</span>

‎src/h5p/build/json-scan.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,18 @@ export function formatSource(text: string): string | null {
326326
return JSON.stringify(JSON.parse(unfence(text).text), null, 2)
327327
}
328328

329+
/**
330+
* Whether the text is a whole source, an H5P content type's `type` and a `title` at its top, which
331+
* a paste puts in place of what the box holds: pasted into an old source, it would break both.
332+
*/
333+
export function wholeSource(text: string): boolean {
334+
if (!text.trim() || jsonProblem(text)) return false
335+
const read: unknown = JSON.parse(unfence(text).text)
336+
if (!read || typeof read !== 'object' || Array.isArray(read)) return false
337+
const { type, title } = read as { type?: unknown; title?: unknown }
338+
return typeof type === 'string' && /^H5P\./.test(type) && typeof title === 'string'
339+
}
340+
329341
/** Where to take the person to in a source: an offset, or what a problem's path points at. */
330342
export type Target = { at: number } | { path: string }
331343

0 commit comments

Comments
 (0)