@lincyaw/kernel 1.0.0-rc.24 → 1.0.0-rc.26
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 +1 -1
- package/src/contracts/preview.ts +0 -3
- package/src/index.ts +1 -0
- package/src/preview-events.ts +3 -2
- package/src/preview-frame-connect.ts +6 -25
- package/src/random-uuid.ts +8 -0
package/package.json
CHANGED
package/src/contracts/preview.ts
CHANGED
|
@@ -204,8 +204,6 @@ export type PreviewReport =
|
|
|
204
204
|
| { type: 'selection', selection: (PreviewAnchor & { text: string, rect?: PreviewRect, obstacles?: PreviewRect[] }) | null }
|
|
205
205
|
/** Where the picked block or the selection is after the page scrolled or resized; null when it is out of sight. */
|
|
206
206
|
| { type: 'rect', rect: PreviewRect | null, obstacles?: PreviewRect[] }
|
|
207
|
-
/** Where the blocks the AI changed are in the preview's viewport while they blink, and again as the page scrolls (at most 16). */
|
|
208
|
-
| { type: 'flashed', rects: PreviewRect[] }
|
|
209
207
|
| { type: 'editing', target: PreviewAnchor, active: boolean }
|
|
210
208
|
/** The text of a block edited in place, before and after (its `textContent`); sent only when it changed. */
|
|
211
209
|
| { type: 'edited', target: PreviewAnchor, before: string, after: string, request?: number, revision?: string, binding?: string }
|
|
@@ -374,7 +372,6 @@ export function toPreviewReport(data: unknown, nonce: string): PreviewReport | u
|
|
|
374
372
|
? { type: 'selection', selection: { ...toAnchor(selected), text: selected.text, ...selected.rect ? { rect: toRect(selected.rect as PreviewRect) } : {}, ...selected.obstacles !== undefined ? { obstacles: (selected.obstacles as PreviewRect[]).map(toRect) } : {} } }
|
|
375
373
|
: undefined
|
|
376
374
|
}
|
|
377
|
-
case 'flashed': return Array.isArray(m.rects) && m.rects.length <= 16 && m.rects.every(rect) ? { type: 'flashed', rects: (m.rects as PreviewRect[]).map(toRect) } : undefined
|
|
378
375
|
case 'rect': return (m.rect === null || rect(m.rect)) && optional(m.obstacles, obstacles) ? { type: 'rect', rect: m.rect === null ? null : toRect(m.rect), ...m.obstacles !== undefined ? { obstacles: (m.obstacles as PreviewRect[]).map(toRect) } : {} } : undefined
|
|
379
376
|
case 'editing': return anchor(m.target) && typeof m.active === 'boolean' ? { type: 'editing', target: toAnchor(m.target), active: m.active } : undefined
|
|
380
377
|
case 'edited': return anchor(m.target) && text(m.before) && text(m.after) && m.before.length <= MAX_EDITED && m.after.length <= MAX_EDITED && editedRequest(m) ? { type: 'edited', target: toAnchor(m.target), before: m.before, after: m.after, ...m.request === undefined ? {} : { request: m.request as number, revision: m.revision as string, binding: m.binding as string } } : undefined
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './contracts.ts'
|
|
2
|
+
export { randomUUID } from './random-uuid.ts'
|
|
2
3
|
export { applyDocumentOperations } from './document-operations.ts'
|
|
3
4
|
export { validateDocumentChange, DOCUMENT_CHANGE_JSON_SCHEMA } from './authoring.ts'
|
|
4
5
|
export { allocateDocumentPage, createDocumentPage, documentPageDefaults, DOCUMENT_PAGE_DEFAULT_TIMING, duplicateDocumentPage, moveDocumentPage, pageContents, pageDeletion, pageDeletionImpact, removeDocumentPage, renameDocumentPage } from './document-pages.ts'
|
package/src/preview-events.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { PREVIEW_EVENTS_PARAM, previewEnvelope } from './contracts/preview.ts'
|
|
2
2
|
import type { PreviewCommand, PreviewEventSubscription, PreviewEventTarget, PreviewEventMessage } from './contracts/preview.ts'
|
|
3
3
|
import { parentOrigins, previewNonce } from './preview-frame.ts'
|
|
4
|
+
import { randomUUID } from './random-uuid.ts'
|
|
4
5
|
|
|
5
6
|
/** Uses the existing preview-ready handshake, origin fence and nonce; never opens a network connection. */
|
|
6
7
|
export interface PreviewEvents {
|
|
@@ -14,7 +15,7 @@ export function createPreviewEvents(origins: readonly string[], document: string
|
|
|
14
15
|
if (typeof window === 'undefined' || window.parent === window || !origins.length || new URL(location.href).searchParams.get(PREVIEW_EVENTS_PARAM) !== 'shared') return
|
|
15
16
|
const nonce = previewNonce()
|
|
16
17
|
if (!nonce) return
|
|
17
|
-
const connection =
|
|
18
|
+
const connection = randomUUID()
|
|
18
19
|
const readers = new Map<string, { target: PreviewEventTarget, received(message: PreviewEventMessage): void }>()
|
|
19
20
|
let ready = false, stopped = false, parked = false
|
|
20
21
|
const targets = parentOrigins(origins)
|
|
@@ -36,7 +37,7 @@ export function createPreviewEvents(origins: readonly string[], document: string
|
|
|
36
37
|
connection,
|
|
37
38
|
subscribe(target, received) {
|
|
38
39
|
if (stopped || target.document !== document) return () => {}
|
|
39
|
-
const id =
|
|
40
|
+
const id = randomUUID()
|
|
40
41
|
readers.set(id, { target, received })
|
|
41
42
|
if (ready && !parked) subscribe(id, target)
|
|
42
43
|
return () => { if (readers.delete(id)) post({ type: 'events-unsubscribe', connection, id }) }
|
|
@@ -20,8 +20,6 @@ const PIN_LABELS = 'data-preview-pin-labels'
|
|
|
20
20
|
const FLASH = 'data-preview-flash'
|
|
21
21
|
// Three blinks of 0.8 s, the length of the animation below.
|
|
22
22
|
const FLASH_MS = 2400
|
|
23
|
-
/** How long the changed blocks' boxes keep being reported after they blink. */
|
|
24
|
-
const TRAIL_MS = 15000
|
|
25
23
|
const PIN_CLASS = 'pm-preview-pin'
|
|
26
24
|
const CONTROLS = 'a, button, input, select, textarea, label, summary, [role="dialog"], [role="menu"], [role="menuitem"], [role="listbox"], [role="option"]'
|
|
27
25
|
const HEADINGS = 'h1, h2, h3, h4, h5, h6'
|
|
@@ -222,22 +220,10 @@ function createHighlight(page: PreviewPage, state: FrameState, locate: Locator)
|
|
|
222
220
|
* Makes the blocks the workbench names blink (a steady tint when the reader asks for less motion), after bringing the first
|
|
223
221
|
* into view; retries while the page brings it there, as the highlight does.
|
|
224
222
|
*/
|
|
225
|
-
function createFlash(page: PreviewPage, state: FrameState, locate: Locator
|
|
223
|
+
function createFlash(page: PreviewPage, state: FrameState, locate: Locator) {
|
|
226
224
|
let ending: ReturnType<typeof setTimeout> | undefined
|
|
227
225
|
let retrying: ReturnType<typeof setTimeout> | undefined
|
|
228
226
|
let frame = 0
|
|
229
|
-
// The changed blocks' boxes go to the workbench while they blink and for a while after, as the page scrolls, so its
|
|
230
|
-
// kitten can walk to them.
|
|
231
|
-
let trail: HTMLElement[] = [], trailUntil = 0, trailFrame = 0
|
|
232
|
-
const reportTrail = () => {
|
|
233
|
-
trailFrame = 0
|
|
234
|
-
if (!isLive(state) || Date.now() > trailUntil) return
|
|
235
|
-
const rects = trail.filter(element => element.isConnected).slice(0, 16).map(element => toRect(element.getBoundingClientRect()))
|
|
236
|
-
post({ type: 'flashed', rects })
|
|
237
|
-
}
|
|
238
|
-
const trailMoved = () => { if (!trailFrame && Date.now() <= trailUntil) trailFrame = requestAnimationFrame(reportTrail) }
|
|
239
|
-
window.addEventListener('scroll', trailMoved, { capture: true, passive: true })
|
|
240
|
-
window.addEventListener('resize', trailMoved, { passive: true })
|
|
241
227
|
const clear = () => {
|
|
242
228
|
clearTimeout(ending)
|
|
243
229
|
cancelAnimationFrame(frame)
|
|
@@ -254,8 +240,6 @@ function createFlash(page: PreviewPage, state: FrameState, locate: Locator, post
|
|
|
254
240
|
frame = requestAnimationFrame(() => {
|
|
255
241
|
for (const element of elements) element.setAttribute(FLASH, '')
|
|
256
242
|
ending = setTimeout(clear, FLASH_MS)
|
|
257
|
-
trail = elements; trailUntil = Date.now() + TRAIL_MS
|
|
258
|
-
reportTrail()
|
|
259
243
|
})
|
|
260
244
|
return true
|
|
261
245
|
}
|
|
@@ -272,13 +256,10 @@ function createFlash(page: PreviewPage, state: FrameState, locate: Locator, post
|
|
|
272
256
|
}
|
|
273
257
|
retrying = setTimeout(retry, 50)
|
|
274
258
|
},
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
cancelAnimationFrame(trailFrame)
|
|
280
|
-
trailFrame = 0
|
|
281
|
-
},
|
|
259
|
+
cancel() {
|
|
260
|
+
clearTimeout(retrying)
|
|
261
|
+
clear()
|
|
262
|
+
},
|
|
282
263
|
}
|
|
283
264
|
}
|
|
284
265
|
|
|
@@ -937,7 +918,7 @@ export function connectPreviewFrame(page: PreviewPage): PreviewFrame | undefined
|
|
|
937
918
|
const locate = createLocator(page)
|
|
938
919
|
const highlight = createHighlight(page, state, locate)
|
|
939
920
|
const pins = createPins(locate)
|
|
940
|
-
const flash = createFlash(page, state, locate
|
|
921
|
+
const flash = createFlash(page, state, locate)
|
|
941
922
|
const rects = createRectReport(page, state, locate, post)
|
|
942
923
|
const picking = createPicking(page, state, locate, rects, post)
|
|
943
924
|
const editing = createEditing(page, state, locate, picking, post)
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Random v4 UUID from crypto.getRandomValues, which browsers expose outside secure contexts. */
|
|
2
|
+
export function randomUUID(): string {
|
|
3
|
+
const bytes = crypto.getRandomValues(new Uint8Array(16))
|
|
4
|
+
bytes[6] = bytes[6]! & 0x0f | 0x40
|
|
5
|
+
bytes[8] = bytes[8]! & 0x3f | 0x80
|
|
6
|
+
const hex = Array.from(bytes, byte => byte.toString(16).padStart(2, '0')).join('')
|
|
7
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`
|
|
8
|
+
}
|