-
Notifications
You must be signed in to change notification settings - Fork 42
/
documents.ts
630 lines (573 loc) · 14.8 KB
/
documents.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import { ViewUpdate } from '@codemirror/view'
import {
CodeExecuteCancelEvent,
CodeExecuteEvent,
} from '@stencila/components/dist/types/components/code/codeTypes'
import { ValidatorTypes } from '@stencila/schema'
import {
Address,
Document,
DocumentEvent,
Operation,
Patch,
} from '@stencila/stencila'
import { Client, ClientId } from './client'
import { KernelId } from './kernels'
import { JsonValue } from './patches/checks'
import * as codemirror from './patches/codemirror'
import { applyPatch } from './patches/dom'
/**
* The path of a document
*/
export type DocumentPath = string
/**
* The id of a document
*/
export type DocumentId = string
/**
* The id of a node within a document
*/
export type NodeId = string
/**
* Possible document subscription topics
*/
type DocumentTopic = 'patched'
/**
* The browser event emitted when a document is patched (e.g. by the
* WYSIWYG article editor)
*/
export interface DocumentPatchEvent extends CustomEvent {
detail: Patch
}
/**
* The browser event emitted when the language of a text editor
* is changed
*/
export interface LanguageChangeEvent extends CustomEvent {
detail: {
ext: string
name: string
}
}
/**
* The browser event emitted when the content of a text editor
* is changed
*/
export interface ContentChangeEvent extends CustomEvent {
detail: ViewUpdate | string
}
/**
* Create a new document
*
* Optionally pass the content for the new document.
* If `content` is not a string and format is 'json' of undefined,
* then `content` will be stringify to JSON.
*/
export async function create(
client: Client,
content?: unknown,
format?: string
): Promise<Document> {
if (
content !== undefined &&
typeof content !== 'string' &&
(format === 'json' || format === undefined)
) {
content = JSON.stringify(content)
format = 'json'
}
return client.call('documents.create', {
content,
format,
}) as Promise<Document>
}
/**
* Open a document
*
* Loads the document into memory on the server and returns a document object
* with an `id` which can be used to subscribe to topics.
*/
export async function open(
client: Client,
documentPath: DocumentPath
): Promise<Document> {
return client.call('documents.open', {
path: documentPath,
}) as Promise<Document>
}
/**
* Close a document
*/
export async function close(
client: Client,
documentId: DocumentId
): Promise<Document> {
return client.call('documents.close', {
documentId,
}) as Promise<Document>
}
/**
* Load content into a document
*
* If `format` is not supplied, the content will be assumed to be the current
* format of the document e.g. `md`. If `content` is not a string and format
* is 'json' of undefined, then `content` will be stringify to JSON.
*/
export async function load(
client: Client,
documentId: DocumentId,
content: unknown,
format?: string
): Promise<Document> {
if (
typeof content !== 'string' &&
(format === 'json' || format === undefined)
) {
content = JSON.stringify(content)
format = 'json'
}
return client.call('documents.load', {
documentId,
content,
format,
}) as Promise<Document>
}
/**
* Dump all, or part, of a document to a string
*
* If `format` is not supplied, the content will be assumed to be the current
* format of the document e.g. `md`. If `nodeId` is supplied then only that
* node will be dumped.
*/
export async function dump(
client: Client,
documentId: DocumentId,
format?: string,
nodeId?: NodeId
): Promise<Document> {
return client.call('documents.dump', {
documentId,
format,
nodeId,
}) as Promise<Document>
}
/**
* Subscribe to a document topic
*/
export async function subscribe(
client: Client,
documentId: DocumentId,
topic: DocumentTopic,
handler: (event: DocumentEvent) => void
): Promise<Document> {
client.on(`documents:${documentId}:${topic}`, handler)
return client.call('documents.subscribe', {
documentId,
topic,
}) as Promise<Document>
}
/**
* Unsubscribe from a document topic
*/
export async function unsubscribe(
client: Client,
documentId: DocumentId,
topic: DocumentTopic
): Promise<Document> {
client.off(`documents:${documentId}:${topic}`)
return client.call('documents.unsubscribe', {
documentId,
topic,
}) as Promise<Document>
}
/**
* Send a document patch to the server
*
* Will generate an error if the patch could not be applied e.g. no node with the id could
* be found or the patch was inconsistent with the node.
*
* The `execute` option can be used to immediately execute the document, starting at the
* patch's target if any, otherwise the entire document. Combining patch and execute operations
* in a single request ensures that they occur in the correct order.
*/
export async function sendPatch(
client: Client,
documentId: DocumentId,
patch: Patch,
execute = false
): Promise<void> {
// During development it's very useful to see the patch operations being sent
if (process.env.NODE_ENV !== 'production') {
const { actor, target, ops } = patch
console.log('📢 Sending patch:', JSON.stringify({ actor, target }))
for (const op of ops) console.log(' ', JSON.stringify(op))
}
return client.call('documents.patch', {
documentId,
patch,
execute,
}) as Promise<void>
}
// Receive a document patch from the server
//
// Handles a 'patched' event by either sending it to the relevant WebComponent
// so that it can make the necessary changes to the DOM, or by calling `applyPatch` which
// makes changes to the DOM directly.
export function receivePatch(
clientId: ClientId,
event: DocumentEvent,
callback: (patch: Patch) => void = applyPatch
): void {
let patch
if (event.type === 'patched') {
patch = event.patch as Patch
} else {
console.error(
`Expected document event to be of type 'patched', got type '${event.type}'`
)
return
}
const { actor, target, ops } = patch
// Ignore any patches where this client was the actor
if (actor === clientId) return
// During development it's useful to see which patches are being received
if (process.env.NODE_ENV !== 'production') {
console.log('📩 Received patch:', JSON.stringify({ actor, target }))
for (const op of ops) console.log(' ', JSON.stringify(op))
}
callback(patch)
}
/**
* Execute a document
*/
export async function execute(
client: Client,
documentId: DocumentId,
nodeId: null | NodeId,
ordering: 'Single' | 'Appearance' | 'Topological' = 'Topological'
): Promise<void> {
return client.call('documents.execute', {
documentId,
nodeId,
ordering,
}) as Promise<void>
}
/**
* Cancel execution of a document
*/
export async function cancel(
client: Client,
documentId: DocumentId,
nodeId: null | NodeId,
scope: 'Single' | 'All' = 'All'
): Promise<void> {
return client.call('documents.cancel', {
documentId,
nodeId,
scope,
}) as Promise<void>
}
/**
* Restart one, or all, of the kernels in a document's kernel space
*
* If `kernelId` is not supplied then all kernels in the kernel space
* will be restarted.
*/
export async function restart(
client: Client,
documentId: DocumentId,
kernelId?: KernelId
): Promise<void> {
return client.call('documents.restart', {
documentId,
kernelId,
}) as Promise<void>
}
/**
* Get a list of kernels in a document's kernel space
*/
export async function kernels(
client: Client,
documentId: DocumentId
): Promise<void> {
return client.call('documents.kernels', {
documentId,
}) as Promise<void>
}
/**
* Get a list of symbols in a document's kernel space
*/
export async function symbols(
client: Client,
documentId: DocumentId
): Promise<void> {
return client.call('documents.symbols', {
documentId,
}) as Promise<void>
}
/**
* Listen to browser window events that require passing on to server
*/
export function listen(
client: Client,
clientId: ClientId,
documentId: DocumentId
): void {
window.addEventListener('stencila-document-patch', (event) =>
onDocumentPatch(client, clientId, documentId, event as DocumentPatchEvent)
)
window.addEventListener('stencila-language-change', (event) =>
onLanguageChange(client, clientId, documentId, event as LanguageChangeEvent)
)
window.addEventListener('stencila-content-change', (event) =>
onContentChange(client, clientId, documentId, event as ContentChangeEvent)
)
window.addEventListener('stencila-validator-change', (event) =>
onValidatorChange(
client,
clientId,
documentId,
event as ValidatorChangeEvent
)
)
window.addEventListener('stencila-parameter-change', (event) =>
onParameterChange(
client,
clientId,
documentId,
event as ParameterChangeEvent
)
)
// Code execution
const executeHandler = async ({
detail,
}: CodeExecuteEvent): Promise<void> => {
await execute(client, documentId, detail.nodeId, detail.ordering)
}
window.addEventListener('stencila-code-execute', (e) => {
executeHandler(e as CodeExecuteEvent).catch(console.error)
})
// Code execution cancellation
const executeCancelHandler = async ({
detail,
}: CodeExecuteCancelEvent): Promise<void> => {
await cancel(client, documentId, detail.nodeId, detail.scope)
}
window.addEventListener('stencila-code-execute-cancel', (e) => {
executeCancelHandler(e as CodeExecuteCancelEvent).catch(console.error)
})
// Kernel restart
const kernelRestartHandler = async (): Promise<void> => {
await restart(client, documentId)
}
window.addEventListener('stencila-kernel-restart', () => {
kernelRestartHandler().catch(console.error)
})
}
/**
* Handle a `DocumentPatchEvent`
*
* These events, created by document editors, are fully formed `Patch`es missing only the actor.
*/
async function onDocumentPatch(
client: Client,
clientId: ClientId,
documentId: DocumentId,
event: DocumentPatchEvent
): Promise<void> {
return sendPatch(client, documentId, {
actor: clientId,
...event.detail,
})
}
/**
* Handle a `LanguageChangeEvent`
*
* These events, created by text editors for individual nodes, need to be
* transformed into a `Patch` targeting that node. The `slot` (the name of the property)
* in the address is dependent upon the type of node
*/
async function onLanguageChange(
client: Client,
clientId: ClientId,
documentId: DocumentId,
event: LanguageChangeEvent
): Promise<void> {
const [nodeType, nodeId] = resolveEventNode(event)
const slot = nodeType.startsWith('Code')
? 'programmingLanguage'
: 'mathLanguage'
const value = (event.detail.ext ?? event.detail.name).toLowerCase()
return sendPatch(client, documentId, {
actor: clientId,
target: nodeId,
ops: [
{
type: 'Replace',
address: [slot],
value,
items: 1,
length: value.length,
},
],
})
}
/**
* Handle a `ContentChangeEvent`
*
* These events, created by text editors for individual nodes, need to be
* transformed into a `Patch` targeting that node.
*/
async function onContentChange(
client: Client,
clientId: ClientId,
documentId: DocumentId,
event: ContentChangeEvent
): Promise<void> {
const [_nodeType, nodeId] = resolveEventNode(event)
const address = ['text']
let ops: Operation[] = []
if (typeof event.detail === 'string') {
const value = event.detail
ops = [
{
type: 'Replace',
address,
items: 1,
value,
length: value.length,
html: undefined,
},
]
} else {
const update = event.detail
if (update.docChanged) {
ops = codemirror.updateToOps(update, address)
} else {
// No change, so early return
return
}
}
const patch = {
actor: clientId,
target: nodeId,
ops,
}
return sendPatch(client, documentId, patch)
}
/**
* The browser event emitted when either the type or property of a parameter validator changes.
*/
export interface ValidatorChangeEvent extends CustomEvent {
detail:
| {
type: 'property'
name: string
value: string
}
| {
type: 'validator'
value: Exclude<ValidatorTypes['type'], 'Validator'>
}
}
/**
* Handle a `ValidatorChangeEvent`
*/
async function onValidatorChange(
client: Client,
clientId: ClientId,
documentId: DocumentId,
event: ValidatorChangeEvent
): Promise<void> {
const [_nodeType, nodeId] = resolveEventNode(event)
const [address, value]: [Address, JsonValue] =
event.detail.type === 'property'
? // The new validator property value
[
[
// ...except for `default` which is actually a property of the parent parameter
...(event.detail.name === 'default' ? [] : ['validator']),
event.detail.name,
],
event.detail.value,
]
: // The new validator as an object with `type`
[['validator'], { type: event.detail.value }]
const op: Operation = {
type: 'Replace',
address,
value,
items: 1,
length: 1,
}
const patch = {
actor: clientId,
target: nodeId,
ops: [op],
}
return sendPatch(client, documentId, patch)
}
/**
* The browser event emitted when either the name of value of the parameter changes.
*/
export interface ParameterChangeEvent extends CustomEvent {
detail: {
property: 'name' | 'value'
value: string
}
}
/**
* Handle a `ParameterChangeEvent`
*/
async function onParameterChange(
client: Client,
clientId: ClientId,
documentId: DocumentId,
event: ParameterChangeEvent
): Promise<void> {
const [_nodeType, nodeId] = resolveEventNode(event)
const op: Operation = {
type: 'Replace',
address: [event.detail.property],
value: event.detail.value,
items: 1,
length: 1,
}
const patch = {
actor: clientId,
target: nodeId,
ops: [op],
}
return sendPatch(client, documentId, patch, true)
}
/**
* Extract the given Element's Schema Node type
* If the node does not have an `itemtype` attribute, this function returns an empty string.
*/
const getElType = (targetEl: Element): string => {
const itemtype = targetEl?.getAttribute('itemtype') ?? ''
const parts = itemtype.split('/')
return parts[parts.length - 1] ?? ''
}
/**
* Get the type and id of the Stencila document node that the event
* was emitted from.
*
* This is necessary to be able to determine the shape and target of the generated patch.
*/
function resolveEventNode(event: Event): [string, string] {
const elem: HTMLElement | null = event.target as HTMLElement
let id = elem.getAttribute('id')
let elType = getElType(elem)
if (id === null || id === '') {
const nodeEl = elem.closest('[itemtype]')
if (nodeEl) {
id = nodeEl.getAttribute('id')
elType = getElType(nodeEl)
}
}
if (id !== null) {
return [elType, id]
}
throw new Error('Unable to resolve the node which emitted the event')
}