@silexlabs/silex 3.0.0-alpha.43 → 3.0.0-alpha.45

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.
Files changed (102) hide show
  1. package/.env.default +18 -0
  2. package/dist/client/client/api.d.ts +6 -6
  3. package/dist/client/client/config.d.ts +26 -211
  4. package/dist/client/client/events.d.ts +2 -0
  5. package/dist/client/client/expose.d.ts +0 -1
  6. package/dist/client/client/grapesjs/LoginDialog.d.ts +1 -0
  7. package/dist/client/client/grapesjs/index.d.ts +3 -209
  8. package/dist/client/client/index.d.ts +1 -0
  9. package/dist/client/constants.d.ts +2 -3
  10. package/dist/client/css/admin.css +2 -0
  11. package/dist/client/js/main.js +47 -47
  12. package/dist/client/types.d.ts +7 -1
  13. package/dist/plugins/client/client/events.d.ts +2 -0
  14. package/dist/plugins/client/client/events.js +2 -0
  15. package/dist/plugins/client/client/events.js.map +1 -1
  16. package/dist/plugins/client/plugins/client/template.js +4 -4
  17. package/dist/plugins/client/plugins/client/template.js.map +1 -1
  18. package/dist/plugins/client/types.d.ts +7 -1
  19. package/dist/plugins/client/types.js +8 -0
  20. package/dist/plugins/client/types.js.map +1 -1
  21. package/dist/plugins/server/constants.d.ts +2 -3
  22. package/dist/plugins/server/constants.js +3 -4
  23. package/dist/plugins/server/constants.js.map +1 -1
  24. package/dist/plugins/server/plugins/server/FtpConnector.d.ts +17 -28
  25. package/dist/plugins/server/plugins/server/FtpConnector.js +16 -16
  26. package/dist/plugins/server/plugins/server/FtpConnector.js.map +1 -1
  27. package/dist/plugins/server/plugins/server/GitlabConnector.d.ts +81 -0
  28. package/dist/plugins/server/plugins/server/GitlabConnector.js +373 -0
  29. package/dist/plugins/server/plugins/server/GitlabConnector.js.map +1 -0
  30. package/dist/plugins/server/server/config.js +1 -1
  31. package/dist/plugins/server/server/config.js.map +1 -1
  32. package/dist/plugins/server/server/connectors/FsStorage.d.ts +13 -12
  33. package/dist/plugins/server/server/connectors/FsStorage.js +22 -15
  34. package/dist/plugins/server/server/connectors/FsStorage.js.map +1 -1
  35. package/dist/plugins/server/server/connectors/connectors.d.ts +5 -2
  36. package/dist/plugins/server/server/connectors/connectors.js +31 -3
  37. package/dist/plugins/server/server/connectors/connectors.js.map +1 -1
  38. package/dist/plugins/server/types.d.ts +7 -1
  39. package/dist/plugins/server/types.js +10 -1
  40. package/dist/plugins/server/types.js.map +1 -1
  41. package/dist/server/constants.js +3 -4
  42. package/dist/server/server/api/connectorApi.js +16 -11
  43. package/dist/server/server/api/publicationApi.js +18 -20
  44. package/dist/server/server/api/websiteApi.js +36 -40
  45. package/dist/server/server/cli.js +34 -16
  46. package/dist/server/server/config.js +1 -1
  47. package/dist/server/server/connectors/FsStorage.js +19 -15
  48. package/dist/server/server/connectors/connectors.js +27 -3
  49. package/dist/server/server/expose.js +27 -0
  50. package/dist/server/server/index.js +28 -2
  51. package/dist/server/types.js +10 -1
  52. package/package.json +4 -1
  53. package/src/css/_variables.scss +1 -0
  54. package/src/css/loader.scss +2 -0
  55. package/src/ts/client/api.ts +8 -11
  56. package/src/ts/client/config.ts +30 -14
  57. package/src/ts/client/events.ts +2 -0
  58. package/src/ts/client/expose.ts +0 -1
  59. package/src/ts/client/grapesjs/LoginDialog.ts +12 -1
  60. package/src/ts/client/grapesjs/PublicationManager.ts +1 -1
  61. package/src/ts/client/grapesjs/index.ts +8 -13
  62. package/src/ts/client/grapesjs/storage.ts +18 -10
  63. package/src/ts/client/index.ts +32 -14
  64. package/src/ts/constants.ts +2 -3
  65. package/src/ts/plugins/client/template.ts +4 -4
  66. package/src/ts/plugins/server/FtpConnector.test.ts +82 -0
  67. package/src/ts/plugins/server/FtpConnector.ts +30 -37
  68. package/src/ts/plugins/server/GitlabConnector.ts +434 -0
  69. package/src/ts/server/api/connectorApi.ts +31 -26
  70. package/src/ts/server/api/publicationApi.ts +23 -23
  71. package/src/ts/server/api/websiteApi.ts +42 -43
  72. package/src/ts/server/cli.ts +23 -11
  73. package/src/ts/server/config.ts +1 -1
  74. package/src/ts/server/connectors/FsStorage.test.ts +51 -0
  75. package/src/ts/server/connectors/FsStorage.ts +35 -25
  76. package/src/ts/server/connectors/connectors.ts +32 -4
  77. package/{dist/plugins/client/server/utils/validation.js → src/ts/server/expose.ts} +3 -18
  78. package/src/ts/server/index.ts +24 -2
  79. package/src/ts/types.ts +10 -1
  80. package/dist/plugins/client/constants.d.ts +0 -30
  81. package/dist/plugins/client/constants.js +0 -47
  82. package/dist/plugins/client/constants.js.map +0 -1
  83. package/dist/plugins/client/server/config.d.ts +0 -59
  84. package/dist/plugins/client/server/config.js +0 -178
  85. package/dist/plugins/client/server/config.js.map +0 -1
  86. package/dist/plugins/client/server/connectors/FsHosting.d.ts +0 -13
  87. package/dist/plugins/client/server/connectors/FsHosting.js +0 -51
  88. package/dist/plugins/client/server/connectors/FsHosting.js.map +0 -1
  89. package/dist/plugins/client/server/connectors/FsStorage.d.ts +0 -41
  90. package/dist/plugins/client/server/connectors/FsStorage.js +0 -220
  91. package/dist/plugins/client/server/connectors/FsStorage.js.map +0 -1
  92. package/dist/plugins/client/server/connectors/connectors.d.ts +0 -83
  93. package/dist/plugins/client/server/connectors/connectors.js +0 -91
  94. package/dist/plugins/client/server/connectors/connectors.js.map +0 -1
  95. package/dist/plugins/client/server/events.d.ts +0 -26
  96. package/dist/plugins/client/server/events.js +0 -12
  97. package/dist/plugins/client/server/events.js.map +0 -1
  98. package/dist/plugins/client/server/jobs.d.ts +0 -6
  99. package/dist/plugins/client/server/jobs.js +0 -67
  100. package/dist/plugins/client/server/jobs.js.map +0 -1
  101. package/dist/plugins/client/server/utils/validation.d.ts +0 -7
  102. package/dist/plugins/client/server/utils/validation.js.map +0 -1
