-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathpage.ts
More file actions
268 lines (225 loc) · 5.69 KB
/
page.ts
File metadata and controls
268 lines (225 loc) · 5.69 KB
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
import urlify from '../utils/urlify.js';
import database, {isEqualIds} from '../database/index.js';
import { EntityId } from '../database/types.js';
const pagesDb = database['pages'];
/**
* @typedef {object} PageData
* @property {string} _id - page id
* @property {string} title - page title
* @property {string} uri - page uri
* @property {*} body - page body
* @property {string} parent - id of parent page
*/
export interface PageData {
_id?: EntityId;
title?: string;
uri?: string;
body?: any;
parent?: EntityId;
}
/**
* @class Page
* @class Page model
* @property {string} _id - page id
* @property {string} title - page title
* @property {string} uri - page uri
* @property {*} body - page body
* @property {string} _parent - id of parent page
*/
class Page {
public _id?: EntityId;
public body?: any;
public title?: string;
public uri?: string;
public _parent?: EntityId;
/**
* @class
* @param {PageData} data - page's data
*/
constructor(data: PageData = {}) {
if (data === null) {
data = {};
}
if (data._id) {
this._id = data._id;
}
this.data = data;
}
/**
* Find and return model of page with given id
*
* @param {string} _id - page id
* @returns {Promise<Page>}
*/
public static async get(_id: EntityId): Promise<Page> {
const data = await pagesDb.findOne({ _id });
return new Page(data);
}
/**
* Find and return model of page with given uri
*
* @param {string} uri - page uri
* @returns {Promise<Page>}
*/
public static async getByUri(uri: string): Promise<Page> {
const data = await pagesDb.findOne({ uri });
return new Page(data);
}
/**
* Find all pages which match passed query object
*
* @param {object} query - input query
* @returns {Promise<Page[]>}
*/
public static async getAll(query: Record<string, unknown> = {}): Promise<Page[]> {
const docs = await pagesDb.find(query);
return docs.map(doc => new Page(doc));
}
/**
* Set PageData object fields to internal model fields
*
* @param {PageData} pageData - page's data
*/
public set data(pageData: PageData) {
const { body, parent, uri } = pageData;
this.body = body || this.body;
this.title = this.extractTitleFromBody();
this.uri = uri || '';
this._parent = parent || this._parent || '0' as EntityId;
}
/**
* Return PageData object
*
* @returns {PageData}
*/
public get data(): PageData {
return {
_id: this._id,
title: this.title,
uri: this.uri,
body: this.body,
parent: this._parent,
};
}
/**
* Link given page as parent
*
* @param {Page} parentPage - the page to be set as parent
*/
public set parent(parentPage: Page) {
this._parent = parentPage._id;
}
/**
* Return parent page model
*
* @returns {Promise<Page>}
*/
public async getParent(): Promise<Page> {
const data = await pagesDb.findOne({ _id: this._parent });
return new Page(data);
}
/**
* Ancestors from root to immediate parent (for breadcrumbs). Omits virtual root id "0".
*/
public async getAncestorChain(): Promise<Page[]> {
const chain: Page[] = [];
let parentId = this._parent;
while (parentId && !isEqualIds(parentId, '0' as EntityId)) {
const data = await pagesDb.findOne({ _id: parentId });
if (!data?._id) {
break;
}
const ancestor = new Page(data);
chain.unshift(ancestor);
parentId = ancestor._parent;
}
return chain;
}
/**
* Return child pages models
*
* @returns {Promise<Page[]>}
*/
public get children(): Promise<Page[]> {
return pagesDb.find({ parent: this._id })
.then(data => {
return data.map(page => new Page(page));
});
}
/**
* Save or update page data in the database
*
* @returns {Promise<Page>}
*/
public async save(): Promise<Page> {
if (this.uri !== undefined) {
this.uri = await this.composeUri(this.uri);
}
if (!this._id) {
const insertedRow = await pagesDb.insert(this.data) as { _id: EntityId };
this._id = insertedRow._id;
} else {
await pagesDb.update({ _id: this._id }, this.data);
}
return this;
}
/**
* Remove page data from the database
*
* @returns {Promise<Page>}
*/
public async destroy(): Promise<Page> {
await pagesDb.remove({ _id: this._id });
delete this._id;
return this;
}
/**
* Return readable page data
*
* @returns {PageData}
*/
public toJSON(): PageData {
return this.data;
}
/**
* Find and return available uri
*
* @returns {Promise<string>}
* @param uri - input uri to be composed
*/
private async composeUri(uri: string): Promise<string> {
let pageWithSameUriCount = 0;
if (!this._id) {
uri = this.transformTitleToUri();
}
if (uri) {
let pageWithSameUri = await Page.getByUri(uri);
while (pageWithSameUri._id && !isEqualIds(pageWithSameUri._id, this._id)) {
pageWithSameUriCount++;
pageWithSameUri = await Page.getByUri(uri + `-${pageWithSameUriCount}`);
}
}
return pageWithSameUriCount ? uri + `-${pageWithSameUriCount}` : uri;
}
/**
* Extract first header from editor data
*
* @returns {string}
*/
private extractTitleFromBody(): string {
const headerBlock = this.body ? this.body.blocks.find((block: Record<string, unknown>) => block.type === 'header') : '';
return headerBlock ? headerBlock.data.text : '';
}
/**
* Transform title for uri
*
* @returns {string}
*/
private transformTitleToUri(): string {
if (this.title === undefined) {
return '';
}
return urlify(this.title);
}
}
export default Page;