-
Notifications
You must be signed in to change notification settings - Fork 4
/
draft.js
153 lines (141 loc) · 6.55 KB
/
draft.js
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
import { Plug } from '/mindtouch-http.js/plug.js';
import { Settings } from './lib/settings.js';
import { utility } from './lib/utility.js';
import { modelParser } from './lib/modelParser.js';
import { PageBase } from './pageBase.js';
import { apiErrorModel } from './models/apiError.model.js';
import { pageModel } from './models/page.model.js';
const _errorParser = modelParser.createParser(apiErrorModel);
/**
* A class for managing a single unpublished draft page.
*/
export class Draft extends PageBase {
/**
* Construct a Draft object.
* @param {Number|String} [id=home] - The id of the draft to construct.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(id = 'home', settings = new Settings()) {
super(id);
this._settings = settings;
this._plug = new Plug(settings.host, settings.plugConfig).at('@api', 'deki', 'drafts', this._id);
}
/**
* Deactivate the current draft and revert to the published page.
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the deactivated page.
*/
deactivate() {
let pageModelParser = modelParser.createParser(pageModel);
return this._plug.at('deactivate').post().then((r) => r.json()).then(pageModelParser);
}
/**
* Publish the draft.
* @param {Object} [params] - the query params that will be used to publish the draft.
* @returns {Promise} - A Promise that, when resolved, indicates a successful publish operation.
*/
publish(params = {}) {
return this._plug.at('publish').withParams(params).post();
}
/**
* Unpublish a live page and create a draft out of it.
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the unpublished page.
*/
unpublish() {
return this._plug.at('unpublish').post().then((r) => r.json()).then(modelParser.createParser(pageModel));
}
/**
* Update display title for a draft
* @param {String} title - The new title for the draft
* @returns {Promise.<pageModel|Error>} - A Promise that will be resolved with the page data for the draft that had its title changed, or rejected with an error specifying the reason for rejection.
*/
setTitle(title) {
if(!title) {
return Promise.reject(new Error('A valid title must be supplied for the draft.'));
}
return this._plug.at('title').put(title, utility.textRequestType).then((r) => r.json()).then(modelParser.createParser(pageModel));
}
}
/**
* A class for managing unpublished draft pages.
*/
export class DraftManager {
/**
* Create a new DraftManager.
* @param {Settings} [settings] - The {@link Settings} information to use in construction. If not supplied, the default settings are used.
*/
constructor(settings = new Settings()) {
this._settings = settings;
this._plug = new Plug(this._settings.host, this._settings.plugConfig).at('@api', 'deki', 'drafts');
}
/**
* Create a new draft on the site where a page does not already exist.
* @param {String} newPath - The path of the new draft.
* @param {Object} [options] - the options that will be used to create the draft
* @param {Number} [options.redirect] - 0 or 1 to tell whether to follow redirects
* @param {Boolean} [options.deleteRedirects] - A boolean value that allows the deletion of redirects
* @returns {Promise.<pageModel>} - A Promise that, when resolved, yields a {@link pageModel} for the newly-created draft.
*/
createDraft(newPath, options = {}) {
const params = {};
if('redirect' in options) {
if(typeof options.redirect !== 'number') {
return Promise.reject(new Error('The redirect option must be a number.'));
}
params.redirect = options.redirect;
}
if('deleteRedirects' in options) {
if(typeof options.deleteRedirects !== 'boolean') {
return Promise.reject(new Error('The deleteredirects option must be a boolean.'));
}
params.deleteRedirects = options.deleteRedirects;
}
return this._plug.at(utility.getResourceId(newPath), 'create').withParams(params).post()
.catch((err) => Promise.reject(_errorParser(err)))
.then((r) => r.json()).then(modelParser.createParser(pageModel));
}
/**
* Get a list of drafts filtered by options.
* @param {Object} [options] - The options that will filter the resulting list of drafts.
* @param {Number|String} [options.parentId] - Only return pages that live under this page id.
* @param {Array} [options.tags] - An array of tags to filter the pages by.
* @param {Number} [options.limit=10] - The maximum number of pages to return (not to exceed 1000)
* @param {Array} [options.include] - An array of elements to include. Currently, only 'tags' is allowed.
* @returns {Promise.<Object|Error>} - A Promise that will be resolved with the drafts listing data, or rejected with an error specifying the reason for rejection.
*/
getDrafts(options = {}) {
const params = {};
if(options.parentId) {
params.parentid = utility.getResourceId(options.parentId, 'home');
}
if(options.tags) {
if(!Array.isArray(options.tags)) {
return Promise.reject(new Error('The `tags` parameter must be an array.'));
}
params.tags = options.tags.join(',');
}
if('limit' in options) {
if(typeof options.limit !== 'number') {
return Promise.reject(new Error('The `limit` parameter must be an number.'));
}
params.limit = options.limit;
}
if(options.include) {
if(!Array.isArray(options.include)) {
return Promise.reject(new Error('The `include` parameter must be an array.'));
}
params.include = options.include.join(',');
}
return this._plug.withParams(params)
.get()
.then((r) => r.json())
.then(modelParser.createParser([ { field: [ 'pages', 'page' ], name: 'pages', isArray: true, transform: pageModel } ]));
}
/**
* Fetch a new Draft object by ID.
* @param {Number|String} [id=home] - The id of the draft to return.
* @returns {Draft} - A new {@link Draft} object.
*/
getDraft(id) {
return new Draft(id, this._settings);
}
}