@@ -34,6 +34,10 @@ const validation_1 = require("../utils/validation");
34
34
  const constants_1 = require("../../constants");
35
35
  const stream_1 = require("stream");
36
36
  const uuid_1 = require("uuid");
37
+ if (!globalThis.__dirname) {
38
+ globalThis.__dirname = (0, path_1.dirname)(process.cwd() + '/src/ts/server/connectors/FsStorage.ts');
39
+ console.log('Redefining __dirname', globalThis.__dirname);
40
+ }
37
41
  const USER_ICON = 'data:image/svg+xml,%3Csvg xmlns=\'http://www.w3.org/2000/svg\' height=\'1em\' viewBox=\'0 0 448 512\'%3E%3Cpath d=\'M304 128a80 80 0 1 0 -160 0 80 80 0 1 0 160 0zM96 128a128 128 0 1 1 256 0A128 128 0 1 1 96 128zM49.3 464H398.7c-8.9-63.3-63.3-112-129-112H178.3c-65.7 0-120.1 48.7-129 112zM0 482.3C0 383.8 79.8 304 178.3 304h91.4C368.2 304 448 383.8 448 482.3c0 16.4-13.3 29.7-29.7 29.7H29.7C13.3 512 0 498.7 0 482.3z\'/%3E%3C/svg%3E';
38
42
  const FILE_ICON = 'data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20stroke%3D%22currentColor%22%20stroke-width%3D%222%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%3E%3Cpath%20d%3D%22M6%202L6%2022%2018%2022%2018%207%2012%202%206%202Z%22%3E%3C%2Fpath%3E%3Cpath%20d%3D%22M18%202L12%202%2012%208%2018%208%2018%202Z%22%3E%3C%2Fpath%3E%3C%2Fsvg%3E';
