-
Notifications
You must be signed in to change notification settings - Fork 42
/
documents.ts
162 lines (147 loc) · 4.13 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
// Node.js bindings for ../../rust/src/documents.rs, see there for more documentation.
import { JSONSchema7 } from 'json-schema'
import { fromJSON } from './prelude'
import * as pubsub from './pubsub'
import { Document, DocumentEvent } from './types'
const addon = require('../index.node')
/**
* Get the JSON Schemas associated with the `documents` module.
*
* @returns An array of JSON Schema v7 objects
*/
export function schemas(): JSONSchema7[] {
return fromJSON<JSONSchema7[]>(addon.documentsSchema())
}
/**
* List documents that are currently open.
*
* @returns An array of document paths (relative to the current working directory)
*/
export function list(): string[] {
return fromJSON<string[]>(addon.documentsList())
}
/**
* Create a new empty document, optionally specifying its format.
*
* @param format Format of the document
* @return A document
*/
export function create(format?: string): Document {
return fromJSON<Document>(addon.documentsCreate(format ?? ''))
}
/**
* Open an existing document, optionally specifying its format.
*
* @param path Path to the document's file
* @param format Format of the document. If `undefined` will be inferred from
* the file extension.
* @return A document
*/
export function open(path: string, format?: string): Document {
return fromJSON<Document>(addon.documentsOpen(path, format ?? ''))
}
/**
* Get a document.
*
* @param id Id of the document
*/
export function get(id: string): Document {
return fromJSON<Document>(addon.documentsGet(id))
}
/**
* Read a document from the file system.
*
* @param id Id of the document
*/
export function read(id: string): string {
return addon.documentsRead(id)
}
/**
* Write the content of a document to the file system.
*
* @param id Id of the document
*/
export function write(id: string, content: string): string {
return addon.documentsWrite(id, content)
}
/**
* Write the content of a document to the file system.
*
* @param id Id of the document
* @param path Path of the new document
* @param format Format of the new document (inferred from path if not supplied)
* @param theme Theme for the new document (only applies to some formats e.g. HTML, PDF)
*/
export function writeAs(
id: string,
path: string,
format: string = '',
theme: string = ''
): string {
return addon.documentsWriteAs(id, path, format, theme)
}
/**
* Dump the current content of a document without reading it
* from the file system. The inverse of `load()`.
*
* @param id Id of the document
*/
export function dump(id: string, format?: string): string {
return addon.documentsDump(id, format ?? '')
}
/**
* Load content into a document without writing it
* to the file system. The inverse of `dump()`.
*
*
* @param id Id of the document
* @param content The content to load into the document
*/
export function load(id: string, content: string): void {
return addon.documentsLoad(id, content)
}
/**
* Subscribe to one or more of a document's topics.
*
* @param id Id of the document
* @param topic See Rust docs for `Document#subscriptions` for valid values
* @param subscriber A subscriber function that will receive published
* events for the document topic/s
*/
export function subscribe(
id: string,
topics: string[],
subscriber: (topic: string, event: DocumentEvent) => unknown
): void {
for (const topic of topics) {
addon.documentsSubscribe(id, topic)
pubsub.subscribe(
`documents:${id}:${topic}`,
subscriber as pubsub.Subscriber
)
}
}
/**
* Unsubscribe from one or more of a document's topics.
*
* @param id Id of the document
* @param subscriber A subscriber function that will receive published
* events for the document topic/s
*/
export function unsubscribe(id: string, topics: string[]): void {
for (const topic of topics) {
addon.documentsUnsubscribe(id, topic)
pubsub.unsubscribe(`documents:${id}:${topic}`)
}
}
/**
* Close a document.
*
* This will drop the document from memory and stop any
* associated file watching thread.
*
* @param id Id of the document
*/
export function close(id: string): void {
addon.documentsClose(id)
}