@lincyaw/kernel 1.0.0-rc.30 → 1.0.0-rc.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/kernel",
3
- "version": "1.0.0-rc.30",
3
+ "version": "1.0.0-rc.32",
4
4
  "description": "Presmium's shared kernel: the IR and plugin contracts, the service registry, anchors and checks",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -0,0 +1,33 @@
1
+ import { Validator } from '@cfworker/json-schema'
2
+ import { DOCUMENT_JSON_SCHEMA } from './document-schema.ts'
3
+ import type { Narration, TeachingDocument } from './contracts/document.ts'
4
+
5
+ const schema = new Validator({ $id: 'urn:presmium:step-narrations:1', $defs: structuredClone(DOCUMENT_JSON_SCHEMA.$defs), type: 'array', items: { $ref: '#/$defs/Narration' } }, '2020-12', false)
6
+
7
+ /** Replace one step's ordered script blocks, retaining entities still used by other steps. */
8
+ export function replaceStepNarrations(source: TeachingDocument, stepId: string, narrations: readonly Narration[]): TeachingDocument {
9
+ const step = source.steps[stepId]
10
+ if (!step || !source.sections[step.sectionId]?.stepOrder.includes(stepId)) throw new Error('document-narration-target')
11
+ if (!schema.validate(narrations).valid) throw new Error('document-narration-content')
12
+ const ids = narrations.map(narration => narration.id)
13
+ if (new Set(ids).size !== ids.length) throw new Error('document-narration-target')
14
+ const content = { ...source,
15
+ steps: { ...source.steps, [stepId]: { ...step, narrationIds: ids } },
16
+ narrations: { ...source.narrations },
17
+ production: { ...source.production,
18
+ ...source.production.narrationOverrides ? { narrationOverrides: { ...source.production.narrationOverrides } } : {},
19
+ ...source.production.cueTimings ? { cueTimings: { ...source.production.cueTimings } } : {},
20
+ },
21
+ }
22
+ for (const narration of narrations) {
23
+ content.narrations[narration.id] = structuredClone(narration)
24
+ }
25
+ const used = new Set(Object.values(content.steps).flatMap(step => step.narrationIds))
26
+ for (const id of step.narrationIds) if (!used.has(id)) {
27
+ delete content.narrations[id]
28
+ if (content.production.narrationOverrides) delete content.production.narrationOverrides[id]
29
+ }
30
+ const cues = new Set(Object.values(content.narrations).flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
31
+ for (const id of Object.keys(content.production.cueTimings ?? {})) if (!cues.has(id)) delete content.production.cueTimings![id]
32
+ return content
33
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './contracts.ts'
2
2
  export { randomUUID } from './random-uuid.ts'
3
3
  export { applyDocumentOperations } from './document-operations.ts'
4
+ export { replaceStepNarrations } from './document-narrations.ts'
4
5
  export { validateDocumentChange, DOCUMENT_CHANGE_JSON_SCHEMA } from './authoring.ts'
5
6
  export { allocateDocumentPage, createDocumentPage, documentPageDefaults, DOCUMENT_PAGE_DEFAULT_TIMING, duplicateDocumentPage, moveDocumentPage, pageContents, pageDeletion, pageDeletionImpact, removeDocumentPage, renameDocumentPage } from './document-pages.ts'
6
7
  export { documentOutline, documentPart, DocumentReadError } from './document-reads.ts'