39
43
  class FsStorage {
@@ -43,9 +47,12 @@ class FsStorage {
43
47
  disableLogout = true;
44
48
  options;
45
49
  connectorType = types_1.ConnectorType.STORAGE;
50
+ color = '#ffffff';
51
+ background = '#006400';
46
52
  constructor(config, opts) {
47
53
  this.options = {
48
- path: __dirname + '../../../../.silex',
54
+ path: (0, path_1.join)(__dirname, '..', '..', '..', '..', 'data'),
55
+ assetsFolder: '/assets',
49
56
  ...opts,
50
57
  };
51
58
  this.initFs();
@@ -54,7 +61,7 @@ class FsStorage {
54
61
  const stat = await promises_1.default.stat(this.options.path).catch(() => null);
55
62
  if (!stat) {
56
63
  const id = constants_1.DEFAULT_WEBSITE_ID;
57
- await promises_1.default.mkdir((0, path_1.join)(this.options.path, id), { recursive: true });
64
+ await promises_1.default.mkdir((0, path_1.join)(this.options.path, id, this.options.assetsFolder), { recursive: true });
58
65
  await this.setWebsiteMeta({}, id, { name: 'Default website', connectorUserSettings: {} });
59
66
  await this.updateWebsite({}, id, types_1.defaultWebsiteData);
60
67
  }
@@ -75,9 +82,7 @@ class FsStorage {
75
82
  getOptions(formData) {
76
83
  return {};
77
84
  }
78
- async getOAuthUrl(session) {
79
- return null;
80
- }
85
+ async getOAuthUrl(session) { return null; }
81
86
  async getLoginForm(session, redirectTo) {
82
87
  return null;
83
88
  }
@@ -120,7 +125,7 @@ class FsStorage {
120
125
  }
121
126
  async createWebsite(session, meta) {
122
127
  const id = (0, uuid_1.v4)();
123
- await promises_1.default.mkdir((0, path_1.join)(this.options.path, id), { recursive: true });
128
+ await promises_1.default.mkdir((0, path_1.join)(this.options.path, id, this.options.assetsFolder), { recursive: true });
124
129
  await this.setWebsiteMeta(session, id, meta);
125
130
  await this.updateWebsite(session, id, types_1.defaultWebsiteData);
126
131
  return id;
@@ -149,18 +154,16 @@ class FsStorage {
149
154
  }));
150
155
  }
151
156
  async getAsset(session, id, path) {
152
- const fullPath = (0, path_1.join)(this.options.path, id, path);
157
+ const fullPath = (0, path_1.join)(this.options.path, id, this.options.assetsFolder, path);
153
158
  const content = await promises_1.default.readFile(fullPath);
154
159
  return { path, content };
155
160
  }
156
161
  async writeAssets(session, id, files, statusCbk) {
157
162
  const filesStatuses = this.initStatus(files);
158
- let error = false;
159
- const filePaths = [];
163
+ let error = null;
160
164
  for (const fileStatus of filesStatuses) {
161
165
  const { file } = fileStatus;
162
- const path = (0, path_1.join)(this.options.path, id, file.path);
163
- filePaths.push(path);
166
+ const path = (0, path_1.join)(this.options.path, id, this.options.assetsFolder, file.path);
164
167
  if (typeof file.content === 'string' || Buffer.isBuffer(file.content)) {
165
168
  fileStatus.message = 'Writing';
166
169
  this.updateStatus(filesStatuses, types_1.JobStatus.IN_PROGRESS, statusCbk);
@@ -170,7 +173,7 @@ class FsStorage {
170
173
  catch (err) {
171
174
  fileStatus.message = `Error (${err})`;
172
175
  this.updateStatus(filesStatuses, types_1.JobStatus.IN_PROGRESS, statusCbk);
173
- error = true;
176
+ error = err;
174
177
  continue;
175
178
  }
176
179
  fileStatus.message = 'Success';
@@ -191,7 +194,7 @@ class FsStorage {
191
194
  console.error('writeStream error', err);
192
195
  fileStatus.message = `Error (${err})`;
193
196
  this.updateStatus(filesStatuses, types_1.JobStatus.IN_PROGRESS, statusCbk);
194
- error = true;
197
+ error = err;
195
198
  resolve(file);
196
199
  });
197
200
  });
@@ -202,7 +205,8 @@ class FsStorage {
202
205
  }
203
206
  }
204
207
  this.updateStatus(filesStatuses, error ? types_1.JobStatus.ERROR : types_1.JobStatus.SUCCESS, statusCbk);
205
- return filePaths;
208
+ if (error)
209
+ throw error;
206
210
  }
207
211
  async deleteAssets(session, id, paths) {
208
212
  for (const path of paths) {
@@ -211,7 +215,7 @@ class FsStorage {
211
215
  }
212
216
  async readAsset(session, websiteId, fileName) {
213
217
  const id = (0, validation_1.requiredParam)(websiteId, 'website id');
214
- const path = (0, path_1.join)(this.options.path, id, fileName);
218
+ const path = (0, path_1.join)(this.options.path, id, this.options.assetsFolder, fileName);
215
219
  return await promises_1.default.readFile(path);
216
220
  }
217
221
  }
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.contentToReadable = exports.contentToString = exports.toConnectorData = exports.getConnector = exports.toConnectorEnum = void 0;
3
+ exports.contentToReadable = exports.contentToBuffer = exports.contentToString = exports.toConnectorData = exports.getConnector = exports.toConnectorEnum = void 0;
4
4
  const stream_1 = require("stream");
5
5
  const types_1 = require("../../types");
6
6
  function toConnectorEnum(type) {
7
- const result = types_1.ConnectorType[type.toUpperCase()];
7
+ const result = types_1.ConnectorType[type.toString().toUpperCase()];
8
8
  if (!result)
9
- throw new Error('Unknown connector ' + type);
9
+ throw new Error(`Unknown connector type ${type}. It should be one of ${Object.keys(types_1.ConnectorType).join(', ')}`);
10
10
  return result;
11
11
  }
12
12
  exports.toConnectorEnum = toConnectorEnum;
@@ -31,6 +31,8 @@ async function toConnectorData(session, connector) {
31
31
  disableLogout: !!connector.disableLogout,
32
32
  isLoggedIn: await connector.isLoggedIn(session),
33
33
  oauthUrl: await connector.getOAuthUrl(session),
34
+ color: connector.color,
35
+ background: connector.background,
34
36
  };
35
37
  }
36
38
  exports.toConnectorData = toConnectorData;
@@ -53,6 +55,28 @@ async function contentToString(content) {
53
55
  });
54
56
  }
55
57
  exports.contentToString = contentToString;
58
+ async function contentToBuffer(content) {
59
+ if (typeof content === 'string')
60
+ return Buffer.from(content, 'utf8');
61
+ if (Buffer.isBuffer(content))
62
+ return content;
63
+ if (content instanceof stream_1.Readable) {
64
+ return new Promise((resolve, reject) => {
65
+ const chunks = [];
66
+ content.on('data', (chunk) => {
67
+ chunks.push(chunk);
68
+ });
69
+ content.on('error', (err) => {
70
+ reject(err);
71
+ });
72
+ content.on('end', () => {
73
+ resolve(Buffer.concat(chunks));
74
+ });
75
+ });
76
+ }
77
+ throw new Error('Unknown content type');
78
+ }
79
+ exports.contentToBuffer = contentToBuffer;
56
80
  function contentToReadable(content) {
57
81
  if (typeof content === 'string')
58
82
  return stream_1.Readable.from([content]);
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.events = void 0;
27
+ exports.events = __importStar(require("./events"));
@@ -22,6 +22,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
25
28
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
29
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
30
  };
@@ -30,9 +33,32 @@ const silexApp = __importStar(require("./express"));
30
33
  const api_1 = __importDefault(require("./api/api"));
31
34
  const config_1 = require("./config");
32
35
  const dotenv_1 = __importDefault(require("dotenv"));
36
+ const path_1 = require("path");
37
+ __exportStar(require("./expose"), exports);
33
38
  async function silex() {
34
- dotenv_1.default.config();
35
- dotenv_1.default.config({ path: '.env.default' });
39
+ const curDirEnv = dotenv_1.default.config();
40
+ if (curDirEnv.error) {
41
+ if (curDirEnv.error['code'] === 'ENOENT') {
42
+ console.info('> No .env file found in current directory');
43
+ }
44
+ else {
45
+ throw curDirEnv.error;
46
+ }
47
+ }
48
+ else {
49
+ console.info('> Env vars loaded from .env file');
50
+ }
51
+ const DEFAULT_ENV_FILE = (0, path_1.join)(__dirname, '../../../.env.default');
52
+ const rootDirEnv = dotenv_1.default.config({ path: DEFAULT_ENV_FILE });
53
+ if (rootDirEnv.error) {
54
+ if (rootDirEnv.error['code'] === 'ENOENT') {
55
+ throw new Error(`\n\nFailed to load default env vars. File not found ${DEFAULT_ENV_FILE}\n\n`);
56
+ }
57
+ else {
58
+ throw new Error(`\n\nFailed to load default env vars. Error in ${DEFAULT_ENV_FILE}: ${rootDirEnv.error.message}\n\n`);
59
+ }
60
+ }
61
+ console.info('> Default env vars loaded');
36
62
  const config = new config_1.ServerConfig();
37
63
  const app = silexApp.create(config);
38
64
  await config.addRoutes(app);
@@ -1,6 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JobStatus = exports.ConnectorType = exports.Unit = exports.defaultWebsiteData = void 0;
3
+ exports.JobStatus = exports.ConnectorType = exports.Unit = exports.defaultWebsiteData = exports.ApiError = void 0;
4
+ class ApiError extends Error {
5
+ httpStatusCode;
6
+ constructor(message, httpStatusCode) {
7
+ super(message);
8
+ this.httpStatusCode = httpStatusCode;
9
+ console.info('API error', httpStatusCode, message);
10
+ }
11
+ }
12
+ exports.ApiError = ApiError;
4
13
  exports.defaultWebsiteData = {};
5
14
  var Unit;
6
15
  (function (Unit) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@silexlabs/silex",
3
3
  "description": "Free and easy website builder for everyone.",
4
- "version": "3.0.0-alpha.43",
4
+ "version": "3.0.0-alpha.45",
5
5
  "version:backwardcompat": "3.0.0",
6
6
  "author": {
7
7
  "name": "Alex Hoyau",
@@ -17,6 +17,7 @@
17
17
  "public",
18
18
  ".silex.js",
19
19
  ".silex",
20
+ ".env.default",
20
21
  "package.json",
21
22
  "package-lock.json",
22
23
  "README.md",
@@ -101,6 +102,7 @@
101
102
  "dependencies": {
102
103
  "@directus/sdk": "10.3.3",
103
104
  "@fortawesome/fontawesome-free": "6.4.0",
105
+ "@gitbeaker/rest": "^39.7.0",
104
106
  "@silexlabs/grapesjs-directus-storage": "1.0.15",
105
107
  "@silexlabs/grapesjs-fonts": "1.0.12",
106
108
  "@silexlabs/grapesjs-loading": "1.0.8",
@@ -116,6 +118,7 @@
116
118
  "cookie-session": "2.0.0",
117
119
  "cors": "2.8.5",
118
120
  "cross-env": "7.0.3",
121
+ "crypto": "^1.0.1",
119
122
  "dotenv": "^16.3.1",
120
123
  "express": "4.18.2",
121
124
  "express-force-ssl": "0.3.2",
@@ -22,6 +22,7 @@ $prefix: 'gjs-';
22
22
  $pn-prefix: '#{$prefix}pn-';
23
23
  $cv-prefix: '#{$prefix}cv-';
24
24
  $z-dialog-inline: 10;
25
+ $z-loader: 10;
25
26
 
26
27
  :root {
27
28
  --primaryColor: #444;
@@ -7,4 +7,6 @@
7
7
  color: variables.$secondaryColor;
8
8
  justify-content: center;
9
9
  align-items: center;
10
+ background-color: variables.$primaryColor;
11
+ z-index: variables.$z-loader;
10
12
  }
@@ -15,8 +15,8 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- import { API_CONNECTOR_LIST, API_CONNECTOR_USER, API_CONNECTOR_LOGOUT, API_CONNECTOR_PATH, API_PATH, API_PUBLICATION_PATH, API_PUBLICATION_PUBLISH, API_PUBLICATION_STATUS, API_WEBSITE_ASSETS_READ, API_WEBSITE_ASSETS_WRITE, API_WEBSITE_DELETE, API_WEBSITE_LIST, API_WEBSITE_PATH, API_WEBSITE_READ, API_WEBSITE_WRITE, API_WEBSITE_META_READ, API_WEBSITE_META_WRITE, API_WEBSITE_CREATE } from '../constants'
19
- import { ApiPublicationPublishBody, ApiPublicationPublishQuery, ApiPublicationPublishResponse, ApiPublicationStatusQuery, ApiPublicationStatusResponse, ConnectorId, JobData, JobId, PublicationJobData, WebsiteId, ApiConnectorListResponse, ApiConnectorListQuery, ConnectorData, ConnectorType, ApiWebsiteReadQuery, ApiWebsiteReadResponse, WebsiteData, ApiWebsiteWriteQuery, ApiWebsiteWriteBody, ApiWebsiteWriteResponse, ApiWebsiteDeleteQuery, ApiWebsiteAssetsReadQuery, ApiWebsiteAssetsReadResponse, ApiWebsiteAssetsWriteQuery, ApiWebsiteAssetsWriteBody, ApiWebsiteAssetsWriteResponse, ClientSideFile, PublicationData, ApiConnectorUserResponse, ConnectorUser, WebsiteMeta, ApiConnectorLogoutQuery, ApiConnectorUserQuery, ApiWebsiteListResponse, ApiWebsiteListQuery, WebsiteMetaFileContent, ApiWebsiteCreateQuery, ApiWebsiteCreateBody, ApiWebsiteCreateResponse, ApiWebsiteMetaWriteQuery, ApiWebsiteMetaWriteBody, ApiWebsiteMetaWriteResponse, ApiWebsiteMetaReadQuery, ApiWebsiteMetaReadResponse, ConnectorOptions } from '../types'
18
+ import { API_CONNECTOR_LIST, API_CONNECTOR_USER, API_CONNECTOR_LOGOUT, API_CONNECTOR_PATH, API_PATH, API_PUBLICATION_PATH, API_PUBLICATION_PUBLISH, API_PUBLICATION_STATUS, API_WEBSITE_ASSET_READ, API_WEBSITE_ASSETS_WRITE, API_WEBSITE_DELETE, API_WEBSITE_LIST, API_WEBSITE_PATH, API_WEBSITE_READ, API_WEBSITE_WRITE, API_WEBSITE_META_READ, API_WEBSITE_META_WRITE, API_WEBSITE_CREATE } from '../constants'
19
+ import { ApiPublicationPublishBody, ApiPublicationPublishQuery, ApiPublicationPublishResponse, ApiPublicationStatusQuery, ApiPublicationStatusResponse, ConnectorId, JobData, JobId, PublicationJobData, WebsiteId, ApiConnectorListResponse, ApiConnectorListQuery, ConnectorData, ConnectorType, ApiWebsiteReadQuery, ApiWebsiteReadResponse, WebsiteData, ApiWebsiteWriteQuery, ApiWebsiteWriteBody, ApiWebsiteWriteResponse, ApiWebsiteDeleteQuery, ApiWebsiteAssetsReadQuery, ApiWebsiteAssetsReadResponse, ApiWebsiteAssetsWriteQuery, ApiWebsiteAssetsWriteBody, ApiWebsiteAssetsWriteResponse, ClientSideFile, PublicationData, ApiConnectorUserResponse, ConnectorUser, WebsiteMeta, ApiConnectorLogoutQuery, ApiConnectorUserQuery, ApiWebsiteListResponse, ApiWebsiteListQuery, WebsiteMetaFileContent, ApiWebsiteCreateQuery, ApiWebsiteCreateBody, ApiWebsiteCreateResponse, ApiWebsiteMetaWriteQuery, ApiWebsiteMetaWriteBody, ApiWebsiteMetaWriteResponse, ApiWebsiteMetaReadQuery, ApiWebsiteMetaReadResponse, ConnectorOptions, ApiError } from '../types'
20
20
 
21
21
  export enum ApiRoute {
22
22
  PUBLICATION_PUBLISH = API_PATH + API_PUBLICATION_PATH + API_PUBLICATION_PUBLISH,
@@ -29,19 +29,12 @@ export enum ApiRoute {
29
29
  WEBSITE_DELETE = API_PATH + API_WEBSITE_PATH + API_WEBSITE_DELETE,
30
30
  WEBSITE_CREATE = API_PATH + API_WEBSITE_PATH + API_WEBSITE_CREATE,
31
31
  WEBSITE_LIST = API_PATH + API_WEBSITE_PATH + API_WEBSITE_LIST,
32
- WEBSITE_ASSETS_READ = API_PATH + API_WEBSITE_PATH + API_WEBSITE_ASSETS_READ,
32
+ WEBSITE_ASSETS_READ = API_PATH + API_WEBSITE_PATH + API_WEBSITE_ASSET_READ,
33
33
  WEBSITE_ASSETS_WRITE = API_PATH + API_WEBSITE_PATH + API_WEBSITE_ASSETS_WRITE,
34
34
  WEBSITE_META_READ = API_PATH + API_WEBSITE_PATH + API_WEBSITE_META_READ,
35
35
  WEBSITE_META_WRITE = API_PATH + API_WEBSITE_PATH + API_WEBSITE_META_WRITE,
36
36
  }
37
37
 
38
- export class ApiError extends Error {
39
- constructor(message: string, public readonly code: number) {
40
- super(message)
41
- console.info('API error', code, message)
42
- }
43
- }
44
-
45
38
  const ROOT_URL = window.location.origin
46
39
 
47
40
  export async function getUser({type, connectorId}: {type: ConnectorType, connectorId?: ConnectorId}): Promise<ConnectorUser> {
@@ -110,13 +103,17 @@ export async function websiteAssetsLoad({path, websiteId, connectorId}: {path: s
110
103
  return api<ApiWebsiteAssetsReadQuery, null, ApiWebsiteAssetsReadResponse>(`${ApiRoute.WEBSITE_ASSETS_READ}/${path}`, 'GET', { websiteId, connectorId: connectorId })
111
104
  }
112
105
 
106
+ /**
107
+ * Not used directly, grapesjs handles the upload
108
+ * @see assetManager in src/ts/client/grapesjs/index.ts
109
+ */
113
110
  export async function websiteAssetsSave({websiteId, connectorId, files}: {websiteId: WebsiteId, connectorId: ConnectorId, files: ClientSideFile[]}): Promise<string[]> {
114
111
  const { data } = await api<ApiWebsiteAssetsWriteQuery, ApiWebsiteAssetsWriteBody, ApiWebsiteAssetsWriteResponse>(ApiRoute.WEBSITE_ASSETS_WRITE, 'POST', { websiteId, connectorId: connectorId }, files)
115
112
  return data
116
113
  }
117
114
 
118
115
  export async function api<ReqQuery, ReqBody, ResBody>(route: ApiRoute | string, method: string, query?: ReqQuery, payload?: ReqBody): Promise<ResBody> {
119
- const url = `${ROOT_URL}${route.toString()}?${new URLSearchParams(Object.entries(query).filter(([key, value]) => typeof value != 'undefined')).toString()}`
116
+ const url = `${ROOT_URL}${route.toString()}?${new URLSearchParams(Object.entries(query).filter(([key, value]) => !!value)).toString()}`
120
117
  const response = await fetch(url, {
121
118
  method,
122
119
  headers: {
@@ -15,43 +15,59 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- import { Config, Plugin } from '@silexlabs/silex-plugins'
18
+ import { Config } from '@silexlabs/silex-plugins'
19
19
  import { getEditorConfig } from './grapesjs'
20
- import { CLIENT_CONFIG_FILE_NAME, DEFAULT_CONNECTOR_ID, DEFAULT_LANGUAGE, DEFAULT_WEBSITE_ID } from '../constants'
20
+ import { CLIENT_CONFIG_FILE_NAME, DEFAULT_LANGUAGE, DEFAULT_WEBSITE_ID } from '../constants'
21
+ import { ConnectorId, WebsiteId } from '../types'
22
+ import { EditorConfig } from 'grapesjs'
21
23
 
22
24
  /**
23
25
  * @fileoverview Silex client side config
24
26
  */
25
27
 
26
- const id = new URL(location.href).searchParams.get('id') ?? DEFAULT_WEBSITE_ID
27
- const connectorId = new URL(location.href).searchParams.get('connectorId') ?? DEFAULT_CONNECTOR_ID
28
- const lang = new URL(location.href).searchParams.get('lang') ?? DEFAULT_LANGUAGE
29
- const rootUrl = window.location.origin
28
+ export class ClientConfig extends Config {
29
+ /**
30
+ * The website to load
31
+ * This is the id of the website in the storage connector
32
+ */
33
+ websiteId: WebsiteId = new URL(location.href).searchParams.get('id') ?? DEFAULT_WEBSITE_ID
30
34
 
31
- export class SilexConfig extends Config {
32
35
  /**
33
- * debug mode
36
+ * The storage connector to use
37
+ * If not found in the URL and the user is not logged in to any storage, use the first storage
34
38
  */
35
- debug = false
39
+ storageId: ConnectorId = new URL(location.href).searchParams.get('connectorId')
36
40
 
37
41
  /**
38
42
  * language for I18n module
39
43
  */
40
- lang = lang
44
+ lang = new URL(location.href).searchParams.get('lang') ?? DEFAULT_LANGUAGE
45
+
46
+ /**
47
+ * root url of Silex app
48
+ */
49
+ rootUrl = window.location.origin
50
+
51
+ /**
52
+ * debug mode
53
+ */
54
+ debug = false
41
55
 
42
56
  /**
43
57
  * Grapesjs config
44
58
  */
45
- grapesJsConfig = getEditorConfig(id, connectorId, rootUrl)
59
+ grapesJsConfig: EditorConfig
46
60
 
47
61
  /**
48
62
  * Client config url
49
63
  * This is the url of the config file which is a plugin
50
64
  */
51
- clientConfigUrl = `${rootUrl}/${CLIENT_CONFIG_FILE_NAME}`
65
+ clientConfigUrl = `${this.rootUrl}/${CLIENT_CONFIG_FILE_NAME}`
52
66
 
53
67
  /**
54
- * GrapesJs plugins
68
+ * Init GrapesJS config which depend on the config file properties
55
69
  */
56
- grapesJsPlugins: Plugin[] = []
70
+ initGrapesConfig() {
71
+ this.grapesJsConfig = getEditorConfig(this.websiteId, this.storageId, this.rootUrl)
72
+ }
57
73
  }
@@ -18,6 +18,8 @@
18
18
  export enum ClientEvent {
19
19
  STARTUP_START = 'silex:startup:start',
20
20
  STARTUP_END = 'silex:startup:end',
21
+ GRAPESJS_START = 'silex:grapesjs:start',
22
+ GRAPESJS_END = 'silex:grapesjs:end',
21
23
  PUBLISH_START = 'silex:publish:start',
22
24
  PUBLISH_DATA = 'silex:publish:data',
23
25
  PUBLISH_END = 'silex:publish:end',
@@ -15,7 +15,6 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- export * from './index'
19
18
  export * from './grapesjs/index'
20
19
  export * from './config'
21
20
 
@@ -3,8 +3,10 @@ import { Editor } from 'grapesjs'
3
3
  import { ApiConnectorLoggedInPostMessage, ConnectorData, ConnectorId, ConnectorType, ConnectorUser } from '../../types'
4
4
  import { connectorList, getUser, logout } from '../api'
5
5
  import { WebsiteId } from '../../types'
6
+ import { API_CONNECTOR_LOGIN, API_CONNECTOR_PATH, API_PATH } from '../../constants'
6
7
 
7
8
  export const cmdLogin = 'silex:auth:login'
9
+ export const cmdLogout = 'silex:auth:logout'
8
10
  export const eventLoggingIn = 'silex:auth:logging-in'
9
11
  export const eventLoggedIn = 'silex:auth:logged-in'
10
12
  export const eventLoginFailed = 'silex:auth:login-failed'
@@ -19,6 +21,7 @@ export async function getCurrentUser(editor: Editor): Promise<ConnectorUser> {
19
21
  }
20
22
 
21
23
  export async function updateUser(editor: Editor, type: ConnectorType, connectorId?: ConnectorId): Promise<ConnectorUser> {
24
+ console.log('updateUser', type, connectorId)
22
25
  const user = await getUser({type, connectorId})
23
26
  editor.getModel().set('user', user)
24
27
  return user
@@ -37,6 +40,13 @@ export default function loginDialogPlugin(editor, opts) {
37
40
  async run(editor: Editor) { await openDialog() },
38
41
  stop(editor: Editor) { closeDialog() },
39
42
  })
43
+ editor.Commands.add(cmdLogout, {
44
+ async run(editor: Editor) {
45
+ await logout({type: ConnectorType.STORAGE })
46
+ editor.getModel().set('user', null)
47
+ editor.trigger(eventLoggedOut)
48
+ },
49
+ })
40
50
  editor.on(eventLoggedOut, async () => openDialog())
41
51
 
42
52
  // Dialog API
@@ -123,7 +133,8 @@ export default function loginDialogPlugin(editor, opts) {
123
133
  }
124
134
  }
125
135
  async function loginWithConnector(connector: ConnectorData) {
126
- window.open(connector.oauthUrl, '_blank')
136
+ const nonOAuthUrl = `${API_PATH}${API_CONNECTOR_PATH}${API_CONNECTOR_LOGIN}?connectorId=${connector.connectorId}&type=${connector.type}`
137
+ window.open(connector.oauthUrl ?? nonOAuthUrl, '_blank')
127
138
  return new Promise(resolve => {
128
139
  render(html`
129
140
  <main>
@@ -239,7 +239,7 @@ export class PublicationManager {
239
239
  this.trackProgress()
240
240
  } catch (e) {
241
241
  console.error('publish error', e)
242
- if(e.code === 401) {
242
+ if(e.code === 401 || e.httpStatusCode === 401) {
243
243
  this.status = PublicationStatus.STATUS_LOGGED_OUT
244
244
  this.settings = {}
245
245
  this.dialog && this.dialog.displayError('Please login.', this.job, this.status)
@@ -15,7 +15,7 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- import grapesjs, { Editor } from 'grapesjs'
18
+ import grapesjs, { Editor, EditorConfig } from 'grapesjs'
19
19
  import openImport from './openImport'
20
20
 
21
21
  /**
@@ -35,7 +35,7 @@ import symbolsPlugin from '@silexlabs/grapesjs-symbols'
35
35
  import loadingPlugin from '@silexlabs/grapesjs-loading'
36
36
  import fontsDialogPlugin, { cmdOpenFonts } from '@silexlabs/grapesjs-fonts'
37
37
  import symbolDialogsPlugin, { cmdPromptAddSymbol } from './symbolDialogs'
38
- import loginDialogPlugin, { LoginDialogOptions } from './LoginDialog'
38
+ import loginDialogPlugin, { LoginDialogOptions, cmdLogout } from './LoginDialog'
39
39
 
40
40
  import { pagePanelPlugin, cmdTogglePages, cmdAddPage } from './page-panel'
41
41
  import { newPageDialog, cmdOpenNewPageDialog } from './new-page-dialog'
@@ -48,6 +48,7 @@ import { internalLinksPlugin } from './internal-links'
48
48
  import publicationManagerPlugin, { PublicationManagerOptions } from './PublicationManager'
49
49
  import { storagePlugin } from './storage'
50
50
  import { ConnectorId, WebsiteId } from '../../types'
51
+ import { API_PATH, API_WEBSITE_ASSETS_WRITE, API_WEBSITE_PATH } from '../../constants'
51
52
 
52
53
  const plugins = [
53
54
  {name: './project-bar', value: projectBarPlugin}, // has to be before panels and dialogs
@@ -84,7 +85,7 @@ plugins
84
85
  const catBasic = 'Containers'
85
86
  const catComponents = 'Components'
86
87
 
87
- export function getEditorConfig(id: WebsiteId, connectorId: ConnectorId, rootUrl: string) {
88
+ export function getEditorConfig(id: WebsiteId, connectorId: ConnectorId, rootUrl: string): EditorConfig {
88
89
  return {
89
90
  container: '#gjs',
90
91
  height: '100%',
@@ -102,7 +103,7 @@ export function getEditorConfig(id: WebsiteId, connectorId: ConnectorId, rootUrl
102
103
  },
103
104
 
104
105
  assetManager: {
105
- upload: `${rootUrl}/assets/?id=${id}`,
106
+ upload: `${rootUrl}${API_PATH}${API_WEBSITE_PATH}${API_WEBSITE_ASSETS_WRITE}/?websiteId=${id}${ connectorId ? `&connectorId=${connectorId}` : ''}`,
106
107
  },
107
108
 
108
109
  storageManager: {
@@ -190,7 +191,7 @@ export function getEditorConfig(id: WebsiteId, connectorId: ConnectorId, rootUrl
190
191
  id: 'logout-button',
191
192
  className: 'page-panel-btn fa fa-fw fa-sign-out',
192
193
  attributes: { title: 'Sign out' },
193
- command: 'relogin',
194
+ command: cmdLogout,
194
195
  },
195
196
  ],
196
197
  },
@@ -235,17 +236,11 @@ export function getEditorConfig(id: WebsiteId, connectorId: ConnectorId, rootUrl
235
236
  // ////////////////////
236
237
  // Keep a ref to the editor singleton
237
238
  let editor: Editor
238
- export async function initEditor(config, grapesJsPlugins) {
239
+ export async function initEditor(config: EditorConfig) {
239
240
  if(editor) throw new Error('Grapesjs editor already created')
240
241
  return new Promise<Editor>((resolve, reject) => {
241
242
  try {
242
- editor = grapesjs.init({
243
- plugins: [
244
- ...grapesJsPlugins,
245
- ...config.plugins,
246
- ],
247
- ...config,
248
- })
243
+ editor = grapesjs.init(config)
249
244
  } catch(e) {
250
245
  console.error('Error initializing GrapesJs with plugins:', plugins, e)
251
246
  reject(e)
@@ -15,8 +15,8 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  */
17
17
 
18
- import { ConnectorId, WebsiteId, WebsiteData, ConnectorUser, ConnectorType } from '../../types'
19
- import { ApiError, websiteLoad, websiteSave } from '../api'
18
+ import { ConnectorId, WebsiteId, WebsiteData, ConnectorUser, ConnectorType, ApiError } from '../../types'
19
+ import { websiteLoad, websiteSave } from '../api'
20
20
  import { cmdLogin, eventLoggedIn, eventLoggedOut, getCurrentUser, updateUser } from './LoginDialog'
21
21
 
22
22
  async function wait(ms: number) {
@@ -28,6 +28,7 @@ export const storagePlugin = (editor) => {
28
28
  editor.Storage.add('connector', {
29
29
  async load(options: { id: WebsiteId, connectorId: ConnectorId }): Promise<WebsiteData> {
30
30
  try {
31
+ console.log('storage get user')
31
32
  const user: ConnectorUser = await getCurrentUser(editor) ?? await updateUser(editor, ConnectorType.STORAGE, options.connectorId)
32
33
  if(user) {
33
34
  const data = await websiteLoad({websiteId: options.id, connectorId: user.storage.connectorId})
@@ -73,17 +74,24 @@ export const storagePlugin = (editor) => {
73
74
  }
74
75
  })
75
76
  // Handle errors
76
- editor.on('storage:error:load', (err) => {
77
+ editor.on('storage:error:load', (err: Error) => {
77
78
  handleError(err)
78
79
  })
79
- editor.on('storage:error:store', (err) => {
80
+ editor.on('storage:error:store', (err: Error) => {
80
81
  handleError(err)
81
82
  })
82
- function handleError(err: Error) {
83
- console.error('Error with loading / saving', err, err instanceof ApiError)
83
+ editor.on('asset:upload:end', (err: Error) => {
84
+ editor.save()
85
+ })
86
+ editor.on('asset:upload:error', (err: Error) => {
87
+ handleError(err)
88
+ })
89
+ function handleError(_err: Error | string) {
90
+ const err: Error = typeof _err === 'string' ? JSON.parse(_err) : _err
91
+ console.error('Error with loading or saving website or uploading asset', err, err instanceof ApiError)
84
92
  if (err instanceof ApiError) {
85
- console.error('ApiError', err.code, err.message)
86
- switch (err.code) {
93
+ console.error('ApiError', err.httpStatusCode, err.message)
94
+ switch (err.httpStatusCode) {
87
95
  case 404:
88
96
  return editor.Modal.open({
89
97
  title: 'Website not found',
@@ -101,13 +109,13 @@ export const storagePlugin = (editor) => {
101
109
  default:
102
110
  return editor.Modal.open({
103
111
  title: 'Error',
104
- content: `An error occured. ${err.message}`,
112
+ content: `An error occured.<br>${err.message}`,
105
113
  })
106
114
  }
107
115
  } else {
108
116
  return editor.Modal.open({
109
117
  title: 'Error',
110
- content: `An error occured. ${err.message}`,
118
+ content: `An error occured.<br>${err.message}`,
111
119
  })
112
120
  }
113
121
  }